@tbela99/css-parser 2.1.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. package/CHANGELOG.md +66 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
  5. package/dist/{src/lib/ast → ast}/arena.js +304 -139
  6. package/dist/{src/lib/ast → ast}/cache.js +40 -24
  7. package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
  8. package/dist/{src/lib/ast → ast}/features/if.js +6 -6
  9. package/dist/ast/features/index.d.ts +7 -0
  10. package/dist/{src/lib/ast → ast}/features/index.js +3 -2
  11. package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
  12. package/dist/ast/features/mixin.js +953 -0
  13. package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
  14. package/dist/ast/features/shorthand.js +129 -0
  15. package/dist/ast/features/transform.js +188 -0
  16. package/dist/{src/lib/ast → ast}/features/type.js +2 -2
  17. package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
  18. package/dist/{src/lib/ast → ast}/math/math.js +2 -2
  19. package/dist/{src/lib/ast → ast}/minify.js +745 -201
  20. package/dist/ast/mixins/custom-function.js +393 -0
  21. package/dist/ast/mixins/utils.js +53 -0
  22. package/dist/ast/mixins/variables.js +678 -0
  23. package/dist/ast/strings.js +910 -0
  24. package/dist/ast/transform/compute.js +560 -0
  25. package/dist/ast/transform/matrix.js +67 -0
  26. package/dist/ast/transform/minify.js +325 -0
  27. package/dist/ast/transform/perspective.js +24 -0
  28. package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
  29. package/dist/ast/transform/scale.js +43 -0
  30. package/dist/ast/transform/skew.js +38 -0
  31. package/dist/ast/transform/translate.js +56 -0
  32. package/dist/ast/transform/utils.js +390 -0
  33. package/dist/{src/lib/ast → ast}/types.js +2 -2
  34. package/dist/{src/lib/ast → ast}/walk.js +2 -2
  35. package/dist/{src/data → data}/properties.json.js +86 -2
  36. package/dist/{src/data → data}/validation.json.js +30 -21
  37. package/dist/{src/lib/fs → fs}/resolve.js +2 -2
  38. package/dist/index-umd-web.js +19727 -10719
  39. package/dist/index.d.ts +738 -100
  40. package/dist/{src/node.js → node.js} +70 -52
  41. package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
  42. package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
  43. package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
  44. package/dist/{src/lib/parser → parser}/input.js +18 -3
  45. package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
  46. package/dist/{src/lib/parser → parser}/parse.js +444 -278
  47. package/dist/{src/lib/parser → parser}/source.js +3 -3
  48. package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
  49. package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
  50. package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
  51. package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
  52. package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
  53. package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
  54. package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
  55. package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
  56. package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
  57. package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
  58. package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
  59. package/dist/parser/utils/cache.js +53 -0
  60. package/dist/parser/utils/canonical.js +78 -0
  61. package/dist/parser/utils/codes.js +93 -0
  62. package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
  63. package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
  64. package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
  65. package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
  66. package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
  67. package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
  68. package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
  69. package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
  70. package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
  71. package/dist/{src/lib/printer → printer}/render.js +582 -153
  72. package/dist/printer/result.js +148 -0
  73. package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
  74. package/dist/printer/writer.js +252 -0
  75. package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
  76. package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
  77. package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
  78. package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
  79. package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
  80. package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
  81. package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
  82. package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
  83. package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
  84. package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
  85. package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
  86. package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
  87. package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
  88. package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
  89. package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
  90. package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
  91. package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
  92. package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
  93. package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
  94. package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
  95. package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
  96. package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
  97. package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
  98. package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
  99. package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
  100. package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
  101. package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
  102. package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
  103. package/dist/{src/types.js → types.js} +2 -2
  104. package/dist/{src/utils → utils}/sync.js +12 -7
  105. package/dist/validation/compiled/builtins.js +32 -0
  106. package/dist/validation/compiled/runtime.js +705 -0
  107. package/dist/validation/compiled/syntaxes.js +1620 -0
  108. package/dist/{src/lib/validation → validation}/config.js +63 -16
  109. package/dist/validation/first.js +574 -0
  110. package/dist/{src/lib/validation → validation}/match.js +376 -161
  111. package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
  112. package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
  113. package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
  114. package/dist/version.js +13 -0
  115. package/dist/{src/web.js → web.js} +68 -55
  116. package/package.json +15 -13
  117. package/dist/package.json.js +0 -10
  118. package/dist/src/lib/ast/features/shorthand.js +0 -95
  119. package/dist/src/lib/ast/features/transform.js +0 -90
  120. package/dist/src/lib/ast/transform/compute.js +0 -362
  121. package/dist/src/lib/ast/transform/matrix.js +0 -88
  122. package/dist/src/lib/ast/transform/minify.js +0 -267
  123. package/dist/src/lib/ast/transform/perspective.js +0 -16
  124. package/dist/src/lib/ast/transform/scale.js +0 -39
  125. package/dist/src/lib/ast/transform/skew.js +0 -30
  126. package/dist/src/lib/ast/transform/translate.js +0 -39
  127. package/dist/src/lib/ast/transform/utils.js +0 -287
  128. package/dist/src/lib/parser/utils/cache.js +0 -54
  129. package/dist/types.d.ts +0 -21
  130. package/dist/utils/sync.d.ts +0 -16
