utrie.umd.js 52 KB

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