strc 2.0.3 → 2.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/jssc.cjs +116 -115
- package/dist/jssc.js +119 -120
- package/dist/jssc.mjs +116 -115
- package/index.js +119 -120
- package/index.min.js +1 -1
- package/package.json +3 -5
- package/src/freqMap.js +112 -0
- package/src/index.js +13 -165
- package/src/meta.js +2 -0
- package/src/prefix.sh +3 -0
- package/src/prefix.txt +34 -0
- package/src/utils.js +51 -0
package/dist/jssc.cjs
CHANGED
|
@@ -41,13 +41,10 @@ SOFTWARE.
|
|
|
41
41
|
|
|
42
42
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
var JUSTC = require('justc');
|
|
45
45
|
|
|
46
46
|
const name__ = 'JSSC';
|
|
47
47
|
const prefix = name__+': ';
|
|
48
|
-
if ((String.fromCharCode(65536).charCodeAt(0) === 65536) || !(String.fromCharCode(256).charCodeAt(0) === 256)) {
|
|
49
|
-
throw new Error(prefix+'Supported UTF-16 only!')
|
|
50
|
-
}
|
|
51
48
|
|
|
52
49
|
function stringCodes(str) {
|
|
53
50
|
let output = [];
|
|
@@ -93,6 +90,121 @@ function binToDec(str) {
|
|
|
93
90
|
return parseInt(str, 2);
|
|
94
91
|
}
|
|
95
92
|
|
|
93
|
+
const freqMap = {
|
|
94
|
+
ESCAPE_BYTE: 0xFF,
|
|
95
|
+
TOP_COUNT: 254,
|
|
96
|
+
SPLITTER: " \u200B",
|
|
97
|
+
|
|
98
|
+
compress(text, splitter = this.SPLITTER) {
|
|
99
|
+
const freq = {};
|
|
100
|
+
for (let char of text) {
|
|
101
|
+
freq[char] = (freq[char] || 0) + 1;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const topChars = Object.entries(freq)
|
|
105
|
+
.sort((a, b) => b[1] - a[1])
|
|
106
|
+
.slice(0, this.TOP_COUNT)
|
|
107
|
+
.map(entry => entry[0]);
|
|
108
|
+
|
|
109
|
+
const charToIndex = new Map(topChars.map((char, i) => [char, i]));
|
|
110
|
+
|
|
111
|
+
let header = String.fromCharCode(topChars.length) + topChars.join('');
|
|
112
|
+
|
|
113
|
+
let bytes = [];
|
|
114
|
+
for (let char of text) {
|
|
115
|
+
if (charToIndex.has(char)) {
|
|
116
|
+
/* frequent */
|
|
117
|
+
bytes.push(charToIndex.get(char));
|
|
118
|
+
} else {
|
|
119
|
+
/* rare */
|
|
120
|
+
bytes.push(this.ESCAPE_BYTE);
|
|
121
|
+
const code = char.charCodeAt(0);
|
|
122
|
+
bytes.push((code >> 8) & 0xFF);
|
|
123
|
+
bytes.push(code & 0xFF);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/* to UTF16 */
|
|
128
|
+
let compressedBody = "";
|
|
129
|
+
for (let i = 0; i < bytes.length; i += 2) {
|
|
130
|
+
const b1 = bytes[i];
|
|
131
|
+
const b2 = (i + 1 < bytes.length) ? bytes[i + 1] : 0x00;
|
|
132
|
+
compressedBody += String.fromCharCode((b1 << 8) | b2);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return header + splitter + compressedBody;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
decompress(compressedText, splitter = this.SPLITTER) {
|
|
139
|
+
const parts = compressedText.split(splitter);
|
|
140
|
+
|
|
141
|
+
if (parts.length < 2) {
|
|
142
|
+
throw new Error(prefix+'Invalid freqMap data: splitter not found');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const headerPart = parts[0];
|
|
146
|
+
const bodyPart = parts.slice(1).join(splitter);
|
|
147
|
+
|
|
148
|
+
const topCount = headerPart.charCodeAt(0);
|
|
149
|
+
const topChars = headerPart.substring(1, topCount + 1);
|
|
150
|
+
|
|
151
|
+
let bytes = [];
|
|
152
|
+
for (let i = 0; i < bodyPart.length; i++) {
|
|
153
|
+
const code = bodyPart.charCodeAt(i);
|
|
154
|
+
bytes.push((code >> 8) & 0xFF);
|
|
155
|
+
bytes.push(code & 0xFF);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let result = "";
|
|
159
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
160
|
+
const b = bytes[i];
|
|
161
|
+
if (b === this.ESCAPE_BYTE) {
|
|
162
|
+
const charCode = (bytes[i + 1] << 8) | bytes[i + 2];
|
|
163
|
+
result += String.fromCharCode(charCode);
|
|
164
|
+
i += 2;
|
|
165
|
+
} else if (b < topCount) {
|
|
166
|
+
result += topChars[b];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 0 = Fail
|
|
174
|
+
* 1 = Success
|
|
175
|
+
* 2 = Remove last character (Success)
|
|
176
|
+
* @param {string} text
|
|
177
|
+
* @param {string?} splitter
|
|
178
|
+
* @returns {number|[number, number, string, string]}
|
|
179
|
+
*/
|
|
180
|
+
test(text, splitter = this.SPLITTER) {
|
|
181
|
+
try {
|
|
182
|
+
if (text.includes(splitter)) return 0;
|
|
183
|
+
const packed = this.compress(text, splitter);
|
|
184
|
+
const unpacked = this.decompress(packed, splitter);
|
|
185
|
+
if (packed.length < text.length) {
|
|
186
|
+
if (unpacked == text) return [1, packed.length, splitter, packed];
|
|
187
|
+
else if (unpacked.slice(0,-1) == text) return [2, packed.length, splitter, packed];
|
|
188
|
+
else return 0;
|
|
189
|
+
}
|
|
190
|
+
return 0;
|
|
191
|
+
} catch (_) {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const freqMapSplitters = [
|
|
198
|
+
" \u200B","\u0000",
|
|
199
|
+
"\u001F", "\u0001",
|
|
200
|
+
"\uFFFD", "\u2022",
|
|
201
|
+
"|§|", "\uFEFF"
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
if ((String.fromCharCode(65536).charCodeAt(0) === 65536) || !(String.fromCharCode(256).charCodeAt(0) === 256)) {
|
|
205
|
+
throw new Error(prefix+'Supported UTF-16 only!')
|
|
206
|
+
}
|
|
207
|
+
|
|
96
208
|
function charsBase() {
|
|
97
209
|
const charsBase = {};
|
|
98
210
|
function addChar(i) {
|
|
@@ -631,117 +743,6 @@ function decompressSequences(str) {
|
|
|
631
743
|
return result;
|
|
632
744
|
}
|
|
633
745
|
|
|
634
|
-
const freqMap = {
|
|
635
|
-
ESCAPE_BYTE: 0xFF,
|
|
636
|
-
TOP_COUNT: 254,
|
|
637
|
-
SPLITTER: " \u200B",
|
|
638
|
-
|
|
639
|
-
compress(text, splitter = this.SPLITTER) {
|
|
640
|
-
const freq = {};
|
|
641
|
-
for (let char of text) {
|
|
642
|
-
freq[char] = (freq[char] || 0) + 1;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
const topChars = Object.entries(freq)
|
|
646
|
-
.sort((a, b) => b[1] - a[1])
|
|
647
|
-
.slice(0, this.TOP_COUNT)
|
|
648
|
-
.map(entry => entry[0]);
|
|
649
|
-
|
|
650
|
-
const charToIndex = new Map(topChars.map((char, i) => [char, i]));
|
|
651
|
-
|
|
652
|
-
let header = String.fromCharCode(topChars.length) + topChars.join('');
|
|
653
|
-
|
|
654
|
-
let bytes = [];
|
|
655
|
-
for (let char of text) {
|
|
656
|
-
if (charToIndex.has(char)) {
|
|
657
|
-
/* frequent */
|
|
658
|
-
bytes.push(charToIndex.get(char));
|
|
659
|
-
} else {
|
|
660
|
-
/* rare */
|
|
661
|
-
bytes.push(this.ESCAPE_BYTE);
|
|
662
|
-
const code = char.charCodeAt(0);
|
|
663
|
-
bytes.push((code >> 8) & 0xFF);
|
|
664
|
-
bytes.push(code & 0xFF);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
/* to UTF16 */
|
|
669
|
-
let compressedBody = "";
|
|
670
|
-
for (let i = 0; i < bytes.length; i += 2) {
|
|
671
|
-
const b1 = bytes[i];
|
|
672
|
-
const b2 = (i + 1 < bytes.length) ? bytes[i + 1] : 0x00;
|
|
673
|
-
compressedBody += String.fromCharCode((b1 << 8) | b2);
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
return header + splitter + compressedBody;
|
|
677
|
-
},
|
|
678
|
-
|
|
679
|
-
decompress(compressedText, splitter = this.SPLITTER) {
|
|
680
|
-
const parts = compressedText.split(splitter);
|
|
681
|
-
|
|
682
|
-
if (parts.length < 2) {
|
|
683
|
-
throw new Error(prefix+'Invalid freqMap data: splitter not found');
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
const headerPart = parts[0];
|
|
687
|
-
const bodyPart = parts.slice(1).join(splitter);
|
|
688
|
-
|
|
689
|
-
const topCount = headerPart.charCodeAt(0);
|
|
690
|
-
const topChars = headerPart.substring(1, topCount + 1);
|
|
691
|
-
|
|
692
|
-
let bytes = [];
|
|
693
|
-
for (let i = 0; i < bodyPart.length; i++) {
|
|
694
|
-
const code = bodyPart.charCodeAt(i);
|
|
695
|
-
bytes.push((code >> 8) & 0xFF);
|
|
696
|
-
bytes.push(code & 0xFF);
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
let result = "";
|
|
700
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
701
|
-
const b = bytes[i];
|
|
702
|
-
if (b === this.ESCAPE_BYTE) {
|
|
703
|
-
const charCode = (bytes[i + 1] << 8) | bytes[i + 2];
|
|
704
|
-
result += String.fromCharCode(charCode);
|
|
705
|
-
i += 2;
|
|
706
|
-
} else if (b < topCount) {
|
|
707
|
-
result += topChars[b];
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
return result;
|
|
711
|
-
},
|
|
712
|
-
|
|
713
|
-
/**
|
|
714
|
-
* 0 = Fail
|
|
715
|
-
* 1 = Success
|
|
716
|
-
* 2 = Remove last character (Success)
|
|
717
|
-
* @param {string} text
|
|
718
|
-
* @param {string?} splitter
|
|
719
|
-
* @returns {number|[number, number, string, string]}
|
|
720
|
-
*/
|
|
721
|
-
test(text, splitter = this.SPLITTER) {
|
|
722
|
-
try {
|
|
723
|
-
if (text.includes(splitter)) return 0;
|
|
724
|
-
const packed = this.compress(text, splitter);
|
|
725
|
-
const unpacked = this.decompress(packed, splitter);
|
|
726
|
-
if (packed.length < text.length) {
|
|
727
|
-
if (unpacked == text) return [1, packed.length, splitter, packed];
|
|
728
|
-
else if (unpacked.slice(0,-1) == text) return [2, packed.length, splitter, packed];
|
|
729
|
-
else return 0;
|
|
730
|
-
}
|
|
731
|
-
return 0;
|
|
732
|
-
} catch (_) {
|
|
733
|
-
return 0;
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
};
|
|
737
|
-
|
|
738
|
-
const freqMapSplitters = [
|
|
739
|
-
" \u200B","\u0000",
|
|
740
|
-
"\u001F", "\u0001",
|
|
741
|
-
"\uFFFD", "\u2022",
|
|
742
|
-
"|§|", "\uFEFF"
|
|
743
|
-
];
|
|
744
|
-
|
|
745
746
|
function segments(str) {
|
|
746
747
|
if (typeof str !== 'string' || str.length === 0) return [];
|
|
747
748
|
|
package/dist/jssc.js
CHANGED
|
@@ -38,18 +38,13 @@ SOFTWARE.
|
|
|
38
38
|
*/
|
|
39
39
|
|
|
40
40
|
(function (global, factory) {
|
|
41
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
42
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
43
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JSSC = {}));
|
|
44
|
-
})(this, (function (exports) { 'use strict';
|
|
45
|
-
|
|
46
|
-
const JUSTC = require('justc');
|
|
41
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('justc')) :
|
|
42
|
+
typeof define === 'function' && define.amd ? define(['exports', 'justc'], factory) :
|
|
43
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JSSC = {}, global.JUSTC));
|
|
44
|
+
})(this, (function (exports, JUSTC) { 'use strict';
|
|
47
45
|
|
|
48
46
|
const name__ = 'JSSC';
|
|
49
47
|
const prefix = name__+': ';
|
|
50
|
-
if ((String.fromCharCode(65536).charCodeAt(0) === 65536) || !(String.fromCharCode(256).charCodeAt(0) === 256)) {
|
|
51
|
-
throw new Error(prefix+'Supported UTF-16 only!')
|
|
52
|
-
}
|
|
53
48
|
|
|
54
49
|
function stringCodes(str) {
|
|
55
50
|
let output = [];
|
|
@@ -95,6 +90,121 @@ SOFTWARE.
|
|
|
95
90
|
return parseInt(str, 2);
|
|
96
91
|
}
|
|
97
92
|
|
|
93
|
+
const freqMap = {
|
|
94
|
+
ESCAPE_BYTE: 0xFF,
|
|
95
|
+
TOP_COUNT: 254,
|
|
96
|
+
SPLITTER: " \u200B",
|
|
97
|
+
|
|
98
|
+
compress(text, splitter = this.SPLITTER) {
|
|
99
|
+
const freq = {};
|
|
100
|
+
for (let char of text) {
|
|
101
|
+
freq[char] = (freq[char] || 0) + 1;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const topChars = Object.entries(freq)
|
|
105
|
+
.sort((a, b) => b[1] - a[1])
|
|
106
|
+
.slice(0, this.TOP_COUNT)
|
|
107
|
+
.map(entry => entry[0]);
|
|
108
|
+
|
|
109
|
+
const charToIndex = new Map(topChars.map((char, i) => [char, i]));
|
|
110
|
+
|
|
111
|
+
let header = String.fromCharCode(topChars.length) + topChars.join('');
|
|
112
|
+
|
|
113
|
+
let bytes = [];
|
|
114
|
+
for (let char of text) {
|
|
115
|
+
if (charToIndex.has(char)) {
|
|
116
|
+
/* frequent */
|
|
117
|
+
bytes.push(charToIndex.get(char));
|
|
118
|
+
} else {
|
|
119
|
+
/* rare */
|
|
120
|
+
bytes.push(this.ESCAPE_BYTE);
|
|
121
|
+
const code = char.charCodeAt(0);
|
|
122
|
+
bytes.push((code >> 8) & 0xFF);
|
|
123
|
+
bytes.push(code & 0xFF);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/* to UTF16 */
|
|
128
|
+
let compressedBody = "";
|
|
129
|
+
for (let i = 0; i < bytes.length; i += 2) {
|
|
130
|
+
const b1 = bytes[i];
|
|
131
|
+
const b2 = (i + 1 < bytes.length) ? bytes[i + 1] : 0x00;
|
|
132
|
+
compressedBody += String.fromCharCode((b1 << 8) | b2);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return header + splitter + compressedBody;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
decompress(compressedText, splitter = this.SPLITTER) {
|
|
139
|
+
const parts = compressedText.split(splitter);
|
|
140
|
+
|
|
141
|
+
if (parts.length < 2) {
|
|
142
|
+
throw new Error(prefix+'Invalid freqMap data: splitter not found');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const headerPart = parts[0];
|
|
146
|
+
const bodyPart = parts.slice(1).join(splitter);
|
|
147
|
+
|
|
148
|
+
const topCount = headerPart.charCodeAt(0);
|
|
149
|
+
const topChars = headerPart.substring(1, topCount + 1);
|
|
150
|
+
|
|
151
|
+
let bytes = [];
|
|
152
|
+
for (let i = 0; i < bodyPart.length; i++) {
|
|
153
|
+
const code = bodyPart.charCodeAt(i);
|
|
154
|
+
bytes.push((code >> 8) & 0xFF);
|
|
155
|
+
bytes.push(code & 0xFF);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let result = "";
|
|
159
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
160
|
+
const b = bytes[i];
|
|
161
|
+
if (b === this.ESCAPE_BYTE) {
|
|
162
|
+
const charCode = (bytes[i + 1] << 8) | bytes[i + 2];
|
|
163
|
+
result += String.fromCharCode(charCode);
|
|
164
|
+
i += 2;
|
|
165
|
+
} else if (b < topCount) {
|
|
166
|
+
result += topChars[b];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return result;
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* 0 = Fail
|
|
174
|
+
* 1 = Success
|
|
175
|
+
* 2 = Remove last character (Success)
|
|
176
|
+
* @param {string} text
|
|
177
|
+
* @param {string?} splitter
|
|
178
|
+
* @returns {number|[number, number, string, string]}
|
|
179
|
+
*/
|
|
180
|
+
test(text, splitter = this.SPLITTER) {
|
|
181
|
+
try {
|
|
182
|
+
if (text.includes(splitter)) return 0;
|
|
183
|
+
const packed = this.compress(text, splitter);
|
|
184
|
+
const unpacked = this.decompress(packed, splitter);
|
|
185
|
+
if (packed.length < text.length) {
|
|
186
|
+
if (unpacked == text) return [1, packed.length, splitter, packed];
|
|
187
|
+
else if (unpacked.slice(0,-1) == text) return [2, packed.length, splitter, packed];
|
|
188
|
+
else return 0;
|
|
189
|
+
}
|
|
190
|
+
return 0;
|
|
191
|
+
} catch (_) {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const freqMapSplitters = [
|
|
198
|
+
" \u200B","\u0000",
|
|
199
|
+
"\u001F", "\u0001",
|
|
200
|
+
"\uFFFD", "\u2022",
|
|
201
|
+
"|§|", "\uFEFF"
|
|
202
|
+
];
|
|
203
|
+
|
|
204
|
+
if ((String.fromCharCode(65536).charCodeAt(0) === 65536) || !(String.fromCharCode(256).charCodeAt(0) === 256)) {
|
|
205
|
+
throw new Error(prefix+'Supported UTF-16 only!')
|
|
206
|
+
}
|
|
207
|
+
|
|
98
208
|
function charsBase() {
|
|
99
209
|
const charsBase = {};
|
|
100
210
|
function addChar(i) {
|
|
@@ -633,117 +743,6 @@ SOFTWARE.
|
|
|
633
743
|
return result;
|
|
634
744
|
}
|
|
635
745
|
|
|
636
|
-
const freqMap = {
|
|
637
|
-
ESCAPE_BYTE: 0xFF,
|
|
638
|
-
TOP_COUNT: 254,
|
|
639
|
-
SPLITTER: " \u200B",
|
|
640
|
-
|
|
641
|
-
compress(text, splitter = this.SPLITTER) {
|
|
642
|
-
const freq = {};
|
|
643
|
-
for (let char of text) {
|
|
644
|
-
freq[char] = (freq[char] || 0) + 1;
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
const topChars = Object.entries(freq)
|
|
648
|
-
.sort((a, b) => b[1] - a[1])
|
|
649
|
-
.slice(0, this.TOP_COUNT)
|
|
650
|
-
.map(entry => entry[0]);
|
|
651
|
-
|
|
652
|
-
const charToIndex = new Map(topChars.map((char, i) => [char, i]));
|
|
653
|
-
|
|
654
|
-
let header = String.fromCharCode(topChars.length) + topChars.join('');
|
|
655
|
-
|
|
656
|
-
let bytes = [];
|
|
657
|
-
for (let char of text) {
|
|
658
|
-
if (charToIndex.has(char)) {
|
|
659
|
-
/* frequent */
|
|
660
|
-
bytes.push(charToIndex.get(char));
|
|
661
|
-
} else {
|
|
662
|
-
/* rare */
|
|
663
|
-
bytes.push(this.ESCAPE_BYTE);
|
|
664
|
-
const code = char.charCodeAt(0);
|
|
665
|
-
bytes.push((code >> 8) & 0xFF);
|
|
666
|
-
bytes.push(code & 0xFF);
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
/* to UTF16 */
|
|
671
|
-
let compressedBody = "";
|
|
672
|
-
for (let i = 0; i < bytes.length; i += 2) {
|
|
673
|
-
const b1 = bytes[i];
|
|
674
|
-
const b2 = (i + 1 < bytes.length) ? bytes[i + 1] : 0x00;
|
|
675
|
-
compressedBody += String.fromCharCode((b1 << 8) | b2);
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
return header + splitter + compressedBody;
|
|
679
|
-
},
|
|
680
|
-
|
|
681
|
-
decompress(compressedText, splitter = this.SPLITTER) {
|
|
682
|
-
const parts = compressedText.split(splitter);
|
|
683
|
-
|
|
684
|
-
if (parts.length < 2) {
|
|
685
|
-
throw new Error(prefix+'Invalid freqMap data: splitter not found');
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
const headerPart = parts[0];
|
|
689
|
-
const bodyPart = parts.slice(1).join(splitter);
|
|
690
|
-
|
|
691
|
-
const topCount = headerPart.charCodeAt(0);
|
|
692
|
-
const topChars = headerPart.substring(1, topCount + 1);
|
|
693
|
-
|
|
694
|
-
let bytes = [];
|
|
695
|
-
for (let i = 0; i < bodyPart.length; i++) {
|
|
696
|
-
const code = bodyPart.charCodeAt(i);
|
|
697
|
-
bytes.push((code >> 8) & 0xFF);
|
|
698
|
-
bytes.push(code & 0xFF);
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
let result = "";
|
|
702
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
703
|
-
const b = bytes[i];
|
|
704
|
-
if (b === this.ESCAPE_BYTE) {
|
|
705
|
-
const charCode = (bytes[i + 1] << 8) | bytes[i + 2];
|
|
706
|
-
result += String.fromCharCode(charCode);
|
|
707
|
-
i += 2;
|
|
708
|
-
} else if (b < topCount) {
|
|
709
|
-
result += topChars[b];
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
return result;
|
|
713
|
-
},
|
|
714
|
-
|
|
715
|
-
/**
|
|
716
|
-
* 0 = Fail
|
|
717
|
-
* 1 = Success
|
|
718
|
-
* 2 = Remove last character (Success)
|
|
719
|
-
* @param {string} text
|
|
720
|
-
* @param {string?} splitter
|
|
721
|
-
* @returns {number|[number, number, string, string]}
|
|
722
|
-
*/
|
|
723
|
-
test(text, splitter = this.SPLITTER) {
|
|
724
|
-
try {
|
|
725
|
-
if (text.includes(splitter)) return 0;
|
|
726
|
-
const packed = this.compress(text, splitter);
|
|
727
|
-
const unpacked = this.decompress(packed, splitter);
|
|
728
|
-
if (packed.length < text.length) {
|
|
729
|
-
if (unpacked == text) return [1, packed.length, splitter, packed];
|
|
730
|
-
else if (unpacked.slice(0,-1) == text) return [2, packed.length, splitter, packed];
|
|
731
|
-
else return 0;
|
|
732
|
-
}
|
|
733
|
-
return 0;
|
|
734
|
-
} catch (_) {
|
|
735
|
-
return 0;
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
};
|
|
739
|
-
|
|
740
|
-
const freqMapSplitters = [
|
|
741
|
-
" \u200B","\u0000",
|
|
742
|
-
"\u001F", "\u0001",
|
|
743
|
-
"\uFFFD", "\u2022",
|
|
744
|
-
"|§|", "\uFEFF"
|
|
745
|
-
];
|
|
746
|
-
|
|
747
746
|
function segments(str) {
|
|
748
747
|
if (typeof str !== 'string' || str.length === 0) return [];
|
|
749
748
|
|