Home

Add a comment

 

Avatar: pudel

Question about _decompress() implementation

I try to port LZString 1.4.4 to Qt, but I struggle with _decompress() function.
There is a loop at the beginning (line 346):
var dictionary = []

for (i = 0; i < 3; i += 1) {
  dictionary[i] = i;
}
which inserts some integers: 0, 1, 2 to dictionary array.
Later there are some strings added to this dictionary too (line 438, 456):
dictionary[dictSize++] = f(bits);
Finally at the end of _decompress() there is condition (line 469):
if (dictionary[c]) {
  entry = dictionary[c];
} else {
  ...
}
What is the purpose for this codition ?
Currently it returns false for dictionary[c] == null, c >= dictionary.length and for c=0 (because dictionary[0] contains 0).

Shouldn't the for loop at the beginning of _decompress() be like:
for (i = 0; i < 3; i += 1) {
  dictionary[i] = f(i);    // or maybe: dictionary[i] = ''+i ???
}
so it would only contains strings ?

Different ports have different implementations:

diogoduailibe/lzstring4j:
List<String> dictionary = new ArrayList<String>(200);
for (int i = 0; i < 3; i += 1) {
   dictionary.add(i, "");
}

(...)

if (d < dictionary.size() && dictionary.get(d) != null) {
   entry = dictionary.get(d);
} else {
   (...)
}
rufushuang/lz-string4java:
ArrayList<String> result = new ArrayList<String>();
for (int i = 0; i < 3; i += 1) {
   dictionary.add(i, f(i));
}

(...)

if (cc < dictionary.size() && dictionary.get(cc) != null) {
   entry = dictionary.get(cc);
} else {
   (...)
}
jawa-the-hutt/lz-string-csharp:
List<string> dictionary = new List<string>();
for (i = 0; i < 3; i++)
{
   dictionary.Add(i.ToString());
}

(...)

if (dictionary.Count - 1 >= c) // if (dictionary[c] ) <------- original Javascript Equivalant
{
   entry = dictionary[c];
}
else
   (...)
}
kreudom/lz-string-csharp:
Dictionary<int, string> dictionary = new Dictionary<int, string>();
for (i = 0; i < 3; i++)
{
   dictionary[i] = Convert.ToChar(i).ToString();
}

(...)

if(dictionary.ContainsKey(c))
{
   entry = dictionary[c];
}
else
{
   (...)
}

Question about _decompress() implementation


Title
Body
HTML : b, strong, i, em, blockquote, br, p, pre, a href="", ul, ol, li, sub, sup
OpenID Login
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

Home