nish 0.35.0 → 0.37.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/docs/AI.md +50 -4
- package/docs/INSTALL.md +73 -59
- package/package.json +19 -5
- package/runtime/async.d.mts +2 -0
- package/runtime/async.mjs +7 -0
- package/runtime/nish.d.ts +4849 -430
- package/runtime/nish.h +25 -0
- package/runtime/page.d.ts +53 -0
- package/runtime/page.mjs +146 -0
- package/runtime/runtime-async.c +557 -0
- package/runtime/runtime-wasm.c +63 -7
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +252 -15
- package/scripts/fuzz.sh +1 -0
- package/scripts/undocumented.txt +14 -0
- package/std/README.md +107 -82
- package/std/base64.ts +34 -5
- package/std/bytes.ts +279 -59
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +128 -52
- package/std/crypto/base64url.ts +29 -5
- package/std/crypto/bcrypt-batch.ts +101 -34
- package/std/crypto/bcrypt.ts +93 -27
- package/std/crypto/chacha20poly1305.ts +69 -14
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +66 -16
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +242 -37
- package/std/crypto/jwt.ts +204 -72
- package/std/crypto/p256.ts +222 -72
- package/std/crypto/p384.ts +51 -23
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +99 -30
- package/std/crypto/rsa.ts +56 -22
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +163 -24
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +133 -66
- package/std/host/ring.ts +124 -0
- package/std/json-equal.ts +31 -11
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +76 -26
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +126 -51
- package/std/json-typed.ts +757 -96
- package/std/json-value.ts +438 -157
- package/std/json.ts +170 -38
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +1267 -290
- package/std/net/handler.ts +296 -80
- package/std/net/hpack.ts +290 -77
- package/std/net/http-compress.ts +152 -36
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +129 -44
- package/std/net/http1-codec.ts +279 -114
- package/std/net/http1-server.ts +409 -139
- package/std/net/http2-frame.ts +289 -51
- package/std/net/http2-tls.ts +152 -49
- package/std/net/http2.ts +314 -160
- package/std/net/http3-frame.ts +165 -27
- package/std/net/http3-server.ts +282 -131
- package/std/net/http3.ts +377 -173
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +898 -260
- package/std/net/prefork.ts +202 -65
- package/std/net/qpack.ts +120 -32
- package/std/net/quic-conn-ack.ts +60 -12
- package/std/net/quic-conn-cid.ts +135 -27
- package/std/net/quic-conn-params.ts +109 -22
- package/std/net/quic-datagram.ts +50 -3
- package/std/net/quic-frame.ts +333 -23
- package/std/net/quic-listener.ts +161 -75
- package/std/net/quic-packet.ts +373 -73
- package/std/net/quic-recovery.ts +287 -65
- package/std/net/quic-stream.ts +436 -146
- package/std/net/quic.ts +693 -323
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +167 -51
- package/std/net/tls/codec.ts +494 -68
- package/std/net/tls/record-client.ts +140 -73
- package/std/net/tls/record-server.ts +233 -105
- package/std/net/tls/record.ts +142 -36
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +161 -40
- package/std/net/tls/verify.ts +57 -10
- package/std/net/tls-tcp.ts +308 -95
- package/std/net/tls.ts +276 -102
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +109 -22
- package/std/net/websocket.ts +140 -26
- package/std/net/webtransport.ts +243 -106
- package/std/pair.ts +10 -2
- package/std/random.ts +42 -15
- package/std/regex.ts +422 -257
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +229 -56
- package/std/sync/shm.ts +203 -23
- package/std/term-io.ts +154 -78
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +77 -35
- package/std/term-rate.ts +199 -118
- package/std/term.ts +400 -175
- package/std/testing.ts +130 -32
- package/std/text.ts +84 -17
- package/std/threads.ts +248 -56
- package/std/yaml-json.ts +111 -97
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +45 -30
- package/std/yaml.ts +764 -384
package/std/regex.ts
CHANGED
|
@@ -17,51 +17,59 @@
|
|
|
17
17
|
* }
|
|
18
18
|
*
|
|
19
19
|
* **The syntax is JavaScript's**, with the `u` flag's strictness: literals,
|
|
20
|
-
* `.`, classes `[a-z]` and `[^…]`, `\d \w \s` and their negations, `\b \B ^
|
|
21
|
-
*
|
|
22
|
-
* {n
|
|
20
|
+
* `.`, classes `[a-z]` and `[^…]`, `\d \w \s` and their negations, `\b \B ^ $`,
|
|
21
|
+
* groups `(…)`, `(?:…)` and `(?<name>…)`, `|`, and the quantifiers `* + ? {n}
|
|
22
|
+
* {n,} {n,m}` with their lazy `?` forms. Escapes are `\n \r \t \f \v \0`,
|
|
23
23
|
* `\cX`, `\xHH`, `\uHHHH`, `\u{H…}` and a backslash before any syntax
|
|
24
|
-
* character; a `{`, `}` or `]` that does not belong to a quantifier or a
|
|
25
|
-
*
|
|
26
|
-
* `\P{…}` are Unicode 16.0's properties as JavaScript names them: a
|
|
27
|
-
* General_Category value (`L`, `Lu`, `Letter`, `gc=Nd`), `Script=` or
|
|
28
|
-
* `Script_Extensions=` a script (`sc=Grek`, `scx=Deva`), or a binary property
|
|
29
|
-
* (`Alphabetic`, `Emoji`, `ASCII`, `Any`, `Assigned`). The flags are `i`, `m`
|
|
30
|
-
* (`^` and `$` at line terminators), `s` (`.` matches them too) and `u`,
|
|
31
|
-
* which changes nothing. `i` folds A-Z to a-z, and above ASCII follows
|
|
32
|
-
* Unicode's simple case folding, so `σ` matches `Σ` and `ς` and `[α-ω]` the
|
|
33
|
-
* capitals; unlike JavaScript nothing folds across ASCII, so the Kelvin sign
|
|
34
|
-
* and `ſ` match no `k` or `s`, though under `i` both are word characters to
|
|
35
|
-
* `\w`, `\W`, `\b` and `\B`, as with JavaScript's `iu`. **Backreferences and
|
|
36
|
-
* lookaround are refused at compile time**: they are what makes a matcher
|
|
37
|
-
* backtrack, and this one never does. A repeat count is at most 1,000, a
|
|
38
|
-
* pattern at most 100,000 instructions once its repeats are written out,
|
|
39
|
-
* 1,024 capturing groups and 10,000 ranges in one class, and its property
|
|
40
|
-
* escapes and case folding add at most 100,000 ranges to its classes.
|
|
24
|
+
* character; a `{`, `}` or `]` that does not belong to a quantifier or a class
|
|
25
|
+
* is an error rather than a literal, so write `\{`.
|
|
41
26
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
27
|
+
* `\p{…}` and `\P{…}` are Unicode 16.0's properties as JavaScript names them: a
|
|
28
|
+
* General_Category value (`L`, `Lu`, `Letter`, `gc=Nd`), `Script=` or
|
|
29
|
+
* `Script_Extensions=` a script (`sc=Grek`, `scx=Deva`), or a binary property
|
|
30
|
+
* (`Alphabetic`, `Emoji`, `ASCII`, `Any`, `Assigned`). The flags are `i`, `m`
|
|
31
|
+
* (`^` and `$` at line terminators), `s` (`.` matches them too) and `u`, which
|
|
32
|
+
* changes nothing.
|
|
33
|
+
*
|
|
34
|
+
* `i` folds A-Z to a-z, and above ASCII follows Unicode's simple case folding,
|
|
35
|
+
* so `σ` matches `Σ` and `ς` and `[α-ω]` the capitals; unlike JavaScript
|
|
36
|
+
* nothing folds across ASCII, so the Kelvin sign and `ſ` match no `k` or `s`,
|
|
37
|
+
* though under `i` both are word characters to `\w`, `\W`, `\b` and `\B`, as
|
|
38
|
+
* with JavaScript's `iu`. **Backreferences and lookaround are refused at
|
|
39
|
+
* compile time**: they are what makes a matcher backtrack, and this one never
|
|
40
|
+
* does.
|
|
41
|
+
*
|
|
42
|
+
* A repeat count is at most 1,000, a pattern at most 100,000 instructions once
|
|
43
|
+
* its repeats are written out, 1,024 capturing groups and 10,000 ranges in one
|
|
44
|
+
* class, and its property escapes and case folding add at most 100,000 ranges
|
|
45
|
+
* to its classes.
|
|
46
|
+
*
|
|
47
|
+
* **Matching never backtracks.** A search runs a lazy DFA first: it builds its
|
|
48
|
+
* states from the compiled program as the text needs them, so it reads a byte
|
|
49
|
+
* per step and the leftmost-first match's end comes from a table lookup. A
|
|
50
|
+
* reverse DFA, run back from that end, finds where the match starts, and a Pike
|
|
51
|
+
* VM, the construction RE2, Go's `regexp` and Rust's `regex` use, runs from
|
|
52
|
+
* that start only when a group's capture is wanted.
|
|
53
|
+
*
|
|
54
|
+
* A pattern with a literal no match can avoid, such as `\w+@example`, is
|
|
55
|
+
* searched for by that literal and the match grown both ways from it. Every
|
|
56
|
+
* engine follows every alternative at once, so a match costs O(pattern × input)
|
|
57
|
+
* at worst and `(a+)+$` against a thousand `a`s and a `b` is as quick as `a+$`.
|
|
58
|
+
*
|
|
59
|
+
* Finding every match (`findAll`, `count`, `replaceAll`, `split`) is one search
|
|
60
|
+
* per match, so it is quadratic when a preferred branch reads far ahead before
|
|
61
|
+
* a short one wins: `<[^>]*>|<` over a text of `<`s rereads the rest of the
|
|
62
|
+
* text for each, as it does in RE2 and Rust's `regex`. Which match is found is
|
|
63
|
+
* the one JavaScript finds — the leftmost, and of those the one the first
|
|
64
|
+
* alternative and the greediest quantifier prefer — and a group inside a
|
|
65
|
+
* quantifier keeps only its last iteration's capture, as in JavaScript.
|
|
66
|
+
*
|
|
67
|
+
* An iteration past a quantifier's minimum that matches empty is rejected, as
|
|
68
|
+
* JavaScript rejects it. The VM keeps one thread per instruction, position and
|
|
69
|
+
* set of iterations begun, so `(a*?)+?$` on "aa" captures "a", as JavaScript
|
|
70
|
+
* does; past three such quantifiers nested in one another the deeper ones share
|
|
71
|
+
* their threads, and a capture there can be a different iteration's than
|
|
72
|
+
* JavaScript's.
|
|
65
73
|
*
|
|
66
74
|
* **Strings are UTF-8 and offsets are bytes**, like `s.length`. The VM steps
|
|
67
75
|
* a code point at a time, so `.` and `[^a]` consume a whole character; a byte
|
|
@@ -70,24 +78,27 @@
|
|
|
70
78
|
* JavaScript; `\s` is JavaScript's whitespace, U+FEFF and U+3000 included.
|
|
71
79
|
* After an empty match `findAll`, `replaceAll` and `split` step one character.
|
|
72
80
|
*
|
|
73
|
-
* **What a call costs.** The VM's two thread lists, its capture tables and
|
|
74
|
-
*
|
|
81
|
+
* **What a call costs.** The VM's two thread lists, its capture tables and its
|
|
82
|
+
* stack are allocated once by `regexCompile`. `test` tracks no captures,
|
|
75
83
|
* `findAll` and a `replace` whose template names no group track only the
|
|
76
84
|
* match's bounds, and only `exec`, `find`, `split` and a template with `$1` or
|
|
77
|
-
* `$<name>` pay for every group.
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
85
|
+
* `$<name>` pay for every group.
|
|
86
|
+
*
|
|
87
|
+
* A pattern that starts with a literal is searched for with `s.indexOf`; one
|
|
88
|
+
* whose first few bytes are each one of a set rare enough to be worth it, like
|
|
89
|
+
* `[A-Z][a-z]` or `cat|dog`, is searched for those bytes by a runtime kernel
|
|
90
|
+
* that tests sixteen or thirty-two starts at a time; any other whose first
|
|
91
|
+
* characters are known skips the bytes that cannot start a match with a
|
|
92
|
+
* 128-entry table; and a pattern that is nothing but a literal never starts an
|
|
93
|
+
* engine at all.
|
|
94
|
+
*
|
|
95
|
+
* The DFA's states are built as searches need them and kept, so only a search
|
|
96
|
+
* that meets new states allocates, and only up to a bound: its table holds at
|
|
97
|
+
* most 2,048 states and 262,144 transitions (1 MiB), and a search that fills it
|
|
98
|
+
* clears it and goes on; one that keeps clearing it without reading at least 10
|
|
99
|
+
* bytes per state hands the rest of the text to the VM, which is slower but
|
|
100
|
+
* never needs more memory. Because the scratch memory lives in the `Regex`, one
|
|
101
|
+
* `Regex` is used by one thread at a time.
|
|
91
102
|
*/
|
|
92
103
|
|
|
93
104
|
import { uncheckedGet } from "nish:unsafe"
|
|
@@ -151,19 +162,25 @@ const REGEX_FOLD_INDEX: string =
|
|
|
151
162
|
"005E0002006?001C00700034007@004F00810065008A007500910085009C009500:400:500:E00;500;500<500;E00=500<600>800<G00?B00=700@L00=I00B100>@00C200?000D300?A00E300@100F500@A00G500A200H600AB00I600B800JF00BK00L400CE00N600KA010100L@011<00M0012M00MA014>00N1016000NA017@00O4018C00OD019F010601;1010F01<A011601>1011F01?C012601A6012F01BG013601D3013F01E3014>01F3014N01G3015>01H3015N01I3016>01J4016N01K5017>01L5017N01M5018>01N5018N01O5019>0205019O021C01:?023301;9024C01;I02630452027G045B029G046202;G046J02=G047:02?G047J02AG04M<02D404ML02FD04N<02I404NL02KD04O<02N4074002OH074E031C0755033C075E035C07@4037?07@D038?07A4039?07AD03:?07B403;?07BD03<?07C403=@07CD03>@07D403?@07DD03@@07E<03AC07EL03BC07F<03CC07FL03DC07G<03EC07GL03FC07H<03GK07I003I107I@03J907J003KA07JI03LK07K<03N307KL03O?07L>040I07LN042107M>043907N<044F07O<046308;5047F08;E0496095I04:E096904<5096I04=E097904?50;0?04@G0;0O04B70;1?04CG0;1O04E70;2?04FG0;2O04H70;3?04IL0;4804K80;4H04L80;5804M80;5H04N80;6804O80;6H05080;7B05180;8>05340;8N055419B6056J19BF057M19C6058M19D8059M19DH05:M19I>05;N19J005<N19J@05=N19K005>N19KI05?N19L<05A119LO05B519M@05CD19N005DO19NF05F91:KF05H11:L605JA1:LF05M11:M605OA1:MF06211OI506471OIE065G1OJ;06772100068I210@06:9211006;I211@06=9212006>I215@06@:216006AJ216@06C:217406DJ217D06F:21;H06GK21<906I;21<L06JK21==06L;234206MM234B06O=2352070M235B072=236?073M236O075=237?076M23:L078>23;F079N265007;@265@07=0266007>@266@07@02KB007AB2KB@07C22KC007DB2KC@07F23J8007GD3J8@07I43J9007JD3J9@07L43J:007MD"
|
|
152
163
|
// gen-regex-unicode:end
|
|
153
164
|
|
|
154
|
-
/** The most instructions a compiled pattern may have. */
|
|
165
|
+
/** The most instructions a compiled pattern may have; `regexCompile` refuses a bigger program. */
|
|
155
166
|
export const REGEX_MAX_INSTRUCTIONS: i32 = 100000
|
|
156
|
-
/** The largest count `{n}`, `{n,}` or `{n,m}` may give. */
|
|
167
|
+
/** The largest count `{n}`, `{n,}` or `{n,m}` may give; a larger one is a compile error. */
|
|
157
168
|
export const REGEX_MAX_REPEAT: i32 = 1000
|
|
158
|
-
/** How deeply groups may nest. */
|
|
169
|
+
/** How deeply groups may nest; a deeper pattern is a compile error. */
|
|
159
170
|
export const REGEX_MAX_DEPTH: i32 = 250
|
|
160
171
|
/** The most entries one thread list's capture table may have: instructions × slots per thread. */
|
|
161
172
|
const REGEX_MAX_SLOT_TABLE: i32 = 4194304
|
|
162
|
-
/**
|
|
173
|
+
/**
|
|
174
|
+
* The most capturing groups a pattern may have, past group 0; one more is a
|
|
175
|
+
* compile error. A name is checked against each.
|
|
176
|
+
*/
|
|
163
177
|
export const REGEX_MAX_GROUPS: i32 = 1024
|
|
164
|
-
/** The most ranges one class may list;
|
|
178
|
+
/** The most ranges one class may list; one more is a compile error. */
|
|
165
179
|
export const REGEX_MAX_CLASS_RANGES: i32 = 10000
|
|
166
|
-
/**
|
|
180
|
+
/**
|
|
181
|
+
* The most ranges the property escapes and case folding of one pattern may
|
|
182
|
+
* add to its classes; one more is a compile error.
|
|
183
|
+
*/
|
|
167
184
|
export const REGEX_MAX_PROPERTY_RANGES: i32 = 100000
|
|
168
185
|
/** Most ranges a class sorts by insertion; a longer one is merge-sorted. */
|
|
169
186
|
const REGEX_INSERTION_SORT: i32 = 64
|
|
@@ -462,47 +479,69 @@ const regexPushUtf8 = (out: u8[], cp: i32): void => {
|
|
|
462
479
|
*/
|
|
463
480
|
class RegexParser {
|
|
464
481
|
// Widest first, so the alignment rules insert no padding.
|
|
465
|
-
src: string
|
|
482
|
+
private src: string
|
|
483
|
+
/** @internal `regexCompile` puts it in the compile error. */
|
|
466
484
|
error: string
|
|
467
485
|
|
|
486
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
468
487
|
kind: i32[]
|
|
488
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
469
489
|
na: i32[]
|
|
490
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
470
491
|
nb: i32[]
|
|
492
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
471
493
|
nc: i32[]
|
|
494
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
472
495
|
nd: i32[]
|
|
496
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
473
497
|
ne: i32[]
|
|
498
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
474
499
|
kid: i32[]
|
|
500
|
+
/** @internal `Regex` compiles the parser's tree from it. */
|
|
475
501
|
next: i32[]
|
|
476
502
|
|
|
503
|
+
/** @internal `Regex.build` copies the group names from it. */
|
|
477
504
|
names: string[]
|
|
478
505
|
|
|
506
|
+
/** @internal `Regex.build` copies the parsed classes from it. */
|
|
479
507
|
clsAscii: u8[]
|
|
508
|
+
/** @internal `Regex.build` copies the parsed classes from it. */
|
|
480
509
|
clsLo: i32[]
|
|
510
|
+
/** @internal `Regex.build` copies the parsed classes from it. */
|
|
481
511
|
clsHi: i32[]
|
|
512
|
+
/** @internal `Regex.build` copies the parsed classes from it. */
|
|
482
513
|
clsFrom: i32[]
|
|
514
|
+
/** @internal `Regex.build` copies the parsed classes from it. */
|
|
483
515
|
clsTo: i32[]
|
|
516
|
+
/** @internal `Regex.build` copies the parsed classes from it. */
|
|
484
517
|
clsNeg: u8[]
|
|
485
518
|
/** The class being parsed: its ranges, unsorted, `tmpCount` of them. */
|
|
486
|
-
tmpLo: i32[]
|
|
487
|
-
tmpHi: i32[]
|
|
519
|
+
private tmpLo: i32[]
|
|
520
|
+
private tmpHi: i32[]
|
|
521
|
+
/** @internal `regexCompile` compares `pos` with it to find a stray `)`. */
|
|
488
522
|
len: i32
|
|
523
|
+
/** @internal `regexCompile` reads it to find a stray `)`. */
|
|
489
524
|
pos: i32
|
|
525
|
+
/** @internal `regexCompile` reads where the parse failed. */
|
|
490
526
|
errorAt: i32
|
|
491
|
-
/** Capture groups, the whole match (group 0) included. */
|
|
527
|
+
/** Capture groups, the whole match (group 0) included. @internal `Regex.build` copies it. */
|
|
492
528
|
groups: i32
|
|
493
|
-
tmpCount: i32
|
|
529
|
+
private tmpCount: i32
|
|
494
530
|
/** What `parseBraces` read. */
|
|
495
|
-
qMin: i32
|
|
496
|
-
qMax: i32
|
|
531
|
+
private qMin: i32
|
|
532
|
+
private qMax: i32
|
|
497
533
|
/** Where `tableNumber` reads next. */
|
|
498
|
-
tablePos: i32
|
|
534
|
+
private tablePos: i32
|
|
499
535
|
/** The ranges property escapes and case folding have added, against `REGEX_MAX_PROPERTY_RANGES`. */
|
|
500
|
-
propertyRanges: i32
|
|
536
|
+
private propertyRanges: i32
|
|
501
537
|
|
|
502
|
-
icase: boolean
|
|
503
|
-
multiline: boolean
|
|
538
|
+
private icase: boolean
|
|
539
|
+
private multiline: boolean
|
|
540
|
+
/** @internal `Regex.compileNode` reads it to compile `.`. */
|
|
504
541
|
dotAll: boolean
|
|
542
|
+
/** @internal `regexCompile` reads it after the parse. */
|
|
505
543
|
failed: boolean
|
|
544
|
+
/** @internal `Regex.build` copies it. */
|
|
506
545
|
hasNames: boolean
|
|
507
546
|
|
|
508
547
|
constructor(src: string, icase: boolean, multiline: boolean, dotAll: boolean) {
|
|
@@ -541,6 +580,7 @@ class RegexParser {
|
|
|
541
580
|
this.propertyRanges = 0
|
|
542
581
|
}
|
|
543
582
|
|
|
583
|
+
/** @internal `regexCompile` calls it for a stray `)`. */
|
|
544
584
|
fail(message: string, at: i32): i32 {
|
|
545
585
|
if (!this.failed) {
|
|
546
586
|
this.failed = true
|
|
@@ -550,11 +590,11 @@ class RegexParser {
|
|
|
550
590
|
return -1
|
|
551
591
|
}
|
|
552
592
|
|
|
553
|
-
byteAt(i: i32): i32 {
|
|
593
|
+
private byteAt(i: i32): i32 {
|
|
554
594
|
return toI32(this.src.charCodeAt(i))
|
|
555
595
|
}
|
|
556
596
|
|
|
557
|
-
node(kind: i32, a: i32, b: i32): i32 {
|
|
597
|
+
private node(kind: i32, a: i32, b: i32): i32 {
|
|
558
598
|
const n: i32 = toI32(this.kind.length)
|
|
559
599
|
this.kind.push(kind)
|
|
560
600
|
this.na.push(a)
|
|
@@ -571,7 +611,7 @@ class RegexParser {
|
|
|
571
611
|
* A node for code point `cp`, with its other case under `i`: one other
|
|
572
612
|
* case is a character's second operand, more make a class of them all.
|
|
573
613
|
*/
|
|
574
|
-
charNode(cp: i32): i32 {
|
|
614
|
+
private charNode(cp: i32): i32 {
|
|
575
615
|
if (!this.icase) {
|
|
576
616
|
return this.node(NODE_CHAR, cp, cp)
|
|
577
617
|
}
|
|
@@ -600,7 +640,7 @@ class RegexParser {
|
|
|
600
640
|
}
|
|
601
641
|
|
|
602
642
|
/** The number at `tablePos` in `table`, a generated table's base-32 digits, `tablePos` moved past it. */
|
|
603
|
-
tableNumber(table: string): i32 {
|
|
643
|
+
private tableNumber(table: string): i32 {
|
|
604
644
|
let v: i32 = 0
|
|
605
645
|
let d: i32 = toI32(table.charCodeAt(this.tablePos)) - 48
|
|
606
646
|
this.tablePos += 1
|
|
@@ -617,7 +657,7 @@ class RegexParser {
|
|
|
617
657
|
* block starting at or before it, or the first. `tablePos` is left at that
|
|
618
658
|
* member's step.
|
|
619
659
|
*/
|
|
620
|
-
foldSeek(cp: i32): i32 {
|
|
660
|
+
private foldSeek(cp: i32): i32 {
|
|
621
661
|
let a: i32 = 0
|
|
622
662
|
let b: i32 = toI32(REGEX_FOLD_INDEX.length) >> 3
|
|
623
663
|
while (b - a > 1) {
|
|
@@ -633,7 +673,7 @@ class RegexParser {
|
|
|
633
673
|
}
|
|
634
674
|
|
|
635
675
|
/** `cp`'s step in `REGEX_FOLD_DATA`, for `regexFoldTarget`, or -1 if it folds to nothing. */
|
|
636
|
-
foldStepOf(cp: i32): i32 {
|
|
676
|
+
private foldStepOf(cp: i32): i32 {
|
|
637
677
|
let c: i32 = this.foldSeek(cp)
|
|
638
678
|
const end: i32 = toI32(REGEX_FOLD_DATA.length)
|
|
639
679
|
while (c < cp && this.tablePos < end) {
|
|
@@ -645,7 +685,7 @@ class RegexParser {
|
|
|
645
685
|
return c === cp && this.tablePos < end ? this.tableNumber(REGEX_FOLD_DATA) : -1
|
|
646
686
|
}
|
|
647
687
|
|
|
648
|
-
/** `alternative ('|' alternative)*`. */
|
|
688
|
+
/** `alternative ('|' alternative)*`. @internal `regexCompile` starts the parse with it. */
|
|
649
689
|
parseAlt(depth: i32): i32 {
|
|
650
690
|
if (depth > REGEX_MAX_DEPTH) {
|
|
651
691
|
return this.fail("groups nest too deeply", this.pos)
|
|
@@ -673,7 +713,7 @@ class RegexParser {
|
|
|
673
713
|
}
|
|
674
714
|
|
|
675
715
|
/** The terms up to a `|`, a `)` or the end. */
|
|
676
|
-
parseSeq(depth: i32): i32 {
|
|
716
|
+
private parseSeq(depth: i32): i32 {
|
|
677
717
|
let first: i32 = -1
|
|
678
718
|
let last: i32 = -1
|
|
679
719
|
let count: i32 = 0
|
|
@@ -706,7 +746,7 @@ class RegexParser {
|
|
|
706
746
|
}
|
|
707
747
|
|
|
708
748
|
/** An atom and at most one quantifier. */
|
|
709
|
-
parseTerm(depth: i32): i32 {
|
|
749
|
+
private parseTerm(depth: i32): i32 {
|
|
710
750
|
const groupsBefore: i32 = this.groups
|
|
711
751
|
const atomAt: i32 = this.pos
|
|
712
752
|
const atom: i32 = this.parseAtom(depth)
|
|
@@ -765,7 +805,7 @@ class RegexParser {
|
|
|
765
805
|
* in `qMin` / `qMax` (-1 unbounded) with the cursor past it. Anything else
|
|
766
806
|
* leaves the cursor where it was.
|
|
767
807
|
*/
|
|
768
|
-
parseBraces(): boolean {
|
|
808
|
+
private parseBraces(): boolean {
|
|
769
809
|
let i: i32 = this.pos + 1
|
|
770
810
|
const start: i32 = this.pos
|
|
771
811
|
let min: i32 = 0
|
|
@@ -814,7 +854,7 @@ class RegexParser {
|
|
|
814
854
|
return true
|
|
815
855
|
}
|
|
816
856
|
|
|
817
|
-
parseAtom(depth: i32): i32 {
|
|
857
|
+
private parseAtom(depth: i32): i32 {
|
|
818
858
|
const c: i32 = this.byteAt(this.pos)
|
|
819
859
|
if (c === 40) {
|
|
820
860
|
return this.parseGroup(depth)
|
|
@@ -861,7 +901,7 @@ class RegexParser {
|
|
|
861
901
|
return this.charNode(packed >> 3)
|
|
862
902
|
}
|
|
863
903
|
|
|
864
|
-
parseGroup(depth: i32): i32 {
|
|
904
|
+
private parseGroup(depth: i32): i32 {
|
|
865
905
|
const open: i32 = this.pos
|
|
866
906
|
this.pos += 1
|
|
867
907
|
let capture: boolean = true
|
|
@@ -929,7 +969,7 @@ class RegexParser {
|
|
|
929
969
|
}
|
|
930
970
|
|
|
931
971
|
/** A `\` outside a class. */
|
|
932
|
-
parseEscape(): i32 {
|
|
972
|
+
private parseEscape(): i32 {
|
|
933
973
|
const at: i32 = this.pos
|
|
934
974
|
this.pos += 1
|
|
935
975
|
if (this.pos >= this.len) {
|
|
@@ -982,7 +1022,7 @@ class RegexParser {
|
|
|
982
1022
|
* `\`. Shared by escapes inside and outside a class; inside one, `\b` is a
|
|
983
1023
|
* backspace and `\-` a hyphen.
|
|
984
1024
|
*/
|
|
985
|
-
parseCharEscape(inClass: boolean): i32 {
|
|
1025
|
+
private parseCharEscape(inClass: boolean): i32 {
|
|
986
1026
|
const at: i32 = this.pos - 1
|
|
987
1027
|
const c: i32 = this.byteAt(this.pos)
|
|
988
1028
|
this.pos += 1
|
|
@@ -1057,7 +1097,7 @@ class RegexParser {
|
|
|
1057
1097
|
}
|
|
1058
1098
|
|
|
1059
1099
|
/** Exactly `n` hex digits at the cursor, or -1 with the cursor left. */
|
|
1060
|
-
parseHex(n: i32): i32 {
|
|
1100
|
+
private parseHex(n: i32): i32 {
|
|
1061
1101
|
if (this.pos + n > this.len) {
|
|
1062
1102
|
return -1
|
|
1063
1103
|
}
|
|
@@ -1076,7 +1116,7 @@ class RegexParser {
|
|
|
1076
1116
|
}
|
|
1077
1117
|
|
|
1078
1118
|
/** `\uHHHH`, a surrogate pair of them, or `\u{H…}`, the cursor after the `u`. */
|
|
1079
|
-
parseUnicodeEscape(at: i32): i32 {
|
|
1119
|
+
private parseUnicodeEscape(at: i32): i32 {
|
|
1080
1120
|
if (this.pos < this.len && this.byteAt(this.pos) === 123) {
|
|
1081
1121
|
let i: i32 = this.pos + 1
|
|
1082
1122
|
let v: i32 = 0
|
|
@@ -1115,7 +1155,7 @@ class RegexParser {
|
|
|
1115
1155
|
return v
|
|
1116
1156
|
}
|
|
1117
1157
|
|
|
1118
|
-
addRange(lo: i32, hi: i32): void {
|
|
1158
|
+
private addRange(lo: i32, hi: i32): void {
|
|
1119
1159
|
if (this.tmpCount < toI32(this.tmpLo.length)) {
|
|
1120
1160
|
this.tmpLo[this.tmpCount] = lo
|
|
1121
1161
|
this.tmpHi[this.tmpCount] = hi
|
|
@@ -1131,7 +1171,7 @@ class RegexParser {
|
|
|
1131
1171
|
* escape's letter — to the class being parsed. A capital adds the complement, the
|
|
1132
1172
|
* gaps between the set's sorted ranges.
|
|
1133
1173
|
*/
|
|
1134
|
-
addSet(letter: i32): void {
|
|
1174
|
+
private addSet(letter: i32): void {
|
|
1135
1175
|
const negate: boolean = letter === 68 || letter === 87 || letter === 83
|
|
1136
1176
|
const lower: i32 = negate ? letter + 32 : letter
|
|
1137
1177
|
let set: i32 = SET_SPACE
|
|
@@ -1215,7 +1255,7 @@ class RegexParser {
|
|
|
1215
1255
|
* alone, or `General_Category`, `Script` or `Script_Extensions` (or `gc`,
|
|
1216
1256
|
* `sc`, `scx`) `=` a value, spelled as ECMA-262 spells them.
|
|
1217
1257
|
*/
|
|
1218
|
-
addProperty(at: i32, negate: boolean): void {
|
|
1258
|
+
private addProperty(at: i32, negate: boolean): void {
|
|
1219
1259
|
this.pos += 1
|
|
1220
1260
|
if (this.pos >= this.len || this.byteAt(this.pos) !== 123) {
|
|
1221
1261
|
this.fail("invalid property name", at)
|
|
@@ -1309,7 +1349,7 @@ class RegexParser {
|
|
|
1309
1349
|
}
|
|
1310
1350
|
|
|
1311
1351
|
/** Count `n` more ranges against `REGEX_MAX_PROPERTY_RANGES`, failing at `at` past it. */
|
|
1312
|
-
countPropertyRanges(n: i32, at: i32): void {
|
|
1352
|
+
private countPropertyRanges(n: i32, at: i32): void {
|
|
1313
1353
|
this.propertyRanges += n
|
|
1314
1354
|
if (this.propertyRanges > REGEX_MAX_PROPERTY_RANGES) {
|
|
1315
1355
|
this.fail("property escapes and case folding add more than 100000 ranges", at)
|
|
@@ -1317,7 +1357,7 @@ class RegexParser {
|
|
|
1317
1357
|
}
|
|
1318
1358
|
|
|
1319
1359
|
/** One member of a class: a code point, or -2 once a set escape has added its ranges. */
|
|
1320
|
-
parseClassAtom(): i32 {
|
|
1360
|
+
private parseClassAtom(): i32 {
|
|
1321
1361
|
const c: i32 = this.byteAt(this.pos)
|
|
1322
1362
|
if (c === 92) {
|
|
1323
1363
|
const at: i32 = this.pos
|
|
@@ -1349,7 +1389,7 @@ class RegexParser {
|
|
|
1349
1389
|
}
|
|
1350
1390
|
|
|
1351
1391
|
/** `[…]` or `[^…]`. */
|
|
1352
|
-
parseClass(): i32 {
|
|
1392
|
+
private parseClass(): i32 {
|
|
1353
1393
|
const open: i32 = this.pos
|
|
1354
1394
|
this.pos += 1
|
|
1355
1395
|
let negated: boolean = false
|
|
@@ -1406,7 +1446,7 @@ class RegexParser {
|
|
|
1406
1446
|
}
|
|
1407
1447
|
|
|
1408
1448
|
/** Store the class in `tmpLo` / `tmpHi` and answer its node. */
|
|
1409
|
-
finishClass(negated: boolean): i32 {
|
|
1449
|
+
private finishClass(negated: boolean): i32 {
|
|
1410
1450
|
const k: i32 = toI32(this.clsFrom.length)
|
|
1411
1451
|
const base: i32 = toI32(this.clsAscii.length)
|
|
1412
1452
|
let j: i32 = 0
|
|
@@ -1463,7 +1503,7 @@ class RegexParser {
|
|
|
1463
1503
|
* Append the non-ASCII part of the class being parsed to `clsLo` /
|
|
1464
1504
|
* `clsHi`, sorted by start and merged, and answer where the merged run ends.
|
|
1465
1505
|
*/
|
|
1466
|
-
storeRanges(): i32 {
|
|
1506
|
+
private storeRanges(): i32 {
|
|
1467
1507
|
const from: i32 = toI32(this.clsLo.length)
|
|
1468
1508
|
let i: i32 = 0
|
|
1469
1509
|
while (i < this.tmpCount) {
|
|
@@ -1519,7 +1559,7 @@ class RegexParser {
|
|
|
1519
1559
|
* scratch copy, so that a class of many ranges, such as a few property
|
|
1520
1560
|
* escapes in one, costs n log n rather than the n² insertion would.
|
|
1521
1561
|
*/
|
|
1522
|
-
mergeSort(from: i32, end: i32): void {
|
|
1562
|
+
private mergeSort(from: i32, end: i32): void {
|
|
1523
1563
|
const n: i32 = end - from
|
|
1524
1564
|
let srcLo: i32[] = []
|
|
1525
1565
|
let srcHi: i32[] = []
|
|
@@ -1584,7 +1624,7 @@ class RegexParser {
|
|
|
1584
1624
|
* folds to; if they do, the class being parsed becomes them and the
|
|
1585
1625
|
* missing code points, to be stored again.
|
|
1586
1626
|
*/
|
|
1587
|
-
foldRanges(from: i32, out: i32): boolean {
|
|
1627
|
+
private foldRanges(from: i32, out: i32): boolean {
|
|
1588
1628
|
// A class closed already, holding no folding code point or all of them, leaves the fold table unread.
|
|
1589
1629
|
let touches: boolean = false
|
|
1590
1630
|
let covers: boolean = true
|
|
@@ -1644,7 +1684,7 @@ class RegexParser {
|
|
|
1644
1684
|
}
|
|
1645
1685
|
|
|
1646
1686
|
/** Whether the sorted, merged ranges `[from, out)` hold `c`. */
|
|
1647
|
-
rangesHave(from: i32, out: i32, c: i32): boolean {
|
|
1687
|
+
private rangesHave(from: i32, out: i32, c: i32): boolean {
|
|
1648
1688
|
let lo: i32 = from
|
|
1649
1689
|
let hi: i32 = out
|
|
1650
1690
|
while (lo < hi) {
|
|
@@ -1928,55 +1968,92 @@ const regexScanFrom = (text: string, masks: u8[], depth: i32, off: i32, width: i
|
|
|
1928
1968
|
|
|
1929
1969
|
/** One match: the input it was found in and the byte offsets of every group. */
|
|
1930
1970
|
export class RegexMatch {
|
|
1931
|
-
/** The text that was searched. */
|
|
1971
|
+
/** The text that was searched. @internal `Regex.exec` fills it in. */
|
|
1932
1972
|
input: string
|
|
1933
|
-
/** `start, end` of group 0, then of each group in order; -1 for a group that took no part. */
|
|
1973
|
+
/** `start, end` of group 0, then of each group in order; -1 for a group that took no part. @internal `Regex.exec` fills it in. */
|
|
1934
1974
|
slots: i32[]
|
|
1935
|
-
/** Each group's name, "" for an unnamed one. */
|
|
1975
|
+
/** Each group's name, "" for an unnamed one. @internal `Regex.exec` fills it in. */
|
|
1936
1976
|
names: string[]
|
|
1937
1977
|
|
|
1978
|
+
/** An empty match with no groups, for `Regex.exec` to fill in. */
|
|
1938
1979
|
constructor() {
|
|
1939
1980
|
this.input = ""
|
|
1940
1981
|
this.slots = []
|
|
1941
1982
|
this.names = []
|
|
1942
1983
|
}
|
|
1943
1984
|
|
|
1944
|
-
/**
|
|
1985
|
+
/**
|
|
1986
|
+
* Where the match starts.
|
|
1987
|
+
*
|
|
1988
|
+
* @returns The byte offset of the match's first byte in the searched text.
|
|
1989
|
+
*/
|
|
1945
1990
|
start(): i32 {
|
|
1946
1991
|
return this.slots[0]
|
|
1947
1992
|
}
|
|
1948
1993
|
|
|
1949
|
-
/**
|
|
1994
|
+
/**
|
|
1995
|
+
* Where the match ends.
|
|
1996
|
+
*
|
|
1997
|
+
* @returns The byte offset one past the match's last byte; equal to `start` for an empty match.
|
|
1998
|
+
*/
|
|
1950
1999
|
end(): i32 {
|
|
1951
2000
|
return this.slots[1]
|
|
1952
2001
|
}
|
|
1953
2002
|
|
|
1954
|
-
/**
|
|
2003
|
+
/**
|
|
2004
|
+
* The matched text.
|
|
2005
|
+
*
|
|
2006
|
+
* @returns The bytes from `start` to `end` of the searched text; "" for an empty match.
|
|
2007
|
+
*/
|
|
1955
2008
|
text(): string {
|
|
1956
2009
|
return this.group(REGEX_WHOLE)
|
|
1957
2010
|
}
|
|
1958
2011
|
|
|
1959
|
-
/**
|
|
2012
|
+
/**
|
|
2013
|
+
* How many groups the pattern has.
|
|
2014
|
+
*
|
|
2015
|
+
* @returns The count with the whole match (group 0) included, so a pattern with no groups gives 1.
|
|
2016
|
+
*/
|
|
1960
2017
|
groupCount(): i32 {
|
|
1961
2018
|
return toI32(this.slots.length) / 2
|
|
1962
2019
|
}
|
|
1963
2020
|
|
|
1964
|
-
/**
|
|
2021
|
+
/**
|
|
2022
|
+
* Whether group `k` took part in the match.
|
|
2023
|
+
*
|
|
2024
|
+
* @param k The group's number, 0 for the whole match.
|
|
2025
|
+
* @returns False for a group that took no part and for a number with no group.
|
|
2026
|
+
*/
|
|
1965
2027
|
matched(k: i32): boolean {
|
|
1966
2028
|
return k >= 0 && k < this.groupCount() && this.slots[2 * k] >= 0
|
|
1967
2029
|
}
|
|
1968
2030
|
|
|
1969
|
-
/**
|
|
2031
|
+
/**
|
|
2032
|
+
* Where group `k` starts.
|
|
2033
|
+
*
|
|
2034
|
+
* @param k The group's number, 0 for the whole match.
|
|
2035
|
+
* @returns The byte offset of the group's first byte, or -1 when the group took no part or does not exist.
|
|
2036
|
+
*/
|
|
1970
2037
|
groupStart(k: i32): i32 {
|
|
1971
2038
|
return this.matched(k) ? this.slots[2 * k] : -1
|
|
1972
2039
|
}
|
|
1973
2040
|
|
|
1974
|
-
/**
|
|
2041
|
+
/**
|
|
2042
|
+
* Where group `k` ends.
|
|
2043
|
+
*
|
|
2044
|
+
* @param k The group's number, 0 for the whole match.
|
|
2045
|
+
* @returns The byte offset one past the group's last byte, or -1 when the group took no part or does not exist.
|
|
2046
|
+
*/
|
|
1975
2047
|
groupEnd(k: i32): i32 {
|
|
1976
2048
|
return this.matched(k) ? this.slots[2 * k + 1] : -1
|
|
1977
2049
|
}
|
|
1978
2050
|
|
|
1979
|
-
/**
|
|
2051
|
+
/**
|
|
2052
|
+
* Group `k`'s text.
|
|
2053
|
+
*
|
|
2054
|
+
* @param k The group's number, 0 for the whole match.
|
|
2055
|
+
* @returns The group's text; "" when it took no part or does not exist, so test `matched` to tell that from an empty capture.
|
|
2056
|
+
*/
|
|
1980
2057
|
group(k: i32): string {
|
|
1981
2058
|
if (!this.matched(k)) {
|
|
1982
2059
|
return ""
|
|
@@ -1984,7 +2061,12 @@ export class RegexMatch {
|
|
|
1984
2061
|
return this.input.slice(this.slots[2 * k], this.slots[2 * k + 1])
|
|
1985
2062
|
}
|
|
1986
2063
|
|
|
1987
|
-
/**
|
|
2064
|
+
/**
|
|
2065
|
+
* The text of a named group.
|
|
2066
|
+
*
|
|
2067
|
+
* @param name The name the pattern gave the group with `(?<name>...)`.
|
|
2068
|
+
* @returns The group's text; "" when no group has that name or it took no part.
|
|
2069
|
+
*/
|
|
1988
2070
|
named(name: string): string {
|
|
1989
2071
|
return this.group(toI32(this.names.indexOf(name)))
|
|
1990
2072
|
}
|
|
@@ -2001,167 +2083,175 @@ export class Regex {
|
|
|
2001
2083
|
/** The flags as written. */
|
|
2002
2084
|
readonly flags: string
|
|
2003
2085
|
/** Each repeat node's mark slot, or -1 before it has one. */
|
|
2004
|
-
markOf: i32[]
|
|
2005
|
-
names: string[]
|
|
2086
|
+
private markOf: i32[]
|
|
2087
|
+
private names: string[]
|
|
2006
2088
|
|
|
2007
|
-
op: i32[]
|
|
2008
|
-
ia: i32[]
|
|
2009
|
-
ib: i32[]
|
|
2089
|
+
private op: i32[]
|
|
2090
|
+
private ia: i32[]
|
|
2091
|
+
private ib: i32[]
|
|
2010
2092
|
|
|
2011
|
-
clsAscii: u8[]
|
|
2012
|
-
clsLo: i32[]
|
|
2013
|
-
clsHi: i32[]
|
|
2014
|
-
clsFrom: i32[]
|
|
2015
|
-
clsTo: i32[]
|
|
2016
|
-
clsNeg: u8[]
|
|
2093
|
+
private clsAscii: u8[]
|
|
2094
|
+
private clsLo: i32[]
|
|
2095
|
+
private clsHi: i32[]
|
|
2096
|
+
private clsFrom: i32[]
|
|
2097
|
+
private clsTo: i32[]
|
|
2098
|
+
private clsNeg: u8[]
|
|
2017
2099
|
|
|
2018
2100
|
/** The literal every match starts with, or "". */
|
|
2019
|
-
prefix: string
|
|
2101
|
+
private prefix: string
|
|
2020
2102
|
/** The prefix's rarest byte, which `findPrefix` searches for, at `rareAt`. */
|
|
2021
|
-
rareStr: string
|
|
2103
|
+
private rareStr: string
|
|
2022
2104
|
/** A literal inside every match that the part before it cannot read, or "": see `dfaInner`. */
|
|
2023
|
-
innerLit: string
|
|
2105
|
+
private innerLit: string
|
|
2024
2106
|
/** Which ASCII bytes a match can start with, or empty when any byte can start one. */
|
|
2025
|
-
firstSet: u8[]
|
|
2107
|
+
private firstSet: u8[]
|
|
2026
2108
|
/**
|
|
2027
2109
|
* The byte sets of a match's first `scanDepth` positions, 32 bytes each in
|
|
2028
2110
|
* `regexScanFrom`'s layout. Every position but the last is ASCII only, so
|
|
2029
2111
|
* each is one byte of the text.
|
|
2030
2112
|
*/
|
|
2031
|
-
posMasks: u8[]
|
|
2113
|
+
private posMasks: u8[]
|
|
2032
2114
|
|
|
2033
2115
|
// Two lists of threads, each with a capture table indexed by pc. A list
|
|
2034
2116
|
// holds only the threads that wait for a character or have matched, in
|
|
2035
2117
|
// priority order; the instructions a closure has passed through are in
|
|
2036
2118
|
// `visit`, stamped with the closure's generation `gen`.
|
|
2037
|
-
cDense: i32[]
|
|
2038
|
-
cSlots: i32[]
|
|
2039
|
-
nDense: i32[]
|
|
2040
|
-
nSlots: i32[]
|
|
2041
|
-
visit: i32[]
|
|
2119
|
+
private cDense: i32[]
|
|
2120
|
+
private cSlots: i32[]
|
|
2121
|
+
private nDense: i32[]
|
|
2122
|
+
private nSlots: i32[]
|
|
2123
|
+
private visit: i32[]
|
|
2042
2124
|
/** The closure's stack: explore `stkA` (kind 0) or restore slot `stkA` to `stkB` (kind 1). */
|
|
2043
|
-
stkKind: i32[]
|
|
2044
|
-
stkA: i32[]
|
|
2045
|
-
stkB: i32[]
|
|
2125
|
+
private stkKind: i32[]
|
|
2126
|
+
private stkA: i32[]
|
|
2127
|
+
private stkB: i32[]
|
|
2046
2128
|
/** The captures of the thread being followed. */
|
|
2047
|
-
caps: i32[]
|
|
2129
|
+
private caps: i32[]
|
|
2048
2130
|
/** The best match so far. */
|
|
2049
|
-
mslots: i32[]
|
|
2131
|
+
private mslots: i32[]
|
|
2050
2132
|
|
|
2051
2133
|
// The lazy DFA, built on first use by `dfaInit`. State `id` lives at
|
|
2052
2134
|
// offset `id << dShift` of `dTrans`, one transition per byte class; its
|
|
2053
2135
|
// instructions are `dPool[dStart[id] .. + dLen[id])` and `dHash` finds a
|
|
2054
2136
|
// state by them, holding `id + 1`.
|
|
2055
|
-
dTrans: i32[]
|
|
2056
|
-
dPool: i32[]
|
|
2057
|
-
dStart: i32[]
|
|
2058
|
-
dLen: i32[]
|
|
2059
|
-
dFlags: i32[]
|
|
2060
|
-
dHash: i32[]
|
|
2137
|
+
private dTrans: i32[]
|
|
2138
|
+
private dPool: i32[]
|
|
2139
|
+
private dStart: i32[]
|
|
2140
|
+
private dLen: i32[]
|
|
2141
|
+
private dFlags: i32[]
|
|
2142
|
+
private dHash: i32[]
|
|
2061
2143
|
// Scratch for one step: the closure's stamps, leaves and stack, and the
|
|
2062
2144
|
// next state's instructions.
|
|
2063
|
-
dVisit: i32[]
|
|
2064
|
-
dLeaf: i32[]
|
|
2065
|
-
dBuf: i32[]
|
|
2066
|
-
dStk: i32[]
|
|
2145
|
+
private dVisit: i32[]
|
|
2146
|
+
private dLeaf: i32[]
|
|
2147
|
+
private dBuf: i32[]
|
|
2148
|
+
private dStk: i32[]
|
|
2067
2149
|
/** The class of each ASCII byte; every byte above it maps to the spare column, never filled. */
|
|
2068
|
-
dCls: i32[]
|
|
2150
|
+
private dCls: i32[]
|
|
2069
2151
|
/** The classes of the code points from `dWlo[i]` up to the next: every one above ASCII. */
|
|
2070
|
-
dWlo: i32[]
|
|
2071
|
-
dWcls: i32[]
|
|
2152
|
+
private dWlo: i32[]
|
|
2153
|
+
private dWcls: i32[]
|
|
2072
2154
|
/** A code point of each class, or -1 for the end of the text, and its kind. */
|
|
2073
|
-
dRep: i32[]
|
|
2074
|
-
dNextK: i32[]
|
|
2155
|
+
private dRep: i32[]
|
|
2156
|
+
private dNextK: i32[]
|
|
2075
2157
|
/** The context a class leaves behind, read forward and in reverse. */
|
|
2076
|
-
dCtxF: i32[]
|
|
2077
|
-
dCtxR: i32[]
|
|
2078
|
-
dMaskF: i32[]
|
|
2079
|
-
dMaskR: i32[]
|
|
2158
|
+
private dCtxF: i32[]
|
|
2159
|
+
private dCtxR: i32[]
|
|
2160
|
+
private dMaskF: i32[]
|
|
2161
|
+
private dMaskR: i32[]
|
|
2080
2162
|
/** Each start state's offset + 1, by mode and context, or 0. */
|
|
2081
|
-
dStartOff: i32[]
|
|
2163
|
+
private dStartOff: i32[]
|
|
2082
2164
|
/** The unanchored start state after the prefix (see `dPrefFast`): its offset + 1, or 0 until it is computed. */
|
|
2083
|
-
dPrefOff: i32
|
|
2165
|
+
private dPrefOff: i32
|
|
2084
2166
|
/** Bytes the inner search read in vain past its literal, and bytes it moved past: see `dfaInner`. */
|
|
2085
|
-
innerWaste: f64
|
|
2086
|
-
innerGain: f64
|
|
2167
|
+
private innerWaste: f64
|
|
2168
|
+
private innerGain: f64
|
|
2087
2169
|
|
|
2088
2170
|
/** Capture groups, the whole match included. */
|
|
2089
2171
|
groups: i32
|
|
2090
2172
|
/** Slots a thread carries: two per group, then one mark per repeat that can iterate empty. */
|
|
2091
|
-
width: i32
|
|
2173
|
+
private width: i32
|
|
2092
2174
|
/**
|
|
2093
2175
|
* How deep the marked iterations nest, up to `REGEX_MAX_LEVELS`, so a
|
|
2094
2176
|
* closure tells `1 << levels` sets of them apart; `checkDepth` is the
|
|
2095
2177
|
* nesting where the compiler is.
|
|
2096
2178
|
*/
|
|
2097
|
-
levels: i32
|
|
2098
|
-
checkDepth: i32
|
|
2099
|
-
cCount: i32
|
|
2100
|
-
nCount: i32
|
|
2101
|
-
gen: i32
|
|
2179
|
+
private levels: i32
|
|
2180
|
+
private checkDepth: i32
|
|
2181
|
+
private cCount: i32
|
|
2182
|
+
private nCount: i32
|
|
2183
|
+
private gen: i32
|
|
2102
2184
|
/** The forward program's length; the reverse one follows it, at `revEntry`. */
|
|
2103
|
-
fwdCount: i32
|
|
2104
|
-
revEntry: i32
|
|
2185
|
+
private fwdCount: i32
|
|
2186
|
+
private revEntry: i32
|
|
2105
2187
|
/** The reverse of the part before `innerLit`, which the inner search runs back from it. */
|
|
2106
|
-
innerEntry: i32
|
|
2188
|
+
private innerEntry: i32
|
|
2107
2189
|
/** Where `dfaForward` stopped reading, and where `dfaInner`'s match starts. */
|
|
2108
|
-
dStop: i32
|
|
2109
|
-
dBegin: i32
|
|
2190
|
+
private dStop: i32
|
|
2191
|
+
private dBegin: i32
|
|
2110
2192
|
/** Where `rareStr` is in `prefix`. */
|
|
2111
|
-
rareAt: i32
|
|
2112
|
-
scanDepth: i32
|
|
2113
|
-
scanWidth: i32
|
|
2114
|
-
scanOff: i32
|
|
2115
|
-
dShift: i32
|
|
2116
|
-
dCount: i32
|
|
2117
|
-
dCap: i32
|
|
2118
|
-
dMax: i32
|
|
2119
|
-
dPoolLen: i32
|
|
2120
|
-
dPoolMax: i32
|
|
2121
|
-
dGen: i32
|
|
2193
|
+
private rareAt: i32
|
|
2194
|
+
private scanDepth: i32
|
|
2195
|
+
private scanWidth: i32
|
|
2196
|
+
private scanOff: i32
|
|
2197
|
+
private dShift: i32
|
|
2198
|
+
private dCount: i32
|
|
2199
|
+
private dCap: i32
|
|
2200
|
+
private dMax: i32
|
|
2201
|
+
private dPoolLen: i32
|
|
2202
|
+
private dPoolMax: i32
|
|
2203
|
+
private dGen: i32
|
|
2122
2204
|
/** Bumped by every flush, so a step can tell its row was cleared under it. */
|
|
2123
|
-
dEpoch: i32
|
|
2124
|
-
dFlushes: i32
|
|
2205
|
+
private dEpoch: i32
|
|
2206
|
+
private dFlushes: i32
|
|
2125
2207
|
/** Where the last flush happened. */
|
|
2126
|
-
dPos: i32
|
|
2208
|
+
private dPos: i32
|
|
2127
2209
|
/** The class of the end of the text, and how many intervals above ASCII there are. */
|
|
2128
|
-
dEot: i32
|
|
2129
|
-
dWide: i32
|
|
2210
|
+
private dEot: i32
|
|
2211
|
+
private dWide: i32
|
|
2130
2212
|
|
|
2131
|
-
hasNames: boolean
|
|
2132
|
-
tooBig: boolean
|
|
2213
|
+
private hasNames: boolean
|
|
2214
|
+
private tooBig: boolean
|
|
2133
2215
|
/** The pattern is `prefix` and nothing else. */
|
|
2134
|
-
literal: boolean
|
|
2216
|
+
private literal: boolean
|
|
2135
2217
|
/** The pattern starts with `^` outside multiline mode. */
|
|
2136
|
-
anchored: boolean
|
|
2218
|
+
private anchored: boolean
|
|
2137
2219
|
/** Whether a byte above ASCII can start a match, when `firstSet` is not empty. */
|
|
2138
|
-
firstWide: boolean
|
|
2220
|
+
private firstWide: boolean
|
|
2139
2221
|
/** The pattern can match the empty string. */
|
|
2140
|
-
nullable: boolean
|
|
2222
|
+
private nullable: boolean
|
|
2141
2223
|
/** The reverse program was compiled. */
|
|
2142
|
-
revOk: boolean
|
|
2224
|
+
private revOk: boolean
|
|
2143
2225
|
/** `dfaInit` has run, and whether the DFA may be used. */
|
|
2144
|
-
dReady: boolean
|
|
2145
|
-
dOk: boolean
|
|
2226
|
+
private dReady: boolean
|
|
2227
|
+
private dOk: boolean
|
|
2146
2228
|
/** An idle state skips ahead with the prefilter. */
|
|
2147
|
-
dSkip: boolean
|
|
2229
|
+
private dSkip: boolean
|
|
2148
2230
|
/** The scan's window is rare enough in text to search with. */
|
|
2149
|
-
scanSel: boolean
|
|
2231
|
+
private scanSel: boolean
|
|
2150
2232
|
/** The scan's first positions are exactly the prefix's bytes, all ASCII: see `prefixIsScan`. */
|
|
2151
|
-
prefixExact: boolean
|
|
2233
|
+
private prefixExact: boolean
|
|
2152
2234
|
/**
|
|
2153
2235
|
* Whether no forward assertion reads the character before it, so every
|
|
2154
2236
|
* forward state after a character holds the same context and a state with
|
|
2155
2237
|
* no thread left is the unanchored start state wherever the text resumes.
|
|
2156
2238
|
*/
|
|
2157
|
-
dCtxFree: boolean
|
|
2239
|
+
private dCtxFree: boolean
|
|
2158
2240
|
/**
|
|
2159
2241
|
* Whether a match can start only where the ASCII `prefix` is and no
|
|
2160
2242
|
* assertion reads the context, so the unanchored start state after the
|
|
2161
2243
|
* prefix's bytes is one state, at `dPrefOff`.
|
|
2162
2244
|
*/
|
|
2163
|
-
dPrefFast: boolean
|
|
2245
|
+
private dPrefFast: boolean
|
|
2164
2246
|
|
|
2247
|
+
/**
|
|
2248
|
+
* @internal Built only by `regexCompile`, which a program calls instead.
|
|
2249
|
+
*
|
|
2250
|
+
* A pattern not compiled yet: `regexCompile` builds its program.
|
|
2251
|
+
*
|
|
2252
|
+
* @param source The pattern as written, kept for `source`.
|
|
2253
|
+
* @param flags The flags as written, kept for `flags`.
|
|
2254
|
+
*/
|
|
2165
2255
|
constructor(source: string, flags: string) {
|
|
2166
2256
|
this.source = source
|
|
2167
2257
|
this.flags = flags
|
|
@@ -2259,12 +2349,21 @@ export class Regex {
|
|
|
2259
2349
|
this.prefixExact = false
|
|
2260
2350
|
}
|
|
2261
2351
|
|
|
2262
|
-
/**
|
|
2352
|
+
/**
|
|
2353
|
+
* How many groups the pattern has.
|
|
2354
|
+
*
|
|
2355
|
+
* @returns The count with the whole match (group 0) included, so a pattern with no groups gives 1.
|
|
2356
|
+
*/
|
|
2263
2357
|
groupCount(): i32 {
|
|
2264
2358
|
return this.groups
|
|
2265
2359
|
}
|
|
2266
2360
|
|
|
2267
|
-
/**
|
|
2361
|
+
/**
|
|
2362
|
+
* The number of a named group.
|
|
2363
|
+
*
|
|
2364
|
+
* @param name The name the pattern gave the group with `(?<name>...)`.
|
|
2365
|
+
* @returns The group's number, for `RegexMatch.group`; -1 when no group has that name or `name` is "".
|
|
2366
|
+
*/
|
|
2268
2367
|
groupIndex(name: string): i32 {
|
|
2269
2368
|
if (name === "") {
|
|
2270
2369
|
return -1
|
|
@@ -2272,7 +2371,7 @@ export class Regex {
|
|
|
2272
2371
|
return toI32(this.names.indexOf(name))
|
|
2273
2372
|
}
|
|
2274
2373
|
|
|
2275
|
-
emit(op: i32, a: i32, b: i32): i32 {
|
|
2374
|
+
private emit(op: i32, a: i32, b: i32): i32 {
|
|
2276
2375
|
const pc: i32 = toI32(this.op.length)
|
|
2277
2376
|
if (pc >= REGEX_MAX_INSTRUCTIONS) {
|
|
2278
2377
|
this.tooBig = true
|
|
@@ -2284,12 +2383,12 @@ export class Regex {
|
|
|
2284
2383
|
return pc
|
|
2285
2384
|
}
|
|
2286
2385
|
|
|
2287
|
-
pc(): i32 {
|
|
2386
|
+
private pc(): i32 {
|
|
2288
2387
|
return toI32(this.op.length)
|
|
2289
2388
|
}
|
|
2290
2389
|
|
|
2291
2390
|
/** One iteration of a repeat's body: its groups cleared, then the body. */
|
|
2292
|
-
compileIteration(p: RegexParser, r: i32, rev: boolean): void {
|
|
2391
|
+
private compileIteration(p: RegexParser, r: i32, rev: boolean): void {
|
|
2293
2392
|
if (p.ne[r] > p.nd[r]) {
|
|
2294
2393
|
this.emit(OP_CLEAR, 2 * p.nd[r], 2 * p.ne[r])
|
|
2295
2394
|
}
|
|
@@ -2301,7 +2400,7 @@ export class Regex {
|
|
|
2301
2400
|
* end: a concatenation's parts come last first and each assertion trades
|
|
2302
2401
|
* places with its mirror, `^` with `$`. The reverse DFA runs that program.
|
|
2303
2402
|
*/
|
|
2304
|
-
compileNode(p: RegexParser, n: i32, rev: boolean): void {
|
|
2403
|
+
private compileNode(p: RegexParser, n: i32, rev: boolean): void {
|
|
2305
2404
|
if (this.tooBig) {
|
|
2306
2405
|
return
|
|
2307
2406
|
}
|
|
@@ -2447,7 +2546,14 @@ export class Regex {
|
|
|
2447
2546
|
* body × min; L: SPLIT M, exit; M: MARK k; body; CHECK k; JMP L (unbounded)
|
|
2448
2547
|
* body × min; (SPLIT M, exit; M: MARK k; body; CHECK k) × (max - min)
|
|
2449
2548
|
*/
|
|
2450
|
-
compileEmptyCheckedRepeat(
|
|
2549
|
+
private compileEmptyCheckedRepeat(
|
|
2550
|
+
p: RegexParser,
|
|
2551
|
+
n: i32,
|
|
2552
|
+
min: i32,
|
|
2553
|
+
max: i32,
|
|
2554
|
+
greedy: boolean,
|
|
2555
|
+
rev: boolean
|
|
2556
|
+
): void {
|
|
2451
2557
|
while (toI32(this.markOf.length) <= n) {
|
|
2452
2558
|
this.markOf.push(REGEX_NONE)
|
|
2453
2559
|
}
|
|
@@ -2492,7 +2598,7 @@ export class Regex {
|
|
|
2492
2598
|
* check that it moved on. The mark records how deep it nests, which the VM
|
|
2493
2599
|
* keys its closure's stamps by.
|
|
2494
2600
|
*/
|
|
2495
|
-
compileMarked(p: RegexParser, n: i32, k: i32, rev: boolean): void {
|
|
2601
|
+
private compileMarked(p: RegexParser, n: i32, k: i32, rev: boolean): void {
|
|
2496
2602
|
const level: i32 = this.checkDepth
|
|
2497
2603
|
if (level < REGEX_MAX_LEVELS && level >= this.levels) {
|
|
2498
2604
|
this.levels = level + 1
|
|
@@ -2504,7 +2610,7 @@ export class Regex {
|
|
|
2504
2610
|
this.emit(OP_CHECK, k, REGEX_NIL)
|
|
2505
2611
|
}
|
|
2506
2612
|
|
|
2507
|
-
/** Compile the parsed pattern and size the VM. Answers an error, or "". */
|
|
2613
|
+
/** Compile the parsed pattern and size the VM. Answers an error, or "". @internal `regexCompile` calls it. */
|
|
2508
2614
|
build(p: RegexParser, root: i32): string {
|
|
2509
2615
|
this.groups = p.groups
|
|
2510
2616
|
this.width = 2 * p.groups
|
|
@@ -2583,7 +2689,7 @@ export class Regex {
|
|
|
2583
2689
|
* run with the rarest first byte, the longest of those, and compiles the
|
|
2584
2690
|
* parts before it reversed, at `innerEntry`.
|
|
2585
2691
|
*/
|
|
2586
|
-
findInner(p: RegexParser, root: i32): void {
|
|
2692
|
+
private findInner(p: RegexParser, root: i32): void {
|
|
2587
2693
|
if (p.kind[root] !== NODE_CAT) {
|
|
2588
2694
|
return
|
|
2589
2695
|
}
|
|
@@ -2648,12 +2754,12 @@ export class Regex {
|
|
|
2648
2754
|
}
|
|
2649
2755
|
|
|
2650
2756
|
/** Whether node `n` is one ASCII character, matched exactly. */
|
|
2651
|
-
isAsciiChar(p: RegexParser, n: i32): boolean {
|
|
2757
|
+
private isAsciiChar(p: RegexParser, n: i32): boolean {
|
|
2652
2758
|
return p.kind[n] === NODE_CHAR && p.na[n] === p.nb[n] && p.na[n] < 128
|
|
2653
2759
|
}
|
|
2654
2760
|
|
|
2655
2761
|
/** Whether node `n` can read the ASCII byte `b`. */
|
|
2656
|
-
canConsume(p: RegexParser, n: i32, b: i32): boolean {
|
|
2762
|
+
private canConsume(p: RegexParser, n: i32, b: i32): boolean {
|
|
2657
2763
|
const kind: i32 = p.kind[n]
|
|
2658
2764
|
if (kind === NODE_CHAR) {
|
|
2659
2765
|
return p.na[n] === b || p.nb[n] === b
|
|
@@ -2680,7 +2786,7 @@ export class Regex {
|
|
|
2680
2786
|
}
|
|
2681
2787
|
|
|
2682
2788
|
/** The literal prefix, whether the pattern is only that literal, the anchor, and the first-byte set. */
|
|
2683
|
-
findPrefilters(): void {
|
|
2789
|
+
private findPrefilters(): void {
|
|
2684
2790
|
const bytes: u8[] = []
|
|
2685
2791
|
let pc: i32 = 0
|
|
2686
2792
|
while (true) {
|
|
@@ -2733,7 +2839,7 @@ export class Regex {
|
|
|
2733
2839
|
* a character above ASCII can start, since the next byte of the text may be
|
|
2734
2840
|
* inside that character.
|
|
2735
2841
|
*/
|
|
2736
|
-
findScan(): void {
|
|
2842
|
+
private findScan(): void {
|
|
2737
2843
|
const count: i32 = this.pc()
|
|
2738
2844
|
const masks: u8[] = new Array<u8>(32 * REGEX_SCAN_DEPTH)
|
|
2739
2845
|
const odds: i32[] = new Array<i32>(REGEX_SCAN_DEPTH)
|
|
@@ -2932,7 +3038,7 @@ export class Regex {
|
|
|
2932
3038
|
* else. The positions after them hold for every match too, so a start the
|
|
2933
3039
|
* scan finds is then the prefix's next occurrence that can begin a match.
|
|
2934
3040
|
*/
|
|
2935
|
-
prefixIsScan(): boolean {
|
|
3041
|
+
private prefixIsScan(): boolean {
|
|
2936
3042
|
const n: i32 = toI32(this.prefix.length)
|
|
2937
3043
|
if (n === 0 || n > this.scanDepth) {
|
|
2938
3044
|
return false
|
|
@@ -2958,7 +3064,7 @@ export class Regex {
|
|
|
2958
3064
|
}
|
|
2959
3065
|
|
|
2960
3066
|
/** Whether class `k` holds code point `cp`, which is at least 0. */
|
|
2961
|
-
classHas(k: i32, cp: i32): boolean {
|
|
3067
|
+
private classHas(k: i32, cp: i32): boolean {
|
|
2962
3068
|
if (cp < 128) {
|
|
2963
3069
|
return this.clsAscii[k * 128 + cp] === 1
|
|
2964
3070
|
}
|
|
@@ -2980,7 +3086,7 @@ export class Regex {
|
|
|
2980
3086
|
}
|
|
2981
3087
|
|
|
2982
3088
|
/** Whether assertion `kind` holds at byte `p` of `text`. */
|
|
2983
|
-
assertHolds(kind: i32, text: string, p: i32, len: i32): boolean {
|
|
3089
|
+
private assertHolds(kind: i32, text: string, p: i32, len: i32): boolean {
|
|
2984
3090
|
if (kind === ASSERT_BEGIN_TEXT) {
|
|
2985
3091
|
return p === 0
|
|
2986
3092
|
}
|
|
@@ -3028,7 +3134,7 @@ export class Regex {
|
|
|
3028
3134
|
}
|
|
3029
3135
|
|
|
3030
3136
|
/** Start a closure that has visited nothing, wrapping the stamps before they overflow. */
|
|
3031
|
-
newGeneration(): void {
|
|
3137
|
+
private newGeneration(): void {
|
|
3032
3138
|
if (this.gen >= 2147483647) {
|
|
3033
3139
|
const count: i32 = toI32(this.visit.length)
|
|
3034
3140
|
let pc: i32 = 0
|
|
@@ -3052,7 +3158,16 @@ export class Regex {
|
|
|
3052
3158
|
* thread that was here before it, since the check it meets next fails for
|
|
3053
3159
|
* it alone. `(?:a*?)*` on "aa" goes on to the second `a` that way.
|
|
3054
3160
|
*/
|
|
3055
|
-
addThread(
|
|
3161
|
+
private addThread(
|
|
3162
|
+
dense: i32[],
|
|
3163
|
+
slots: i32[],
|
|
3164
|
+
count0: i32,
|
|
3165
|
+
pc0: i32,
|
|
3166
|
+
text: string,
|
|
3167
|
+
p: i32,
|
|
3168
|
+
len: i32,
|
|
3169
|
+
ns: i32
|
|
3170
|
+
): i32 {
|
|
3056
3171
|
let count: i32 = count0
|
|
3057
3172
|
const states: i32 = 1 << this.levels
|
|
3058
3173
|
this.stkKind[0] = 0
|
|
@@ -3153,7 +3268,7 @@ export class Regex {
|
|
|
3153
3268
|
* The first match of `prefix` at or after `from`, or -1: a search for its
|
|
3154
3269
|
* rarest byte, then a comparison of the bytes around it.
|
|
3155
3270
|
*/
|
|
3156
|
-
findPrefix(text: string, from: i32): i32 {
|
|
3271
|
+
private findPrefix(text: string, from: i32): i32 {
|
|
3157
3272
|
if (this.scanSel) {
|
|
3158
3273
|
return this.findPrefixScan(text, from)
|
|
3159
3274
|
}
|
|
@@ -3188,7 +3303,7 @@ export class Regex {
|
|
|
3188
3303
|
* alone. When `prefixExact` the scan's positions are the prefix's bytes and
|
|
3189
3304
|
* a start it finds is a match.
|
|
3190
3305
|
*/
|
|
3191
|
-
findPrefixScan(text: string, from: i32): i32 {
|
|
3306
|
+
private findPrefixScan(text: string, from: i32): i32 {
|
|
3192
3307
|
const len: i32 = toI32(text.length)
|
|
3193
3308
|
const n: i32 = toI32(this.prefix.length)
|
|
3194
3309
|
let p: i32 = from
|
|
@@ -3216,7 +3331,7 @@ export class Regex {
|
|
|
3216
3331
|
* The first index at or after `from`, which is at least 0, whose bytes pass
|
|
3217
3332
|
* every position of `posMasks`, or -1. No match starts anywhere it skips.
|
|
3218
3333
|
*/
|
|
3219
|
-
scanTo(text: string, from: i32): i32 {
|
|
3334
|
+
private scanTo(text: string, from: i32): i32 {
|
|
3220
3335
|
if (from > toI32(text.length)) {
|
|
3221
3336
|
return -1
|
|
3222
3337
|
}
|
|
@@ -3224,7 +3339,7 @@ export class Regex {
|
|
|
3224
3339
|
}
|
|
3225
3340
|
|
|
3226
3341
|
/** Where the next match can start at or after `p`, by the prefilter, or -1. */
|
|
3227
|
-
dSkipTo(text: string, p: i32): i32 {
|
|
3342
|
+
private dSkipTo(text: string, p: i32): i32 {
|
|
3228
3343
|
if (this.prefix !== "") {
|
|
3229
3344
|
return this.findPrefix(text, p)
|
|
3230
3345
|
}
|
|
@@ -3239,7 +3354,7 @@ export class Regex {
|
|
|
3239
3354
|
* it does not. Leaves `dOk` false when the split or the table would be too
|
|
3240
3355
|
* big, and the VM does all the work.
|
|
3241
3356
|
*/
|
|
3242
|
-
dfaInit(): void {
|
|
3357
|
+
private dfaInit(): void {
|
|
3243
3358
|
this.dReady = true
|
|
3244
3359
|
const total: i32 = this.pc()
|
|
3245
3360
|
const count: i32 = this.fwdCount
|
|
@@ -3523,7 +3638,7 @@ export class Regex {
|
|
|
3523
3638
|
* Split each of the `n` classes of `cls` by `hit`, numbering the new
|
|
3524
3639
|
* classes in order of first appearance. Answers how many there are.
|
|
3525
3640
|
*/
|
|
3526
|
-
dRefine(cls: i32[], hit: u8[], units: i32, n: i32, remap: i32[]): i32 {
|
|
3641
|
+
private dRefine(cls: i32[], hit: u8[], units: i32, n: i32, remap: i32[]): i32 {
|
|
3527
3642
|
let k: i32 = 0
|
|
3528
3643
|
while (k < 2 * n && k < toI32(remap.length)) {
|
|
3529
3644
|
remap[k] = -1
|
|
@@ -3547,7 +3662,7 @@ export class Regex {
|
|
|
3547
3662
|
}
|
|
3548
3663
|
|
|
3549
3664
|
/** The smallest power of two that holds `cap` states at most half full. */
|
|
3550
|
-
dHashSize(cap: i32): i32 {
|
|
3665
|
+
private dHashSize(cap: i32): i32 {
|
|
3551
3666
|
let size: i32 = 1
|
|
3552
3667
|
while (size < 2 * cap) {
|
|
3553
3668
|
size *= 2
|
|
@@ -3556,7 +3671,7 @@ export class Regex {
|
|
|
3556
3671
|
}
|
|
3557
3672
|
|
|
3558
3673
|
/** The class of code point `cp`, which is above ASCII. */
|
|
3559
|
-
dWideClass(cp: i32): i32 {
|
|
3674
|
+
private dWideClass(cp: i32): i32 {
|
|
3560
3675
|
if (this.dWide === 1) {
|
|
3561
3676
|
return this.dWcls[0]
|
|
3562
3677
|
}
|
|
@@ -3574,7 +3689,7 @@ export class Regex {
|
|
|
3574
3689
|
}
|
|
3575
3690
|
|
|
3576
3691
|
/** Whether assertion `kind` holds between a character of kind `ctx` and one of kind `nk`. */
|
|
3577
|
-
dAssert(kind: i32, ctx: i32, nk: i32): boolean {
|
|
3692
|
+
private dAssert(kind: i32, ctx: i32, nk: i32): boolean {
|
|
3578
3693
|
if (kind === ASSERT_BEGIN_TEXT) {
|
|
3579
3694
|
return ctx === CTX_START
|
|
3580
3695
|
}
|
|
@@ -3597,7 +3712,7 @@ export class Regex {
|
|
|
3597
3712
|
* wait for a character or match to `dLeaf` in priority order. Answers the
|
|
3598
3713
|
* new length.
|
|
3599
3714
|
*/
|
|
3600
|
-
dClose(pc0: i32, ctx: i32, nk: i32, n0: i32): i32 {
|
|
3715
|
+
private dClose(pc0: i32, ctx: i32, nk: i32, n0: i32): i32 {
|
|
3601
3716
|
let n: i32 = n0
|
|
3602
3717
|
this.dStk[0] = pc0
|
|
3603
3718
|
let top: i32 = 1
|
|
@@ -3643,7 +3758,7 @@ export class Regex {
|
|
|
3643
3758
|
* Compute and record the transition from the state at `off` on class `c`,
|
|
3644
3759
|
* read at byte `p`. Answers the entry as stored, or 0 when the cache gave up.
|
|
3645
3760
|
*/
|
|
3646
|
-
dStep(off: i32, c: i32, p: i32): i32 {
|
|
3761
|
+
private dStep(off: i32, c: i32, p: i32): i32 {
|
|
3647
3762
|
const id: i32 = off >> this.dShift
|
|
3648
3763
|
const start: i32 = this.dStart[id]
|
|
3649
3764
|
const len: i32 = this.dLen[id]
|
|
@@ -3748,7 +3863,7 @@ export class Regex {
|
|
|
3748
3863
|
}
|
|
3749
3864
|
|
|
3750
3865
|
/** The hash of a state's flags and its `n` instructions in `src` from `at`. */
|
|
3751
|
-
dHashOf(src: i32[], at: i32, n: i32, flags: i32): i32 {
|
|
3866
|
+
private dHashOf(src: i32[], at: i32, n: i32, flags: i32): i32 {
|
|
3752
3867
|
let h: i32 = flags
|
|
3753
3868
|
let i: i32 = 0
|
|
3754
3869
|
while (i < n) {
|
|
@@ -3763,7 +3878,7 @@ export class Regex {
|
|
|
3763
3878
|
* `dBuf`, made if it is new. Answers -1 when the cache is full and was
|
|
3764
3879
|
* cleared too recently, at byte `p`, to clear again.
|
|
3765
3880
|
*/
|
|
3766
|
-
dIntern(n: i32, flags: i32, p: i32): i32 {
|
|
3881
|
+
private dIntern(n: i32, flags: i32, p: i32): i32 {
|
|
3767
3882
|
const mask: i32 = toI32(this.dHash.length) - 1
|
|
3768
3883
|
const at: i32 = 0
|
|
3769
3884
|
let slot: i32 = this.dHashOf(this.dBuf, at, n, flags) & mask
|
|
@@ -3835,7 +3950,7 @@ export class Regex {
|
|
|
3835
3950
|
}
|
|
3836
3951
|
|
|
3837
3952
|
/** Double the state table, up to `dMax` states. */
|
|
3838
|
-
dGrow(): void {
|
|
3953
|
+
private dGrow(): void {
|
|
3839
3954
|
let cap: i32 = 2 * this.dCap
|
|
3840
3955
|
if (cap > this.dMax) {
|
|
3841
3956
|
cap = this.dMax
|
|
@@ -3885,7 +4000,7 @@ export class Regex {
|
|
|
3885
4000
|
* state for nearly every byte and the VM is cheaper. Answers whether it
|
|
3886
4001
|
* cleared.
|
|
3887
4002
|
*/
|
|
3888
|
-
dFlush(p: i32): boolean {
|
|
4003
|
+
private dFlush(p: i32): boolean {
|
|
3889
4004
|
this.dFlushes += 1
|
|
3890
4005
|
const moved: i32 = p > this.dPos ? p - this.dPos : this.dPos - p
|
|
3891
4006
|
if (this.dFlushes > DFA_FREE_FLUSHES && moved < DFA_BYTES_PER_STATE * this.dMax) {
|
|
@@ -3910,7 +4025,7 @@ export class Regex {
|
|
|
3910
4025
|
}
|
|
3911
4026
|
|
|
3912
4027
|
/** The start state for `mode` after a character of kind `ctx`, or -1. */
|
|
3913
|
-
dStartAt(mode: i32, ctx: i32, p: i32): i32 {
|
|
4028
|
+
private dStartAt(mode: i32, ctx: i32, p: i32): i32 {
|
|
3914
4029
|
const k: i32 = mode * CTX_SLOTS + ctx
|
|
3915
4030
|
const cached: i32 = this.dStartOff[k]
|
|
3916
4031
|
if (cached > 0) {
|
|
@@ -3940,7 +4055,7 @@ export class Regex {
|
|
|
3940
4055
|
* `p`, or -1 when a step on the way is not a plain transition; then
|
|
3941
4056
|
* `dPrefFast` goes off.
|
|
3942
4057
|
*/
|
|
3943
|
-
dAfterPrefix(p: i32): i32 {
|
|
4058
|
+
private dAfterPrefix(p: i32): i32 {
|
|
3944
4059
|
let off: i32 = this.dStartAt(DFA_UNANCHORED, this.dMaskF[CTX_OTHER], p)
|
|
3945
4060
|
const n: i32 = toI32(this.prefix.length)
|
|
3946
4061
|
let k: i32 = 0
|
|
@@ -3962,7 +4077,7 @@ export class Regex {
|
|
|
3962
4077
|
}
|
|
3963
4078
|
|
|
3964
4079
|
/** The kind of the character before byte `p`, as the forward program tells kinds apart. */
|
|
3965
|
-
ctxBefore(text: string, p: i32): i32 {
|
|
4080
|
+
private ctxBefore(text: string, p: i32): i32 {
|
|
3966
4081
|
let kind: i32 = CTX_OTHER
|
|
3967
4082
|
if (p <= 0) {
|
|
3968
4083
|
kind = CTX_START
|
|
@@ -3989,7 +4104,7 @@ export class Regex {
|
|
|
3989
4104
|
}
|
|
3990
4105
|
|
|
3991
4106
|
/** The kind of the character at byte `p`, as the reverse program tells kinds apart. */
|
|
3992
|
-
ctxAt(text: string, p: i32): i32 {
|
|
4107
|
+
private ctxAt(text: string, p: i32): i32 {
|
|
3993
4108
|
const len: i32 = toI32(text.length)
|
|
3994
4109
|
let kind: i32 = CTX_OTHER
|
|
3995
4110
|
if (p >= len) {
|
|
@@ -4020,7 +4135,7 @@ export class Regex {
|
|
|
4020
4135
|
* `DFA_NONE` or `DFA_GAVE_UP` otherwise, and leaves where it stopped
|
|
4021
4136
|
* reading in `dStop`.
|
|
4022
4137
|
*/
|
|
4023
|
-
dfaForward(text: string, from: i32, first: boolean, pinned: boolean): i32 {
|
|
4138
|
+
private dfaForward(text: string, from: i32, first: boolean, pinned: boolean): i32 {
|
|
4024
4139
|
const len: i32 = toI32(text.length)
|
|
4025
4140
|
const mode: i32 = this.anchored || pinned ? DFA_ANCHORED : DFA_UNANCHORED
|
|
4026
4141
|
let p: i32 = from
|
|
@@ -4143,7 +4258,7 @@ export class Regex {
|
|
|
4143
4258
|
* `innerLit` does when the literal is at `e`. Answers `DFA_NONE` or
|
|
4144
4259
|
* `DFA_GAVE_UP` otherwise.
|
|
4145
4260
|
*/
|
|
4146
|
-
dfaReverse(text: string, from: i32, e: i32, mode: i32): i32 {
|
|
4261
|
+
private dfaReverse(text: string, from: i32, e: i32, mode: i32): i32 {
|
|
4147
4262
|
const len: i32 = toI32(text.length)
|
|
4148
4263
|
let p: i32 = e
|
|
4149
4264
|
let off: i32 = this.dStartAt(mode, this.ctxAt(text, e), e)
|
|
@@ -4245,7 +4360,7 @@ export class Regex {
|
|
|
4245
4360
|
* the regex: past twice those and a slack, the inner search is dropped for
|
|
4246
4361
|
* the plain one, which keeps the cost linear.
|
|
4247
4362
|
*/
|
|
4248
|
-
dfaInner(text: string, from: i32, first: boolean): i32 {
|
|
4363
|
+
private dfaInner(text: string, from: i32, first: boolean): i32 {
|
|
4249
4364
|
let lower: i32 = from
|
|
4250
4365
|
while (this.innerLit !== "") {
|
|
4251
4366
|
const i: i32 = toI32(text.indexOf(this.innerLit, lower))
|
|
@@ -4285,7 +4400,7 @@ export class Regex {
|
|
|
4285
4400
|
* whether there is one and where it ends, the reverse DFA where it starts,
|
|
4286
4401
|
* and the VM, from that start, the groups.
|
|
4287
4402
|
*/
|
|
4288
|
-
run(text: string, from: i32, want: i32): boolean {
|
|
4403
|
+
private run(text: string, from: i32, want: i32): boolean {
|
|
4289
4404
|
const len: i32 = toI32(text.length)
|
|
4290
4405
|
if (from < 0 || from > len) {
|
|
4291
4406
|
return false
|
|
@@ -4353,7 +4468,7 @@ export class Regex {
|
|
|
4353
4468
|
* Any slots at all bring the marks along, because which match comes first
|
|
4354
4469
|
* depends on them. `pinned` tries only a match that starts at `from`.
|
|
4355
4470
|
*/
|
|
4356
|
-
vm(text: string, from: i32, want: i32, pinned: boolean): boolean {
|
|
4471
|
+
private vm(text: string, from: i32, want: i32, pinned: boolean): boolean {
|
|
4357
4472
|
const ns: i32 = want > 0 && this.width > 2 * this.groups ? this.width : want
|
|
4358
4473
|
const len: i32 = toI32(text.length)
|
|
4359
4474
|
const anchored: boolean = this.anchored || pinned
|
|
@@ -4480,7 +4595,12 @@ export class Regex {
|
|
|
4480
4595
|
return matched
|
|
4481
4596
|
}
|
|
4482
4597
|
|
|
4483
|
-
/**
|
|
4598
|
+
/**
|
|
4599
|
+
* Whether the pattern matches anywhere in `text`.
|
|
4600
|
+
*
|
|
4601
|
+
* @param text The text to search.
|
|
4602
|
+
* @returns True when there is at least one match.
|
|
4603
|
+
*/
|
|
4484
4604
|
test(text: string): boolean {
|
|
4485
4605
|
return this.run(text, REGEX_TEXT_START, REGEX_WANT_NONE)
|
|
4486
4606
|
}
|
|
@@ -4489,6 +4609,11 @@ export class Regex {
|
|
|
4489
4609
|
* Search `text` from byte `from` and fill `m` with the first match and
|
|
4490
4610
|
* every group. Answers whether there was one; `m` is untouched when not.
|
|
4491
4611
|
* Reusing one `RegexMatch` makes a loop of searches allocate nothing.
|
|
4612
|
+
*
|
|
4613
|
+
* @param text The text to search.
|
|
4614
|
+
* @param from The byte offset to start searching at; a match may start there or later.
|
|
4615
|
+
* @param m Where the match goes; overwritten on a match, left as it was otherwise.
|
|
4616
|
+
* @returns True when there was a match.
|
|
4492
4617
|
*/
|
|
4493
4618
|
exec(text: string, from: i32, m: RegexMatch): boolean {
|
|
4494
4619
|
const ns: i32 = 2 * this.groups
|
|
@@ -4508,7 +4633,13 @@ export class Regex {
|
|
|
4508
4633
|
return true
|
|
4509
4634
|
}
|
|
4510
4635
|
|
|
4511
|
-
/**
|
|
4636
|
+
/**
|
|
4637
|
+
* The first match at or after byte `from`.
|
|
4638
|
+
*
|
|
4639
|
+
* @param text The text to search.
|
|
4640
|
+
* @param from The byte offset to start searching at; a match may start there or later.
|
|
4641
|
+
* @returns A new match, or null when there is none.
|
|
4642
|
+
*/
|
|
4512
4643
|
find(text: string, from: i32): RegexMatch | null {
|
|
4513
4644
|
const m: RegexMatch = new RegexMatch()
|
|
4514
4645
|
if (this.exec(text, from, m)) {
|
|
@@ -4517,7 +4648,13 @@ export class Regex {
|
|
|
4517
4648
|
return null
|
|
4518
4649
|
}
|
|
4519
4650
|
|
|
4520
|
-
/**
|
|
4651
|
+
/**
|
|
4652
|
+
* The text of every match, left to right, none overlapping. After an empty
|
|
4653
|
+
* match the search resumes one character on.
|
|
4654
|
+
*
|
|
4655
|
+
* @param text The text to search.
|
|
4656
|
+
* @returns Each match's text in order; empty when nothing matches.
|
|
4657
|
+
*/
|
|
4521
4658
|
findAll(text: string): string[] {
|
|
4522
4659
|
const out: string[] = []
|
|
4523
4660
|
const len: i32 = toI32(text.length)
|
|
@@ -4538,7 +4675,12 @@ export class Regex {
|
|
|
4538
4675
|
return out
|
|
4539
4676
|
}
|
|
4540
4677
|
|
|
4541
|
-
/**
|
|
4678
|
+
/**
|
|
4679
|
+
* How many matches `findAll` would answer, without making their strings.
|
|
4680
|
+
*
|
|
4681
|
+
* @param text The text to search.
|
|
4682
|
+
* @returns The number of non-overlapping matches.
|
|
4683
|
+
*/
|
|
4542
4684
|
count(text: string): i32 {
|
|
4543
4685
|
const len: i32 = toI32(text.length)
|
|
4544
4686
|
let n: i32 = 0
|
|
@@ -4598,17 +4740,27 @@ export class Regex {
|
|
|
4598
4740
|
* `String.prototype.replace`'s template: `$$` is `$`, `$&` the match,
|
|
4599
4741
|
* `` $` `` and `$'` the text before and after it, `$1` to `$99` a group
|
|
4600
4742
|
* and `$<name>` a named one; a group that took no part is "".
|
|
4743
|
+
*
|
|
4744
|
+
* @param text The text to search.
|
|
4745
|
+
* @param replacement The template each match is replaced by.
|
|
4746
|
+
* @returns The replaced text; `text` unchanged when nothing matches.
|
|
4601
4747
|
*/
|
|
4602
4748
|
replace(text: string, replacement: string): string {
|
|
4603
4749
|
return this.replaceMatches(text, replacement, false)
|
|
4604
4750
|
}
|
|
4605
4751
|
|
|
4606
|
-
/**
|
|
4752
|
+
/**
|
|
4753
|
+
* `text` with every match replaced by `replacement`, as `replace` reads it.
|
|
4754
|
+
*
|
|
4755
|
+
* @param text The text to search.
|
|
4756
|
+
* @param replacement The template each match is replaced by.
|
|
4757
|
+
* @returns The replaced text; `text` unchanged when nothing matches.
|
|
4758
|
+
*/
|
|
4607
4759
|
replaceAll(text: string, replacement: string): string {
|
|
4608
4760
|
return this.replaceMatches(text, replacement, true)
|
|
4609
4761
|
}
|
|
4610
4762
|
|
|
4611
|
-
replaceMatches(text: string, replacement: string, all: boolean): string {
|
|
4763
|
+
private replaceMatches(text: string, replacement: string, all: boolean): string {
|
|
4612
4764
|
const len: i32 = toI32(text.length)
|
|
4613
4765
|
// Track every group only when the template can name one.
|
|
4614
4766
|
let ns: i32 = 2
|
|
@@ -4650,7 +4802,7 @@ export class Regex {
|
|
|
4650
4802
|
}
|
|
4651
4803
|
|
|
4652
4804
|
/** Group `k` of the match in `mslots`, or "". */
|
|
4653
|
-
slotText(text: string, ns: i32, k: i32): string {
|
|
4805
|
+
private slotText(text: string, ns: i32, k: i32): string {
|
|
4654
4806
|
if (k < 0 || 2 * k + 1 >= ns || this.mslots[2 * k] < 0) {
|
|
4655
4807
|
return ""
|
|
4656
4808
|
}
|
|
@@ -4658,7 +4810,7 @@ export class Regex {
|
|
|
4658
4810
|
}
|
|
4659
4811
|
|
|
4660
4812
|
/** Append the replacement template, expanded for the match in `mslots`, to `parts`. */
|
|
4661
|
-
expand(rep: string, text: string, ns: i32, parts: string[]): void {
|
|
4813
|
+
private expand(rep: string, text: string, ns: i32, parts: string[]): void {
|
|
4662
4814
|
const n: i32 = toI32(rep.length)
|
|
4663
4815
|
const groupsAfterZero: i32 = this.groups - 1
|
|
4664
4816
|
let lit: i32 = 0
|
|
@@ -4714,6 +4866,9 @@ export class Regex {
|
|
|
4714
4866
|
* part), an empty match where the last piece ended or at the end cuts
|
|
4715
4867
|
* nothing, and "" splits to nothing when the pattern matches it, else to
|
|
4716
4868
|
* `[""]`.
|
|
4869
|
+
*
|
|
4870
|
+
* @param text The text to cut.
|
|
4871
|
+
* @returns The pieces in order, each group's text after the piece before its match.
|
|
4717
4872
|
*/
|
|
4718
4873
|
split(text: string): string[] {
|
|
4719
4874
|
const len: i32 = toI32(text.length)
|
|
@@ -4755,6 +4910,10 @@ export class Regex {
|
|
|
4755
4910
|
* Compile `pattern` with `flags`, a string of `i`, `m`, `s` and `u` in any
|
|
4756
4911
|
* order; `u` changes nothing, as patterns are always read by code point.
|
|
4757
4912
|
* The error names what is wrong and its byte in the pattern.
|
|
4913
|
+
*
|
|
4914
|
+
* @param pattern The pattern, in JavaScript's syntax without the slashes.
|
|
4915
|
+
* @param flags Any of `i` (ignore case), `m` (`^` and `$` match at line ends), `s` (`.` matches a newline) and `u`, each at most once.
|
|
4916
|
+
* @returns The compiled pattern, or the reason it does not compile, such as "regex: missing ) at byte 0" or `regex: invalid flags "x"`.
|
|
4758
4917
|
*/
|
|
4759
4918
|
export const regexCompile = (pattern: string, flags: string): Result<Regex, string> => {
|
|
4760
4919
|
let icase: boolean = false
|
|
@@ -4794,7 +4953,13 @@ export const regexCompile = (pattern: string, flags: string): Result<Regex, stri
|
|
|
4794
4953
|
return Ok(re)
|
|
4795
4954
|
}
|
|
4796
4955
|
|
|
4797
|
-
/**
|
|
4956
|
+
/**
|
|
4957
|
+
* `text` with every syntax character escaped, so that it compiles to a
|
|
4958
|
+
* pattern matching exactly `text`.
|
|
4959
|
+
*
|
|
4960
|
+
* @param text The literal text to match.
|
|
4961
|
+
* @returns A pattern for `regexCompile`; `text` unchanged when it holds no syntax character.
|
|
4962
|
+
*/
|
|
4798
4963
|
export const regexEscape = (text: string): string => {
|
|
4799
4964
|
const parts: string[] = []
|
|
4800
4965
|
const n: i32 = toI32(text.length)
|