private static int readBits(int numBits, Decompress_Data data) { int res = 0; int maxpower = (int)Math.Pow(2, numBits); int power = 1; while (power != maxpower) { res |= readBit(data) * power; power <<= 1; } return(res); }
private static int readBit(Decompress_Data data) { var res = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = 32768; // This 'if' check doesn't appear in the orginal lz-string javascript code. // Added as a check to make sure we don't exceed the length of data.str // The javascript charCodeAt will return a NaN if it exceeds the index but will not error out if (data.index < data.str.Length) { data.val = data.str[data.index++]; // data.val = data.string.charCodeAt(data.index++); <---javascript equivilant } } return(res > 0 ? 1 : 0); }
public static string decompress(string compressed) { Decompress_Data data = new Decompress_Data(); List <string> dictionary = new List <string>(); int next = 0; int enlargeIn = 4; int numBits = 3; string entry = ""; string result = ""; int i = 0; dynamic w = ""; dynamic c = ""; int errorCount = 0; data.str = compressed; data.val = (int)compressed[0]; data.position = 32768; data.index = 1; try { for (i = 0; i < 3; i++) { dictionary.Add(i.ToString()); } next = readBits(2, data); switch (next) { case 0: c = Convert.ToChar(readBits(8, data)).ToString(); break; case 1: c = Convert.ToChar(readBits(16, data)).ToString(); break; case 2: return(""); } dictionary.Add(c); w = result = c; while (true) { c = readBits(numBits, data); int cc = (int)(c); switch (cc) { case 0: if (errorCount++ > 10000) { throw new Exception("To many errors"); } c = Convert.ToChar(readBits(8, data)).ToString(); dictionary.Add(c); c = dictionary.Count - 1; enlargeIn--; break; case 1: c = Convert.ToChar(readBits(16, data)).ToString(); dictionary.Add(c); c = dictionary.Count - 1; enlargeIn--; break; case 2: return(result); } if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } if (dictionary.ElementAtOrDefault((int)c) != null) // if (dictionary[c] ) <------- original Javascript Equivalant { entry = dictionary[c]; } else { if (c == dictionary.Count) { entry = w + w[0]; } else { return(null); } } result += entry; dictionary.Add(w + entry[0]); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } } } catch (Exception ex) { return(ex.Message); } }
public static string decompress(string compressed) { Decompress_Data data = new Decompress_Data(); List<string> dictionary = new List<string>(); int next = 0; int enlargeIn = 4; int numBits = 3; string entry = ""; string result = ""; int i = 0; dynamic w = ""; dynamic c = ""; int errorCount = 0; data.str = compressed; data.val = (int)compressed[0]; data.position = 32768; data.index = 1; try { for (i = 0; i < 3; i++) { dictionary.Add(i.ToString()); } next = readBits(2, data); switch (next) { case 0: c = Convert.ToChar(readBits(8, data)).ToString(); break; case 1: c = Convert.ToChar(readBits(16, data)).ToString(); break; case 2: return ""; } dictionary.Add(c); w = result = c; while (true) { c = readBits(numBits, data); int cc = (int)(c); switch (cc) { case 0: if (errorCount++ > 10000) throw new Exception("To many errors"); c = Convert.ToChar(readBits(8, data)).ToString(); dictionary.Add(c); c = dictionary.Count - 1; enlargeIn--; break; case 1: c = Convert.ToChar(readBits(16, data)).ToString(); dictionary.Add(c); c = dictionary.Count - 1; enlargeIn--; break; case 2: return result; } if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } if (dictionary.ElementAtOrDefault((int)c) != null) // if (dictionary[c] ) <------- original Javascript Equivalant { entry = dictionary[c]; } else { if (c == dictionary.Count) { entry = w + w[0]; } else { return null; } } result += entry; dictionary.Add(w + entry[0]); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } } } catch (Exception ex) { return ex.Message; } }
private static int readBits(int numBits, Decompress_Data data) { int res = 0; int maxpower = (int)Math.Pow(2, numBits); int power = 1; while (power != maxpower) { res |= readBit(data) * power; power <<= 1; } return res; }
private static int readBit(Decompress_Data data) { var res = data.val & data.position; data.position >>= 1; if (data.position == 0) { data.position = 32768; // This 'if' check doesn't appear in the orginal lz-string javascript code. // Added as a check to make sure we don't exceed the length of data.str // The javascript charCodeAt will return a NaN if it exceeds the index but will not error out if (data.index < data.str.Length) { data.val = data.str[data.index++]; // data.val = data.string.charCodeAt(data.index++); <---javascript equivilant } } return res > 0 ? 1 : 0; }