Created
January 29, 2013 02:48
-
-
Save morganestes/4661296 to your computer and use it in GitHub Desktop.
Financial Times compressor ported from JavaScript to C#. Original article at http://labs.ft.com/2012/06/text-re-encoding-for-optimising-storage-capacity-in-the-browser/.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.IO; | |
namespace ftCompress | |
{ | |
class Program | |
{ | |
public static string CompressString(string s) | |
{ | |
int i, l; | |
int intStr1, intStr2; | |
char[] character = s.ToCharArray(); | |
string strOut = ""; | |
char snowman = Convert.ToChar(9731); | |
// pad the string | |
if (s.Length % 2 != 0) { | |
s += " "; | |
} | |
// compress the string | |
for (i = 0, l = s.Length; i < l; i += 2) | |
{ | |
// Char.ConvertToUtf32(string, pos) is JS's string.charCodeAt(pos) | |
intStr1 = Char.ConvertToUtf32(s, i) * 256; | |
intStr2 = Char.ConvertToUtf32(s, i + 1); | |
// Char.ConvertFromUtf32(int) is JS's String.fromCharCode(int) | |
strOut += Char.ConvertFromUtf32(intStr1 + intStr2); | |
} | |
return snowman + strOut; | |
} | |
static void Main(string[] args) | |
{ | |
string strNew = CompressString("hello world"); | |
string js = "var strCompressed = '" + strNew + "';"; | |
System.IO.File.WriteAllText(@"C:\temp\ft-compressedString.js", js); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment