static int ReadBits(int numBits, DecompressData 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 ReadBits(int numBits, DecompressData data) { var res = 0; var maxpower = (int)Math.Pow(2, numBits); var power = 1; while (power != maxpower) { res |= ReadBit(data) * power; power <<= 1; } return(res); }
static int ReadBit(DecompressData 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) { if (compressed == null) { throw new ArgumentNullException(nameof(compressed)); } DecompressData data = new DecompressData(); List <string> dictionary = new List <string>(); int next = 0; int enlargeIn = 4; int numBits = 3; string entry = ""; StringBuilder result = new StringBuilder(); int i = 0; string w = ""; string sc = ""; int c = 0; int errorCount = 0; data.Str = compressed; data.Val = compressed[0]; data.Position = 32768; data.Index = 1; for (i = 0; i < 3; i++) { dictionary.Add(i.ToString()); } next = ReadBits(2, data); switch (next) { case 0: sc = Convert.ToChar(ReadBits(8, data)).ToString(); break; case 1: sc = Convert.ToChar(ReadBits(16, data)).ToString(); break; case 2: return(""); } dictionary.Add(sc); result.Append(sc); w = result.ToString(); while (true) { c = ReadBits(numBits, data); int cc = c; switch (cc) { case 0: if (errorCount++ > 10000) { throw new Exception("To many errors"); } sc = Convert.ToChar(ReadBits(8, data)).ToString(); dictionary.Add(sc); c = dictionary.Count - 1; enlargeIn--; break; case 1: sc = Convert.ToChar(ReadBits(16, data)).ToString(); dictionary.Add(sc); c = dictionary.Count - 1; enlargeIn--; break; case 2: return(result.ToString()); } if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } if (dictionary.Count - 1 >= c) // if (dictionary[c] ) <------- original Javascript Equivalant { entry = dictionary[c]; } else { if (c == dictionary.Count) { entry = w + w[0]; } else { return(null); } } result.Append(entry); dictionary.Add(w + entry[0]); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } } }
private static int ReadBits(int numBits, DecompressData data) { var res = 0; var maxpower = (int)Math.Pow(2, numBits); var power = 1; while (power != maxpower) { res |= ReadBit(data) * power; power <<= 1; } return res; }
private static int ReadBit(DecompressData 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) { var data = new DecompressData(); var dictionary = new List<string>(); var next = 0; var enlargeIn = 4; var numBits = 3; var entry = ""; var result = new StringBuilder(); var i = 0; var w = ""; var sc = ""; var c = 0; var errorCount = 0; data.str = compressed; data.val = 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: sc = Convert.ToChar(ReadBits(8, data)).ToString(); break; case 1: sc = Convert.ToChar(ReadBits(16, data)).ToString(); break; case 2: return ""; } dictionary.Add(sc); result.Append(sc); w = result.ToString(); while (true) { c = ReadBits(numBits, data); var cc = c; switch (cc) { case 0: if (errorCount++ > 10000) { throw new Exception("To many errors"); } sc = Convert.ToChar(ReadBits(8, data)).ToString(); dictionary.Add(sc); c = dictionary.Count - 1; enlargeIn--; break; case 1: sc = Convert.ToChar(ReadBits(16, data)).ToString(); dictionary.Add(sc); c = dictionary.Count - 1; enlargeIn--; break; case 2: return result.ToString(); } if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } if (dictionary.Count - 1 >= c) // if (dictionary[c] ) <------- original Javascript Equivalant { entry = dictionary[c]; } else { if (c == dictionary.Count) { entry = w + w[0]; } else { return null; } } result.Append(entry); dictionary.Add(w + entry[0]); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } } } catch (Exception ex) { throw ex; } }
public static string Decompress(string compressed) { var data = new DecompressData(); var dictionary = new List <string>(); var next = 0; var enlargeIn = 4; var numBits = 3; var entry = ""; var result = new StringBuilder(); var i = 0; var w = ""; var sc = ""; var c = 0; var errorCount = 0; data.str = compressed; data.val = 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: sc = Convert.ToChar(ReadBits(8, data)).ToString(); break; case 1: sc = Convert.ToChar(ReadBits(16, data)).ToString(); break; case 2: return(""); } dictionary.Add(sc); result.Append(sc); w = result.ToString(); while (true) { c = ReadBits(numBits, data); var cc = c; switch (cc) { case 0: if (errorCount++ > 10000) { throw new Exception("To many errors"); } sc = Convert.ToChar(ReadBits(8, data)).ToString(); dictionary.Add(sc); c = dictionary.Count - 1; enlargeIn--; break; case 1: sc = Convert.ToChar(ReadBits(16, data)).ToString(); dictionary.Add(sc); c = dictionary.Count - 1; enlargeIn--; break; case 2: return(result.ToString()); } if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } if (dictionary.Count - 1 >= c) // if (dictionary[c] ) <------- original Javascript Equivalant { entry = dictionary[c]; } else { if (c == dictionary.Count) { entry = w + w[0]; } else { return(null); } } result.Append(entry); dictionary.Add(w + entry[0]); enlargeIn--; w = entry; if (enlargeIn == 0) { enlargeIn = (int)Math.Pow(2, numBits); numBits++; } } } catch (Exception ex) { throw ex; } }
public static string Decompress(string compressed) { var data = new DecompressData(); var dictionary = new List <string>(); var enlargeIn = 4; var numBits = 3; dynamic c = ""; var errorCount = 0; data.Str = compressed; data.Val = compressed[0]; data.Position = 32768; data.Index = 1; try { for (var i = 0; i < 3; i++) { dictionary.Add(i.ToString()); } var 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); string result; dynamic w = result = c; while (true) { c = ReadBits(numBits, data); var 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++; } string entry; if (dictionary.ElementAtOrDefault((int)c) != null) { 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); } }