source: imaps-frontend/node_modules/utrie/dist/utrie.umd.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 52.1 KB
Line 
1/*
2 * utrie 1.0.2 <https://github.com/niklasvh/utrie>
3 * Copyright (c) 2022 Niklas von Hertzen <https://hertzen.com>
4 * Released under MIT License
5 */
6(function (global, factory) {
7 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
8 typeof define === 'function' && define.amd ? define(['exports'], factory) :
9 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.utrie = {}));
10}(this, (function (exports) { 'use strict';
11
12 var chars$1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
13 // Use a lookup table to find the index.
14 var lookup$1 = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
15 for (var i$1 = 0; i$1 < chars$1.length; i$1++) {
16 lookup$1[chars$1.charCodeAt(i$1)] = i$1;
17 }
18 var decode = function (base64) {
19 var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
20 if (base64[base64.length - 1] === '=') {
21 bufferLength--;
22 if (base64[base64.length - 2] === '=') {
23 bufferLength--;
24 }
25 }
26 var buffer = typeof ArrayBuffer !== 'undefined' &&
27 typeof Uint8Array !== 'undefined' &&
28 typeof Uint8Array.prototype.slice !== 'undefined'
29 ? new ArrayBuffer(bufferLength)
30 : new Array(bufferLength);
31 var bytes = Array.isArray(buffer) ? buffer : new Uint8Array(buffer);
32 for (i = 0; i < len; i += 4) {
33 encoded1 = lookup$1[base64.charCodeAt(i)];
34 encoded2 = lookup$1[base64.charCodeAt(i + 1)];
35 encoded3 = lookup$1[base64.charCodeAt(i + 2)];
36 encoded4 = lookup$1[base64.charCodeAt(i + 3)];
37 bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
38 bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
39 bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
40 }
41 return buffer;
42 };
43 var polyUint16Array = function (buffer) {
44 var length = buffer.length;
45 var bytes = [];
46 for (var i = 0; i < length; i += 2) {
47 bytes.push((buffer[i + 1] << 8) | buffer[i]);
48 }
49 return bytes;
50 };
51 var polyUint32Array = function (buffer) {
52 var length = buffer.length;
53 var bytes = [];
54 for (var i = 0; i < length; i += 4) {
55 bytes.push((buffer[i + 3] << 24) | (buffer[i + 2] << 16) | (buffer[i + 1] << 8) | buffer[i]);
56 }
57 return bytes;
58 };
59
60 /** Shift size for getting the index-2 table offset. */
61 var UTRIE2_SHIFT_2 = 5;
62 /** Shift size for getting the index-1 table offset. */
63 var UTRIE2_SHIFT_1 = 6 + 5;
64 /**
65 * Shift size for shifting left the index array values.
66 * Increases possible data size with 16-bit index values at the cost
67 * of compactability.
68 * This requires data blocks to be aligned by UTRIE2_DATA_GRANULARITY.
69 */
70 var UTRIE2_INDEX_SHIFT = 2;
71 /**
72 * Difference between the two shift sizes,
73 * for getting an index-1 offset from an index-2 offset. 6=11-5
74 */
75 var UTRIE2_SHIFT_1_2 = UTRIE2_SHIFT_1 - UTRIE2_SHIFT_2;
76 /**
77 * The part of the index-2 table for U+D800..U+DBFF stores values for
78 * lead surrogate code _units_ not code _points_.
79 * Values for lead surrogate code _points_ are indexed with this portion of the table.
80 * Length=32=0x20=0x400>>UTRIE2_SHIFT_2. (There are 1024=0x400 lead surrogates.)
81 */
82 var UTRIE2_LSCP_INDEX_2_OFFSET = 0x10000 >> UTRIE2_SHIFT_2;
83 /** Number of entries in a data block. 32=0x20 */
84 var UTRIE2_DATA_BLOCK_LENGTH = 1 << UTRIE2_SHIFT_2;
85 /** Mask for getting the lower bits for the in-data-block offset. */
86 var UTRIE2_DATA_MASK = UTRIE2_DATA_BLOCK_LENGTH - 1;
87 var UTRIE2_LSCP_INDEX_2_LENGTH = 0x400 >> UTRIE2_SHIFT_2;
88 /** Count the lengths of both BMP pieces. 2080=0x820 */
89 var UTRIE2_INDEX_2_BMP_LENGTH = UTRIE2_LSCP_INDEX_2_OFFSET + UTRIE2_LSCP_INDEX_2_LENGTH;
90 /**
91 * The 2-byte UTF-8 version of the index-2 table follows at offset 2080=0x820.
92 * Length 32=0x20 for lead bytes C0..DF, regardless of UTRIE2_SHIFT_2.
93 */
94 var UTRIE2_UTF8_2B_INDEX_2_OFFSET = UTRIE2_INDEX_2_BMP_LENGTH;
95 var UTRIE2_UTF8_2B_INDEX_2_LENGTH = 0x800 >> 6; /* U+0800 is the first code point after 2-byte UTF-8 */
96 /**
97 * The index-1 table, only used for supplementary code points, at offset 2112=0x840.
98 * Variable length, for code points up to highStart, where the last single-value range starts.
99 * Maximum length 512=0x200=0x100000>>UTRIE2_SHIFT_1.
100 * (For 0x100000 supplementary code points U+10000..U+10ffff.)
101 *
102 * The part of the index-2 table for supplementary code points starts
103 * after this index-1 table.
104 *
105 * Both the index-1 table and the following part of the index-2 table
106 * are omitted completely if there is only BMP data.
107 */
108 var UTRIE2_INDEX_1_OFFSET = UTRIE2_UTF8_2B_INDEX_2_OFFSET + UTRIE2_UTF8_2B_INDEX_2_LENGTH;
109 /**
110 * Number of index-1 entries for the BMP. 32=0x20
111 * This part of the index-1 table is omitted from the serialized form.
112 */
113 var UTRIE2_OMITTED_BMP_INDEX_1_LENGTH = 0x10000 >> UTRIE2_SHIFT_1;
114 /** Number of entries in an index-2 block. 64=0x40 */
115 var UTRIE2_INDEX_2_BLOCK_LENGTH = 1 << UTRIE2_SHIFT_1_2;
116 /** Mask for getting the lower bits for the in-index-2-block offset. */
117 var UTRIE2_INDEX_2_MASK = UTRIE2_INDEX_2_BLOCK_LENGTH - 1;
118 var slice16 = function (view, start, end) {
119 if (view.slice) {
120 return view.slice(start, end);
121 }
122 return new Uint16Array(Array.prototype.slice.call(view, start, end));
123 };
124 var slice32 = function (view, start, end) {
125 if (view.slice) {
126 return view.slice(start, end);
127 }
128 return new Uint32Array(Array.prototype.slice.call(view, start, end));
129 };
130 var createTrieFromBase64 = function (base64, _byteLength) {
131 var buffer = decode(base64);
132 var view32 = Array.isArray(buffer) ? polyUint32Array(buffer) : new Uint32Array(buffer);
133 var view16 = Array.isArray(buffer) ? polyUint16Array(buffer) : new Uint16Array(buffer);
134 var headerLength = 24;
135 var index = slice16(view16, headerLength / 2, view32[4] / 2);
136 var data = view32[5] === 2
137 ? slice16(view16, (headerLength + view32[4]) / 2)
138 : slice32(view32, Math.ceil((headerLength + view32[4]) / 4));
139 return new Trie(view32[0], view32[1], view32[2], view32[3], index, data);
140 };
141 var Trie = /** @class */ (function () {
142 function Trie(initialValue, errorValue, highStart, highValueIndex, index, data) {
143 this.initialValue = initialValue;
144 this.errorValue = errorValue;
145 this.highStart = highStart;
146 this.highValueIndex = highValueIndex;
147 this.index = index;
148 this.data = data;
149 }
150 /**
151 * Get the value for a code point as stored in the Trie.
152 *
153 * @param codePoint the code point
154 * @return the value
155 */
156 Trie.prototype.get = function (codePoint) {
157 var ix;
158 if (codePoint >= 0) {
159 if (codePoint < 0x0d800 || (codePoint > 0x0dbff && codePoint <= 0x0ffff)) {
160 // Ordinary BMP code point, excluding leading surrogates.
161 // BMP uses a single level lookup. BMP index starts at offset 0 in the Trie2 index.
162 // 16 bit data is stored in the index array itself.
163 ix = this.index[codePoint >> UTRIE2_SHIFT_2];
164 ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
165 return this.data[ix];
166 }
167 if (codePoint <= 0xffff) {
168 // Lead Surrogate Code Point. A Separate index section is stored for
169 // lead surrogate code units and code points.
170 // The main index has the code unit data.
171 // For this function, we need the code point data.
172 // Note: this expression could be refactored for slightly improved efficiency, but
173 // surrogate code points will be so rare in practice that it's not worth it.
174 ix = this.index[UTRIE2_LSCP_INDEX_2_OFFSET + ((codePoint - 0xd800) >> UTRIE2_SHIFT_2)];
175 ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
176 return this.data[ix];
177 }
178 if (codePoint < this.highStart) {
179 // Supplemental code point, use two-level lookup.
180 ix = UTRIE2_INDEX_1_OFFSET - UTRIE2_OMITTED_BMP_INDEX_1_LENGTH + (codePoint >> UTRIE2_SHIFT_1);
181 ix = this.index[ix];
182 ix += (codePoint >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK;
183 ix = this.index[ix];
184 ix = (ix << UTRIE2_INDEX_SHIFT) + (codePoint & UTRIE2_DATA_MASK);
185 return this.data[ix];
186 }
187 if (codePoint <= 0x10ffff) {
188 return this.data[this.highValueIndex];
189 }
190 }
191 // Fall through. The code point is outside of the legal range of 0..0x10ffff.
192 return this.errorValue;
193 };
194 return Trie;
195 }());
196
197 /*
198 * base64-arraybuffer 1.0.2 <https://github.com/niklasvh/base64-arraybuffer>
199 * Copyright (c) 2022 Niklas von Hertzen <https://hertzen.com>
200 * Released under MIT License
201 */
202 var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
203 // Use a lookup table to find the index.
204 var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
205 for (var i = 0; i < chars.length; i++) {
206 lookup[chars.charCodeAt(i)] = i;
207 }
208 var encode = function (arraybuffer) {
209 var bytes = new Uint8Array(arraybuffer), i, len = bytes.length, base64 = '';
210 for (i = 0; i < len; i += 3) {
211 base64 += chars[bytes[i] >> 2];
212 base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
213 base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
214 base64 += chars[bytes[i + 2] & 63];
215 }
216 if (len % 3 === 2) {
217 base64 = base64.substring(0, base64.length - 1) + '=';
218 }
219 else if (len % 3 === 1) {
220 base64 = base64.substring(0, base64.length - 2) + '==';
221 }
222 return base64;
223 };
224
225 /**
226 * Trie2 constants, defining shift widths, index array lengths, etc.
227 *
228 * These are needed for the runtime macros but users can treat these as
229 * implementation details and skip to the actual public API further below.
230 */
231 // const UTRIE2_OPTIONS_VALUE_BITS_MASK = 0x000f;
232 /** Number of code points per index-1 table entry. 2048=0x800 */
233 var UTRIE2_CP_PER_INDEX_1_ENTRY = 1 << UTRIE2_SHIFT_1;
234 /** The alignment size of a data block. Also the granularity for compaction. */
235 var UTRIE2_DATA_GRANULARITY = 1 << UTRIE2_INDEX_SHIFT;
236 /* Fixed layout of the first part of the index array. ------------------- */
237 /**
238 * The BMP part of the index-2 table is fixed and linear and starts at offset 0.
239 * Length=2048=0x800=0x10000>>UTRIE2_SHIFT_2.
240 */
241 var UTRIE2_INDEX_2_OFFSET = 0;
242 var UTRIE2_MAX_INDEX_1_LENGTH = 0x100000 >> UTRIE2_SHIFT_1;
243 /*
244 * Fixed layout of the first part of the data array. -----------------------
245 * Starts with 4 blocks (128=0x80 entries) for ASCII.
246 */
247 /**
248 * The illegal-UTF-8 data block follows the ASCII block, at offset 128=0x80.
249 * Used with linear access for single bytes 0..0xbf for simple error handling.
250 * Length 64=0x40, not UTRIE2_DATA_BLOCK_LENGTH.
251 */
252 var UTRIE2_BAD_UTF8_DATA_OFFSET = 0x80;
253 /** The start of non-linear-ASCII data blocks, at offset 192=0xc0. */
254 var UTRIE2_DATA_START_OFFSET = 0xc0;
255 /* Building a Trie2 ---------------------------------------------------------- */
256 /*
257 * These definitions are mostly needed by utrie2_builder.c, but also by
258 * utrie2_get32() and utrie2_enum().
259 */
260 /*
261 * At build time, leave a gap in the index-2 table,
262 * at least as long as the maximum lengths of the 2-byte UTF-8 index-2 table
263 * and the supplementary index-1 table.
264 * Round up to UTRIE2_INDEX_2_BLOCK_LENGTH for proper compacting.
265 */
266 var UNEWTRIE2_INDEX_GAP_OFFSET = UTRIE2_INDEX_2_BMP_LENGTH;
267 var UNEWTRIE2_INDEX_GAP_LENGTH = (UTRIE2_UTF8_2B_INDEX_2_LENGTH + UTRIE2_MAX_INDEX_1_LENGTH + UTRIE2_INDEX_2_MASK) & ~UTRIE2_INDEX_2_MASK;
268 /**
269 * Maximum length of the build-time index-2 array.
270 * Maximum number of Unicode code points (0x110000) shifted right by UTRIE2_SHIFT_2,
271 * plus the part of the index-2 table for lead surrogate code points,
272 * plus the build-time index gap,
273 * plus the null index-2 block.
274 */
275 var UNEWTRIE2_MAX_INDEX_2_LENGTH = (0x110000 >> UTRIE2_SHIFT_2) +
276 UTRIE2_LSCP_INDEX_2_LENGTH +
277 UNEWTRIE2_INDEX_GAP_LENGTH +
278 UTRIE2_INDEX_2_BLOCK_LENGTH;
279 var UNEWTRIE2_INDEX_1_LENGTH = 0x110000 >> UTRIE2_SHIFT_1;
280 /**
281 * Maximum length of the build-time data array.
282 * One entry per 0x110000 code points, plus the illegal-UTF-8 block and the null block,
283 * plus values for the 0x400 surrogate code units.
284 */
285 var UNEWTRIE2_MAX_DATA_LENGTH = 0x110000 + 0x40 + 0x40 + 0x400;
286 /* Start with allocation of 16k data entries. */
287 var UNEWTRIE2_INITIAL_DATA_LENGTH = 1 << 14;
288 /* Grow about 8x each time. */
289 var UNEWTRIE2_MEDIUM_DATA_LENGTH = 1 << 17;
290 /** The null index-2 block, following the gap in the index-2 table. */
291 var UNEWTRIE2_INDEX_2_NULL_OFFSET = UNEWTRIE2_INDEX_GAP_OFFSET + UNEWTRIE2_INDEX_GAP_LENGTH;
292 /** The start of allocated index-2 blocks. */
293 var UNEWTRIE2_INDEX_2_START_OFFSET = UNEWTRIE2_INDEX_2_NULL_OFFSET + UTRIE2_INDEX_2_BLOCK_LENGTH;
294 /**
295 * The null data block.
296 * Length 64=0x40 even if UTRIE2_DATA_BLOCK_LENGTH is smaller,
297 * to work with 6-bit trail bytes from 2-byte UTF-8.
298 */
299 var UNEWTRIE2_DATA_NULL_OFFSET = UTRIE2_DATA_START_OFFSET;
300 /** The start of allocated data blocks. */
301 var UNEWTRIE2_DATA_START_OFFSET = UNEWTRIE2_DATA_NULL_OFFSET + 0x40;
302 /**
303 * The start of data blocks for U+0800 and above.
304 * Below, compaction uses a block length of 64 for 2-byte UTF-8.
305 * From here on, compaction uses UTRIE2_DATA_BLOCK_LENGTH.
306 * Data values for 0x780 code points beyond ASCII.
307 */
308 var UNEWTRIE2_DATA_0800_OFFSET = UNEWTRIE2_DATA_START_OFFSET + 0x780;
309 /**
310 * Maximum length of the runtime index array.
311 * Limited by its own 16-bit index values, and by uint16_t UTrie2Header.indexLength.
312 * (The actual maximum length is lower,
313 * (0x110000>>UTRIE2_SHIFT_2)+UTRIE2_UTF8_2B_INDEX_2_LENGTH+UTRIE2_MAX_INDEX_1_LENGTH.)
314 */
315 var UTRIE2_MAX_INDEX_LENGTH = 0xffff;
316 /**
317 * Maximum length of the runtime data array.
318 * Limited by 16-bit index values that are left-shifted by UTRIE2_INDEX_SHIFT,
319 * and by uint16_t UTrie2Header.shiftedDataLength.
320 */
321 var UTRIE2_MAX_DATA_LENGTH = 0xffff << UTRIE2_INDEX_SHIFT;
322 var BITS_16 = 16;
323 var BITS_32 = 32;
324 var isHighSurrogate = function (c) { return c >= 0xd800 && c <= 0xdbff; };
325 var equalInt = function (a, s, t, length) {
326 for (var i = 0; i < length; i++) {
327 if (a[s + i] !== a[t + i]) {
328 return false;
329 }
330 }
331 return true;
332 };
333 var TrieBuilder = /** @class */ (function () {
334 function TrieBuilder(initialValue, errorValue) {
335 if (initialValue === void 0) { initialValue = 0; }
336 if (errorValue === void 0) { errorValue = 0; }
337 this.initialValue = initialValue;
338 this.errorValue = errorValue;
339 this.highStart = 0x110000;
340 this.data = new Uint32Array(UNEWTRIE2_INITIAL_DATA_LENGTH);
341 this.dataCapacity = UNEWTRIE2_INITIAL_DATA_LENGTH;
342 this.highStart = 0x110000;
343 this.firstFreeBlock = 0; /* no free block in the list */
344 this.isCompacted = false;
345 this.index1 = new Uint32Array(UNEWTRIE2_INDEX_1_LENGTH);
346 this.index2 = new Uint32Array(UNEWTRIE2_MAX_INDEX_2_LENGTH);
347 /*
348 * Multi-purpose per-data-block table.
349 *
350 * Before compacting:
351 *
352 * Per-data-block reference counters/free-block list.
353 * 0: unused
354 * >0: reference counter (number of index-2 entries pointing here)
355 * <0: next free data block in free-block list
356 *
357 * While compacting:
358 *
359 * Map of adjusted indexes, used in compactData() and compactIndex2().
360 * Maps from original indexes to new ones.
361 */
362 this.map = new Uint32Array(UNEWTRIE2_MAX_DATA_LENGTH >> UTRIE2_SHIFT_2);
363 /*
364 * preallocate and reset
365 * - ASCII
366 * - the bad-UTF-8-data block
367 * - the null data block
368 */
369 var i, j;
370 for (i = 0; i < 0x80; ++i) {
371 this.data[i] = initialValue;
372 }
373 for (; i < 0xc0; ++i) {
374 this.data[i] = errorValue;
375 }
376 for (i = UNEWTRIE2_DATA_NULL_OFFSET; i < UNEWTRIE2_DATA_START_OFFSET; ++i) {
377 this.data[i] = initialValue;
378 }
379 this.dataNullOffset = UNEWTRIE2_DATA_NULL_OFFSET;
380 this.dataLength = UNEWTRIE2_DATA_START_OFFSET;
381 /* set the index-2 indexes for the 2=0x80>>UTRIE2_SHIFT_2 ASCII data blocks */
382 for (i = 0, j = 0; j < 0x80; ++i, j += UTRIE2_DATA_BLOCK_LENGTH) {
383 this.index2[i] = j;
384 this.map[i] = 1;
385 }
386 /* reference counts for the bad-UTF-8-data block */
387 for (; j < 0xc0; ++i, j += UTRIE2_DATA_BLOCK_LENGTH) {
388 this.map[i] = 0;
389 }
390 /*
391 * Reference counts for the null data block: all blocks except for the ASCII blocks.
392 * Plus 1 so that we don't drop this block during compaction.
393 * Plus as many as needed for lead surrogate code points.
394 */
395 /* i==newTrie->dataNullOffset */
396 this.map[i++] = (0x110000 >> UTRIE2_SHIFT_2) - (0x80 >> UTRIE2_SHIFT_2) + 1 + UTRIE2_LSCP_INDEX_2_LENGTH;
397 j += UTRIE2_DATA_BLOCK_LENGTH;
398 for (; j < UNEWTRIE2_DATA_START_OFFSET; ++i, j += UTRIE2_DATA_BLOCK_LENGTH) {
399 this.map[i] = 0;
400 }
401 /*
402 * set the remaining indexes in the BMP index-2 block
403 * to the null data block
404 */
405 for (i = 0x80 >> UTRIE2_SHIFT_2; i < UTRIE2_INDEX_2_BMP_LENGTH; ++i) {
406 this.index2[i] = UNEWTRIE2_DATA_NULL_OFFSET;
407 }
408 /*
409 * Fill the index gap with impossible values so that compaction
410 * does not overlap other index-2 blocks with the gap.
411 */
412 for (i = 0; i < UNEWTRIE2_INDEX_GAP_LENGTH; ++i) {
413 this.index2[UNEWTRIE2_INDEX_GAP_OFFSET + i] = -1;
414 }
415 /* set the indexes in the null index-2 block */
416 for (i = 0; i < UTRIE2_INDEX_2_BLOCK_LENGTH; ++i) {
417 this.index2[UNEWTRIE2_INDEX_2_NULL_OFFSET + i] = UNEWTRIE2_DATA_NULL_OFFSET;
418 }
419 this.index2NullOffset = UNEWTRIE2_INDEX_2_NULL_OFFSET;
420 this.index2Length = UNEWTRIE2_INDEX_2_START_OFFSET;
421 /* set the index-1 indexes for the linear index-2 block */
422 for (i = 0, j = 0; i < UTRIE2_OMITTED_BMP_INDEX_1_LENGTH; ++i, j += UTRIE2_INDEX_2_BLOCK_LENGTH) {
423 this.index1[i] = j;
424 }
425 /* set the remaining index-1 indexes to the null index-2 block */
426 for (; i < UNEWTRIE2_INDEX_1_LENGTH; ++i) {
427 this.index1[i] = UNEWTRIE2_INDEX_2_NULL_OFFSET;
428 }
429 /*
430 * Preallocate and reset data for U+0080..U+07ff,
431 * for 2-byte UTF-8 which will be compacted in 64-blocks
432 * even if UTRIE2_DATA_BLOCK_LENGTH is smaller.
433 */
434 for (i = 0x80; i < 0x800; i += UTRIE2_DATA_BLOCK_LENGTH) {
435 this.set(i, initialValue);
436 }
437 }
438 /**
439 * Set a value for a code point.
440 *
441 * @param c the code point
442 * @param value the value
443 */
444 TrieBuilder.prototype.set = function (c, value) {
445 if (c < 0 || c > 0x10ffff) {
446 throw new Error('Invalid code point.');
447 }
448 this._set(c, true, value);
449 return this;
450 };
451 /**
452 * Set a value in a range of code points [start..end].
453 * All code points c with start<=c<=end will get the value if
454 * overwrite is TRUE or if the old value is the initial value.
455 *
456 * @param start the first code point to get the value
457 * @param end the last code point to get the value (inclusive)
458 * @param value the value
459 * @param overwrite flag for whether old non-initial values are to be overwritten
460 */
461 TrieBuilder.prototype.setRange = function (start, end, value, overwrite) {
462 if (overwrite === void 0) { overwrite = false; }
463 /*
464 * repeat value in [start..end]
465 * mark index values for repeat-data blocks by setting bit 31 of the index values
466 * fill around existing values if any, if(overwrite)
467 */
468 var block, rest, repeatBlock;
469 if (start > 0x10ffff || start < 0 || end > 0x10ffff || end < 0 || start > end) {
470 throw new Error('Invalid code point range.');
471 }
472 if (!overwrite && value === this.initialValue) {
473 return this; /* nothing to do */
474 }
475 if (this.isCompacted) {
476 throw new Error('Trie was already compacted');
477 }
478 var limit = end + 1;
479 if ((start & UTRIE2_DATA_MASK) !== 0) {
480 /* set partial block at [start..following block boundary[ */
481 block = this.getDataBlock(start, true);
482 var nextStart = (start + UTRIE2_DATA_BLOCK_LENGTH) & ~UTRIE2_DATA_MASK;
483 if (nextStart <= limit) {
484 this.fillBlock(block, start & UTRIE2_DATA_MASK, UTRIE2_DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);
485 start = nextStart;
486 }
487 else {
488 this.fillBlock(block, start & UTRIE2_DATA_MASK, limit & UTRIE2_DATA_MASK, value, this.initialValue, overwrite);
489 return this;
490 }
491 }
492 /* number of positions in the last, partial block */
493 rest = limit & UTRIE2_DATA_MASK;
494 /* round down limit to a block boundary */
495 limit &= ~UTRIE2_DATA_MASK;
496 /* iterate over all-value blocks */
497 repeatBlock = value === this.initialValue ? this.dataNullOffset : -1;
498 while (start < limit) {
499 var i2 = void 0;
500 var setRepeatBlock = false;
501 if (value === this.initialValue && this.isInNullBlock(start, true)) {
502 start += UTRIE2_DATA_BLOCK_LENGTH; /* nothing to do */
503 continue;
504 }
505 /* get index value */
506 i2 = this.getIndex2Block(start, true);
507 i2 += (start >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK;
508 block = this.index2[i2];
509 if (this.isWritableBlock(block)) {
510 /* already allocated */
511 if (overwrite && block >= UNEWTRIE2_DATA_0800_OFFSET) {
512 /*
513 * We overwrite all values, and it's not a
514 * protected (ASCII-linear or 2-byte UTF-8) block:
515 * replace with the repeatBlock.
516 */
517 setRepeatBlock = true;
518 }
519 else {
520 /* !overwrite, or protected block: just write the values into this block */
521 this.fillBlock(block, 0, UTRIE2_DATA_BLOCK_LENGTH, value, this.initialValue, overwrite);
522 }
523 }
524 else if (this.data[block] !== value && (overwrite || block === this.dataNullOffset)) {
525 /*
526 * Set the repeatBlock instead of the null block or previous repeat block:
527 *
528 * If !isWritableBlock() then all entries in the block have the same value
529 * because it's the null block or a range block (the repeatBlock from a previous
530 * call to utrie2_setRange32()).
531 * No other blocks are used multiple times before compacting.
532 *
533 * The null block is the only non-writable block with the initialValue because
534 * of the repeatBlock initialization above. (If value==initialValue, then
535 * the repeatBlock will be the null data block.)
536 *
537 * We set our repeatBlock if the desired value differs from the block's value,
538 * and if we overwrite any data or if the data is all initial values
539 * (which is the same as the block being the null block, see above).
540 */
541 setRepeatBlock = true;
542 }
543 if (setRepeatBlock) {
544 if (repeatBlock >= 0) {
545 this.setIndex2Entry(i2, repeatBlock);
546 }
547 else {
548 /* create and set and fill the repeatBlock */
549 repeatBlock = this.getDataBlock(start, true);
550 this.writeBlock(repeatBlock, value);
551 }
552 }
553 start += UTRIE2_DATA_BLOCK_LENGTH;
554 }
555 if (rest > 0) {
556 /* set partial block at [last block boundary..limit[ */
557 block = this.getDataBlock(start, true);
558 this.fillBlock(block, 0, rest, value, this.initialValue, overwrite);
559 }
560 return this;
561 };
562 /**
563 * Get the value for a code point as stored in the Trie2.
564 *
565 * @param codePoint the code point
566 * @return the value
567 */
568 TrieBuilder.prototype.get = function (codePoint) {
569 if (codePoint < 0 || codePoint > 0x10ffff) {
570 return this.errorValue;
571 }
572 else {
573 return this._get(codePoint, true);
574 }
575 };
576 TrieBuilder.prototype._get = function (c, fromLSCP) {
577 var i2;
578 if (c >= this.highStart && (!(c >= 0xd800 && c < 0xdc00) || fromLSCP)) {
579 return this.data[this.dataLength - UTRIE2_DATA_GRANULARITY];
580 }
581 if (c >= 0xd800 && c < 0xdc00 && fromLSCP) {
582 i2 = UTRIE2_LSCP_INDEX_2_OFFSET - (0xd800 >> UTRIE2_SHIFT_2) + (c >> UTRIE2_SHIFT_2);
583 }
584 else {
585 i2 = this.index1[c >> UTRIE2_SHIFT_1] + ((c >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK);
586 }
587 var block = this.index2[i2];
588 return this.data[block + (c & UTRIE2_DATA_MASK)];
589 };
590 TrieBuilder.prototype.freeze = function (valueBits) {
591 if (valueBits === void 0) { valueBits = BITS_32; }
592 var i;
593 var allIndexesLength;
594 var dataMove; /* >0 if the data is moved to the end of the index array */
595 /* compact if necessary */
596 if (!this.isCompacted) {
597 this.compactTrie();
598 }
599 allIndexesLength = this.highStart <= 0x10000 ? UTRIE2_INDEX_1_OFFSET : this.index2Length;
600 if (valueBits === BITS_16) {
601 // dataMove = allIndexesLength;
602 dataMove = 0;
603 }
604 else {
605 dataMove = 0;
606 }
607 /* are indexLength and dataLength within limits? */
608 if (
609 /* for unshifted indexLength */
610 allIndexesLength > UTRIE2_MAX_INDEX_LENGTH ||
611 /* for unshifted dataNullOffset */
612 dataMove + this.dataNullOffset > 0xffff ||
613 /* for unshifted 2-byte UTF-8 index-2 values */
614 dataMove + UNEWTRIE2_DATA_0800_OFFSET > 0xffff ||
615 /* for shiftedDataLength */
616 dataMove + this.dataLength > UTRIE2_MAX_DATA_LENGTH) {
617 throw new Error('Trie data is too large.');
618 }
619 var index = new Uint16Array(allIndexesLength);
620 /* write the index-2 array values shifted right by UTRIE2_INDEX_SHIFT, after adding dataMove */
621 var destIdx = 0;
622 for (i = 0; i < UTRIE2_INDEX_2_BMP_LENGTH; i++) {
623 index[destIdx++] = (this.index2[i] + dataMove) >> UTRIE2_INDEX_SHIFT;
624 }
625 /* write UTF-8 2-byte index-2 values, not right-shifted */
626 for (i = 0; i < 0xc2 - 0xc0; ++i) {
627 /* C0..C1 */
628 index[destIdx++] = dataMove + UTRIE2_BAD_UTF8_DATA_OFFSET;
629 }
630 for (; i < 0xe0 - 0xc0; ++i) {
631 /* C2..DF */
632 index[destIdx++] = dataMove + this.index2[i << (6 - UTRIE2_SHIFT_2)];
633 }
634 if (this.highStart > 0x10000) {
635 var index1Length = (this.highStart - 0x10000) >> UTRIE2_SHIFT_1;
636 var index2Offset = UTRIE2_INDEX_2_BMP_LENGTH + UTRIE2_UTF8_2B_INDEX_2_LENGTH + index1Length;
637 /* write 16-bit index-1 values for supplementary code points */
638 for (i = 0; i < index1Length; i++) {
639 index[destIdx++] = UTRIE2_INDEX_2_OFFSET + this.index1[i + UTRIE2_OMITTED_BMP_INDEX_1_LENGTH];
640 }
641 /*
642 * write the index-2 array values for supplementary code points,
643 * shifted right by UTRIE2_INDEX_SHIFT, after adding dataMove
644 */
645 for (i = 0; i < this.index2Length - index2Offset; i++) {
646 index[destIdx++] = (dataMove + this.index2[index2Offset + i]) >> UTRIE2_INDEX_SHIFT;
647 }
648 }
649 /* write the 16/32-bit data array */
650 switch (valueBits) {
651 case BITS_16:
652 /* write 16-bit data values */
653 var data16 = new Uint16Array(this.dataLength);
654 for (i = 0; i < this.dataLength; i++) {
655 data16[i] = this.data[i];
656 }
657 return new Trie(this.initialValue, this.errorValue, this.highStart, dataMove + this.dataLength - UTRIE2_DATA_GRANULARITY, index, data16);
658 case BITS_32:
659 /* write 32-bit data values */
660 var data32 = new Uint32Array(this.dataLength);
661 for (i = 0; i < this.dataLength; i++) {
662 data32[i] = this.data[i];
663 }
664 return new Trie(this.initialValue, this.errorValue, this.highStart, dataMove + this.dataLength - UTRIE2_DATA_GRANULARITY, index, data32);
665 default:
666 throw new Error('Bits should be either 16 or 32');
667 }
668 };
669 /*
670 * Find the start of the last range in the trie by enumerating backward.
671 * Indexes for supplementary code points higher than this will be omitted.
672 */
673 TrieBuilder.prototype.findHighStart = function (highValue) {
674 var value;
675 var i2, j, i2Block, prevI2Block, block, prevBlock;
676 /* set variables for previous range */
677 if (highValue === this.initialValue) {
678 prevI2Block = this.index2NullOffset;
679 prevBlock = this.dataNullOffset;
680 }
681 else {
682 prevI2Block = -1;
683 prevBlock = -1;
684 }
685 var prev = 0x110000;
686 /* enumerate index-2 blocks */
687 var i1 = UNEWTRIE2_INDEX_1_LENGTH;
688 var c = prev;
689 while (c > 0) {
690 i2Block = this.index1[--i1];
691 if (i2Block === prevI2Block) {
692 /* the index-2 block is the same as the previous one, and filled with highValue */
693 c -= UTRIE2_CP_PER_INDEX_1_ENTRY;
694 continue;
695 }
696 prevI2Block = i2Block;
697 if (i2Block === this.index2NullOffset) {
698 /* this is the null index-2 block */
699 if (highValue !== this.initialValue) {
700 return c;
701 }
702 c -= UTRIE2_CP_PER_INDEX_1_ENTRY;
703 }
704 else {
705 /* enumerate data blocks for one index-2 block */
706 for (i2 = UTRIE2_INDEX_2_BLOCK_LENGTH; i2 > 0;) {
707 block = this.index2[i2Block + --i2];
708 if (block === prevBlock) {
709 /* the block is the same as the previous one, and filled with highValue */
710 c -= UTRIE2_DATA_BLOCK_LENGTH;
711 continue;
712 }
713 prevBlock = block;
714 if (block === this.dataNullOffset) {
715 /* this is the null data block */
716 if (highValue !== this.initialValue) {
717 return c;
718 }
719 c -= UTRIE2_DATA_BLOCK_LENGTH;
720 }
721 else {
722 for (j = UTRIE2_DATA_BLOCK_LENGTH; j > 0;) {
723 value = this.data[block + --j];
724 if (value !== highValue) {
725 return c;
726 }
727 --c;
728 }
729 }
730 }
731 }
732 }
733 /* deliver last range */
734 return 0;
735 };
736 /*
737 * Compact a build-time trie.
738 *
739 * The compaction
740 * - removes blocks that are identical with earlier ones
741 * - overlaps adjacent blocks as much as possible (if overlap==TRUE)
742 * - moves blocks in steps of the data granularity
743 * - moves and overlaps blocks that overlap with multiple values in the overlap region
744 *
745 * It does not
746 * - try to move and overlap blocks that are not already adjacent
747 */
748 TrieBuilder.prototype.compactData = function () {
749 var start, movedStart;
750 var blockLength, overlap;
751 var i, mapIndex, blockCount;
752 /* do not compact linear-ASCII data */
753 var newStart = UTRIE2_DATA_START_OFFSET;
754 for (start = 0, i = 0; start < newStart; start += UTRIE2_DATA_BLOCK_LENGTH, ++i) {
755 this.map[i] = start;
756 }
757 /*
758 * Start with a block length of 64 for 2-byte UTF-8,
759 * then switch to UTRIE2_DATA_BLOCK_LENGTH.
760 */
761 blockLength = 64;
762 blockCount = blockLength >> UTRIE2_SHIFT_2;
763 for (start = newStart; start < this.dataLength;) {
764 /*
765 * start: index of first entry of current block
766 * newStart: index where the current block is to be moved
767 * (right after current end of already-compacted data)
768 */
769 if (start === UNEWTRIE2_DATA_0800_OFFSET) {
770 blockLength = UTRIE2_DATA_BLOCK_LENGTH;
771 blockCount = 1;
772 }
773 /* skip blocks that are not used */
774 if (this.map[start >> UTRIE2_SHIFT_2] <= 0) {
775 /* advance start to the next block */
776 start += blockLength;
777 /* leave newStart with the previous block! */
778 continue;
779 }
780 /* search for an identical block */
781 movedStart = this.findSameDataBlock(newStart, start, blockLength);
782 if (movedStart >= 0) {
783 /* found an identical block, set the other block's index value for the current block */
784 for (i = blockCount, mapIndex = start >> UTRIE2_SHIFT_2; i > 0; --i) {
785 this.map[mapIndex++] = movedStart;
786 movedStart += UTRIE2_DATA_BLOCK_LENGTH;
787 }
788 /* advance start to the next block */
789 start += blockLength;
790 /* leave newStart with the previous block! */
791 continue;
792 }
793 /* see if the beginning of this block can be overlapped with the end of the previous block */
794 /* look for maximum overlap (modulo granularity) with the previous, adjacent block */
795 for (overlap = blockLength - UTRIE2_DATA_GRANULARITY; overlap > 0 && !equalInt(this.data, newStart - overlap, start, overlap); overlap -= UTRIE2_DATA_GRANULARITY) { }
796 if (overlap > 0 || newStart < start) {
797 /* some overlap, or just move the whole block */
798 movedStart = newStart - overlap;
799 for (i = blockCount, mapIndex = start >> UTRIE2_SHIFT_2; i > 0; --i) {
800 this.map[mapIndex++] = movedStart;
801 movedStart += UTRIE2_DATA_BLOCK_LENGTH;
802 }
803 /* move the non-overlapping indexes to their new positions */
804 start += overlap;
805 for (i = blockLength - overlap; i > 0; --i) {
806 this.data[newStart++] = this.data[start++];
807 }
808 }
809 else {
810 /* no overlap && newStart==start */
811 for (i = blockCount, mapIndex = start >> UTRIE2_SHIFT_2; i > 0; --i) {
812 this.map[mapIndex++] = start;
813 start += UTRIE2_DATA_BLOCK_LENGTH;
814 }
815 newStart = start;
816 }
817 }
818 /* now adjust the index-2 table */
819 for (i = 0; i < this.index2Length; ++i) {
820 if (i === UNEWTRIE2_INDEX_GAP_OFFSET) {
821 /* Gap indexes are invalid (-1). Skip over the gap. */
822 i += UNEWTRIE2_INDEX_GAP_LENGTH;
823 }
824 this.index2[i] = this.map[this.index2[i] >> UTRIE2_SHIFT_2];
825 }
826 this.dataNullOffset = this.map[this.dataNullOffset >> UTRIE2_SHIFT_2];
827 /* ensure dataLength alignment */
828 while ((newStart & (UTRIE2_DATA_GRANULARITY - 1)) !== 0) {
829 this.data[newStart++] = this.initialValue;
830 }
831 this.dataLength = newStart;
832 };
833 TrieBuilder.prototype.findSameDataBlock = function (dataLength, otherBlock, blockLength) {
834 var block = 0;
835 /* ensure that we do not even partially get past dataLength */
836 dataLength -= blockLength;
837 for (; block <= dataLength; block += UTRIE2_DATA_GRANULARITY) {
838 if (equalInt(this.data, block, otherBlock, blockLength)) {
839 return block;
840 }
841 }
842 return -1;
843 };
844 TrieBuilder.prototype.compactTrie = function () {
845 var highValue = this.get(0x10ffff);
846 /* find highStart and round it up */
847 var localHighStart = this.findHighStart(highValue);
848 localHighStart = (localHighStart + (UTRIE2_CP_PER_INDEX_1_ENTRY - 1)) & ~(UTRIE2_CP_PER_INDEX_1_ENTRY - 1);
849 if (localHighStart === 0x110000) {
850 highValue = this.errorValue;
851 }
852 /*
853 * Set trie->highStart only after utrie2_get32(trie, highStart).
854 * Otherwise utrie2_get32(trie, highStart) would try to read the highValue.
855 */
856 this.highStart = localHighStart;
857 if (this.highStart < 0x110000) {
858 /* Blank out [highStart..10ffff] to release associated data blocks. */
859 var suppHighStart = this.highStart <= 0x10000 ? 0x10000 : this.highStart;
860 this.setRange(suppHighStart, 0x10ffff, this.initialValue, true);
861 }
862 this.compactData();
863 if (this.highStart > 0x10000) {
864 this.compactIndex2();
865 }
866 /*
867 * Store the highValue in the data array and round up the dataLength.
868 * Must be done after compactData() because that assumes that dataLength
869 * is a multiple of UTRIE2_DATA_BLOCK_LENGTH.
870 */
871 this.data[this.dataLength++] = highValue;
872 while ((this.dataLength & (UTRIE2_DATA_GRANULARITY - 1)) !== 0) {
873 this.data[this.dataLength++] = this.initialValue;
874 }
875 this.isCompacted = true;
876 };
877 TrieBuilder.prototype.compactIndex2 = function () {
878 var i, start, movedStart, overlap;
879 /* do not compact linear-BMP index-2 blocks */
880 var newStart = UTRIE2_INDEX_2_BMP_LENGTH;
881 for (start = 0, i = 0; start < newStart; start += UTRIE2_INDEX_2_BLOCK_LENGTH, ++i) {
882 this.map[i] = start;
883 }
884 /* Reduce the index table gap to what will be needed at runtime. */
885 newStart += UTRIE2_UTF8_2B_INDEX_2_LENGTH + ((this.highStart - 0x10000) >> UTRIE2_SHIFT_1);
886 for (start = UNEWTRIE2_INDEX_2_NULL_OFFSET; start < this.index2Length;) {
887 /*
888 * start: index of first entry of current block
889 * newStart: index where the current block is to be moved
890 * (right after current end of already-compacted data)
891 */
892 /* search for an identical block */
893 if ((movedStart = this.findSameIndex2Block(newStart, start)) >= 0) {
894 /* found an identical block, set the other block's index value for the current block */
895 this.map[start >> UTRIE2_SHIFT_1_2] = movedStart;
896 /* advance start to the next block */
897 start += UTRIE2_INDEX_2_BLOCK_LENGTH;
898 /* leave newStart with the previous block! */
899 continue;
900 }
901 /* see if the beginning of this block can be overlapped with the end of the previous block */
902 /* look for maximum overlap with the previous, adjacent block */
903 for (overlap = UTRIE2_INDEX_2_BLOCK_LENGTH - 1; overlap > 0 && !equalInt(this.index2, newStart - overlap, start, overlap); --overlap) { }
904 if (overlap > 0 || newStart < start) {
905 /* some overlap, or just move the whole block */
906 this.map[start >> UTRIE2_SHIFT_1_2] = newStart - overlap;
907 /* move the non-overlapping indexes to their new positions */
908 start += overlap;
909 for (i = UTRIE2_INDEX_2_BLOCK_LENGTH - overlap; i > 0; --i) {
910 this.index2[newStart++] = this.index2[start++];
911 }
912 }
913 else {
914 /* no overlap && newStart==start */ this.map[start >> UTRIE2_SHIFT_1_2] = start;
915 start += UTRIE2_INDEX_2_BLOCK_LENGTH;
916 newStart = start;
917 }
918 }
919 /* now adjust the index-1 table */
920 for (i = 0; i < UNEWTRIE2_INDEX_1_LENGTH; ++i) {
921 this.index1[i] = this.map[this.index1[i] >> UTRIE2_SHIFT_1_2];
922 }
923 this.index2NullOffset = this.map[this.index2NullOffset >> UTRIE2_SHIFT_1_2];
924 /*
925 * Ensure data table alignment:
926 * Needs to be granularity-aligned for 16-bit trie
927 * (so that dataMove will be down-shiftable),
928 * and 2-aligned for uint32_t data.
929 */
930 while ((newStart & ((UTRIE2_DATA_GRANULARITY - 1) | 1)) !== 0) {
931 /* Arbitrary value: 0x3fffc not possible for real data. */
932 this.index2[newStart++] = 0x0000ffff << UTRIE2_INDEX_SHIFT;
933 }
934 this.index2Length = newStart;
935 };
936 TrieBuilder.prototype.findSameIndex2Block = function (index2Length, otherBlock) {
937 /* ensure that we do not even partially get past index2Length */
938 index2Length -= UTRIE2_INDEX_2_BLOCK_LENGTH;
939 for (var block = 0; block <= index2Length; ++block) {
940 if (equalInt(this.index2, block, otherBlock, UTRIE2_INDEX_2_BLOCK_LENGTH)) {
941 return block;
942 }
943 }
944 return -1;
945 };
946 TrieBuilder.prototype._set = function (c, forLSCP, value) {
947 if (this.isCompacted) {
948 throw new Error('Trie was already compacted');
949 }
950 var block = this.getDataBlock(c, forLSCP);
951 this.data[block + (c & UTRIE2_DATA_MASK)] = value;
952 return this;
953 };
954 TrieBuilder.prototype.writeBlock = function (block, value) {
955 var limit = block + UTRIE2_DATA_BLOCK_LENGTH;
956 while (block < limit) {
957 this.data[block++] = value;
958 }
959 };
960 TrieBuilder.prototype.isInNullBlock = function (c, forLSCP) {
961 var i2 = isHighSurrogate(c) && forLSCP
962 ? UTRIE2_LSCP_INDEX_2_OFFSET - (0xd800 >> UTRIE2_SHIFT_2) + (c >> UTRIE2_SHIFT_2)
963 : this.index1[c >> UTRIE2_SHIFT_1] + ((c >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK);
964 var block = this.index2[i2];
965 return block === this.dataNullOffset;
966 };
967 TrieBuilder.prototype.fillBlock = function (block, start, limit, value, initialValue, overwrite) {
968 var pLimit = block + limit;
969 if (overwrite) {
970 for (var i = block + start; i < pLimit; i++) {
971 this.data[i] = value;
972 }
973 }
974 else {
975 for (var i = block + start; i < pLimit; i++) {
976 if (this.data[i] === initialValue) {
977 this.data[i] = value;
978 }
979 }
980 }
981 };
982 TrieBuilder.prototype.setIndex2Entry = function (i2, block) {
983 ++this.map[block >> UTRIE2_SHIFT_2]; /* increment first, in case block==oldBlock! */
984 var oldBlock = this.index2[i2];
985 if (0 === --this.map[oldBlock >> UTRIE2_SHIFT_2]) {
986 this.releaseDataBlock(oldBlock);
987 }
988 this.index2[i2] = block;
989 };
990 TrieBuilder.prototype.releaseDataBlock = function (block) {
991 /* put this block at the front of the free-block chain */
992 this.map[block >> UTRIE2_SHIFT_2] = -this.firstFreeBlock;
993 this.firstFreeBlock = block;
994 };
995 TrieBuilder.prototype.getDataBlock = function (c, forLSCP) {
996 var i2 = this.getIndex2Block(c, forLSCP);
997 i2 += (c >> UTRIE2_SHIFT_2) & UTRIE2_INDEX_2_MASK;
998 var oldBlock = this.index2[i2];
999 if (this.isWritableBlock(oldBlock)) {
1000 return oldBlock;
1001 }
1002 /* allocate a new data block */
1003 var newBlock = this.allocDataBlock(oldBlock);
1004 this.setIndex2Entry(i2, newBlock);
1005 return newBlock;
1006 };
1007 TrieBuilder.prototype.isWritableBlock = function (block) {
1008 return block !== this.dataNullOffset && 1 === this.map[block >> UTRIE2_SHIFT_2];
1009 };
1010 TrieBuilder.prototype.getIndex2Block = function (c, forLSCP) {
1011 if (c >= 0xd800 && c < 0xdc00 && forLSCP) {
1012 return UTRIE2_LSCP_INDEX_2_OFFSET;
1013 }
1014 var i1 = c >> UTRIE2_SHIFT_1;
1015 var i2 = this.index1[i1];
1016 if (i2 === this.index2NullOffset) {
1017 i2 = this.allocIndex2Block();
1018 this.index1[i1] = i2;
1019 }
1020 return i2;
1021 };
1022 TrieBuilder.prototype.allocDataBlock = function (copyBlock) {
1023 var newBlock;
1024 if (this.firstFreeBlock !== 0) {
1025 /* get the first free block */
1026 newBlock = this.firstFreeBlock;
1027 this.firstFreeBlock = -this.map[newBlock >> UTRIE2_SHIFT_2];
1028 }
1029 else {
1030 /* get a new block from the high end */
1031 newBlock = this.dataLength;
1032 var newTop = newBlock + UTRIE2_DATA_BLOCK_LENGTH;
1033 if (newTop > this.dataCapacity) {
1034 var capacity = void 0;
1035 /* out of memory in the data array */
1036 if (this.dataCapacity < UNEWTRIE2_MEDIUM_DATA_LENGTH) {
1037 capacity = UNEWTRIE2_MEDIUM_DATA_LENGTH;
1038 }
1039 else if (this.dataCapacity < UNEWTRIE2_MAX_DATA_LENGTH) {
1040 capacity = UNEWTRIE2_MAX_DATA_LENGTH;
1041 }
1042 else {
1043 /*
1044 * Should never occur.
1045 * Either UNEWTRIE2_MAX_DATA_LENGTH is incorrect,
1046 * or the code writes more values than should be possible.
1047 */
1048 throw new Error('Internal error in Trie creation.');
1049 }
1050 var newData = new Uint32Array(capacity);
1051 newData.set(this.data.subarray(0, this.dataLength));
1052 this.data = newData;
1053 this.dataCapacity = capacity;
1054 }
1055 this.dataLength = newTop;
1056 }
1057 this.data.set(this.data.subarray(copyBlock, copyBlock + UTRIE2_DATA_BLOCK_LENGTH), newBlock);
1058 this.map[newBlock >> UTRIE2_SHIFT_2] = 0;
1059 return newBlock;
1060 };
1061 TrieBuilder.prototype.allocIndex2Block = function () {
1062 var newBlock = this.index2Length;
1063 var newTop = newBlock + UTRIE2_INDEX_2_BLOCK_LENGTH;
1064 if (newTop > this.index2.length) {
1065 throw new Error('Internal error in Trie creation.');
1066 /*
1067 * Should never occur.
1068 * Either UTRIE2_MAX_BUILD_TIME_INDEX_LENGTH is incorrect,
1069 * or the code writes more values than should be possible.
1070 */
1071 }
1072 this.index2Length = newTop;
1073 this.index2.set(this.index2.subarray(this.index2NullOffset, this.index2NullOffset + UTRIE2_INDEX_2_BLOCK_LENGTH), newBlock);
1074 return newBlock;
1075 };
1076 return TrieBuilder;
1077 }());
1078 var serializeBase64 = function (trie) {
1079 var index = trie.index;
1080 var data = trie.data;
1081 if (!(index instanceof Uint16Array) || !(data instanceof Uint16Array || data instanceof Uint32Array)) {
1082 throw new Error('TrieBuilder serializer only support TypedArrays');
1083 }
1084 var headerLength = Uint32Array.BYTES_PER_ELEMENT * 6;
1085 var bufferLength = headerLength + index.byteLength + data.byteLength;
1086 var buffer = new ArrayBuffer(Math.ceil(bufferLength / 4) * 4);
1087 var view32 = new Uint32Array(buffer);
1088 var view16 = new Uint16Array(buffer);
1089 view32[0] = trie.initialValue;
1090 view32[1] = trie.errorValue;
1091 view32[2] = trie.highStart;
1092 view32[3] = trie.highValueIndex;
1093 view32[4] = index.byteLength;
1094 // $FlowFixMe
1095 view32[5] = data.BYTES_PER_ELEMENT;
1096 view16.set(index, headerLength / Uint16Array.BYTES_PER_ELEMENT);
1097 if (data.BYTES_PER_ELEMENT === Uint16Array.BYTES_PER_ELEMENT) {
1098 view16.set(data, (headerLength + index.byteLength) / Uint16Array.BYTES_PER_ELEMENT);
1099 }
1100 else {
1101 view32.set(data, Math.ceil((headerLength + index.byteLength) / Uint32Array.BYTES_PER_ELEMENT));
1102 }
1103 return [encode(new Uint8Array(buffer)), buffer.byteLength];
1104 };
1105
1106 exports.Trie = Trie;
1107 exports.TrieBuilder = TrieBuilder;
1108 exports.createTrieFromBase64 = createTrieFromBase64;
1109 exports.serializeBase64 = serializeBase64;
1110
1111 Object.defineProperty(exports, '__esModule', { value: true });
1112
1113})));
1114//# sourceMappingURL=utrie.umd.js.map
Note: See TracBrowser for help on using the repository browser.