Skip to content

Commit f6862e4

Browse files
author
skipness
committed
Fix #1 which makes non english characters cannot be decompress
Add some documentation
1 parent 5f90102 commit f6862e4

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

lib/lzstring.dart

+48-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import 'dart:typed_data';
66
typedef String GetCharFromInt(int a);
77
typedef int GetNextValue(int index);
88

9-
class Data {
9+
class _Data {
1010
int value, position, index;
11-
Data(this.value, this.position, this.index);
11+
_Data(this.value, this.position, this.index);
1212
}
1313

1414
class LZString {
@@ -29,6 +29,13 @@ class LZString {
2929
return _baseReverseDic[alphabet][character];
3030
}
3131

32+
/**
33+
* Produces ASCII UTF-16 strings representing the original string encoded in Base64 from [input].
34+
*
35+
* Can be decompressed with `decompressFromBase64`.
36+
*
37+
* This works by using only 6bits of storage per character. The strings produced are therefore 166% bigger than those produced by `compress`.
38+
*/
3239
static String compressToBase64(String input) {
3340
if (input == null) return "";
3441
String res = _compress(input, 6, (a) => _keyStrBase64[a]);
@@ -45,25 +52,43 @@ class LZString {
4552
return null;
4653
}
4754

55+
/**
56+
* Decompress base64 [input] which produces by `compressToBase64`.
57+
*/
4858
static String decompressFromBase64(String input) {
4959
if (input == null) return "";
5060
if (input == "") return null;
5161
return _decompress(input.length, 32,
5262
(index) => _getBaseValue(_keyStrBase64, input[index]));
5363
}
5464

65+
/**
66+
* Produces "valid" UTF-16 strings from [input].
67+
*
68+
* Can be decompressed with `decompressFromUTF16`.
69+
*
70+
* This works by using only 15 bits of storage per character. The strings produced are therefore 6.66% bigger than those produced by `compress`.
71+
*/
5572
static String compressToUTF16(String input) {
5673
if (input == null) return "";
5774
return _compress(input, 15, (a) => String.fromCharCode(a + 32)) + " ";
5875
}
5976

77+
/**
78+
* Decompress "valid" UTF-16 string which produces by `compressToUTF16`
79+
*/
6080
static String decompressFromUTF16(String compressed) {
6181
if (compressed == null) return "";
6282
if (compressed == "") return null;
6383
return _decompress(
6484
compressed.length, 16384, (index) => compressed.codeUnitAt(index) - 32);
6585
}
6686

87+
/**
88+
* Produces an uint8Array.
89+
*
90+
* Can be decompressed with `decompressFromUint8Array`
91+
*/
6792
static Uint8List compressToUint8Array(String uncompressed) {
6893
String compressed = compress(uncompressed);
6994
Uint8List buf = Uint8List(compressed.length * 2);
@@ -75,6 +100,9 @@ class LZString {
75100
return buf;
76101
}
77102

103+
/**
104+
* Decompress uint8Array which produces by `compressToUint8Array`.
105+
*/
78106
static String decompressFromUint8Array(Uint8List compressed) {
79107
if (compressed == null) {
80108
return "";
@@ -89,6 +117,9 @@ class LZString {
89117
}
90118
}
91119

120+
/**
121+
* Decompress ASCII strings [input] which produces by `compressToEncodedURIComponent`.
122+
*/
92123
static String decompressFromEncodedURIComponent(String input) {
93124
if (input == null) return "";
94125
if (input == "") return null;
@@ -97,11 +128,21 @@ class LZString {
97128
(index) => _getBaseValue(_keyStrUriSafe, input[index]));
98129
}
99130

131+
/**
132+
* Produces ASCII strings representing the original string encoded in Base64 with a few tweaks to make these URI safe.
133+
*
134+
* Can be decompressed with `decompressFromEncodedURIComponent`
135+
*/
100136
static String compressToEncodedURIComponent(String input) {
101137
if (input == null) return "";
102138
return _compress(input, 6, (a) => _keyStrUriSafe[a]);
103139
}
104140

141+
/**
142+
* Produces invalid UTF-16 strings from [uncompressed].
143+
*
144+
* Can be decompressed with `decompress`.
145+
*/
105146
static String compress(final String uncompressed) {
106147
return _compress(uncompressed, 16, (a) => String.fromCharCode(a));
107148
}
@@ -321,6 +362,9 @@ class LZString {
321362
return contextData.toString();
322363
}
323364

365+
/**
366+
* Decompress invalid UTF-16 strings which produces by `compress`.
367+
*/
324368
static String decompress(final String compressed) {
325369
if (compressed == null) return "";
326370
if (compressed.isEmpty) return null;
@@ -342,7 +386,7 @@ class LZString {
342386
resb;
343387
String entry = "", c, w;
344388
StringBuffer result = StringBuffer();
345-
Data data = Data(getNextValue(0), resetValue, 1);
389+
_Data data = _Data(getNextValue(0), resetValue, 1);
346390

347391
for (i = 0; i < 3; i++) {
348392
dictionary[i] = i.toString();
@@ -448,7 +492,7 @@ class LZString {
448492
data.position = resetValue;
449493
data.value = getNextValue(data.index++);
450494
}
451-
bits |= (resb > 0 ? 1 : 0);
495+
bits |= (resb > 0 ? 1 : 0) * power;
452496
power <<= 1;
453497
}
454498
dictionary[dictSize++] = String.fromCharCode(bits);

0 commit comments

Comments
 (0)