@@ -6,9 +6,9 @@ import 'dart:typed_data';
6
6
typedef String GetCharFromInt (int a);
7
7
typedef int GetNextValue (int index);
8
8
9
- class Data {
9
+ class _Data {
10
10
int value, position, index;
11
- Data (this .value, this .position, this .index);
11
+ _Data (this .value, this .position, this .index);
12
12
}
13
13
14
14
class LZString {
@@ -29,6 +29,13 @@ class LZString {
29
29
return _baseReverseDic[alphabet][character];
30
30
}
31
31
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
+ */
32
39
static String compressToBase64 (String input) {
33
40
if (input == null ) return "" ;
34
41
String res = _compress (input, 6 , (a) => _keyStrBase64[a]);
@@ -45,25 +52,43 @@ class LZString {
45
52
return null ;
46
53
}
47
54
55
+ /**
56
+ * Decompress base64 [input] which produces by `compressToBase64` .
57
+ */
48
58
static String decompressFromBase64 (String input) {
49
59
if (input == null ) return "" ;
50
60
if (input == "" ) return null ;
51
61
return _decompress (input.length, 32 ,
52
62
(index) => _getBaseValue (_keyStrBase64, input[index]));
53
63
}
54
64
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
+ */
55
72
static String compressToUTF16 (String input) {
56
73
if (input == null ) return "" ;
57
74
return _compress (input, 15 , (a) => String .fromCharCode (a + 32 )) + " " ;
58
75
}
59
76
77
+ /**
78
+ * Decompress "valid" UTF-16 string which produces by `compressToUTF16`
79
+ */
60
80
static String decompressFromUTF16 (String compressed) {
61
81
if (compressed == null ) return "" ;
62
82
if (compressed == "" ) return null ;
63
83
return _decompress (
64
84
compressed.length, 16384 , (index) => compressed.codeUnitAt (index) - 32 );
65
85
}
66
86
87
+ /**
88
+ * Produces an uint8Array.
89
+ *
90
+ * Can be decompressed with `decompressFromUint8Array`
91
+ */
67
92
static Uint8List compressToUint8Array (String uncompressed) {
68
93
String compressed = compress (uncompressed);
69
94
Uint8List buf = Uint8List (compressed.length * 2 );
@@ -75,6 +100,9 @@ class LZString {
75
100
return buf;
76
101
}
77
102
103
+ /**
104
+ * Decompress uint8Array which produces by `compressToUint8Array` .
105
+ */
78
106
static String decompressFromUint8Array (Uint8List compressed) {
79
107
if (compressed == null ) {
80
108
return "" ;
@@ -89,6 +117,9 @@ class LZString {
89
117
}
90
118
}
91
119
120
+ /**
121
+ * Decompress ASCII strings [input] which produces by `compressToEncodedURIComponent` .
122
+ */
92
123
static String decompressFromEncodedURIComponent (String input) {
93
124
if (input == null ) return "" ;
94
125
if (input == "" ) return null ;
@@ -97,11 +128,21 @@ class LZString {
97
128
(index) => _getBaseValue (_keyStrUriSafe, input[index]));
98
129
}
99
130
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
+ */
100
136
static String compressToEncodedURIComponent (String input) {
101
137
if (input == null ) return "" ;
102
138
return _compress (input, 6 , (a) => _keyStrUriSafe[a]);
103
139
}
104
140
141
+ /**
142
+ * Produces invalid UTF-16 strings from [uncompressed] .
143
+ *
144
+ * Can be decompressed with `decompress` .
145
+ */
105
146
static String compress (final String uncompressed) {
106
147
return _compress (uncompressed, 16 , (a) => String .fromCharCode (a));
107
148
}
@@ -321,6 +362,9 @@ class LZString {
321
362
return contextData.toString ();
322
363
}
323
364
365
+ /**
366
+ * Decompress invalid UTF-16 strings which produces by `compress` .
367
+ */
324
368
static String decompress (final String compressed) {
325
369
if (compressed == null ) return "" ;
326
370
if (compressed.isEmpty) return null ;
@@ -342,7 +386,7 @@ class LZString {
342
386
resb;
343
387
String entry = "" , c, w;
344
388
StringBuffer result = StringBuffer ();
345
- Data data = Data (getNextValue (0 ), resetValue, 1 );
389
+ _Data data = _Data (getNextValue (0 ), resetValue, 1 );
346
390
347
391
for (i = 0 ; i < 3 ; i++ ) {
348
392
dictionary[i] = i.toString ();
@@ -448,7 +492,7 @@ class LZString {
448
492
data.position = resetValue;
449
493
data.value = getNextValue (data.index++ );
450
494
}
451
- bits | = (resb > 0 ? 1 : 0 );
495
+ bits | = (resb > 0 ? 1 : 0 ) * power ;
452
496
power << = 1 ;
453
497
}
454
498
dictionary[dictSize++ ] = String .fromCharCode (bits);
0 commit comments