spliterator 2.2.2 → 3.0.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/LICENSE.md +21 -650
- package/README.md +42 -0
- package/out/index.d.ts +2 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +2 -1
- package/out/index.js.map +1 -1
- package/out/lib/AsyncSpliterator.d.ts +33 -28
- package/out/lib/AsyncSpliterator.d.ts.map +1 -1
- package/out/lib/AsyncSpliterator.js +84 -70
- package/out/lib/AsyncSpliterator.js.map +1 -1
- package/out/lib/BufferController.d.ts +1 -1
- package/out/lib/BufferController.js +1 -1
- package/out/lib/CSVSpliterator.d.ts +1 -1
- package/out/lib/CSVSpliterator.d.ts.map +1 -1
- package/out/lib/CSVSpliterator.js +5 -3
- package/out/lib/CSVSpliterator.js.map +1 -1
- package/out/lib/CharacterSequence.d.ts +15 -64
- package/out/lib/CharacterSequence.d.ts.map +1 -1
- package/out/lib/CharacterSequence.js +194 -89
- package/out/lib/CharacterSequence.js.map +1 -1
- package/out/lib/CompositeDataView.d.ts +1 -1
- package/out/lib/CompositeDataView.js +1 -1
- package/out/lib/IndexQueue.d.ts +1 -1
- package/out/lib/IndexQueue.d.ts.map +1 -1
- package/out/lib/IndexQueue.js +40 -11
- package/out/lib/IndexQueue.js.map +1 -1
- package/out/lib/JSONSpliterator.d.ts +1 -1
- package/out/lib/JSONSpliterator.js +1 -1
- package/out/lib/PSVSpliterator.d.ts +1 -1
- package/out/lib/PSVSpliterator.js +1 -1
- package/out/lib/SlidingWindow.d.ts +1 -1
- package/out/lib/SlidingWindow.d.ts.map +1 -1
- package/out/lib/SlidingWindow.js +11 -10
- package/out/lib/SlidingWindow.js.map +1 -1
- package/out/lib/Spliterator.d.ts +1 -1
- package/out/lib/Spliterator.d.ts.map +1 -1
- package/out/lib/Spliterator.js +99 -110
- package/out/lib/Spliterator.js.map +1 -1
- package/out/lib/TSVSpliterator.d.ts +1 -1
- package/out/lib/TSVSpliterator.js +1 -1
- package/out/lib/TextSpliterator.d.ts +1 -1
- package/out/lib/TextSpliterator.js +1 -1
- package/out/lib/casing.d.ts +1 -1
- package/out/lib/casing.js +1 -1
- package/out/lib/segment-runtime.d.ts +27 -0
- package/out/lib/segment-runtime.d.ts.map +1 -0
- package/out/lib/segment-runtime.js +38 -0
- package/out/lib/segment-runtime.js.map +1 -0
- package/out/lib/segment-worker-entry.d.ts +10 -0
- package/out/lib/segment-worker-entry.d.ts.map +1 -0
- package/out/lib/segment-worker-entry.js +56 -0
- package/out/lib/segment-worker-entry.js.map +1 -0
- package/out/lib/segment-workers.d.ts +41 -0
- package/out/lib/segment-workers.d.ts.map +1 -0
- package/out/lib/segment-workers.js +105 -0
- package/out/lib/segment-workers.js.map +1 -0
- package/out/lib/segments.d.ts +25 -0
- package/out/lib/segments.d.ts.map +1 -0
- package/out/lib/segments.js +50 -0
- package/out/lib/segments.js.map +1 -0
- package/out/lib/shared.d.ts +1 -1
- package/out/lib/shared.js +1 -1
- package/out/lib/take.d.ts +1 -1
- package/out/lib/take.js +1 -1
- package/out/lib/wasm_base64.d.ts +16 -0
- package/out/lib/wasm_base64.d.ts.map +1 -0
- package/out/lib/wasm_base64.js +16 -0
- package/out/lib/wasm_base64.js.map +1 -0
- package/out/lib/wasm_module.d.ts +36 -0
- package/out/lib/wasm_module.d.ts.map +1 -0
- package/out/lib/wasm_module.js +33 -0
- package/out/lib/wasm_module.js.map +1 -0
- package/out/lib/writer.d.ts +1 -1
- package/out/lib/writer.js +1 -1
- package/out/node/cli/commands/csv.d.ts +1 -1
- package/out/node/cli/commands/csv.js +1 -1
- package/out/node/cli/commands/iterate.d.ts +1 -1
- package/out/node/cli/commands/iterate.js +1 -1
- package/out/node/cli/index.d.ts +1 -1
- package/out/node/cli/index.js +1 -1
- package/out/node/cli/utils.d.ts +1 -1
- package/out/node/cli/utils.js +1 -1
- package/out/node/fs/index.d.ts +14 -2
- package/out/node/fs/index.d.ts.map +1 -1
- package/out/node/fs/index.js +26 -2
- package/out/node/fs/index.js.map +1 -1
- package/package.json +5 -3
|
@@ -1,97 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @copyright Sister Software
|
|
3
|
-
* @license
|
|
3
|
+
* @license MIT
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*/
|
|
6
|
+
import type { ByteRange } from "./shared.js";
|
|
7
|
+
import { type MatchResult } from "./wasm_module.js";
|
|
9
8
|
export declare function isArrayLike<T>(input: unknown): input is ArrayLike<T>;
|
|
10
|
-
/**
|
|
11
|
-
* A possible input for a delimiter:
|
|
12
|
-
*
|
|
13
|
-
* - A single character code.
|
|
14
|
-
* - A string of characters.
|
|
15
|
-
* - An array of character codes.
|
|
16
|
-
* - A buffer.
|
|
17
|
-
* - An iterable of character codes.
|
|
18
|
-
*/
|
|
19
9
|
export type CharacterSequenceInput = number | string | DataView | ArrayBuffer | Buffer | Iterable<number>;
|
|
20
|
-
/**
|
|
21
|
-
* Common delimiter values.
|
|
22
|
-
*/
|
|
23
10
|
export declare const Delimiters: {
|
|
24
|
-
/**
|
|
25
|
-
* Null (␀)
|
|
26
|
-
*/
|
|
27
11
|
readonly Null: 0;
|
|
28
|
-
/**
|
|
29
|
-
* Newline (␊)
|
|
30
|
-
*/
|
|
31
12
|
readonly LineFeed: 10;
|
|
32
|
-
/**
|
|
33
|
-
* Carriage return (␍)
|
|
34
|
-
*/
|
|
35
13
|
readonly CarriageReturn: 13;
|
|
36
|
-
/**
|
|
37
|
-
* Comma (–)
|
|
38
|
-
*/
|
|
39
14
|
readonly Comma: 44;
|
|
40
|
-
/**
|
|
41
|
-
* Tab (␉)
|
|
42
|
-
*/
|
|
43
15
|
readonly Tab: 9;
|
|
44
|
-
/**
|
|
45
|
-
* Space (␠)
|
|
46
|
-
*/
|
|
47
16
|
readonly Space: 32;
|
|
48
|
-
/**
|
|
49
|
-
* One (1)
|
|
50
|
-
*/
|
|
51
17
|
readonly One: 49;
|
|
52
|
-
/**
|
|
53
|
-
* Zero (0)
|
|
54
|
-
*/
|
|
55
18
|
readonly Zero: 48;
|
|
56
|
-
/**
|
|
57
|
-
* Double quote (")
|
|
58
|
-
*/
|
|
59
19
|
readonly DoubleQuote: 34;
|
|
60
|
-
/**
|
|
61
|
-
* Record separator (␞)
|
|
62
|
-
*/
|
|
63
20
|
readonly RecordSeparator: 30;
|
|
64
|
-
/**
|
|
65
|
-
* Pipe (|)
|
|
66
|
-
*/
|
|
67
21
|
readonly Pipe: 124;
|
|
68
22
|
};
|
|
69
23
|
export declare const VisibleDelimiterMap: Map<number, string>;
|
|
70
24
|
export declare function debugAsVisibleCharacters(delimiter: Uint8Array): string;
|
|
71
25
|
export declare function normalizeCharacterInput(input: CharacterSequenceInput): Uint8Array;
|
|
72
|
-
/**
|
|
73
|
-
* An encoded sequence of characters, typically bytes representing a delimiter.
|
|
74
|
-
*
|
|
75
|
-
* Unlike a string, this class is optimized for searching and slicing.
|
|
76
|
-
*
|
|
77
|
-
* This also allows for multi-byte delimiters, such as CRLF or unicode characters.
|
|
78
|
-
*/
|
|
79
26
|
export declare class CharacterSequence extends Uint8Array {
|
|
80
27
|
#private;
|
|
81
28
|
/**
|
|
82
|
-
*
|
|
29
|
+
* Resolve once the WASM SIMD scanner has finished loading, yielding whether it is active.
|
|
83
30
|
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* @returns The byte index of the pattern in the text, or -1 if not found.
|
|
31
|
+
* The module loads asynchronously, so synchronous callers (`Spliterator.fromSync`, `CSVSpliterator.from`) that run to
|
|
32
|
+
* completion in a single tick would otherwise always fall back to the JS scanner. Await this first to opt into SIMD
|
|
33
|
+
* acceleration.
|
|
89
34
|
*/
|
|
35
|
+
static whenReady(): Promise<boolean>;
|
|
90
36
|
search(haystack: Uint8Array, start?: number, end?: number): number;
|
|
91
|
-
|
|
37
|
+
searchAll(haystack: Uint8Array, start?: number, end?: number): ByteRange[];
|
|
92
38
|
/**
|
|
93
|
-
*
|
|
39
|
+
* Scan for two patterns simultaneously (delimiter + quote) for CSV parsing.
|
|
40
|
+
*
|
|
41
|
+
* Returns sorted MatchResult[] with patternId 0=delimiter, 1=quote. Uses WASM SIMD double-scan when available; JS
|
|
42
|
+
* fallback otherwise.
|
|
94
43
|
*/
|
|
44
|
+
searchMatches(haystack: Uint8Array, quotePattern: Uint8Array, start?: number, end?: number): MatchResult[];
|
|
45
|
+
decode(encoding?: string): string;
|
|
95
46
|
constructor(input?: CharacterSequenceInput);
|
|
96
47
|
}
|
|
97
48
|
//# sourceMappingURL=CharacterSequence.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CharacterSequence.d.ts","sourceRoot":"","sources":["../../lib/CharacterSequence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH
|
|
1
|
+
{"version":3,"file":"CharacterSequence.d.ts","sourceRoot":"","sources":["../../lib/CharacterSequence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAC5C,OAAO,EAIN,KAAK,WAAW,EAGhB,MAAM,kBAAkB,CAAA;AAEzB,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAAC,CAAC,CAAC,CAEpE;AAED,MAAM,MAAM,sBAAsB,GAAG,MAAM,GAAG,MAAM,GAAG,QAAQ,GAAG,WAAW,GAAG,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAA;AAEzG,eAAO,MAAM,UAAU;;;;;;;;;;;;CAYoB,CAAA;AAE3C,eAAO,MAAM,mBAAmB,qBAS9B,CAAA;AAEF,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM,CAItE;AAID,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,sBAAsB,GAAG,UAAU,CAcjF;AAaD,qBAAa,iBAAkB,SAAQ,UAAU;;IA4BhD;;;;;;OAMG;WACW,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAIpC,MAAM,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,GAAE,MAAU,EAAE,GAAG,GAAE,MAAwB,GAAG,MAAM;IAuDtF,SAAS,CAAC,QAAQ,EAAE,UAAU,EAAE,KAAK,GAAE,MAAU,EAAE,GAAG,GAAE,MAAwB,GAAG,SAAS,EAAE;IA8DrG;;;;;OAKG;IACI,aAAa,CACnB,QAAQ,EAAE,UAAU,EACpB,YAAY,EAAE,UAAU,EACxB,KAAK,GAAE,MAAU,EACjB,GAAG,GAAE,MAAwB,GAC3B,WAAW,EAAE;IAqET,MAAM,CAAC,QAAQ,GAAE,MAAgB,GAAG,MAAM;gBAIrC,KAAK,GAAE,sBAA4C;CAO/D"}
|
|
@@ -1,155 +1,260 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @copyright Sister Software
|
|
3
|
-
* @license
|
|
3
|
+
* @license MIT
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
|
-
* Type-predicate to determine if a value is an array-like object.
|
|
8
|
-
*/
|
|
6
|
+
import { loadWasmModule, WASM_MAX_RESULTS, WASM_THRESHOLD, } from "./wasm_module.js";
|
|
9
7
|
export function isArrayLike(input) {
|
|
10
8
|
return Boolean(input && typeof input === "object" && "length" in input);
|
|
11
9
|
}
|
|
12
|
-
/**
|
|
13
|
-
* Common delimiter values.
|
|
14
|
-
*/
|
|
15
10
|
export const Delimiters = {
|
|
16
|
-
/**
|
|
17
|
-
* Null (␀)
|
|
18
|
-
*/
|
|
19
11
|
Null: 0,
|
|
20
|
-
/**
|
|
21
|
-
* Newline (␊)
|
|
22
|
-
*/
|
|
23
12
|
LineFeed: 10,
|
|
24
|
-
/**
|
|
25
|
-
* Carriage return (␍)
|
|
26
|
-
*/
|
|
27
13
|
CarriageReturn: 13,
|
|
28
|
-
/**
|
|
29
|
-
* Comma (–)
|
|
30
|
-
*/
|
|
31
14
|
Comma: 44,
|
|
32
|
-
/**
|
|
33
|
-
* Tab (␉)
|
|
34
|
-
*/
|
|
35
15
|
Tab: 9,
|
|
36
|
-
/**
|
|
37
|
-
* Space (␠)
|
|
38
|
-
*/
|
|
39
16
|
Space: 32,
|
|
40
|
-
/**
|
|
41
|
-
* One (1)
|
|
42
|
-
*/
|
|
43
17
|
One: 49,
|
|
44
|
-
/**
|
|
45
|
-
* Zero (0)
|
|
46
|
-
*/
|
|
47
18
|
Zero: 48,
|
|
48
|
-
/**
|
|
49
|
-
* Double quote (")
|
|
50
|
-
*/
|
|
51
19
|
DoubleQuote: 34,
|
|
52
|
-
/**
|
|
53
|
-
* Record separator (␞)
|
|
54
|
-
*/
|
|
55
20
|
RecordSeparator: 30,
|
|
56
|
-
/**
|
|
57
|
-
* Pipe (|)
|
|
58
|
-
*/
|
|
59
21
|
Pipe: 124,
|
|
60
22
|
};
|
|
61
23
|
export const VisibleDelimiterMap = new Map([
|
|
62
|
-
[Delimiters.LineFeed, "
|
|
63
|
-
[Delimiters.CarriageReturn, "
|
|
24
|
+
[Delimiters.LineFeed, "\u2424"],
|
|
25
|
+
[Delimiters.CarriageReturn, "\u240D"],
|
|
64
26
|
[Delimiters.Comma, "-"],
|
|
65
|
-
[Delimiters.Tab, "
|
|
66
|
-
[Delimiters.Space, "
|
|
27
|
+
[Delimiters.Tab, "\u2409"],
|
|
28
|
+
[Delimiters.Space, "\u2420"],
|
|
67
29
|
[Delimiters.DoubleQuote, '"'],
|
|
68
|
-
[Delimiters.RecordSeparator, "
|
|
69
|
-
[Delimiters.Null, "
|
|
30
|
+
[Delimiters.RecordSeparator, "\u241E"],
|
|
31
|
+
[Delimiters.Null, "\u2400"],
|
|
70
32
|
]);
|
|
71
33
|
export function debugAsVisibleCharacters(delimiter) {
|
|
72
34
|
return Array.from(delimiter)
|
|
73
|
-
.map((
|
|
74
|
-
const visible = VisibleDelimiterMap.get(charCode);
|
|
75
|
-
return visible ?? String.fromCharCode(charCode);
|
|
76
|
-
})
|
|
35
|
+
.map((c) => VisibleDelimiterMap.get(c) ?? String.fromCharCode(c))
|
|
77
36
|
.join("");
|
|
78
37
|
}
|
|
38
|
+
const encoder = new TextEncoder();
|
|
79
39
|
export function normalizeCharacterInput(input) {
|
|
80
40
|
switch (typeof input) {
|
|
81
41
|
case "number":
|
|
82
|
-
if (!Number.isInteger(input))
|
|
83
|
-
throw new TypeError(`Numeric delimiters must be integers
|
|
84
|
-
}
|
|
42
|
+
if (!Number.isInteger(input))
|
|
43
|
+
throw new TypeError(`Numeric delimiters must be integers.`);
|
|
85
44
|
return Uint8Array.from([input]);
|
|
86
45
|
case "string":
|
|
87
|
-
return
|
|
46
|
+
return encoder.encode(input);
|
|
88
47
|
case "object":
|
|
89
|
-
if (isArrayLike(input))
|
|
48
|
+
if (isArrayLike(input))
|
|
90
49
|
return input;
|
|
91
|
-
|
|
92
|
-
if (Symbol.iterator in input) {
|
|
50
|
+
if (Symbol.iterator in input)
|
|
93
51
|
return Uint8Array.from(input);
|
|
94
|
-
|
|
95
|
-
throw new TypeError(`Invalid delimiter type. Received an object, but it is not an array or buffer.`);
|
|
52
|
+
throw new TypeError(`Invalid delimiter type.`);
|
|
96
53
|
default:
|
|
97
|
-
throw new TypeError(`Invalid delimiter type
|
|
54
|
+
throw new TypeError(`Invalid delimiter type.`);
|
|
98
55
|
}
|
|
99
56
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
*/
|
|
57
|
+
function ensureWasmCapacity(memory, required) {
|
|
58
|
+
if (required <= memory.buffer.byteLength)
|
|
59
|
+
return;
|
|
60
|
+
const pages = Math.ceil((required - memory.buffer.byteLength) / 65536);
|
|
61
|
+
memory.grow(pages);
|
|
62
|
+
}
|
|
63
|
+
/** Round `offset` up to the next multiple of 4 — `Int32Array` views require a 4-byte-aligned base. */
|
|
64
|
+
function alignTo4(offset) {
|
|
65
|
+
return Math.ceil(offset / 4) * 4;
|
|
66
|
+
}
|
|
107
67
|
export class CharacterSequence extends Uint8Array {
|
|
108
|
-
/**
|
|
109
|
-
* A jump table for the Boyer-Moore-Horspool search algorithm.
|
|
110
|
-
*/
|
|
111
68
|
#skipIndex;
|
|
69
|
+
static #wasmScanner;
|
|
70
|
+
static #wasmHaystack = null;
|
|
71
|
+
static #wasmPatternOffset = 0;
|
|
72
|
+
static #wasmReadyPromise;
|
|
73
|
+
static #loadWasm() {
|
|
74
|
+
if (CharacterSequence.#wasmReadyPromise === undefined) {
|
|
75
|
+
// Reflect "load in progress" synchronously so search()'s fast path stops
|
|
76
|
+
// re-triggering loads on every call while the module compiles.
|
|
77
|
+
if (CharacterSequence.#wasmScanner === undefined)
|
|
78
|
+
CharacterSequence.#wasmScanner = null;
|
|
79
|
+
CharacterSequence.#wasmReadyPromise = loadWasmModule().then((mod) => {
|
|
80
|
+
CharacterSequence.#wasmScanner = mod;
|
|
81
|
+
return mod;
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return CharacterSequence.#wasmReadyPromise;
|
|
85
|
+
}
|
|
86
|
+
static #ensureWasm() {
|
|
87
|
+
void CharacterSequence.#loadWasm();
|
|
88
|
+
}
|
|
112
89
|
/**
|
|
113
|
-
*
|
|
90
|
+
* Resolve once the WASM SIMD scanner has finished loading, yielding whether it is active.
|
|
114
91
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @returns The byte index of the pattern in the text, or -1 if not found.
|
|
92
|
+
* The module loads asynchronously, so synchronous callers (`Spliterator.fromSync`, `CSVSpliterator.from`) that run to
|
|
93
|
+
* completion in a single tick would otherwise always fall back to the JS scanner. Await this first to opt into SIMD
|
|
94
|
+
* acceleration.
|
|
120
95
|
*/
|
|
96
|
+
static whenReady() {
|
|
97
|
+
return CharacterSequence.#loadWasm().then((mod) => mod !== null);
|
|
98
|
+
}
|
|
121
99
|
search(haystack, start = 0, end = haystack.length) {
|
|
122
100
|
const sequenceLength = this.length;
|
|
101
|
+
// Single-byte delimiters (newline, comma, tab — the common case) are far faster via
|
|
102
|
+
// the native indexOf than the per-match Boyer-Moore-Horspool loop below. indexOf has
|
|
103
|
+
// no end bound, so clamp the result to keep `end` exclusive.
|
|
104
|
+
if (sequenceLength === 1) {
|
|
105
|
+
const index = haystack.indexOf(this[0], start);
|
|
106
|
+
return index !== -1 && index < end ? index : -1;
|
|
107
|
+
}
|
|
108
|
+
if (sequenceLength > 1 && end - start >= WASM_THRESHOLD) {
|
|
109
|
+
const wasm = CharacterSequence.#wasmScanner;
|
|
110
|
+
if (wasm) {
|
|
111
|
+
// Cache the *entire* haystack at offset 0 (so WASM byte `i` === `haystack[i]`),
|
|
112
|
+
// keyed by identity. This keeps repeated searches over the same source (the
|
|
113
|
+
// Spliterator fill loop) copy-free while staying correct for any start/end — the
|
|
114
|
+
// previous "copy [start,end)" scheme mis-mapped offsets when start !== 0 and
|
|
115
|
+
// silently ignored a shrinking `end`.
|
|
116
|
+
if (haystack !== CharacterSequence.#wasmHaystack) {
|
|
117
|
+
const fullLen = haystack.length;
|
|
118
|
+
const totalNeeded = fullLen + sequenceLength;
|
|
119
|
+
ensureWasmCapacity(wasm.memory, totalNeeded);
|
|
120
|
+
const buffer = new Uint8Array(wasm.memory.buffer, 0, totalNeeded);
|
|
121
|
+
buffer.set(haystack, 0);
|
|
122
|
+
buffer.set(this, fullLen);
|
|
123
|
+
CharacterSequence.#wasmHaystack = haystack;
|
|
124
|
+
CharacterSequence.#wasmPatternOffset = fullLen;
|
|
125
|
+
}
|
|
126
|
+
const result = wasm.findDelimiter(start, end - start, CharacterSequence.#wasmPatternOffset, sequenceLength);
|
|
127
|
+
return result >= 0 ? start + result : -1;
|
|
128
|
+
}
|
|
129
|
+
if (CharacterSequence.#wasmScanner === undefined)
|
|
130
|
+
CharacterSequence.#ensureWasm();
|
|
131
|
+
}
|
|
132
|
+
CharacterSequence.#wasmHaystack = null;
|
|
123
133
|
let startIndex = start;
|
|
124
134
|
while (startIndex <= end - sequenceLength) {
|
|
125
135
|
let lastIndex = sequenceLength - 1;
|
|
126
|
-
|
|
127
|
-
while (lastIndex >= 0 && this[lastIndex] === haystack[startIndex + lastIndex]) {
|
|
136
|
+
while (lastIndex >= 0 && this[lastIndex] === haystack[startIndex + lastIndex])
|
|
128
137
|
lastIndex--;
|
|
129
|
-
}
|
|
130
|
-
// Pattern found
|
|
131
138
|
if (lastIndex < 0)
|
|
132
139
|
return startIndex;
|
|
133
|
-
// Jump based on the last character in the window
|
|
134
140
|
startIndex += this.#skipIndex[haystack[startIndex + sequenceLength - 1]];
|
|
135
141
|
}
|
|
136
142
|
return -1;
|
|
137
143
|
}
|
|
138
|
-
|
|
139
|
-
|
|
144
|
+
searchAll(haystack, start = 0, end = haystack.length) {
|
|
145
|
+
const sequenceLength = this.length;
|
|
146
|
+
const haystackLen = end - start;
|
|
147
|
+
if (sequenceLength >= 1 && haystackLen >= WASM_THRESHOLD) {
|
|
148
|
+
const wasm = CharacterSequence.#wasmScanner;
|
|
149
|
+
if (wasm) {
|
|
150
|
+
const resultsOffset = alignTo4(haystackLen + sequenceLength);
|
|
151
|
+
const resultsSize = WASM_MAX_RESULTS * 2 * 4;
|
|
152
|
+
const totalNeeded = resultsOffset + resultsSize;
|
|
153
|
+
ensureWasmCapacity(wasm.memory, totalNeeded);
|
|
154
|
+
const buffer = new Uint8Array(wasm.memory.buffer, 0, totalNeeded);
|
|
155
|
+
buffer.set(haystack.subarray(start, end), 0);
|
|
156
|
+
buffer.set(this, haystackLen);
|
|
157
|
+
// We just overwrote offset 0; any haystack search() cached there is now stale.
|
|
158
|
+
CharacterSequence.#wasmHaystack = null;
|
|
159
|
+
const count = wasm.findAllDelimiters(0, haystackLen, haystackLen, sequenceLength, resultsOffset, WASM_MAX_RESULTS);
|
|
160
|
+
const rv = new Int32Array(wasm.memory.buffer, resultsOffset, count * 2);
|
|
161
|
+
const ranges = [];
|
|
162
|
+
for (let i = 0; i < count; i++)
|
|
163
|
+
ranges.push([start + rv[i * 2], start + rv[i * 2 + 1]]);
|
|
164
|
+
// A full results buffer means the scan may have hit the cap and dropped
|
|
165
|
+
// trailing delimiters; fall back to the uncapped JS scan rather than truncate.
|
|
166
|
+
if (count < WASM_MAX_RESULTS)
|
|
167
|
+
return ranges;
|
|
168
|
+
}
|
|
169
|
+
if (CharacterSequence.#wasmScanner === undefined)
|
|
170
|
+
CharacterSequence.#ensureWasm();
|
|
171
|
+
}
|
|
172
|
+
const ranges = [];
|
|
173
|
+
let searchStart = start, rangeStart = start;
|
|
174
|
+
while (searchStart <= end - sequenceLength) {
|
|
175
|
+
let lastIndex = sequenceLength - 1;
|
|
176
|
+
while (lastIndex >= 0 && this[lastIndex] === haystack[searchStart + lastIndex])
|
|
177
|
+
lastIndex--;
|
|
178
|
+
if (lastIndex < 0) {
|
|
179
|
+
ranges.push([rangeStart, searchStart]);
|
|
180
|
+
searchStart += sequenceLength;
|
|
181
|
+
rangeStart = searchStart;
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
searchStart += this.#skipIndex[haystack[searchStart + sequenceLength - 1]];
|
|
185
|
+
}
|
|
186
|
+
if (rangeStart <= end)
|
|
187
|
+
ranges.push([rangeStart, end]);
|
|
188
|
+
return ranges;
|
|
140
189
|
}
|
|
141
190
|
/**
|
|
142
|
-
*
|
|
191
|
+
* Scan for two patterns simultaneously (delimiter + quote) for CSV parsing.
|
|
192
|
+
*
|
|
193
|
+
* Returns sorted MatchResult[] with patternId 0=delimiter, 1=quote. Uses WASM SIMD double-scan when available; JS
|
|
194
|
+
* fallback otherwise.
|
|
143
195
|
*/
|
|
196
|
+
searchMatches(haystack, quotePattern, start = 0, end = haystack.length) {
|
|
197
|
+
const delimiterLen = this.length;
|
|
198
|
+
const quoteLen = quotePattern.length;
|
|
199
|
+
const haystackLen = end - start;
|
|
200
|
+
if (delimiterLen === 0 || haystackLen === 0)
|
|
201
|
+
return [];
|
|
202
|
+
const wasm = CharacterSequence.#wasmScanner;
|
|
203
|
+
if (wasm && haystackLen >= WASM_THRESHOLD) {
|
|
204
|
+
const patternsSize = delimiterLen + quoteLen;
|
|
205
|
+
const resultsOffset = alignTo4(haystackLen + patternsSize);
|
|
206
|
+
const resultsSize = WASM_MAX_RESULTS * 2 * 4;
|
|
207
|
+
const totalNeeded = resultsOffset + resultsSize;
|
|
208
|
+
ensureWasmCapacity(wasm.memory, totalNeeded);
|
|
209
|
+
const buffer = new Uint8Array(wasm.memory.buffer, 0, totalNeeded);
|
|
210
|
+
buffer.set(haystack.subarray(start, end), 0);
|
|
211
|
+
buffer.set(this, haystackLen);
|
|
212
|
+
buffer.set(quotePattern, haystackLen + delimiterLen);
|
|
213
|
+
// We just overwrote offset 0; any haystack search() cached there is now stale.
|
|
214
|
+
CharacterSequence.#wasmHaystack = null;
|
|
215
|
+
const count = wasm.findAllMatches(0, haystackLen, haystackLen, delimiterLen, quoteLen, resultsOffset, WASM_MAX_RESULTS);
|
|
216
|
+
const rv = new Int32Array(wasm.memory.buffer, resultsOffset, count * 2);
|
|
217
|
+
const matches = [];
|
|
218
|
+
for (let i = 0; i < count; i++)
|
|
219
|
+
matches.push({ offset: start + rv[i * 2], patternId: rv[i * 2 + 1] });
|
|
220
|
+
// A full results buffer means the scan may have hit the cap and dropped
|
|
221
|
+
// trailing matches; fall back to the uncapped JS scan rather than truncate.
|
|
222
|
+
if (count < WASM_MAX_RESULTS)
|
|
223
|
+
return matches;
|
|
224
|
+
}
|
|
225
|
+
if (CharacterSequence.#wasmScanner === undefined)
|
|
226
|
+
CharacterSequence.#ensureWasm();
|
|
227
|
+
// JS fallback: scan both patterns independently and merge in offset order. Both
|
|
228
|
+
// searches honour `end` exclusivity and handle multi-byte patterns (the quote is
|
|
229
|
+
// wrapped in a CharacterSequence so it isn't limited to a single byte).
|
|
230
|
+
const matches = [];
|
|
231
|
+
const quoteSeq = new CharacterSequence(quotePattern);
|
|
232
|
+
let searchStart = start;
|
|
233
|
+
while (searchStart <= end) {
|
|
234
|
+
const delimIdx = this.search(haystack, searchStart, end);
|
|
235
|
+
const quoteIdx = quoteSeq.search(haystack, searchStart, end);
|
|
236
|
+
if (delimIdx < 0 && quoteIdx < 0)
|
|
237
|
+
break;
|
|
238
|
+
if (delimIdx >= 0 && (quoteIdx < 0 || delimIdx <= quoteIdx)) {
|
|
239
|
+
matches.push({ offset: delimIdx, patternId: 0 });
|
|
240
|
+
searchStart = delimIdx + delimiterLen;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
matches.push({ offset: quoteIdx, patternId: 1 });
|
|
244
|
+
searchStart = quoteIdx + quoteLen;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return matches;
|
|
248
|
+
}
|
|
249
|
+
decode(encoding = "utf-8") {
|
|
250
|
+
return new TextDecoder(encoding).decode(this);
|
|
251
|
+
}
|
|
144
252
|
constructor(input = Delimiters.LineFeed) {
|
|
145
253
|
const bytes = normalizeCharacterInput(input);
|
|
146
254
|
super(bytes);
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
// Build the jump table - simpler than full Boyer-Moore
|
|
150
|
-
for (let i = 0; i < this.length - 1; i++) {
|
|
255
|
+
this.#skipIndex = Array.from({ length: 256 }, () => this.length);
|
|
256
|
+
for (let i = 0; i < this.length - 1; i++)
|
|
151
257
|
this.#skipIndex[this[i]] = this.length - 1 - i;
|
|
152
|
-
}
|
|
153
258
|
}
|
|
154
259
|
}
|
|
155
260
|
//# sourceMappingURL=CharacterSequence.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CharacterSequence.js","sourceRoot":"","sources":["../../lib/CharacterSequence.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"CharacterSequence.js","sourceRoot":"","sources":["../../lib/CharacterSequence.ts"],"names":[],"mappings":"AAAA;;;;GA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AAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAEhE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,EAAE;YAAE,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAE,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAA;IAC1F,CAAC"}
|
package/out/lib/IndexQueue.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IndexQueue.d.ts","sourceRoot":"","sources":["../../lib/IndexQueue.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"IndexQueue.d.ts","sourceRoot":"","sources":["../../lib/IndexQueue.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAS5C;;GAEG;AACH,qBAAa,UAAW,YAAW,gBAAgB,CAAC,SAAS,CAAC;;IAW7D;;OAEG;IACH,IAAW,IAAI,IAAI,MAAM,CAExB;IAID;;;;OAIG;IACH,IAAW,UAAU,IAAI,MAAM,CAE9B;IAED;;OAEG;IACI,OAAO,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI;IAOtC;;OAEG;IACI,OAAO,IAAI,SAAS,GAAG,SAAS;IAuBvC;;OAEG;IACI,IAAI,IAAI,SAAS,GAAG,SAAS;IAMpC;;OAEG;IACI,QAAQ,IAAI,SAAS,GAAG,SAAS;IAQxC;;OAEG;IACI,KAAK,IAAI,IAAI;IAMpB;;OAEG;IACI,IAAI,IAAI,cAAc,CAAC,SAAS,CAAC;IAUjC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,UAAU;CAGtC"}
|
package/out/lib/IndexQueue.js
CHANGED
|
@@ -1,18 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @copyright Sister Software
|
|
3
|
-
* @license
|
|
3
|
+
* @license MIT
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*/
|
|
6
|
+
/**
|
|
7
|
+
* Number of consumed slots (`start`/`end` values, i.e. 2 per dequeued tuple) the head pointer may advance before the
|
|
8
|
+
* backing array is compacted. Bounds wasted space under partial draining while keeping compaction rare enough that
|
|
9
|
+
* `dequeue` stays amortized O(1).
|
|
10
|
+
*/
|
|
11
|
+
const COMPACT_THRESHOLD = 1024;
|
|
6
12
|
/**
|
|
7
13
|
* A first-in, first-out queue for marking and dequeuing index tuples.
|
|
8
14
|
*/
|
|
9
15
|
export class IndexQueue {
|
|
16
|
+
/**
|
|
17
|
+
* Flat backing store of `[start, end]` pairs. Consumed entries before `#head` are kept until the queue drains or
|
|
18
|
+
* `#head` crosses {@link COMPACT_THRESHOLD}, so `dequeue` stays O(1) instead of shifting the whole array on every
|
|
19
|
+
* yield.
|
|
20
|
+
*/
|
|
10
21
|
#tuples = [];
|
|
22
|
+
/** Index of the next tuple's `start` in {@link #tuples}; advances by 2 per dequeue. */
|
|
23
|
+
#head = 0;
|
|
11
24
|
/**
|
|
12
25
|
* The number of tuples in the queue.
|
|
13
26
|
*/
|
|
14
27
|
get size() {
|
|
15
|
-
return this.#tuples.length / 2;
|
|
28
|
+
return (this.#tuples.length - this.#head) / 2;
|
|
16
29
|
}
|
|
17
30
|
#byteLength = 0;
|
|
18
31
|
/**
|
|
@@ -27,40 +40,56 @@ export class IndexQueue {
|
|
|
27
40
|
* Append a new tuple to the queue.
|
|
28
41
|
*/
|
|
29
42
|
enqueue(tuple) {
|
|
30
|
-
this.#tuples.push(
|
|
43
|
+
this.#tuples.push(tuple[0]);
|
|
44
|
+
this.#tuples.push(tuple[1]);
|
|
31
45
|
this.#byteLength += tuple[1] - tuple[0];
|
|
32
46
|
}
|
|
33
47
|
/**
|
|
34
48
|
* Get the next tuple in the queue, removing it.
|
|
35
49
|
*/
|
|
36
50
|
dequeue() {
|
|
37
|
-
if (this.#tuples.length
|
|
51
|
+
if (this.#head >= this.#tuples.length)
|
|
38
52
|
return;
|
|
39
|
-
const
|
|
40
|
-
this.#
|
|
41
|
-
|
|
53
|
+
const start = this.#tuples[this.#head];
|
|
54
|
+
const end = this.#tuples[this.#head + 1];
|
|
55
|
+
this.#head += 2;
|
|
56
|
+
this.#byteLength -= end - start;
|
|
57
|
+
// Reclaim consumed space: reset outright once fully drained (the common per-fill pattern
|
|
58
|
+
// in both engines), otherwise compact periodically so the backing array can't grow
|
|
59
|
+
// unbounded when the queue is only partially drained between enqueues.
|
|
60
|
+
if (this.#head >= this.#tuples.length) {
|
|
61
|
+
this.#tuples.length = 0;
|
|
62
|
+
this.#head = 0;
|
|
63
|
+
}
|
|
64
|
+
else if (this.#head >= COMPACT_THRESHOLD) {
|
|
65
|
+
this.#tuples.splice(0, this.#head);
|
|
66
|
+
this.#head = 0;
|
|
67
|
+
}
|
|
68
|
+
return [start, end];
|
|
42
69
|
}
|
|
43
70
|
/**
|
|
44
71
|
* Peek at the next tuple in the queue without removing it.
|
|
45
72
|
*/
|
|
46
73
|
peek() {
|
|
47
|
-
if (this.#tuples.length
|
|
74
|
+
if (this.#head >= this.#tuples.length)
|
|
48
75
|
return;
|
|
49
|
-
return this.#tuples
|
|
76
|
+
return [this.#tuples[this.#head], this.#tuples[this.#head + 1]];
|
|
50
77
|
}
|
|
51
78
|
/**
|
|
52
79
|
* Peek at the last tuple in the queue without removing it.
|
|
53
80
|
*/
|
|
54
81
|
peekLast() {
|
|
55
|
-
if (this.#tuples.length < 2)
|
|
82
|
+
if (this.#tuples.length - this.#head < 2)
|
|
56
83
|
return;
|
|
57
|
-
|
|
84
|
+
const end = this.#tuples.length;
|
|
85
|
+
return [this.#tuples[end - 2], this.#tuples[end - 1]];
|
|
58
86
|
}
|
|
59
87
|
/**
|
|
60
88
|
* Clear the queue.
|
|
61
89
|
*/
|
|
62
90
|
clear() {
|
|
63
91
|
this.#tuples.length = 0;
|
|
92
|
+
this.#head = 0;
|
|
64
93
|
this.#byteLength = 0;
|
|
65
94
|
}
|
|
66
95
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IndexQueue.js","sourceRoot":"","sources":["../../lib/IndexQueue.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;GAEG;AACH,MAAM,OAAO,UAAU;IACtB,OAAO,GAAa,EAAE,CAAA;IAEtB;;OAEG;IACH,IAAW,IAAI;QACd,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"IndexQueue.js","sourceRoot":"","sources":["../../lib/IndexQueue.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;GAIG;AACH,MAAM,iBAAiB,GAAG,IAAI,CAAA;AAE9B;;GAEG;AACH,MAAM,OAAO,UAAU;IACtB;;;;OAIG;IACH,OAAO,GAAa,EAAE,CAAA;IAEtB,uFAAuF;IACvF,KAAK,GAAG,CAAC,CAAA;IAET;;OAEG;IACH,IAAW,IAAI;QACd,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC9C,CAAC;IAED,WAAW,GAAG,CAAC,CAAA;IAEf;;;;OAIG;IACH,IAAW,UAAU;QACpB,OAAO,IAAI,CAAC,WAAW,CAAA;IACxB,CAAC;IAED;;OAEG;IACI,OAAO,CAAC,KAAgB;QAC9B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QAC3B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QAE3B,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAA;IACxC,CAAC;IAED;;OAEG;IACI,OAAO;QACb,IAAI,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