@chr33s/pdf-unicode-trie 5.0.0
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/LICENSE.md +8 -0
- package/README.md +80 -0
- package/dist/builder.d.ts +12 -0
- package/dist/builder.js +835 -0
- package/dist/builder.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/swap.d.ts +1 -0
- package/dist/swap.js +19 -0
- package/dist/swap.js.map +1 -0
- package/dist/unicode-trie.d.ts +26 -0
- package/dist/unicode-trie.js +110 -0
- package/dist/unicode-trie.js.map +1 -0
- package/package.json +37 -0
- package/src/builder.ts +1012 -0
- package/src/index.ts +4 -0
- package/src/swap.ts +21 -0
- package/src/unicode-trie.ts +148 -0
- package/test/builder.test.ts +151 -0
- package/test/swap.test.ts +36 -0
- package/test/trie.test.ts +125 -0
- package/test/unicode-trie.test.ts +258 -0
- package/tsconfig.json +9 -0
- package/tsconfig.typecheck.json +14 -0
- package/vitest.config.ts +8 -0
package/src/builder.ts
ADDED
|
@@ -0,0 +1,1012 @@
|
|
|
1
|
+
import { deflate } from "@chr33s/pdf-common";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
|
|
4
|
+
import UnicodeTrie from "./index.js";
|
|
5
|
+
import { swap32LE } from "./swap.js";
|
|
6
|
+
|
|
7
|
+
type IntegerArray = Int32Array | Uint32Array;
|
|
8
|
+
|
|
9
|
+
// Shift size for getting the index-1 table offset.
|
|
10
|
+
const SHIFT_1 = 6 + 5;
|
|
11
|
+
|
|
12
|
+
// Shift size for getting the index-2 table offset.
|
|
13
|
+
const SHIFT_2 = 5;
|
|
14
|
+
|
|
15
|
+
// Difference between the two shift sizes,
|
|
16
|
+
// for getting an index-1 offset from an index-2 offset. 6=11-5
|
|
17
|
+
const SHIFT_1_2 = SHIFT_1 - SHIFT_2;
|
|
18
|
+
|
|
19
|
+
// Number of index-1 entries for the BMP. 32=0x20
|
|
20
|
+
// This part of the index-1 table is omitted from the serialized form.
|
|
21
|
+
const OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> SHIFT_1;
|
|
22
|
+
|
|
23
|
+
// Number of code points per index-1 table entry. 2048=0x800
|
|
24
|
+
const CP_PER_INDEX_1_ENTRY = 1 << SHIFT_1;
|
|
25
|
+
|
|
26
|
+
// Number of entries in an index-2 block. 64=0x40
|
|
27
|
+
const INDEX_2_BLOCK_LENGTH = 1 << SHIFT_1_2;
|
|
28
|
+
|
|
29
|
+
// Mask for getting the lower bits for the in-index-2-block offset. */
|
|
30
|
+
const INDEX_2_MASK = INDEX_2_BLOCK_LENGTH - 1;
|
|
31
|
+
|
|
32
|
+
// Number of entries in a data block. 32=0x20
|
|
33
|
+
const DATA_BLOCK_LENGTH = 1 << SHIFT_2;
|
|
34
|
+
|
|
35
|
+
// Mask for getting the lower bits for the in-data-block offset.
|
|
36
|
+
const DATA_MASK = DATA_BLOCK_LENGTH - 1;
|
|
37
|
+
|
|
38
|
+
// Shift size for shifting left the index array values.
|
|
39
|
+
// Increases possible data size with 16-bit index values at the cost
|
|
40
|
+
// of compactability.
|
|
41
|
+
// This requires data blocks to be aligned by DATA_GRANULARITY.
|
|
42
|
+
const INDEX_SHIFT = 2;
|
|
43
|
+
|
|
44
|
+
// The alignment size of a data block. Also the granularity for compaction.
|
|
45
|
+
const DATA_GRANULARITY = 1 << INDEX_SHIFT;
|
|
46
|
+
|
|
47
|
+
// The BMP part of the index-2 table is fixed and linear and starts at offset 0.
|
|
48
|
+
// Length=2048=0x800=0x10000>>SHIFT_2.
|
|
49
|
+
const INDEX_2_OFFSET = 0;
|
|
50
|
+
|
|
51
|
+
// The part of the index-2 table for U+D800..U+DBFF stores values for
|
|
52
|
+
// lead surrogate code _units_ not code _points_.
|
|
53
|
+
// Values for lead surrogate code _points_ are indexed with this portion of the table.
|
|
54
|
+
// Length=32=0x20=0x400>>SHIFT_2. (There are 1024=0x400 lead surrogates.)
|
|
55
|
+
const LSCP_INDEX_2_OFFSET = 0x10000 >> SHIFT_2;
|
|
56
|
+
const LSCP_INDEX_2_LENGTH = 0x400 >> SHIFT_2;
|
|
57
|
+
|
|
58
|
+
// Count the lengths of both BMP pieces. 2080=0x820
|
|
59
|
+
const INDEX_2_BMP_LENGTH = LSCP_INDEX_2_OFFSET + LSCP_INDEX_2_LENGTH;
|
|
60
|
+
|
|
61
|
+
// The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
|
|
62
|
+
// Length 32=0x20 for lead bytes C0..DF, regardless of SHIFT_2.
|
|
63
|
+
const UTF8_2B_INDEX_2_OFFSET = INDEX_2_BMP_LENGTH;
|
|
64
|
+
const UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6; // U+0800 is the first code point after 2-byte UTF-8
|
|
65
|
+
|
|
66
|
+
// The index-1 table, only used for supplementary code points, at offset 2112=0x840.
|
|
67
|
+
// Variable length, for code points up to highStart, where the last single-value range starts.
|
|
68
|
+
// Maximum length 512=0x200=0x100000>>SHIFT_1.
|
|
69
|
+
// (For 0x100000 supplementary code points U+10000..U+10ffff.)
|
|
70
|
+
//
|
|
71
|
+
// The part of the index-2 table for supplementary code points starts
|
|
72
|
+
// after this index-1 table.
|
|
73
|
+
//
|
|
74
|
+
// Both the index-1 table and the following part of the index-2 table
|
|
75
|
+
// are omitted completely if there is only BMP data.
|
|
76
|
+
const INDEX_1_OFFSET = UTF8_2B_INDEX_2_OFFSET + UTF8_2B_INDEX_2_LENGTH;
|
|
77
|
+
const MAX_INDEX_1_LENGTH = 0x100000 >> SHIFT_1;
|
|
78
|
+
|
|
79
|
+
// The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
|
|
80
|
+
// Used with linear access for single bytes 0..0xbf for simple error handling.
|
|
81
|
+
// Length 64=0x40, not DATA_BLOCK_LENGTH.
|
|
82
|
+
const BAD_UTF8_DATA_OFFSET = 0x80;
|
|
83
|
+
|
|
84
|
+
// The start of non-linear-ASCII data blocks, at offset 192=0xc0.
|
|
85
|
+
// !!!!
|
|
86
|
+
const DATA_START_OFFSET = 0xc0;
|
|
87
|
+
|
|
88
|
+
// The null data block.
|
|
89
|
+
// Length 64=0x40 even if DATA_BLOCK_LENGTH is smaller,
|
|
90
|
+
// to work with 6-bit trail bytes from 2-byte UTF-8.
|
|
91
|
+
const DATA_NULL_OFFSET = DATA_START_OFFSET;
|
|
92
|
+
|
|
93
|
+
// The start of allocated data blocks.
|
|
94
|
+
const NEW_DATA_START_OFFSET = DATA_NULL_OFFSET + 0x40;
|
|
95
|
+
|
|
96
|
+
// The start of data blocks for U+0800 and above.
|
|
97
|
+
// Below, compaction uses a block length of 64 for 2-byte UTF-8.
|
|
98
|
+
// From here on, compaction uses DATA_BLOCK_LENGTH.
|
|
99
|
+
// Data values for 0x780 code points beyond ASCII.
|
|
100
|
+
const DATA_0800_OFFSET = NEW_DATA_START_OFFSET + 0x780;
|
|
101
|
+
|
|
102
|
+
// Start with allocation of 16k data entries. */
|
|
103
|
+
const INITIAL_DATA_LENGTH = 1 << 14;
|
|
104
|
+
|
|
105
|
+
// Grow about 8x each time.
|
|
106
|
+
const MEDIUM_DATA_LENGTH = 1 << 17;
|
|
107
|
+
|
|
108
|
+
// Maximum length of the runtime data array.
|
|
109
|
+
// Limited by 16-bit index values that are left-shifted by INDEX_SHIFT,
|
|
110
|
+
// and by uint16_t UTrie2Header.shiftedDataLength.
|
|
111
|
+
const MAX_DATA_LENGTH_RUNTIME = 0xffff << INDEX_SHIFT;
|
|
112
|
+
|
|
113
|
+
const INDEX_1_LENGTH = 0x110000 >> SHIFT_1;
|
|
114
|
+
|
|
115
|
+
// Maximum length of the build-time data array.
|
|
116
|
+
// One entry per 0x110000 code points, plus the illegal-UTF-8 block and the null block,
|
|
117
|
+
// plus values for the 0x400 surrogate code units.
|
|
118
|
+
const MAX_DATA_LENGTH_BUILDTIME = 0x110000 + 0x40 + 0x40 + 0x400;
|
|
119
|
+
|
|
120
|
+
// At build time, leave a gap in the index-2 table,
|
|
121
|
+
// at least as long as the maximum lengths of the 2-byte UTF-8 index-2 table
|
|
122
|
+
// and the supplementary index-1 table.
|
|
123
|
+
// Round up to INDEX_2_BLOCK_LENGTH for proper compacting.
|
|
124
|
+
const INDEX_GAP_OFFSET = INDEX_2_BMP_LENGTH;
|
|
125
|
+
const INDEX_GAP_LENGTH =
|
|
126
|
+
(UTF8_2B_INDEX_2_LENGTH + MAX_INDEX_1_LENGTH + INDEX_2_MASK) & ~INDEX_2_MASK;
|
|
127
|
+
|
|
128
|
+
// Maximum length of the build-time index-2 array.
|
|
129
|
+
// Maximum number of Unicode code points (0x110000) shifted right by SHIFT_2,
|
|
130
|
+
// plus the part of the index-2 table for lead surrogate code points,
|
|
131
|
+
// plus the build-time index gap,
|
|
132
|
+
// plus the null index-2 block.)
|
|
133
|
+
const MAX_INDEX_2_LENGTH =
|
|
134
|
+
(0x110000 >> SHIFT_2) + LSCP_INDEX_2_LENGTH + INDEX_GAP_LENGTH + INDEX_2_BLOCK_LENGTH;
|
|
135
|
+
|
|
136
|
+
// The null index-2 block, following the gap in the index-2 table.
|
|
137
|
+
const INDEX_2_NULL_OFFSET = INDEX_GAP_OFFSET + INDEX_GAP_LENGTH;
|
|
138
|
+
|
|
139
|
+
// The start of allocated index-2 blocks.
|
|
140
|
+
const INDEX_2_START_OFFSET = INDEX_2_NULL_OFFSET + INDEX_2_BLOCK_LENGTH;
|
|
141
|
+
|
|
142
|
+
// Maximum length of the runtime index array.
|
|
143
|
+
// Limited by its own 16-bit index values, and by uint16_t UTrie2Header.indexLength.
|
|
144
|
+
// (The actual maximum length is lower,
|
|
145
|
+
// (0x110000>>SHIFT_2)+UTF8_2B_INDEX_2_LENGTH+MAX_INDEX_1_LENGTH.)
|
|
146
|
+
const MAX_INDEX_LENGTH = 0xffff;
|
|
147
|
+
|
|
148
|
+
const equal_int = (a: IntegerArray, s: number, t: number, length: number): boolean => {
|
|
149
|
+
for (let i = 0; i < length; i++) {
|
|
150
|
+
if (a[s + i] !== a[t + i]) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return true;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
class UnicodeTrieBuilder {
|
|
159
|
+
#initialValue: number;
|
|
160
|
+
#errorValue: number;
|
|
161
|
+
#index1: Int32Array;
|
|
162
|
+
#index2: Int32Array;
|
|
163
|
+
#highStart: number;
|
|
164
|
+
#data: Uint32Array;
|
|
165
|
+
#dataCapacity: number;
|
|
166
|
+
#firstFreeBlock: number;
|
|
167
|
+
#isCompacted: boolean;
|
|
168
|
+
#map: Int32Array;
|
|
169
|
+
#dataNullOffset: number;
|
|
170
|
+
#dataLength: number;
|
|
171
|
+
#index2NullOffset: number;
|
|
172
|
+
#index2Length: number;
|
|
173
|
+
constructor(initialValue: number | null = 0, errorValue: number | null = 0) {
|
|
174
|
+
let i, j;
|
|
175
|
+
if (initialValue == null) {
|
|
176
|
+
initialValue = 0;
|
|
177
|
+
}
|
|
178
|
+
this.#initialValue = initialValue;
|
|
179
|
+
if (errorValue == null) {
|
|
180
|
+
errorValue = 0;
|
|
181
|
+
}
|
|
182
|
+
this.#errorValue = errorValue;
|
|
183
|
+
this.#index1 = new Int32Array(INDEX_1_LENGTH);
|
|
184
|
+
this.#index2 = new Int32Array(MAX_INDEX_2_LENGTH);
|
|
185
|
+
this.#highStart = 0x110000;
|
|
186
|
+
|
|
187
|
+
this.#data = new Uint32Array(INITIAL_DATA_LENGTH);
|
|
188
|
+
this.#dataCapacity = INITIAL_DATA_LENGTH;
|
|
189
|
+
|
|
190
|
+
this.#firstFreeBlock = 0;
|
|
191
|
+
this.#isCompacted = false;
|
|
192
|
+
|
|
193
|
+
// Multi-purpose per-data-block table.
|
|
194
|
+
//
|
|
195
|
+
// Before compacting:
|
|
196
|
+
//
|
|
197
|
+
// Per-data-block reference counters/free-block list.
|
|
198
|
+
// 0: unused
|
|
199
|
+
// >0: reference counter (number of index-2 entries pointing here)
|
|
200
|
+
// <0: next free data block in free-block list
|
|
201
|
+
//
|
|
202
|
+
// While compacting:
|
|
203
|
+
//
|
|
204
|
+
// Map of adjusted indexes, used in compactData() and compactIndex2().
|
|
205
|
+
// Maps from original indexes to new ones.
|
|
206
|
+
this.#map = new Int32Array(MAX_DATA_LENGTH_BUILDTIME >> SHIFT_2);
|
|
207
|
+
|
|
208
|
+
for (i = 0; i < 0x80; i++) {
|
|
209
|
+
this.#data[i] = this.#initialValue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
for (; i < 0xc0; i++) {
|
|
213
|
+
this.#data[i] = this.#errorValue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
for (i = DATA_NULL_OFFSET; i < NEW_DATA_START_OFFSET; i++) {
|
|
217
|
+
this.#data[i] = this.#initialValue;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
this.#dataNullOffset = DATA_NULL_OFFSET;
|
|
221
|
+
this.#dataLength = NEW_DATA_START_OFFSET;
|
|
222
|
+
|
|
223
|
+
// set the index-2 indexes for the 2=0x80>>SHIFT_2 ASCII data blocks
|
|
224
|
+
i = 0;
|
|
225
|
+
for (j = 0; j < 0x80; j += DATA_BLOCK_LENGTH) {
|
|
226
|
+
this.#index2[i] = j;
|
|
227
|
+
this.#map[i++] = 1;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// reference counts for the bad-UTF-8-data block
|
|
231
|
+
for (; j < 0xc0; j += DATA_BLOCK_LENGTH) {
|
|
232
|
+
this.#map[i++] = 0;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Reference counts for the null data block: all blocks except for the ASCII blocks.
|
|
236
|
+
// Plus 1 so that we don't drop this block during compaction.
|
|
237
|
+
// Plus as many as needed for lead surrogate code points.
|
|
238
|
+
// i==newTrie->dataNullOffset
|
|
239
|
+
this.#map[i++] = (0x110000 >> SHIFT_2) - (0x80 >> SHIFT_2) + 1 + LSCP_INDEX_2_LENGTH;
|
|
240
|
+
j += DATA_BLOCK_LENGTH;
|
|
241
|
+
for (; j < NEW_DATA_START_OFFSET; j += DATA_BLOCK_LENGTH) {
|
|
242
|
+
this.#map[i++] = 0;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// set the remaining indexes in the BMP index-2 block
|
|
246
|
+
// to the null data block
|
|
247
|
+
for (i = 0x80 >> SHIFT_2; i < INDEX_2_BMP_LENGTH; i++) {
|
|
248
|
+
this.#index2[i] = DATA_NULL_OFFSET;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Fill the index gap with impossible values so that compaction
|
|
252
|
+
// does not overlap other index-2 blocks with the gap.
|
|
253
|
+
for (i = 0; i < INDEX_GAP_LENGTH; i++) {
|
|
254
|
+
this.#index2[INDEX_GAP_OFFSET + i] = -1;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// set the indexes in the null index-2 block
|
|
258
|
+
for (i = 0; i < INDEX_2_BLOCK_LENGTH; i++) {
|
|
259
|
+
this.#index2[INDEX_2_NULL_OFFSET + i] = DATA_NULL_OFFSET;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this.#index2NullOffset = INDEX_2_NULL_OFFSET;
|
|
263
|
+
this.#index2Length = INDEX_2_START_OFFSET;
|
|
264
|
+
|
|
265
|
+
// set the index-1 indexes for the linear index-2 block
|
|
266
|
+
j = 0;
|
|
267
|
+
for (i = 0; i < OMITTED_BMP_INDEX_1_LENGTH; i++) {
|
|
268
|
+
this.#index1[i] = j;
|
|
269
|
+
j += INDEX_2_BLOCK_LENGTH;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// set the remaining index-1 indexes to the null index-2 block
|
|
273
|
+
for (; i < INDEX_1_LENGTH; i++) {
|
|
274
|
+
this.#index1[i] = INDEX_2_NULL_OFFSET;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Preallocate and reset data for U+0080..U+07ff,
|
|
278
|
+
// for 2-byte UTF-8 which will be compacted in 64-blocks
|
|
279
|
+
// even if DATA_BLOCK_LENGTH is smaller.
|
|
280
|
+
for (i = 0x80; i < 0x800; i += DATA_BLOCK_LENGTH) {
|
|
281
|
+
this.set(i, this.#initialValue);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
set(codePoint: number, value: number): this {
|
|
286
|
+
if (codePoint < 0 || codePoint > 0x10ffff) {
|
|
287
|
+
throw new Error("Invalid code point");
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (this.#isCompacted) {
|
|
291
|
+
throw new Error("Already compacted");
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const block = this.#getDataBlock(codePoint, true);
|
|
295
|
+
this.#data[block + (codePoint & DATA_MASK)] = value;
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
setRange(start: number, end: number, value: number, overwrite: boolean | null = true): this {
|
|
300
|
+
let block, repeatBlock;
|
|
301
|
+
const shouldOverwrite = overwrite ?? true;
|
|
302
|
+
if (start > 0x10ffff || end > 0x10ffff || start > end) {
|
|
303
|
+
throw new Error("Invalid code point");
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (this.#isCompacted) {
|
|
307
|
+
throw new Error("Already compacted");
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (!shouldOverwrite && value === this.#initialValue) {
|
|
311
|
+
return this; // nothing to do
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
let limit = end + 1;
|
|
315
|
+
if ((start & DATA_MASK) !== 0) {
|
|
316
|
+
// set partial block at [start..following block boundary
|
|
317
|
+
block = this.#getDataBlock(start, true);
|
|
318
|
+
|
|
319
|
+
const nextStart = (start + DATA_BLOCK_LENGTH) & ~DATA_MASK;
|
|
320
|
+
if (nextStart <= limit) {
|
|
321
|
+
this.#fillBlock(
|
|
322
|
+
block,
|
|
323
|
+
start & DATA_MASK,
|
|
324
|
+
DATA_BLOCK_LENGTH,
|
|
325
|
+
value,
|
|
326
|
+
this.#initialValue,
|
|
327
|
+
shouldOverwrite,
|
|
328
|
+
);
|
|
329
|
+
start = nextStart;
|
|
330
|
+
} else {
|
|
331
|
+
this.#fillBlock(
|
|
332
|
+
block,
|
|
333
|
+
start & DATA_MASK,
|
|
334
|
+
limit & DATA_MASK,
|
|
335
|
+
value,
|
|
336
|
+
this.#initialValue,
|
|
337
|
+
shouldOverwrite,
|
|
338
|
+
);
|
|
339
|
+
return this;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// number of positions in the last, partial block
|
|
344
|
+
const rest = limit & DATA_MASK;
|
|
345
|
+
|
|
346
|
+
// round down limit to a block boundary
|
|
347
|
+
limit &= ~DATA_MASK;
|
|
348
|
+
|
|
349
|
+
// iterate over all-value blocks
|
|
350
|
+
if (value === this.#initialValue) {
|
|
351
|
+
repeatBlock = this.#dataNullOffset;
|
|
352
|
+
} else {
|
|
353
|
+
repeatBlock = -1;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
while (start < limit) {
|
|
357
|
+
let setRepeatBlock = false;
|
|
358
|
+
|
|
359
|
+
if (value === this.#initialValue && this.#isInNullBlock(start, true)) {
|
|
360
|
+
start += DATA_BLOCK_LENGTH; // nothing to do
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// get index value
|
|
365
|
+
let i2 = this.#getIndex2Block(start, true);
|
|
366
|
+
i2 += (start >> SHIFT_2) & INDEX_2_MASK;
|
|
367
|
+
|
|
368
|
+
block = this.#index2[i2];
|
|
369
|
+
if (this.#isWritableBlock(block)) {
|
|
370
|
+
// already allocated
|
|
371
|
+
if (shouldOverwrite && block >= DATA_0800_OFFSET) {
|
|
372
|
+
// We overwrite all values, and it's not a
|
|
373
|
+
// protected (ASCII-linear or 2-byte UTF-8) block:
|
|
374
|
+
// replace with the repeatBlock.
|
|
375
|
+
setRepeatBlock = true;
|
|
376
|
+
} else {
|
|
377
|
+
// protected block: just write the values into this block
|
|
378
|
+
this.#fillBlock(block, 0, DATA_BLOCK_LENGTH, value, this.#initialValue, shouldOverwrite);
|
|
379
|
+
}
|
|
380
|
+
} else if (
|
|
381
|
+
this.#data[block] !== value &&
|
|
382
|
+
(shouldOverwrite || block === this.#dataNullOffset)
|
|
383
|
+
) {
|
|
384
|
+
// Set the repeatBlock instead of the null block or previous repeat block:
|
|
385
|
+
//
|
|
386
|
+
// If !isWritableBlock() then all entries in the block have the same value
|
|
387
|
+
// because it's the null block or a range block (the repeatBlock from a previous
|
|
388
|
+
// call to utrie2_setRange32()).
|
|
389
|
+
// No other blocks are used multiple times before compacting.
|
|
390
|
+
//
|
|
391
|
+
// The null block is the only non-writable block with the initialValue because
|
|
392
|
+
// of the repeatBlock initialization above. (If value==initialValue, then
|
|
393
|
+
// the repeatBlock will be the null data block.)
|
|
394
|
+
//
|
|
395
|
+
// We set our repeatBlock if the desired value differs from the block's value,
|
|
396
|
+
// and if we overwrite any data or if the data is all initial values
|
|
397
|
+
// (which is the same as the block being the null block, see above).
|
|
398
|
+
setRepeatBlock = true;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (setRepeatBlock) {
|
|
402
|
+
if (repeatBlock >= 0) {
|
|
403
|
+
this.#setIndex2Entry(i2, repeatBlock);
|
|
404
|
+
} else {
|
|
405
|
+
// create and set and fill the repeatBlock
|
|
406
|
+
repeatBlock = this.#getDataBlock(start, true);
|
|
407
|
+
this.#writeBlock(repeatBlock, value);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
start += DATA_BLOCK_LENGTH;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (rest > 0) {
|
|
415
|
+
// set partial block at [last block boundary..limit
|
|
416
|
+
block = this.#getDataBlock(start, true);
|
|
417
|
+
this.#fillBlock(block, 0, rest, value, this.#initialValue, shouldOverwrite);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return this;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
get(c: number, fromLSCP: boolean | null = true): number {
|
|
424
|
+
let i2;
|
|
425
|
+
const useLSCP = fromLSCP ?? true;
|
|
426
|
+
if (c < 0 || c > 0x10ffff) {
|
|
427
|
+
return this.#errorValue;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (c >= this.#highStart && (!(c >= 0xd800 && c < 0xdc00) || useLSCP)) {
|
|
431
|
+
return this.#data[this.#dataLength - DATA_GRANULARITY];
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (c >= 0xd800 && c < 0xdc00 && useLSCP) {
|
|
435
|
+
i2 = LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2) + (c >> SHIFT_2);
|
|
436
|
+
} else {
|
|
437
|
+
i2 = this.#index1[c >> SHIFT_1] + ((c >> SHIFT_2) & INDEX_2_MASK);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const block = this.#index2[i2];
|
|
441
|
+
return this.#data[block + (c & DATA_MASK)];
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
#isInNullBlock(c: number, forLSCP: boolean): boolean {
|
|
445
|
+
let i2;
|
|
446
|
+
if ((c & 0xfffffc00) === 0xd800 && forLSCP) {
|
|
447
|
+
i2 = LSCP_INDEX_2_OFFSET - (0xd800 >> SHIFT_2) + (c >> SHIFT_2);
|
|
448
|
+
} else {
|
|
449
|
+
i2 = this.#index1[c >> SHIFT_1] + ((c >> SHIFT_2) & INDEX_2_MASK);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const block = this.#index2[i2];
|
|
453
|
+
return block === this.#dataNullOffset;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
#allocIndex2Block(): number {
|
|
457
|
+
const newBlock = this.#index2Length;
|
|
458
|
+
const newTop = newBlock + INDEX_2_BLOCK_LENGTH;
|
|
459
|
+
if (newTop > this.#index2.length) {
|
|
460
|
+
// Should never occur.
|
|
461
|
+
// Either MAX_BUILD_TIME_INDEX_LENGTH is incorrect,
|
|
462
|
+
// or the code writes more values than should be possible.
|
|
463
|
+
throw new Error("Internal error in Trie2 creation.");
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
this.#index2Length = newTop;
|
|
467
|
+
this.#index2.set(
|
|
468
|
+
this.#index2.subarray(this.#index2NullOffset, this.#index2NullOffset + INDEX_2_BLOCK_LENGTH),
|
|
469
|
+
newBlock,
|
|
470
|
+
);
|
|
471
|
+
|
|
472
|
+
return newBlock;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
#getIndex2Block(c: number, forLSCP: boolean): number {
|
|
476
|
+
if (c >= 0xd800 && c < 0xdc00 && forLSCP) {
|
|
477
|
+
return LSCP_INDEX_2_OFFSET;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const i1 = c >> SHIFT_1;
|
|
481
|
+
let i2 = this.#index1[i1];
|
|
482
|
+
if (i2 === this.#index2NullOffset) {
|
|
483
|
+
i2 = this.#allocIndex2Block();
|
|
484
|
+
this.#index1[i1] = i2;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
return i2;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
#isWritableBlock(block: number): boolean {
|
|
491
|
+
return block !== this.#dataNullOffset && this.#map[block >> SHIFT_2] === 1;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
#allocDataBlock(copyBlock: number): number {
|
|
495
|
+
let newBlock;
|
|
496
|
+
if (this.#firstFreeBlock !== 0) {
|
|
497
|
+
// get the first free block
|
|
498
|
+
newBlock = this.#firstFreeBlock;
|
|
499
|
+
this.#firstFreeBlock = -this.#map[newBlock >> SHIFT_2];
|
|
500
|
+
} else {
|
|
501
|
+
// get a new block from the high end
|
|
502
|
+
newBlock = this.#dataLength;
|
|
503
|
+
const newTop = newBlock + DATA_BLOCK_LENGTH;
|
|
504
|
+
if (newTop > this.#dataCapacity) {
|
|
505
|
+
// out of memory in the data array
|
|
506
|
+
let capacity;
|
|
507
|
+
if (this.#dataCapacity < MEDIUM_DATA_LENGTH) {
|
|
508
|
+
capacity = MEDIUM_DATA_LENGTH;
|
|
509
|
+
} else if (this.#dataCapacity < MAX_DATA_LENGTH_BUILDTIME) {
|
|
510
|
+
capacity = MAX_DATA_LENGTH_BUILDTIME;
|
|
511
|
+
} else {
|
|
512
|
+
// Should never occur.
|
|
513
|
+
// Either MAX_DATA_LENGTH_BUILDTIME is incorrect,
|
|
514
|
+
// or the code writes more values than should be possible.
|
|
515
|
+
throw new Error("Internal error in Trie2 creation.");
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const newData = new Uint32Array(capacity);
|
|
519
|
+
newData.set(this.#data.subarray(0, this.#dataLength));
|
|
520
|
+
this.#data = newData;
|
|
521
|
+
this.#dataCapacity = capacity;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
this.#dataLength = newTop;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
this.#data.set(this.#data.subarray(copyBlock, copyBlock + DATA_BLOCK_LENGTH), newBlock);
|
|
528
|
+
this.#map[newBlock >> SHIFT_2] = 0;
|
|
529
|
+
return newBlock;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
#releaseDataBlock(block: number): void {
|
|
533
|
+
// put this block at the front of the free-block chain
|
|
534
|
+
this.#map[block >> SHIFT_2] = -this.#firstFreeBlock;
|
|
535
|
+
this.#firstFreeBlock = block;
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
#setIndex2Entry(i2: number, block: number): void {
|
|
539
|
+
++this.#map[block >> SHIFT_2]; // increment first, in case block == oldBlock!
|
|
540
|
+
const oldBlock = this.#index2[i2];
|
|
541
|
+
if (--this.#map[oldBlock >> SHIFT_2] === 0) {
|
|
542
|
+
this.#releaseDataBlock(oldBlock);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
this.#index2[i2] = block;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
#getDataBlock(c: number, forLSCP: boolean): number {
|
|
549
|
+
let i2 = this.#getIndex2Block(c, forLSCP);
|
|
550
|
+
i2 += (c >> SHIFT_2) & INDEX_2_MASK;
|
|
551
|
+
|
|
552
|
+
const oldBlock = this.#index2[i2];
|
|
553
|
+
if (this.#isWritableBlock(oldBlock)) {
|
|
554
|
+
return oldBlock;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// allocate a new data block
|
|
558
|
+
const newBlock = this.#allocDataBlock(oldBlock);
|
|
559
|
+
this.#setIndex2Entry(i2, newBlock);
|
|
560
|
+
return newBlock;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
#fillBlock(
|
|
564
|
+
block: number,
|
|
565
|
+
start: number,
|
|
566
|
+
limit: number,
|
|
567
|
+
value: number,
|
|
568
|
+
initialValue: number,
|
|
569
|
+
overwrite: boolean,
|
|
570
|
+
): void {
|
|
571
|
+
let i;
|
|
572
|
+
if (overwrite) {
|
|
573
|
+
for (i = block + start; i < block + limit; i++) {
|
|
574
|
+
this.#data[i] = value;
|
|
575
|
+
}
|
|
576
|
+
} else {
|
|
577
|
+
for (i = block + start; i < block + limit; i++) {
|
|
578
|
+
if (this.#data[i] === initialValue) {
|
|
579
|
+
this.#data[i] = value;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
#writeBlock(block: number, value: number): void {
|
|
586
|
+
const limit = block + DATA_BLOCK_LENGTH;
|
|
587
|
+
while (block < limit) {
|
|
588
|
+
this.#data[block++] = value;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
#findHighStart(highValue: number): number {
|
|
593
|
+
let prevBlock, prevI2Block;
|
|
594
|
+
const data32 = this.#data;
|
|
595
|
+
const initialValue = this.#initialValue;
|
|
596
|
+
const index2NullOffset = this.#index2NullOffset;
|
|
597
|
+
const nullBlock = this.#dataNullOffset;
|
|
598
|
+
|
|
599
|
+
// set variables for previous range
|
|
600
|
+
if (highValue === initialValue) {
|
|
601
|
+
prevI2Block = index2NullOffset;
|
|
602
|
+
prevBlock = nullBlock;
|
|
603
|
+
} else {
|
|
604
|
+
prevI2Block = -1;
|
|
605
|
+
prevBlock = -1;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
const prev = 0x110000;
|
|
609
|
+
|
|
610
|
+
// enumerate index-2 blocks
|
|
611
|
+
let i1 = INDEX_1_LENGTH;
|
|
612
|
+
let c = prev;
|
|
613
|
+
while (c > 0) {
|
|
614
|
+
const i2Block = this.#index1[--i1];
|
|
615
|
+
if (i2Block === prevI2Block) {
|
|
616
|
+
// the index-2 block is the same as the previous one, and filled with highValue
|
|
617
|
+
c -= CP_PER_INDEX_1_ENTRY;
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
prevI2Block = i2Block;
|
|
622
|
+
if (i2Block === index2NullOffset) {
|
|
623
|
+
// this is the null index-2 block
|
|
624
|
+
if (highValue !== initialValue) {
|
|
625
|
+
return c;
|
|
626
|
+
}
|
|
627
|
+
c -= CP_PER_INDEX_1_ENTRY;
|
|
628
|
+
} else {
|
|
629
|
+
// enumerate data blocks for one index-2 block
|
|
630
|
+
let i2 = INDEX_2_BLOCK_LENGTH;
|
|
631
|
+
while (i2 > 0) {
|
|
632
|
+
const block = this.#index2[i2Block + --i2];
|
|
633
|
+
if (block === prevBlock) {
|
|
634
|
+
// the block is the same as the previous one, and filled with highValue
|
|
635
|
+
c -= DATA_BLOCK_LENGTH;
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
prevBlock = block;
|
|
640
|
+
if (block === nullBlock) {
|
|
641
|
+
// this is the null data block
|
|
642
|
+
if (highValue !== initialValue) {
|
|
643
|
+
return c;
|
|
644
|
+
}
|
|
645
|
+
c -= DATA_BLOCK_LENGTH;
|
|
646
|
+
} else {
|
|
647
|
+
let j = DATA_BLOCK_LENGTH;
|
|
648
|
+
while (j > 0) {
|
|
649
|
+
const value = data32[block + --j];
|
|
650
|
+
if (value !== highValue) {
|
|
651
|
+
return c;
|
|
652
|
+
}
|
|
653
|
+
--c;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// deliver last range
|
|
661
|
+
return 0;
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
#findSameDataBlock(dataLength: number, otherBlock: number, blockLength: number): number {
|
|
665
|
+
// ensure that we do not even partially get past dataLength
|
|
666
|
+
dataLength -= blockLength;
|
|
667
|
+
let block = 0;
|
|
668
|
+
while (block <= dataLength) {
|
|
669
|
+
if (equal_int(this.#data, block, otherBlock, blockLength)) {
|
|
670
|
+
return block;
|
|
671
|
+
}
|
|
672
|
+
block += DATA_GRANULARITY;
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
return -1;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
#findSameIndex2Block(index2Length: number, otherBlock: number): number {
|
|
679
|
+
// ensure that we do not even partially get past index2Length
|
|
680
|
+
index2Length -= INDEX_2_BLOCK_LENGTH;
|
|
681
|
+
for (let block = 0; block <= index2Length; block++) {
|
|
682
|
+
if (equal_int(this.#index2, block, otherBlock, INDEX_2_BLOCK_LENGTH)) {
|
|
683
|
+
return block;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
return -1;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
#compactData(): void {
|
|
691
|
+
// do not compact linear-ASCII data
|
|
692
|
+
let newStart = DATA_START_OFFSET;
|
|
693
|
+
let start = 0;
|
|
694
|
+
let i = 0;
|
|
695
|
+
|
|
696
|
+
while (start < newStart) {
|
|
697
|
+
this.#map[i++] = start;
|
|
698
|
+
start += DATA_BLOCK_LENGTH;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// Start with a block length of 64 for 2-byte UTF-8,
|
|
702
|
+
// then switch to DATA_BLOCK_LENGTH.
|
|
703
|
+
let blockLength = 64;
|
|
704
|
+
let blockCount = blockLength >> SHIFT_2;
|
|
705
|
+
start = newStart;
|
|
706
|
+
while (start < this.#dataLength) {
|
|
707
|
+
// start: index of first entry of current block
|
|
708
|
+
// newStart: index where the current block is to be moved
|
|
709
|
+
// (right after current end of already-compacted data)
|
|
710
|
+
let mapIndex, movedStart;
|
|
711
|
+
if (start === DATA_0800_OFFSET) {
|
|
712
|
+
blockLength = DATA_BLOCK_LENGTH;
|
|
713
|
+
blockCount = 1;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
// skip blocks that are not used
|
|
717
|
+
if (this.#map[start >> SHIFT_2] <= 0) {
|
|
718
|
+
// advance start to the next block
|
|
719
|
+
start += blockLength;
|
|
720
|
+
|
|
721
|
+
// leave newStart with the previous block!
|
|
722
|
+
continue;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// search for an identical block
|
|
726
|
+
if ((movedStart = this.#findSameDataBlock(newStart, start, blockLength)) >= 0) {
|
|
727
|
+
// found an identical block, set the other block's index value for the current block
|
|
728
|
+
mapIndex = start >> SHIFT_2;
|
|
729
|
+
for (i = blockCount; i > 0; i--) {
|
|
730
|
+
this.#map[mapIndex++] = movedStart;
|
|
731
|
+
movedStart += DATA_BLOCK_LENGTH;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// advance start to the next block
|
|
735
|
+
start += blockLength;
|
|
736
|
+
|
|
737
|
+
// leave newStart with the previous block!
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
// see if the beginning of this block can be overlapped with the end of the previous block
|
|
742
|
+
// look for maximum overlap (modulo granularity) with the previous, adjacent block
|
|
743
|
+
let overlap = blockLength - DATA_GRANULARITY;
|
|
744
|
+
while (overlap > 0 && !equal_int(this.#data, newStart - overlap, start, overlap)) {
|
|
745
|
+
overlap -= DATA_GRANULARITY;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
if (overlap > 0 || newStart < start) {
|
|
749
|
+
// some overlap, or just move the whole block
|
|
750
|
+
movedStart = newStart - overlap;
|
|
751
|
+
mapIndex = start >> SHIFT_2;
|
|
752
|
+
|
|
753
|
+
for (i = blockCount; i > 0; i--) {
|
|
754
|
+
this.#map[mapIndex++] = movedStart;
|
|
755
|
+
movedStart += DATA_BLOCK_LENGTH;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// move the non-overlapping indexes to their new positions
|
|
759
|
+
start += overlap;
|
|
760
|
+
for (i = blockLength - overlap; i > 0; i--) {
|
|
761
|
+
this.#data[newStart++] = this.#data[start++];
|
|
762
|
+
}
|
|
763
|
+
} else {
|
|
764
|
+
// no overlap && newStart==start
|
|
765
|
+
mapIndex = start >> SHIFT_2;
|
|
766
|
+
for (i = blockCount; i > 0; i--) {
|
|
767
|
+
this.#map[mapIndex++] = start;
|
|
768
|
+
start += DATA_BLOCK_LENGTH;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
newStart = start;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
// now adjust the index-2 table
|
|
776
|
+
i = 0;
|
|
777
|
+
while (i < this.#index2Length) {
|
|
778
|
+
// Gap indexes are invalid (-1). Skip over the gap.
|
|
779
|
+
if (i === INDEX_GAP_OFFSET) {
|
|
780
|
+
i += INDEX_GAP_LENGTH;
|
|
781
|
+
}
|
|
782
|
+
this.#index2[i] = this.#map[this.#index2[i] >> SHIFT_2];
|
|
783
|
+
++i;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
this.#dataNullOffset = this.#map[this.#dataNullOffset >> SHIFT_2];
|
|
787
|
+
|
|
788
|
+
// ensure dataLength alignment
|
|
789
|
+
while ((newStart & (DATA_GRANULARITY - 1)) !== 0) {
|
|
790
|
+
this.#data[newStart++] = this.#initialValue;
|
|
791
|
+
}
|
|
792
|
+
this.#dataLength = newStart;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
#compactIndex2(): void {
|
|
796
|
+
// do not compact linear-BMP index-2 blocks
|
|
797
|
+
let newStart = INDEX_2_BMP_LENGTH;
|
|
798
|
+
let start = 0;
|
|
799
|
+
let i = 0;
|
|
800
|
+
|
|
801
|
+
while (start < newStart) {
|
|
802
|
+
this.#map[i++] = start;
|
|
803
|
+
start += INDEX_2_BLOCK_LENGTH;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// Reduce the index table gap to what will be needed at runtime.
|
|
807
|
+
newStart += UTF8_2B_INDEX_2_LENGTH + ((this.#highStart - 0x10000) >> SHIFT_1);
|
|
808
|
+
start = INDEX_2_NULL_OFFSET;
|
|
809
|
+
while (start < this.#index2Length) {
|
|
810
|
+
// start: index of first entry of current block
|
|
811
|
+
// newStart: index where the current block is to be moved
|
|
812
|
+
// (right after current end of already-compacted data)
|
|
813
|
+
|
|
814
|
+
// search for an identical block
|
|
815
|
+
let movedStart;
|
|
816
|
+
if ((movedStart = this.#findSameIndex2Block(newStart, start)) >= 0) {
|
|
817
|
+
// found an identical block, set the other block's index value for the current block
|
|
818
|
+
this.#map[start >> SHIFT_1_2] = movedStart;
|
|
819
|
+
|
|
820
|
+
// advance start to the next block
|
|
821
|
+
start += INDEX_2_BLOCK_LENGTH;
|
|
822
|
+
|
|
823
|
+
// leave newStart with the previous block!
|
|
824
|
+
continue;
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
// see if the beginning of this block can be overlapped with the end of the previous block
|
|
828
|
+
// look for maximum overlap with the previous, adjacent block
|
|
829
|
+
let overlap = INDEX_2_BLOCK_LENGTH - 1;
|
|
830
|
+
while (overlap > 0 && !equal_int(this.#index2, newStart - overlap, start, overlap)) {
|
|
831
|
+
--overlap;
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
if (overlap > 0 || newStart < start) {
|
|
835
|
+
// some overlap, or just move the whole block
|
|
836
|
+
this.#map[start >> SHIFT_1_2] = newStart - overlap;
|
|
837
|
+
|
|
838
|
+
// move the non-overlapping indexes to their new positions
|
|
839
|
+
start += overlap;
|
|
840
|
+
for (i = INDEX_2_BLOCK_LENGTH - overlap; i > 0; i--) {
|
|
841
|
+
this.#index2[newStart++] = this.#index2[start++];
|
|
842
|
+
}
|
|
843
|
+
} else {
|
|
844
|
+
// no overlap && newStart==start
|
|
845
|
+
this.#map[start >> SHIFT_1_2] = start;
|
|
846
|
+
start += INDEX_2_BLOCK_LENGTH;
|
|
847
|
+
newStart = start;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// now adjust the index-1 table
|
|
852
|
+
for (i = 0; i < INDEX_1_LENGTH; i++) {
|
|
853
|
+
this.#index1[i] = this.#map[this.#index1[i] >> SHIFT_1_2];
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
this.#index2NullOffset = this.#map[this.#index2NullOffset >> SHIFT_1_2];
|
|
857
|
+
|
|
858
|
+
// Ensure data table alignment:
|
|
859
|
+
// Needs to be granularity-aligned for 16-bit trie
|
|
860
|
+
// (so that dataMove will be down-shiftable),
|
|
861
|
+
// and 2-aligned for uint32_t data.
|
|
862
|
+
|
|
863
|
+
// Arbitrary value: 0x3fffc not possible for real data.
|
|
864
|
+
while ((newStart & ((DATA_GRANULARITY - 1) | 1)) !== 0) {
|
|
865
|
+
this.#index2[newStart++] = 0x0000ffff << INDEX_SHIFT;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
this.#index2Length = newStart;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
#compact(): void {
|
|
872
|
+
// find highStart and round it up
|
|
873
|
+
let highValue = this.get(0x10ffff);
|
|
874
|
+
let highStart = this.#findHighStart(highValue);
|
|
875
|
+
highStart = (highStart + (CP_PER_INDEX_1_ENTRY - 1)) & ~(CP_PER_INDEX_1_ENTRY - 1);
|
|
876
|
+
if (highStart === 0x110000) {
|
|
877
|
+
highValue = this.#errorValue;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
// Set trie->highStart only after utrie2_get32(trie, highStart).
|
|
881
|
+
// Otherwise utrie2_get32(trie, highStart) would try to read the highValue.
|
|
882
|
+
this.#highStart = highStart;
|
|
883
|
+
if (this.#highStart < 0x110000) {
|
|
884
|
+
// Blank out [highStart..10ffff] to release associated data blocks.
|
|
885
|
+
const suppHighStart = this.#highStart <= 0x10000 ? 0x10000 : this.#highStart;
|
|
886
|
+
this.setRange(suppHighStart, 0x10ffff, this.#initialValue, true);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
this.#compactData();
|
|
890
|
+
if (this.#highStart > 0x10000) {
|
|
891
|
+
this.#compactIndex2();
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
// Store the highValue in the data array and round up the dataLength.
|
|
895
|
+
// Must be done after compactData() because that assumes that dataLength
|
|
896
|
+
// is a multiple of DATA_BLOCK_LENGTH.
|
|
897
|
+
this.#data[this.#dataLength++] = highValue;
|
|
898
|
+
while ((this.#dataLength & (DATA_GRANULARITY - 1)) !== 0) {
|
|
899
|
+
this.#data[this.#dataLength++] = this.#initialValue;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
this.#isCompacted = true;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
freeze(): UnicodeTrie {
|
|
906
|
+
let allIndexesLength, i;
|
|
907
|
+
if (!this.#isCompacted) {
|
|
908
|
+
this.#compact();
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
if (this.#highStart <= 0x10000) {
|
|
912
|
+
allIndexesLength = INDEX_1_OFFSET;
|
|
913
|
+
} else {
|
|
914
|
+
allIndexesLength = this.#index2Length;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
const dataMove = allIndexesLength;
|
|
918
|
+
|
|
919
|
+
// are indexLength and dataLength within limits?
|
|
920
|
+
if (
|
|
921
|
+
allIndexesLength > MAX_INDEX_LENGTH || // for unshifted indexLength
|
|
922
|
+
dataMove + this.#dataNullOffset > 0xffff || // for unshifted dataNullOffset
|
|
923
|
+
dataMove + DATA_0800_OFFSET > 0xffff || // for unshifted 2-byte UTF-8 index-2 values
|
|
924
|
+
dataMove + this.#dataLength > MAX_DATA_LENGTH_RUNTIME
|
|
925
|
+
) {
|
|
926
|
+
// for shiftedDataLength
|
|
927
|
+
throw new Error("Trie data is too large.");
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// calculate the sizes of, and allocate, the index and data arrays
|
|
931
|
+
const indexLength = allIndexesLength + this.#dataLength;
|
|
932
|
+
const data = new Uint32Array(indexLength);
|
|
933
|
+
|
|
934
|
+
// write the index-2 array values shifted right by INDEX_SHIFT, after adding dataMove
|
|
935
|
+
let destIdx = 0;
|
|
936
|
+
for (i = 0; i < INDEX_2_BMP_LENGTH; i++) {
|
|
937
|
+
data[destIdx++] = (this.#index2[i] + dataMove) >> INDEX_SHIFT;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// write UTF-8 2-byte index-2 values, not right-shifted
|
|
941
|
+
for (i = 0; i < 0xc2 - 0xc0; i++) {
|
|
942
|
+
// C0..C1
|
|
943
|
+
data[destIdx++] = dataMove + BAD_UTF8_DATA_OFFSET;
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
for (; i < 0xe0 - 0xc0; i++) {
|
|
947
|
+
// C2..DF
|
|
948
|
+
data[destIdx++] = dataMove + this.#index2[i << (6 - SHIFT_2)];
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
if (this.#highStart > 0x10000) {
|
|
952
|
+
const index1Length = (this.#highStart - 0x10000) >> SHIFT_1;
|
|
953
|
+
const index2Offset = INDEX_2_BMP_LENGTH + UTF8_2B_INDEX_2_LENGTH + index1Length;
|
|
954
|
+
|
|
955
|
+
// write 16-bit index-1 values for supplementary code points
|
|
956
|
+
for (i = 0; i < index1Length; i++) {
|
|
957
|
+
data[destIdx++] = INDEX_2_OFFSET + this.#index1[i + OMITTED_BMP_INDEX_1_LENGTH];
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
// write the index-2 array values for supplementary code points,
|
|
961
|
+
// shifted right by INDEX_SHIFT, after adding dataMove
|
|
962
|
+
for (i = 0; i < this.#index2Length - index2Offset; i++) {
|
|
963
|
+
data[destIdx++] = (dataMove + this.#index2[index2Offset + i]) >> INDEX_SHIFT;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
// write 16-bit data values
|
|
968
|
+
for (i = 0; i < this.#dataLength; i++) {
|
|
969
|
+
data[destIdx++] = this.#data[i];
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
const dest = UnicodeTrie.fromJSON({
|
|
973
|
+
data,
|
|
974
|
+
highStart: this.#highStart,
|
|
975
|
+
errorValue: this.#errorValue,
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
return dest;
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// Generates a Buffer containing the serialized and compressed trie.
|
|
982
|
+
// Trie data is compressed twice using the deflate algorithm to minimize file size.
|
|
983
|
+
// Format:
|
|
984
|
+
// uint32_t highStart;
|
|
985
|
+
// uint32_t errorValue;
|
|
986
|
+
// uint32_t uncompressedDataLength;
|
|
987
|
+
// uint8_t trieData[dataLength];
|
|
988
|
+
async toBuffer(): Promise<Buffer> {
|
|
989
|
+
const trie = this.freeze();
|
|
990
|
+
|
|
991
|
+
const data = new Uint8Array(trie.data.buffer);
|
|
992
|
+
|
|
993
|
+
// swap bytes to little-endian
|
|
994
|
+
swap32LE(data);
|
|
995
|
+
|
|
996
|
+
let compressed = await deflate(data, "deflate-raw");
|
|
997
|
+
compressed = await deflate(compressed, "deflate-raw"); // NOTE: double deflate ??
|
|
998
|
+
|
|
999
|
+
const buf = Buffer.alloc(compressed.length + 12);
|
|
1000
|
+
buf.writeUInt32LE(trie.highStart, 0);
|
|
1001
|
+
buf.writeUInt32LE(trie.errorValue, 4);
|
|
1002
|
+
buf.writeUInt32LE(data.length, 8);
|
|
1003
|
+
for (let i = 0; i < compressed.length; i++) {
|
|
1004
|
+
const b = compressed[i];
|
|
1005
|
+
buf[i + 12] = b;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
return buf;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
export default UnicodeTrieBuilder;
|