@@ -0,0 +1,910 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.3.0
6
+ License: MIT
7
+ */
8
+ import { hashString, hashRange } from '../parser/utils/codes.js';
9
+ import { canonicalText, canonicalCodes } from '../parser/utils/canonical.js';
10
+
11
+ /**
12
+ * String table of an arena.
13
+ *
14
+ * The strings of the nodes (selectors, names, identifiers, strings, units, ...) are interned: a node stores a string id.
15
+ * An interned string is not necessarily a string object, it is a range of a text:
16
+ *
17
+ * - a range of the content of a source file (`source.content`): the values of the tokens read by the tokenizer. the
18
+ * table stores the source index and the start and end offsets, no string is created
19
+ * - a range of the canonical text (see {@link canonicalText}): the spelling of the known units and function names
20
+ * - a range of the pool: the code units of the strings that are not a range of a source (decoded escape sequences,
21
+ * the values parsed from a string by `parseString()`) are copied to the pool
22
+ * - a string: the strings interned by {@link StringTable.intern} (generated by the minifier, the visitors, ...) are
23
+ * kept as they are
24
+ *
25
+ * The table is keyed by the hash of the code units: a string and a range with the same content have the same id.
26
+ * When the hashes are equal, the code units are compared. The strings are created when they are read
27
+ * ({@link StringTable.get}), once per id.
28
+ *
29
+ * @module
30
+ * @internal
31
+ */
32
+ /**
33
+ * where the code units of an interned string are stored. a value that is not negative is the index of a source
34
+ * (see {@link StringTable.setText})
35
+ */
36
+ var StringSource;
37
+ (function (StringSource) {
38
+ /**
39
+ * the code units are copied to the pool
40
+ */
41
+ StringSource[StringSource["Pool"] = -1] = "Pool";
42
+ /**
43
+ * range of the canonical text
44
+ */
45
+ StringSource[StringSource["Canonical"] = -2] = "Canonical";
46
+ /**
47
+ * a string
48
+ */
49
+ StringSource[StringSource["Value"] = -3] = "Value";
50
+ })(StringSource || (StringSource = {}));
51
+ /**
52
+ * boolean properties of the strings computed once per string id, see {@link StringTable.flag}. 4 flags at most
53
+ */
54
+ var StringFlag;
55
+ (function (StringFlag) {
56
+ /**
57
+ * the string is a named color, a system color or `currentcolor` / `transparent`, ignoring the case
58
+ */
59
+ StringFlag[StringFlag["Color"] = 0] = "Color";
60
+ /**
61
+ * a rule selector that is not modified by the minifier (see `isSimpleSelector()`)
62
+ */
63
+ StringFlag[StringFlag["SimpleSelector"] = 1] = "SimpleSelector";
64
+ /**
65
+ * a rule selector that contains a vendor prefixed pseudo class (see `selectorPrefixes()`)
66
+ */
67
+ StringFlag[StringFlag["VendorPrefix"] = 2] = "VendorPrefix";
68
+ })(StringFlag || (StringFlag = {}));
69
+ /**
70
+ * values derived from the strings computed once per string id, see {@link StringTable.derived}
71
+ */
72
+ var StringDerived;
73
+ (function (StringDerived) {
74
+ /**
75
+ * the validation syntax of a function (`name()`)
76
+ */
77
+ StringDerived[StringDerived["FunctionSyntax"] = 0] = "FunctionSyntax";
78
+ /**
79
+ * the validation syntax of a selector function (`:name()`)
80
+ */
81
+ StringDerived[StringDerived["SelectorFunctionSyntax"] = 1] = "SelectorFunctionSyntax";
82
+ /**
83
+ * the kind of a transform function name or of a transform property name (see `transformKind()`)
84
+ */
85
+ StringDerived[StringDerived["TransformKind"] = 2] = "TransformKind";
86
+ /**
87
+ * true if the rules are allowed in the block of an at-rule (by name, without `@`), see `parseSelector()`
88
+ */
89
+ StringDerived[StringDerived["AtRuleAcceptsRules"] = 3] = "AtRuleAcceptsRules";
90
+ })(StringDerived || (StringDerived = {}));
91
+ /**
92
+ * string of a range of code units
93
+ * @param codes
94
+ * @param start
95
+ * @param end
96
+ */
97
+ function fromCodes(codes, start, end) {
98
+ let result = "";
99
+ // String.fromCharCode() with a limited number of arguments
100
+ while (start < end) {
101
+ const next = end - start > 4096 ? start + 4096 : end;
102
+ result += String.fromCharCode.apply(null, codes.subarray(start, next));
103
+ start = next;
104
+ }
105
+ return result;
106
+ }
107
+ /**
108
+ * builds a string from code units, strings and interned strings, then interns it: no string is created
109
+ *
110
+ * @internal
111
+ */
112
+ class StringBuilder {
113
+ /**
114
+ * code units
115
+ */
116
+ codes = new Uint16Array(256);
117
+ /**
118
+ * number of code units
119
+ */
120
+ length = 0;
121
+ /**
122
+ * drop the code units
123
+ */
124
+ reset() {
125
+ this.length = 0;
126
+ return this;
127
+ }
128
+ /**
129
+ * append the code units of an interned string
130
+ * @param table
131
+ * @param id
132
+ */
133
+ appendId(table, id) {
134
+ const length = table.length(id);
135
+ this.reserve(length);
136
+ this.length += table.copy(id, this.codes, this.length);
137
+ return this;
138
+ }
139
+ /**
140
+ * append a string
141
+ * @param str
142
+ */
143
+ appendString(str) {
144
+ this.reserve(str.length);
145
+ const codes = this.codes;
146
+ let length = this.length;
147
+ for (let i = 0; i < str.length; i++) {
148
+ codes[length++] = str.charCodeAt(i);
149
+ }
150
+ this.length = length;
151
+ return this;
152
+ }
153
+ /**
154
+ * append a code unit
155
+ * @param code
156
+ */
157
+ appendCode(code) {
158
+ this.reserve(1);
159
+ this.codes[this.length++] = code;
160
+ return this;
161
+ }
162
+ /**
163
+ * intern the string
164
+ * @param table
165
+ */
166
+ intern(table) {
167
+ return table.internRange(this.codes, 0, this.length, -1 /* StringSource.Pool */);
168
+ }
169
+ /**
170
+ * make room for more code units
171
+ * @param size
172
+ */
173
+ reserve(size) {
174
+ if (this.length + size > this.codes.length) {
175
+ const codes = new Uint16Array(Math.max(this.codes.length * 2, this.length + size));
176
+ codes.set(this.codes.subarray(0, this.length));
177
+ this.codes = codes;
178
+ }
179
+ }
180
+ }
181
+ /**
182
+ * interned strings of an arena
183
+ *
184
+ * @internal
185
+ */
186
+ class StringTable {
187
+ /**
188
+ * An instance is kept alive: the hidden class of the instances would otherwise be collected by a full garbage
189
+ * collection when no instance is alive, and the optimized code that depends on it discarded (deoptimized)
190
+ */
191
+ static shape = new StringTable();
192
+ /**
193
+ * number of strings
194
+ */
195
+ count = 0;
196
+ /**
197
+ * strings by id: the strings that have been read (see {@link get}) and the `StringSource.Value` strings
198
+ */
199
+ cache = [];
200
+ /**
201
+ * identity of the ids: replaced when the ids change (see {@link compact}). the caches keyed by string ids use it
202
+ * as a key
203
+ */
204
+ epoch = {};
205
+ /**
206
+ * hash of each string
207
+ */
208
+ hashes = new Int32Array(256);
209
+ /**
210
+ * source of each string, see {@link StringSource}
211
+ */
212
+ sources = new Int32Array(256);
213
+ /**
214
+ * start of each string in its source
215
+ */
216
+ starts = new Int32Array(256);
217
+ /**
218
+ * end of each string in its source
219
+ */
220
+ ends = new Int32Array(256);
221
+ /**
222
+ * hash table: string id + 1 by slot, 0 when the slot is empty (open addressing, linear probing). the load factor
223
+ * is at most 0.5
224
+ */
225
+ slots = new Int32Array(512);
226
+ /**
227
+ * text of the sources, by source index
228
+ */
229
+ texts = [];
230
+ /**
231
+ * code units of the sources being tokenized, by source index
232
+ */
233
+ codes = [];
234
+ /**
235
+ * values derived from the strings, by slot and by id (see {@link derived})
236
+ */
237
+ derivedValues = [];
238
+ /**
239
+ * properties of the strings, by id (see {@link flag})
240
+ */
241
+ flags = new Uint8Array(0);
242
+ /**
243
+ * id of the lowercase string + 1, by id. 0 when it is not computed (see {@link lower})
244
+ */
245
+ lowers = new Int32Array(0);
246
+ /**
247
+ * builder of the lowercase strings
248
+ */
249
+ builder = new StringBuilder();
250
+ /**
251
+ * code units of the strings that are not a range of a source
252
+ */
253
+ pool = new Uint16Array(1024);
254
+ /**
255
+ * used length of the pool
256
+ */
257
+ poolLength = 0;
258
+ /**
259
+ * set the text of a source: the strings of the source are ranges of the text
260
+ * @param source source index
261
+ * @param text content of the source
262
+ * @param codes code units of the text, while the source is tokenized: the ranges are compared with the code units
263
+ * (faster than the text). see {@link releaseCodes}
264
+ */
265
+ setText(source, text, codes = null) {
266
+ this.texts[source] = text;
267
+ this.codes[source] = codes ?? undefined;
268
+ }
269
+ /**
270
+ * drop the code units of a source: the source is no longer tokenized, see {@link setText}
271
+ * @param source
272
+ */
273
+ releaseCodes(source) {
274
+ this.codes[source] = undefined;
275
+ }
276
+ /**
277
+ * intern a string
278
+ * @param str
279
+ * @returns the string id
280
+ */
281
+ intern(str) {
282
+ if (typeof str !== "string") {
283
+ str = String(str);
284
+ }
285
+ const hash = hashString(str);
286
+ const mask = this.slots.length - 1;
287
+ let slot = hash & mask;
288
+ let entry;
289
+ while ((entry = this.slots[slot]) !== 0) {
290
+ if (this.hashes[entry - 1] === hash && this.equalsString(entry - 1, str)) {
291
+ return entry - 1;
292
+ }
293
+ slot = (slot + 1) & mask;
294
+ }
295
+ // the string is kept
296
+ const id = this.add(hash, slot, -3 /* StringSource.Value */, 0, str.length);
297
+ this.cache[id] = str;
298
+ return id;
299
+ }
300
+ /**
301
+ * intern a range of code units
302
+ * @param codes code units
303
+ * @param start
304
+ * @param end
305
+ * @param source where the range is stored when it is interned: the index of the source the code units are read
306
+ * from (the range is kept), `StringSource.Canonical` (the code units are the canonical text) or `StringSource.Pool`
307
+ * (the code units are copied)
308
+ * @returns the string id
309
+ */
310
+ internRange(codes, start, end, source) {
311
+ const hash = hashRange(codes, start, end);
312
+ const mask = this.slots.length - 1;
313
+ let slot = hash & mask;
314
+ let entry;
315
+ while ((entry = this.slots[slot]) !== 0) {
316
+ if (this.hashes[entry - 1] === hash && this.equalsRange(entry - 1, codes, start, end)) {
317
+ return entry - 1;
318
+ }
319
+ slot = (slot + 1) & mask;
320
+ }
321
+ if (source !== -1 /* StringSource.Pool */) {
322
+ return this.add(hash, slot, source, start, end);
323
+ }
324
+ // copy the code units to the pool
325
+ const length = end - start;
326
+ if (this.poolLength + length > this.pool.length) {
327
+ let size = this.pool.length * 2;
328
+ while (size < this.poolLength + length) {
329
+ size *= 2;
330
+ }
331
+ const pool = new Uint16Array(size);
332
+ pool.set(this.pool.subarray(0, this.poolLength));
333
+ this.pool = pool;
334
+ }
335
+ const pool = this.pool;
336
+ const offset = this.poolLength;
337
+ for (let i = 0; i < length; i++) {
338
+ pool[offset + i] = codes[start + i];
339
+ }
340
+ this.poolLength += length;
341
+ return this.add(hash, slot, -1 /* StringSource.Pool */, offset, offset + length);
342
+ }
343
+ /**
344
+ * id of a string, -1 when the string is not interned
345
+ * @param str
346
+ */
347
+ lookup(str) {
348
+ const hash = hashString(str);
349
+ const mask = this.slots.length - 1;
350
+ let slot = hash & mask;
351
+ let entry;
352
+ while ((entry = this.slots[slot]) !== 0) {
353
+ if (this.hashes[entry - 1] === hash && this.equalsString(entry - 1, str)) {
354
+ return entry - 1;
355
+ }
356
+ slot = (slot + 1) & mask;
357
+ }
358
+ return -1;
359
+ }
360
+ /**
361
+ * the string of an id. the string is created the first time it is read
362
+ * @param id
363
+ */
364
+ get(id) {
365
+ let str = this.cache[id];
366
+ if (str === undefined && id < this.count) {
367
+ const source = this.sources[id];
368
+ str =
369
+ source === -1 /* StringSource.Pool */
370
+ ? fromCodes(this.pool, this.starts[id], this.ends[id])
371
+ : (source === -2 /* StringSource.Canonical */ ? canonicalText() : this.texts[source]).slice(this.starts[id], this.ends[id]);
372
+ this.cache[id] = str;
373
+ }
374
+ return str;
375
+ }
376
+ /**
377
+ * length of a string
378
+ * @param id
379
+ */
380
+ length(id) {
381
+ return this.ends[id] - this.starts[id];
382
+ }
383
+ /**
384
+ * code unit of a string
385
+ * @param id
386
+ * @param index
387
+ */
388
+ codeAt(id, index) {
389
+ const source = this.sources[id];
390
+ const offset = this.starts[id] + index;
391
+ if (source >= 0) {
392
+ return this.texts[source].charCodeAt(offset);
393
+ }
394
+ switch (source) {
395
+ case -1 /* StringSource.Pool */:
396
+ return this.pool[offset];
397
+ case -2 /* StringSource.Canonical */:
398
+ return canonicalText().charCodeAt(offset);
399
+ default:
400
+ return this.cache[id].charCodeAt(index);
401
+ }
402
+ }
403
+ /**
404
+ * copy the code units of a string to a buffer. the string is not created
405
+ * @param id
406
+ * @param target
407
+ * @param offset offset in the target
408
+ * @returns the number of code units
409
+ */
410
+ copy(id, target, offset) {
411
+ const source = this.sources[id];
412
+ const start = this.starts[id];
413
+ const length = this.ends[id] - start;
414
+ if (source === -1 /* StringSource.Pool */) {
415
+ target.set(this.pool.subarray(start, start + length), offset);
416
+ }
417
+ else if (source === -2 /* StringSource.Canonical */) {
418
+ target.set(canonicalCodes().subarray(start, start + length), offset);
419
+ }
420
+ else {
421
+ const text = source === -3 /* StringSource.Value */ ? this.cache[id] : this.texts[source];
422
+ const base = source === -3 /* StringSource.Value */ ? 0 : start;
423
+ for (let i = 0; i < length; i++) {
424
+ target[offset + i] = text.charCodeAt(base + i);
425
+ }
426
+ }
427
+ return length;
428
+ }
429
+ /**
430
+ * a boolean property of a string, computed once per id: the string is read once (see {@link StringFlag})
431
+ * @param id
432
+ * @param flag
433
+ * @param compute computes the property from the string
434
+ */
435
+ flag(id, flag, compute) {
436
+ if (id >= this.flags.length) {
437
+ const flags = new Uint8Array(Math.max(this.count, id + 1) * 2);
438
+ flags.set(this.flags);
439
+ this.flags = flags;
440
+ }
441
+ // 2 bits by flag: computed, value
442
+ const shift = flag * 2;
443
+ let value = (this.flags[id] >> shift) & 3;
444
+ if (value === 0) {
445
+ value = compute(this.get(id)) ? 3 : 1;
446
+ this.flags[id] |= value << shift;
447
+ }
448
+ return value === 3;
449
+ }
450
+ /**
451
+ * a boolean property of a string, computed once per id from the string id (the string is not necessarily read),
452
+ * see {@link flag}
453
+ * @param id
454
+ * @param flag
455
+ * @param compute computes the property from the string id
456
+ */
457
+ flagId(id, flag, compute) {
458
+ if (id >= this.flags.length) {
459
+ const flags = new Uint8Array(Math.max(this.count, id + 1) * 2);
460
+ flags.set(this.flags);
461
+ this.flags = flags;
462
+ }
463
+ const shift = flag * 2;
464
+ let value = (this.flags[id] >> shift) & 3;
465
+ if (value === 0) {
466
+ value = compute(id) ? 3 : 1;
467
+ this.flags[id] |= value << shift;
468
+ }
469
+ return value === 3;
470
+ }
471
+ /**
472
+ * a value derived from a string, computed once per id (see {@link StringDerived})
473
+ * @param id
474
+ * @param slot
475
+ * @param compute computes the value from the string id
476
+ */
477
+ derived(id, slot, compute) {
478
+ let values = this.derivedValues[slot];
479
+ if (values === undefined) {
480
+ values = this.derivedValues[slot] = new Map();
481
+ }
482
+ let value = values.get(id);
483
+ if (value === undefined && !values.has(id)) {
484
+ value = compute(id);
485
+ values.set(id, value);
486
+ }
487
+ return value;
488
+ }
489
+ /**
490
+ * copy the code units of a string to a byte buffer when they are all ascii (UTF-8 encoding). the string is not
491
+ * created
492
+ * @param id
493
+ * @param target
494
+ * @param offset offset in the target. the target has room for the string
495
+ * @returns the number of bytes, -1 when the string is not ascii
496
+ */
497
+ copyAscii(id, target, offset) {
498
+ const source = this.sources[id];
499
+ const start = this.starts[id];
500
+ const length = this.ends[id] - start;
501
+ let code;
502
+ if (source === -1 /* StringSource.Pool */ || source === -2 /* StringSource.Canonical */) {
503
+ const codes = source === -1 /* StringSource.Pool */ ? this.pool : canonicalCodes();
504
+ for (let i = 0; i < length; i++) {
505
+ if ((code = codes[start + i]) > 0x7f) {
506
+ return -1;
507
+ }
508
+ target[offset + i] = code;
509
+ }
510
+ }
511
+ else {
512
+ const text = source === -3 /* StringSource.Value */ ? this.cache[id] : this.texts[source];
513
+ const base = source === -3 /* StringSource.Value */ ? 0 : start;
514
+ for (let i = 0; i < length; i++) {
515
+ if ((code = text.charCodeAt(base + i)) > 0x7f) {
516
+ return -1;
517
+ }
518
+ target[offset + i] = code;
519
+ }
520
+ }
521
+ return length;
522
+ }
523
+ /**
524
+ * index of the first occurrence of a code unit in a string, -1 when it is not found. the string is not created
525
+ * @param id
526
+ * @param code
527
+ */
528
+ indexOfCode(id, code) {
529
+ const source = this.sources[id];
530
+ const start = this.starts[id];
531
+ const length = this.ends[id] - start;
532
+ if (source === -1 /* StringSource.Pool */ || source === -2 /* StringSource.Canonical */) {
533
+ const codes = source === -1 /* StringSource.Pool */ ? this.pool : canonicalCodes();
534
+ for (let i = 0; i < length; i++) {
535
+ if (codes[start + i] === code) {
536
+ return i;
537
+ }
538
+ }
539
+ return -1;
540
+ }
541
+ const text = source === -3 /* StringSource.Value */ ? this.cache[id] : this.texts[source];
542
+ const base = source === -3 /* StringSource.Value */ ? 0 : start;
543
+ for (let i = 0; i < length; i++) {
544
+ if (text.charCodeAt(base + i) === code) {
545
+ return i;
546
+ }
547
+ }
548
+ return -1;
549
+ }
550
+ /**
551
+ * true if the string of an id equals a string. the string is not created
552
+ * @param id
553
+ * @param str
554
+ */
555
+ equals(id, str) {
556
+ const cached = this.cache[id];
557
+ if (cached !== undefined) {
558
+ return cached === str;
559
+ }
560
+ if (id >= this.count || this.length(id) !== str.length) {
561
+ return false;
562
+ }
563
+ for (let i = 0; i < str.length; i++) {
564
+ if (this.codeAt(id, i) !== str.charCodeAt(i)) {
565
+ return false;
566
+ }
567
+ }
568
+ return true;
569
+ }
570
+ /**
571
+ * id of the lowercase string (like `toLowerCase()`): the id itself when the string has no uppercase letter. the
572
+ * string is not created when it is ascii
573
+ * @param id
574
+ */
575
+ lower(id) {
576
+ if (id < this.lowers.length && this.lowers[id] !== 0) {
577
+ return this.lowers[id] - 1;
578
+ }
579
+ const length = this.length(id);
580
+ let result = id;
581
+ for (let i = 0; i < length; i++) {
582
+ const code = this.codeAt(id, i);
583
+ if (code > 0x7f) {
584
+ // unicode case mapping
585
+ const str = this.get(id);
586
+ const lower = str.toLowerCase();
587
+ result = lower === str ? id : this.intern(lower);
588
+ break;
589
+ }
590
+ if (code >= 0x41 && code <= 0x5a) {
591
+ // ascii: lowercased in the builder
592
+ const builder = this.builder.reset();
593
+ builder.appendId(this, id);
594
+ const codes = builder.codes;
595
+ for (let k = i; k < length; k++) {
596
+ if (codes[k] >= 0x41 && codes[k] <= 0x5a) {
597
+ codes[k] += 32;
598
+ }
599
+ }
600
+ result = builder.intern(this);
601
+ break;
602
+ }
603
+ }
604
+ if (id >= this.lowers.length) {
605
+ const lowers = new Int32Array(Math.max(this.count, id + 1) * 2);
606
+ lowers.set(this.lowers);
607
+ this.lowers = lowers;
608
+ }
609
+ this.lowers[id] = result + 1;
610
+ return result;
611
+ }
612
+ /**
613
+ * true if the string of an id starts with a string. the string is not created
614
+ * @param id
615
+ * @param str
616
+ */
617
+ startsWith(id, str) {
618
+ if (id >= this.count || this.length(id) < str.length) {
619
+ return false;
620
+ }
621
+ for (let i = 0; i < str.length; i++) {
622
+ if (this.codeAt(id, i) !== str.charCodeAt(i)) {
623
+ return false;
624
+ }
625
+ }
626
+ return true;
627
+ }
628
+ /**
629
+ * true if the string of an id equals a string, ignoring the case of the ascii letters. the string is not created
630
+ * @param id
631
+ * @param str
632
+ */
633
+ equalsIgnoreCase(id, str) {
634
+ if (id >= this.count || this.length(id) !== str.length) {
635
+ return false;
636
+ }
637
+ for (let i = 0; i < str.length; i++) {
638
+ let a = this.codeAt(id, i);
639
+ let b = str.charCodeAt(i);
640
+ if (a >= 65 && a <= 90) {
641
+ a += 32;
642
+ }
643
+ if (b >= 65 && b <= 90) {
644
+ b += 32;
645
+ }
646
+ if (a !== b) {
647
+ return false;
648
+ }
649
+ }
650
+ return true;
651
+ }
652
+ /**
653
+ * drop the strings that are not used and renumber the others
654
+ * @param used used strings, by id
655
+ * @returns the new id of each string
656
+ */
657
+ compact(used) {
658
+ const mapping = new Uint32Array(this.count);
659
+ const previous = this.clone();
660
+ let pool = new Uint16Array(Math.max(1024, this.poolLength));
661
+ let poolLength = 0;
662
+ let count = 0;
663
+ for (let id = 0; id < previous.count; id++) {
664
+ if (used[id]) {
665
+ mapping[id] = count++;
666
+ }
667
+ }
668
+ this.reset(count);
669
+ for (let id = 0; id < previous.count; id++) {
670
+ if (!used[id]) {
671
+ continue;
672
+ }
673
+ const target = mapping[id];
674
+ let start = previous.starts[id];
675
+ let end = previous.ends[id];
676
+ // the pool only keeps the used strings
677
+ if (previous.sources[id] === -1 /* StringSource.Pool */) {
678
+ pool.set(previous.pool.subarray(start, end), poolLength);
679
+ end = poolLength + end - start;
680
+ start = poolLength;
681
+ poolLength = end;
682
+ }
683
+ this.hashes[target] = previous.hashes[id];
684
+ this.sources[target] = previous.sources[id];
685
+ this.starts[target] = start;
686
+ this.ends[target] = end;
687
+ this.cache[target] = previous.cache[id];
688
+ }
689
+ this.pool = pool;
690
+ this.poolLength = poolLength;
691
+ this.texts = previous.texts;
692
+ this.rehash();
693
+ return mapping;
694
+ }
695
+ /**
696
+ * structured-cloneable copy of the table
697
+ */
698
+ export() {
699
+ const values = [];
700
+ for (let id = 0; id < this.count; id++) {
701
+ if (this.sources[id] === -3 /* StringSource.Value */) {
702
+ values[id] = this.cache[id];
703
+ }
704
+ }
705
+ return {
706
+ count: this.count,
707
+ hashes: this.hashes.slice(0, this.count),
708
+ sources: this.sources.slice(0, this.count),
709
+ starts: this.starts.slice(0, this.count),
710
+ ends: this.ends.slice(0, this.count),
711
+ texts: this.texts.slice(),
712
+ pool: this.pool.slice(0, this.poolLength),
713
+ values,
714
+ };
715
+ }
716
+ /**
717
+ * initialize the table from an exported copy
718
+ * @param data
719
+ */
720
+ import(data) {
721
+ this.reset(data.count);
722
+ this.hashes.set(data.hashes);
723
+ this.sources.set(data.sources);
724
+ this.starts.set(data.starts);
725
+ this.ends.set(data.ends);
726
+ this.texts = data.texts.slice();
727
+ this.pool = data.pool.length > 0 ? data.pool.slice() : new Uint16Array(1024);
728
+ this.poolLength = data.pool.length;
729
+ for (let id = 0; id < data.count; id++) {
730
+ if (data.sources[id] === -3 /* StringSource.Value */) {
731
+ this.cache[id] = data.values[id];
732
+ }
733
+ }
734
+ this.rehash();
735
+ }
736
+ /**
737
+ * add a string
738
+ * @param hash
739
+ * @param slot empty slot of the hash table
740
+ * @param source
741
+ * @param start
742
+ * @param end
743
+ */
744
+ add(hash, slot, source, start, end) {
745
+ const id = this.count++;
746
+ if (id === this.hashes.length) {
747
+ this.grow(id * 2);
748
+ }
749
+ this.hashes[id] = hash;
750
+ this.sources[id] = source;
751
+ this.starts[id] = start;
752
+ this.ends[id] = end;
753
+ this.slots[slot] = id + 1;
754
+ if (this.count * 2 > this.slots.length) {
755
+ this.rehash();
756
+ }
757
+ return id;
758
+ }
759
+ /**
760
+ * true if a string has the code units of a range
761
+ * @param id
762
+ * @param codes
763
+ * @param start
764
+ * @param end
765
+ */
766
+ equalsRange(id, codes, start, end) {
767
+ const length = end - start;
768
+ const source = this.sources[id];
769
+ const offset = this.starts[id];
770
+ if (this.ends[id] - offset !== length) {
771
+ return false;
772
+ }
773
+ if (source === -1 /* StringSource.Pool */) {
774
+ const pool = this.pool;
775
+ for (let i = 0; i < length; i++) {
776
+ if (pool[offset + i] !== codes[start + i]) {
777
+ return false;
778
+ }
779
+ }
780
+ return true;
781
+ }
782
+ if (source === -2 /* StringSource.Canonical */) {
783
+ const canonical = canonicalCodes();
784
+ for (let i = 0; i < length; i++) {
785
+ if (canonical[offset + i] !== codes[start + i]) {
786
+ return false;
787
+ }
788
+ }
789
+ return true;
790
+ }
791
+ const sourceCodes = source >= 0 ? this.codes[source] : undefined;
792
+ // the source being tokenized: the code units are compared
793
+ if (sourceCodes !== undefined) {
794
+ for (let i = 0; i < length; i++) {
795
+ if (sourceCodes[offset + i] !== codes[start + i]) {
796
+ return false;
797
+ }
798
+ }
799
+ return true;
800
+ }
801
+ const text = source === -3 /* StringSource.Value */ ? this.cache[id] : this.texts[source];
802
+ for (let i = 0; i < length; i++) {
803
+ if (text.charCodeAt(offset + i) !== codes[start + i]) {
804
+ return false;
805
+ }
806
+ }
807
+ return true;
808
+ }
809
+ /**
810
+ * true if a string equals a string
811
+ * @param id
812
+ * @param str
813
+ */
814
+ equalsString(id, str) {
815
+ const source = this.sources[id];
816
+ if (source === -3 /* StringSource.Value */ || this.cache[id] !== undefined) {
817
+ return this.cache[id] === str;
818
+ }
819
+ const length = str.length;
820
+ const offset = this.starts[id];
821
+ if (this.ends[id] - offset !== length) {
822
+ return false;
823
+ }
824
+ if (source === -1 /* StringSource.Pool */) {
825
+ const pool = this.pool;
826
+ for (let i = 0; i < length; i++) {
827
+ if (pool[offset + i] !== str.charCodeAt(i)) {
828
+ return false;
829
+ }
830
+ }
831
+ return true;
832
+ }
833
+ const text = source === -2 /* StringSource.Canonical */ ? canonicalText() : this.texts[source];
834
+ for (let i = 0; i < length; i++) {
835
+ if (text.charCodeAt(offset + i) !== str.charCodeAt(i)) {
836
+ return false;
837
+ }
838
+ }
839
+ return true;
840
+ }
841
+ /**
842
+ * grow the columns of the strings
843
+ * @param size
844
+ */
845
+ grow(size) {
846
+ const grow = (array) => {
847
+ const result = new Int32Array(size);
848
+ result.set(array.subarray(0, Math.min(array.length, size)));
849
+ return result;
850
+ };
851
+ this.hashes = grow(this.hashes);
852
+ this.sources = grow(this.sources);
853
+ this.starts = grow(this.starts);
854
+ this.ends = grow(this.ends);
855
+ }
856
+ /**
857
+ * drop the strings, allocate the columns for `count` strings. the ids change
858
+ * @param count
859
+ */
860
+ reset(count) {
861
+ const size = Math.max(256, count);
862
+ this.count = count;
863
+ this.cache = [];
864
+ this.epoch = {};
865
+ this.lowers = new Int32Array(0);
866
+ this.flags = new Uint8Array(0);
867
+ this.derivedValues = [];
868
+ this.hashes = new Int32Array(size);
869
+ this.sources = new Int32Array(size);
870
+ this.starts = new Int32Array(size);
871
+ this.ends = new Int32Array(size);
872
+ }
873
+ /**
874
+ * rebuild the hash table
875
+ */
876
+ rehash() {
877
+ let size = 512;
878
+ while (size < this.count * 4) {
879
+ size *= 2;
880
+ }
881
+ const slots = new Int32Array(size);
882
+ const mask = size - 1;
883
+ for (let id = 0; id < this.count; id++) {
884
+ let slot = this.hashes[id] & mask;
885
+ while (slots[slot] !== 0) {
886
+ slot = (slot + 1) & mask;
887
+ }
888
+ slots[slot] = id + 1;
889
+ }
890
+ this.slots = slots;
891
+ }
892
+ /**
893
+ * shallow copy of the table
894
+ */
895
+ clone() {
896
+ const table = new StringTable();
897
+ table.count = this.count;
898
+ table.cache = this.cache;
899
+ table.hashes = this.hashes;
900
+ table.sources = this.sources;
901
+ table.starts = this.starts;
902
+ table.ends = this.ends;
903
+ table.texts = this.texts;
904
+ table.pool = this.pool;
905
+ table.poolLength = this.poolLength;
906
+ return table;
907
+ }
908
+ }
909
+
910
+ export { StringBuilder, StringDerived, StringFlag, StringSource, StringTable };