index.js 54 KB

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