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