Skip to content

Instantly share code, notes, and snippets.

@mchome
Created March 5, 2019 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchome/91aae7f796b7d89dfcdc9fc70dd4ba94 to your computer and use it in GitHub Desktop.
Save mchome/91aae7f796b7d89dfcdc9fc70dd4ba94 to your computer and use it in GitHub Desktop.
lzStringDecompressFromBase64
String lzStringDecompressFromBase64(String str) {
final List<int> valStrBase64 = [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
62,
0,
0,
0,
63,
52,
53,
54,
55,
56,
57,
58,
59,
60,
61,
0,
0,
0,
64,
0,
0,
0,
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
0,
0,
0,
0,
0,
0,
26,
27,
28,
29,
30,
31,
32,
33,
34,
35,
36,
37,
38,
39,
40,
41,
42,
43,
44,
45,
46,
47,
48,
49,
50,
51,
];
if (str == null) return null;
if (str.isEmpty) return '';
List<int> input = str.runes.toList();
int offset = 0;
int resetValue = 32;
List<String> dictionary = [];
int enlargeIn = 4;
int dictSize = 4;
int numBits = 3;
int position = 32;
int index = 1;
int resb;
int maxpower;
int power;
String entry;
String w;
String c;
List<String> result = [];
int bits = (valStrBase64 == null) ? (input[0] + 0) : valStrBase64[input[0]];
int val = bits;
for (var i = 0; i < 3; i++) {
dictionary.add(String.fromCharCode(i));
}
bits = 0;
maxpower = 2;
power = 0;
while (power != maxpower) {
resb = val & position;
position >>= 1;
if (position == 0) {
position = resetValue;
val = (valStrBase64 == null) ? (input[index++] + offset) : valStrBase64[input[index++]];
}
bits |= (resb > 0 ? 1 : 0) << power++;
}
switch (bits) {
case 0:
bits = 0;
maxpower = 8;
power = 0;
while (power != maxpower) {
resb = val & position;
position >>= 1;
if (position == 0) {
position = resetValue;
val = (valStrBase64 == null) ? (input[index++] + offset) : valStrBase64[input[index++]];
}
bits |= (resb > 0 ? 1 : 0) << power++;
}
c = String.fromCharCode(bits);
break;
case 1:
bits = 0;
maxpower = 16;
power = 0;
while (power != maxpower) {
resb = val & position;
position >>= 1;
if (position == 0) {
position = resetValue;
val = (valStrBase64 == null) ? (input[index++] + offset) : valStrBase64[input[index++]];
}
bits |= (resb > 0 ? 1 : 0) << power++;
}
c = String.fromCharCode(bits);
break;
default:
return '';
}
dictionary.add(c);
w = c;
result.add(w);
while (true) {
if (index > input.length) return '';
bits = 0;
maxpower = numBits;
power = 0;
while (power != maxpower) {
resb = val & position;
position >>= 1;
if (position == 0) {
position = resetValue;
val = (valStrBase64 == null) ? (input[index++] + offset) : valStrBase64[input[index++]];
}
bits |= (resb > 0 ? 1 : 0) << power++;
}
int cc;
switch (cc = bits) {
case 0:
bits = 0;
maxpower = 8;
power = 0;
while (power != maxpower) {
resb = val & position;
position >>= 1;
if (position == 0) {
position = resetValue;
val = (valStrBase64 == null) ? (input[index++] + offset) : valStrBase64[input[index++]];
}
bits |= (resb > 0 ? 1 : 0) << power++;
}
dictionary.add(String.fromCharCode(bits));
cc = dictSize++;
enlargeIn--;
break;
case 1:
bits = 0;
maxpower = 16;
power = 0;
while (power != maxpower) {
resb = val & position;
position >>= 1;
if (position == 0) {
position = resetValue;
val = (valStrBase64 == null) ? (input[index++] + offset) : valStrBase64[input[index++]];
}
bits |= (resb > 0 ? 1 : 0) << power++;
}
dictionary.add(String.fromCharCode(bits));
cc = dictSize++;
enlargeIn--;
break;
case 2:
StringBuffer sb = StringBuffer();
for (String s in result) sb.write(s);
return sb.toString();
}
if (enlargeIn == 0) {
enlargeIn = 1 << numBits;
numBits++;
}
if (cc < dictionary.length && dictionary[cc] != null) {
entry = dictionary[cc];
} else {
if (cc == dictSize)
entry = w + w[0];
else
return '';
}
result.add(entry);
dictionary.add(w + entry[0]);
dictSize++;
enlargeIn--;
w = entry;
if (enlargeIn == 0) {
enlargeIn = 1 << numBits;
numBits++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment