morganestes
1/29/2013 - 2:48 AM

Financial Times compressor ported from JavaScript to C#. Original article at http://labs.ft.com/2012/06/text-re-encoding-for-optimising-st

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/.

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);
			
		}

	}
}