@amritk/nish-aarch64-linux 0.44.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
package/std/regex.ts CHANGED
@@ -272,19 +272,27 @@ const SET_SPACE: i32 = 2
272
272
  /** The highest code point. */
273
273
  const REGEX_MAX_CP: i32 = 0x10ffff
274
274
 
275
- /** The number `table` holds at `at`, four base-32 digits. */
275
+ /**
276
+ * The number `table` holds at `at`, four base-32 digits, `0` to `O`. Every
277
+ * `at` read is a record's start inside its generated table, so the 0 the
278
+ * range test answers outside one is never read; the masks keep a digit's
279
+ * value and bound the sum.
280
+ */
276
281
  const regexFixedAt = (table: string, at: i32): i32 => {
277
- let v: i32 = 0
278
- let i: i32 = 0
279
- while (i < 4) {
280
- v = v * 32 + toI32(table.charCodeAt(at + i)) - 48
281
- i += 1
282
- }
283
- return v
282
+ if (at < 0 || at > toI32(table.length) - 4) {
283
+ return 0
284
+ }
285
+ const d0: i32 = (toI32(table.charCodeAt(at)) - 48) & 31
286
+ const d1: i32 = (toI32(table.charCodeAt(at + 1)) - 48) & 31
287
+ const d2: i32 = (toI32(table.charCodeAt(at + 2)) - 48) & 31
288
+ const d3: i32 = (toI32(table.charCodeAt(at + 3)) - 48) & 31
289
+ return ((d0 * 32 + d1) * 32 + d2) * 32 + d3
284
290
  }
285
291
 
286
292
  /** The member a `REGEX_FOLD_DATA` step leads to from `c`: the step is a zigzag-coded difference, doubled, plus one in a pair. */
287
- const regexFoldTarget = (c: i32, step: i32): i32 => ((step & 2) === 2 ? c - (step >> 2) - 1 : c + (step >> 2))
293
+ const regexFoldTarget = (c: i32, step: i32): i32 =>
294
+ // Unsigned: a code point and a table's difference are below 2^21, so nothing wraps.
295
+ toI32((step & 2) === 2 ? toU32(c) - toU32(step >>> 2) - 1 : toU32(c) + toU32(step >>> 2))
288
296
 
289
297
  /** The code point `REGEX_FOLD_SPANS` holds at `at`. */
290
298
  const regexSpanAt = (at: i32): i32 => regexFixedAt(REGEX_FOLD_SPANS, at)
@@ -294,18 +302,20 @@ const regexFoldsIn = (lo: i32, hi: i32): boolean => {
294
302
  if (hi < regexSpanAt(0)) {
295
303
  return false
296
304
  }
297
- // The first run ending at or after `lo`, by binary search.
298
- let a: i32 = 0
299
- let b: i32 = toI32(REGEX_FOLD_SPANS.length) >> 3
305
+ // The first run ending at or after `lo`, by binary search. Unsigned: the
306
+ // run numbers are an eighth of the table's length, so no sum wraps.
307
+ const runs: u32 = toU32(REGEX_FOLD_SPANS.length) >>> 3
308
+ let a: u32 = 0
309
+ let b: u32 = runs
300
310
  while (a < b) {
301
- const mid: i32 = (a + b) >> 1
302
- if (regexSpanAt(mid * 8 + 4) < lo) {
311
+ const mid: u32 = (a + b) >>> 1
312
+ if (regexSpanAt(toI32(mid * 8 + 4)) < lo) {
303
313
  a = mid + 1
304
314
  } else {
305
315
  b = mid
306
316
  }
307
317
  }
308
- return a < toI32(REGEX_FOLD_SPANS.length) >> 3 && regexSpanAt(a * 8) <= hi
318
+ return a < runs && regexSpanAt(toI32(a * 8)) <= hi
309
319
  }
310
320
  /** A byte `b` that is not part of valid UTF-8 is the character `REGEX_INVALID + b`, which nothing written in a pattern equals. */
311
321
  const REGEX_INVALID: i32 = 0x110000
@@ -327,7 +337,7 @@ const isRegexWordByte = (b: i32): boolean =>
327
337
 
328
338
  /** Whether U+017F (C5 BF) or U+212A (E2 84 AA), word characters under `i`, ends at byte `p`. */
329
339
  const regexFoldWordBefore = (text: string, p: i32): boolean => {
330
- if (p < 2) {
340
+ if (p < 2 || p > toI32(text.length)) {
331
341
  return false
332
342
  }
333
343
  const b: i32 = toI32(text.charCodeAt(p - 1))
@@ -339,9 +349,10 @@ const regexFoldWordBefore = (text: string, p: i32): boolean => {
339
349
  )
340
350
  }
341
351
 
342
- /** Whether U+017F or U+212A starts at byte `p` of `text`, `len` bytes long. */
343
- const regexFoldWordAt = (text: string, p: i32, len: i32): boolean => {
344
- if (p + 1 >= len) {
352
+ /** Whether U+017F or U+212A starts at byte `p` of `text`. */
353
+ const regexFoldWordAt = (text: string, p: i32): boolean => {
354
+ const len: i32 = toI32(text.length)
355
+ if (p < 0 || p >= len - 1) {
345
356
  return false
346
357
  }
347
358
  const b: i32 = toI32(text.charCodeAt(p))
@@ -350,7 +361,7 @@ const regexFoldWordAt = (text: string, p: i32, len: i32): boolean => {
350
361
  }
351
362
  return (
352
363
  b === 0xe2 &&
353
- p + 2 < len &&
364
+ p < len - 2 &&
354
365
  toI32(text.charCodeAt(p + 1)) === 0x84 &&
355
366
  toI32(text.charCodeAt(p + 2)) === 0xaa
356
367
  )
@@ -359,7 +370,8 @@ const regexFoldWordAt = (text: string, p: i32, len: i32): boolean => {
359
370
  /** Where the value of `key` starts in `REGEX_PROPERTY_NAMES`, or -1 if it has none. */
360
371
  const regexPropertyEntry = (key: string): i32 => {
361
372
  const i: i32 = toI32(REGEX_PROPERTY_NAMES.indexOf(`|${key}=`))
362
- return i < 0 ? -1 : i + toI32(key.length) + 2
373
+ // Unsigned: the entry found ends inside the table, so the sum cannot wrap.
374
+ return i < 0 ? -1 : toI32(toU32(i) + toU32(key.length) + 2)
363
375
  }
364
376
 
365
377
  /** Whether every character of `s` is ASCII. */
@@ -390,16 +402,23 @@ const regexHexValue = (c: i32): i32 => {
390
402
 
391
403
  /**
392
404
  * The code point at byte `p` of `text`, packed as `cp * 8 + width`. `p` is
393
- * below `len`. A byte that does not start a valid UTF-8 sequence — a stray
405
+ * inside the text. A byte that does not start a valid UTF-8 sequence — a stray
394
406
  * continuation byte, an overlong form, a surrogate, a truncated sequence — is
395
407
  * one character, `REGEX_INVALID + byte`, of width 1.
396
408
  */
397
- const regexDecodeAt = (text: string, p: i32, len: i32): i32 => {
409
+ const regexDecodeAt = (text: string, p: i32): i32 => {
410
+ const len: i32 = toI32(text.length)
411
+ // Callers read inside the text; outside it there is no byte, which reads as
412
+ // the invalid character a byte above 0xff would be.
413
+ if (p < 0 || p >= len) {
414
+ return toI32((toU32(REGEX_INVALID) + 0x100) * 8 + 1)
415
+ }
398
416
  const b0: i32 = toI32(text.charCodeAt(p))
399
417
  if (b0 < 0x80) {
400
418
  return b0 * 8 + 1
401
419
  }
402
- const invalid: i32 = (REGEX_INVALID + (b0 & 0xff)) * 8 + 1
420
+ // Unsigned: below 0x110100 * 8, far from wrapping.
421
+ const invalid: i32 = toI32((toU32(REGEX_INVALID) + toU32(b0 & 0xff)) * 8 + 1)
403
422
  if (b0 < 0xc2 || b0 > 0xf4) {
404
423
  return invalid
405
424
  }
@@ -411,7 +430,7 @@ const regexDecodeAt = (text: string, p: i32, len: i32): i32 => {
411
430
  return invalid
412
431
  }
413
432
  if (b0 < 0xe0) {
414
- return (((b0 & 0x1f) << 6) | (b1 & 0x3f)) * 8 + 2
433
+ return ((b0 & 0x1f) * 64 + (b1 & 0x3f)) * 8 + 2
415
434
  }
416
435
  if (
417
436
  (b0 === 0xe0 && b1 < 0xa0) ||
@@ -429,7 +448,7 @@ const regexDecodeAt = (text: string, p: i32, len: i32): i32 => {
429
448
  return invalid
430
449
  }
431
450
  if (b0 < 0xf0) {
432
- return (((b0 & 0x0f) << 12) | ((b1 & 0x3f) << 6) | (b2 & 0x3f)) * 8 + 3
451
+ return (((b0 & 0x0f) * 64 + (b1 & 0x3f)) * 64 + (b2 & 0x3f)) * 8 + 3
433
452
  }
434
453
  if (p + 3 >= len) {
435
454
  return invalid
@@ -438,15 +457,15 @@ const regexDecodeAt = (text: string, p: i32, len: i32): i32 => {
438
457
  if ((b3 & 0xc0) !== 0x80) {
439
458
  return invalid
440
459
  }
441
- return (((b0 & 0x07) << 18) | ((b1 & 0x3f) << 12) | ((b2 & 0x3f) << 6) | (b3 & 0x3f)) * 8 + 4
460
+ return ((((b0 & 0x07) * 64 + (b1 & 0x3f)) * 64 + (b2 & 0x3f)) * 64 + (b3 & 0x3f)) * 8 + 4
442
461
  }
443
462
 
444
463
  /** How many bytes the character at `p` takes: 1 at or past the end. */
445
- const regexWidthAt = (text: string, p: i32, len: i32): i32 => {
446
- if (p >= len) {
464
+ const regexWidthAt = (text: string, p: i32): i32 => {
465
+ if (p >= toI32(text.length)) {
447
466
  return 1
448
467
  }
449
- return regexDecodeAt(text, p, len) & 7
468
+ return regexDecodeAt(text, p) & 7
450
469
  }
451
470
 
452
471
  /** Append the UTF-8 encoding of `cp` to `out`. */
@@ -532,6 +551,8 @@ class RegexParser {
532
551
  private qMax: i32
533
552
  /** Where `tableNumber` reads next. */
534
553
  private tablePos: i32
554
+ /** Where the digits `decimal` read end. */
555
+ private digitsEnd: i32
535
556
  /** The ranges property escapes and case folding have added, against `REGEX_MAX_PROPERTY_RANGES`. */
536
557
  private propertyRanges: i32
537
558
 
@@ -577,6 +598,7 @@ class RegexParser {
577
598
  this.qMin = 0
578
599
  this.qMax = 0
579
600
  this.tablePos = 0
601
+ this.digitsEnd = 0
580
602
  this.propertyRanges = 0
581
603
  }
582
604
 
@@ -590,8 +612,74 @@ class RegexParser {
590
612
  return -1
591
613
  }
592
614
 
615
+ /**
616
+ * Moves `pos` on `k` bytes. Unsigned: `pos` stays within a byte or two of
617
+ * the pattern, whose length is far below 2^31, and `k` is a few bytes.
618
+ */
619
+ private skip(k: i32): void {
620
+ this.pos = this.ahead(k)
621
+ }
622
+
623
+ /** `pos` plus `k`, unsigned for the same reason as `skip`. */
624
+ private ahead(k: i32): i32 {
625
+ return toI32(toU32(this.pos) + toU32(k))
626
+ }
627
+
628
+ /** The pattern's byte at `i`, or -1 past either end, which no byte test matches. */
593
629
  private byteAt(i: i32): i32 {
594
- return toI32(this.src.charCodeAt(i))
630
+ const s: string = this.src
631
+ if (i < 0 || i >= toI32(s.length)) {
632
+ return -1
633
+ }
634
+ return toI32(s.charCodeAt(i))
635
+ }
636
+
637
+ // The node readers below answer for ids `node` handed out, which are all
638
+ // any caller passes: the answer outside the tree (an empty node with no
639
+ // children) is never read, and the test is what proves the index.
640
+
641
+ /** @internal Node `n`'s kind. */
642
+ kindOf(n: i32): i32 {
643
+ return regexItem(this.kind, n, NODE_EMPTY)
644
+ }
645
+ /** @internal Node `n`'s first operand. */
646
+ aOf(n: i32): i32 {
647
+ return regexItem(this.na, n, REGEX_NIL)
648
+ }
649
+ /** @internal Node `n`'s second operand. */
650
+ bOf(n: i32): i32 {
651
+ return regexItem(this.nb, n, REGEX_NIL)
652
+ }
653
+ /** @internal Repeat `n`'s greediness, 1 for greedy. */
654
+ cOf(n: i32): i32 {
655
+ return regexItem(this.nc, n, REGEX_NIL)
656
+ }
657
+ /** @internal The first group inside repeat `n`. */
658
+ dOf(n: i32): i32 {
659
+ return regexItem(this.nd, n, REGEX_NIL)
660
+ }
661
+ /** @internal The first group after repeat `n`. */
662
+ eOf(n: i32): i32 {
663
+ return regexItem(this.ne, n, REGEX_NIL)
664
+ }
665
+ /** @internal Node `n`'s first child, or -1. */
666
+ kidOf(n: i32): i32 {
667
+ return regexItem(this.kid, n, REGEX_NONE)
668
+ }
669
+ /** @internal Node `n`'s next sibling, or -1. */
670
+ nextOf(n: i32): i32 {
671
+ return regexItem(this.next, n, REGEX_NONE)
672
+ }
673
+ private setKid(n: i32, kid: i32): void {
674
+ regexPut(this.kid, n, kid)
675
+ }
676
+ private setNext(n: i32, next: i32): void {
677
+ regexPut(this.next, n, next)
678
+ }
679
+ private setRepeat(n: i32, greedy: i32, first: i32, after: i32): void {
680
+ regexPut(this.nc, n, greedy)
681
+ regexPut(this.nd, n, first)
682
+ regexPut(this.ne, n, after)
595
683
  }
596
684
 
597
685
  private node(kind: i32, a: i32, b: i32): i32 {
@@ -641,15 +729,24 @@ class RegexParser {
641
729
 
642
730
  /** The number at `tablePos` in `table`, a generated table's base-32 digits, `tablePos` moved past it. */
643
731
  private tableNumber(table: string): i32 {
644
- let v: i32 = 0
645
- let d: i32 = toI32(table.charCodeAt(this.tablePos)) - 48
646
- this.tablePos += 1
647
- while (d >= 32) {
648
- v = v * 32 + d - 32
649
- d = toI32(table.charCodeAt(this.tablePos)) - 48
650
- this.tablePos += 1
651
- }
652
- return v * 32 + d
732
+ const n: i32 = toI32(table.length)
733
+ let p: i32 = this.tablePos
734
+ // Unsigned: the generated numbers are below 2^21, so nothing wraps.
735
+ let v: u32 = 0
736
+ // A digit of 32 or more says another follows. The generator ends every
737
+ // number inside its table, so the bound never stops the loop and the
738
+ // partial number after it is never read.
739
+ while (p >= 0 && p < n) {
740
+ const d: i32 = toI32(table.charCodeAt(p)) - 48
741
+ p += 1
742
+ if (d < 32) {
743
+ this.tablePos = p
744
+ return toI32(v * 32 + toU32(d))
745
+ }
746
+ v = v * 32 + toU32(d) - 32
747
+ }
748
+ this.tablePos = p
749
+ return toI32(v)
653
750
  }
654
751
 
655
752
  /**
@@ -658,18 +755,20 @@ class RegexParser {
658
755
  * member's step.
659
756
  */
660
757
  private foldSeek(cp: i32): i32 {
661
- let a: i32 = 0
662
- let b: i32 = toI32(REGEX_FOLD_INDEX.length) >> 3
758
+ // Unsigned: the block numbers are an eighth of the table's length, and
759
+ // `a` stays below `b`, so nothing wraps.
760
+ let a: u32 = 0
761
+ let b: u32 = toU32(REGEX_FOLD_INDEX.length) >>> 3
663
762
  while (b - a > 1) {
664
- const mid: i32 = (a + b) >> 1
665
- if (regexFixedAt(REGEX_FOLD_INDEX, mid * 8) <= cp) {
763
+ const mid: u32 = (a + b) >>> 1
764
+ if (regexFixedAt(REGEX_FOLD_INDEX, toI32(mid * 8)) <= cp) {
666
765
  a = mid
667
766
  } else {
668
767
  b = mid
669
768
  }
670
769
  }
671
- this.tablePos = regexFixedAt(REGEX_FOLD_INDEX, a * 8 + 4)
672
- return regexFixedAt(REGEX_FOLD_INDEX, a * 8)
770
+ this.tablePos = regexFixedAt(REGEX_FOLD_INDEX, toI32(a * 8 + 4))
771
+ return regexFixedAt(REGEX_FOLD_INDEX, toI32(a * 8))
673
772
  }
674
773
 
675
774
  /** `cp`'s step in `REGEX_FOLD_DATA`, for `regexFoldTarget`, or -1 if it folds to nothing. */
@@ -679,7 +778,8 @@ class RegexParser {
679
778
  while (c < cp && this.tablePos < end) {
680
779
  this.tableNumber(REGEX_FOLD_DATA)
681
780
  if (this.tablePos < end) {
682
- c += this.tableNumber(REGEX_FOLD_DATA)
781
+ // Unsigned: code points and their gaps are below 2^21.
782
+ c = toI32(toU32(c) + toU32(this.tableNumber(REGEX_FOLD_DATA)))
683
783
  }
684
784
  }
685
785
  return c === cp && this.tablePos < end ? this.tableNumber(REGEX_FOLD_DATA) : -1
@@ -698,15 +798,15 @@ class RegexParser {
698
798
  return first
699
799
  }
700
800
  const alt: i32 = this.node(NODE_ALT, REGEX_NIL, REGEX_NIL)
701
- this.kid[alt] = first
801
+ this.setKid(alt, first)
702
802
  let last: i32 = first
703
803
  while (this.pos < this.len && this.byteAt(this.pos) === 124) {
704
- this.pos += 1
804
+ this.skip(toI32(1))
705
805
  const n: i32 = this.parseSeq(depth)
706
806
  if (this.failed) {
707
807
  return -1
708
808
  }
709
- this.next[last] = n
809
+ this.setNext(last, n)
710
810
  last = n
711
811
  }
712
812
  return alt
@@ -716,7 +816,9 @@ class RegexParser {
716
816
  private parseSeq(depth: i32): i32 {
717
817
  let first: i32 = -1
718
818
  let last: i32 = -1
719
- let count: i32 = 0
819
+ // Unsigned: every term reads at least a byte of the pattern, so there
820
+ // are fewer of them than 2^31.
821
+ let count: u32 = 0
720
822
  while (this.pos < this.len) {
721
823
  const c: i32 = this.byteAt(this.pos)
722
824
  if (c === 124 || c === 41) {
@@ -729,7 +831,7 @@ class RegexParser {
729
831
  if (first === -1) {
730
832
  first = t
731
833
  } else {
732
- this.next[last] = t
834
+ this.setNext(last, t)
733
835
  }
734
836
  last = t
735
837
  count += 1
@@ -741,7 +843,7 @@ class RegexParser {
741
843
  return first
742
844
  }
743
845
  const cat: i32 = this.node(NODE_CAT, REGEX_NIL, REGEX_NIL)
744
- this.kid[cat] = first
846
+ this.setKid(cat, first)
745
847
  return cat
746
848
  }
747
849
 
@@ -758,14 +860,14 @@ class RegexParser {
758
860
  let max: i32 = 0
759
861
  if (c === 42) {
760
862
  max = -1
761
- this.pos += 1
863
+ this.skip(toI32(1))
762
864
  } else if (c === 43) {
763
865
  min = 1
764
866
  max = -1
765
- this.pos += 1
867
+ this.skip(toI32(1))
766
868
  } else if (c === 63) {
767
869
  max = 1
768
- this.pos += 1
870
+ this.skip(toI32(1))
769
871
  } else if (c === 123 && this.parseBraces()) {
770
872
  if (this.failed) {
771
873
  return -1
@@ -777,13 +879,13 @@ class RegexParser {
777
879
  }
778
880
  // A bare `^`, `$`, `\b` or `\B` takes no quantifier, as in JavaScript,
779
881
  // but one in a group does: `(?:^)?` is an assertion node too.
780
- if (this.kind[atom] === NODE_ASSERT && this.byteAt(atomAt) !== 40) {
882
+ if (this.kindOf(atom) === NODE_ASSERT && this.byteAt(atomAt) !== 40) {
781
883
  return this.fail("nothing to repeat", atomAt)
782
884
  }
783
885
  let greedy: i32 = 1
784
886
  if (this.pos < this.len && this.byteAt(this.pos) === 63) {
785
887
  greedy = 0
786
- this.pos += 1
888
+ this.skip(toI32(1))
787
889
  }
788
890
  if (this.pos < this.len) {
789
891
  const at: i32 = this.pos
@@ -793,10 +895,8 @@ class RegexParser {
793
895
  }
794
896
  }
795
897
  const r: i32 = this.node(NODE_REPEAT, min, max)
796
- this.nc[r] = greedy
797
- this.nd[r] = groupsBefore
798
- this.ne[r] = this.groups
799
- this.kid[r] = atom
898
+ this.setRepeat(r, greedy, groupsBefore, this.groups)
899
+ this.setKid(r, atom)
800
900
  return r
801
901
  }
802
902
 
@@ -806,36 +906,21 @@ class RegexParser {
806
906
  * leaves the cursor where it was.
807
907
  */
808
908
  private parseBraces(): boolean {
809
- let i: i32 = this.pos + 1
909
+ let i: i32 = this.ahead(toI32(1))
810
910
  const start: i32 = this.pos
811
- let min: i32 = 0
812
- let digits: i32 = 0
813
- while (i < this.len && this.byteAt(i) >= 48 && this.byteAt(i) <= 57) {
814
- if (min <= REGEX_MAX_REPEAT) {
815
- min = min * 10 + (this.byteAt(i) - 48)
816
- }
817
- digits += 1
818
- i += 1
819
- }
820
- if (digits === 0 || i >= this.len) {
911
+ const minAt: i32 = i
912
+ const min: i32 = this.decimal(i)
913
+ i = this.digitsEnd
914
+ if (i === minAt || i >= this.len) {
821
915
  return false
822
916
  }
823
917
  let max: i32 = min
824
918
  if (this.byteAt(i) === 44) {
825
919
  i += 1
826
- max = -1
827
- let maxDigits: i32 = 0
828
- let m: i32 = 0
829
- while (i < this.len && this.byteAt(i) >= 48 && this.byteAt(i) <= 57) {
830
- if (m <= REGEX_MAX_REPEAT) {
831
- m = m * 10 + (this.byteAt(i) - 48)
832
- }
833
- maxDigits += 1
834
- i += 1
835
- }
836
- if (maxDigits > 0) {
837
- max = m
838
- }
920
+ const maxAt: i32 = i
921
+ const m: i32 = this.decimal(i)
922
+ i = this.digitsEnd
923
+ max = i > maxAt ? m : -1
839
924
  }
840
925
  if (i >= this.len || this.byteAt(i) !== 125) {
841
926
  return false
@@ -854,6 +939,28 @@ class RegexParser {
854
939
  return true
855
940
  }
856
941
 
942
+ /**
943
+ * The decimal number at `i`, with `digitsEnd` left past its digits. It
944
+ * stops growing once over `REGEX_MAX_REPEAT`, which is all its callers
945
+ * need to know of a larger one.
946
+ */
947
+ private decimal(i: i32): i32 {
948
+ // Unsigned: the number stops growing at 10 times the limit plus 9,
949
+ // and `i` stays within the pattern.
950
+ let v: u32 = 0
951
+ let p: u32 = toU32(i)
952
+ let d: i32 = this.byteAt(toI32(p))
953
+ while (d >= 48 && d <= 57) {
954
+ if (v <= toU32(REGEX_MAX_REPEAT)) {
955
+ v = v * 10 + toU32(d - 48)
956
+ }
957
+ p += 1
958
+ d = this.byteAt(toI32(p))
959
+ }
960
+ this.digitsEnd = toI32(p)
961
+ return toI32(v)
962
+ }
963
+
857
964
  private parseAtom(depth: i32): i32 {
858
965
  const c: i32 = this.byteAt(this.pos)
859
966
  if (c === 40) {
@@ -863,15 +970,15 @@ class RegexParser {
863
970
  return this.parseClass()
864
971
  }
865
972
  if (c === 46) {
866
- this.pos += 1
973
+ this.skip(toI32(1))
867
974
  return this.node(NODE_ANY, REGEX_NIL, REGEX_NIL)
868
975
  }
869
976
  if (c === 94) {
870
- this.pos += 1
977
+ this.skip(toI32(1))
871
978
  return this.node(NODE_ASSERT, this.multiline ? ASSERT_BEGIN_LINE : ASSERT_BEGIN_TEXT, REGEX_NIL)
872
979
  }
873
980
  if (c === 36) {
874
- this.pos += 1
981
+ this.skip(toI32(1))
875
982
  return this.node(NODE_ASSERT, this.multiline ? ASSERT_END_LINE : ASSERT_END_TEXT, REGEX_NIL)
876
983
  }
877
984
  if (c === 92) {
@@ -893,32 +1000,32 @@ class RegexParser {
893
1000
  if (c === 93) {
894
1001
  return this.fail("lone ]", this.pos)
895
1002
  }
896
- const packed: i32 = regexDecodeAt(this.src, this.pos, this.len)
1003
+ const packed: i32 = regexDecodeAt(this.src, this.pos)
897
1004
  if (packed >> 3 >= REGEX_INVALID) {
898
1005
  return this.fail("the pattern is not valid UTF-8", this.pos)
899
1006
  }
900
- this.pos += packed & 7
1007
+ this.skip(packed & 7)
901
1008
  return this.charNode(packed >> 3)
902
1009
  }
903
1010
 
904
1011
  private parseGroup(depth: i32): i32 {
905
1012
  const open: i32 = this.pos
906
- this.pos += 1
1013
+ this.skip(toI32(1))
907
1014
  let capture: boolean = true
908
1015
  let name: string = ""
909
1016
  if (this.pos < this.len && this.byteAt(this.pos) === 63) {
910
- const k: i32 = this.pos + 1 < this.len ? this.byteAt(this.pos + 1) : -1
1017
+ const k: i32 = this.byteAt(this.ahead(toI32(1)))
911
1018
  if (k === 58) {
912
1019
  capture = false
913
- this.pos += 2
1020
+ this.skip(toI32(2))
914
1021
  } else if (k === 61 || k === 33) {
915
1022
  return this.fail("lookahead is not supported", open)
916
1023
  } else if (k === 60) {
917
- const k2: i32 = this.pos + 2 < this.len ? this.byteAt(this.pos + 2) : -1
1024
+ const k2: i32 = this.byteAt(this.ahead(toI32(2)))
918
1025
  if (k2 === 61 || k2 === 33) {
919
1026
  return this.fail("lookbehind is not supported", open)
920
1027
  }
921
- const nameAt: i32 = this.pos + 2
1028
+ const nameAt: i32 = this.ahead(toI32(2))
922
1029
  let i: i32 = nameAt
923
1030
  while (i < this.len && this.byteAt(i) !== 62) {
924
1031
  const b: i32 = this.byteAt(i)
@@ -931,7 +1038,7 @@ class RegexParser {
931
1038
  if (i >= this.len || i === nameAt) {
932
1039
  return this.fail("invalid group name", nameAt)
933
1040
  }
934
- name = this.src.slice(nameAt, i)
1041
+ name = regexSpan(this.src, nameAt, i)
935
1042
  if (this.names.indexOf(name) >= 0) {
936
1043
  return this.fail("duplicate group name", nameAt)
937
1044
  }
@@ -946,38 +1053,40 @@ class RegexParser {
946
1053
  return this.fail("too many groups", open)
947
1054
  }
948
1055
  index = this.groups
949
- this.groups += 1
1056
+ // Unsigned: the test above stops the count past the limit.
1057
+ this.groups = toI32(toU32(index) + 1)
950
1058
  this.names.push(name)
951
1059
  if (name !== "") {
952
1060
  this.hasNames = true
953
1061
  }
954
1062
  }
955
- const inner: i32 = this.parseAlt(depth + 1)
1063
+ // Unsigned: `parseAlt` fails past `REGEX_MAX_DEPTH`, so nothing wraps.
1064
+ const inner: i32 = this.parseAlt(toI32(toU32(depth) + 1))
956
1065
  if (this.failed) {
957
1066
  return -1
958
1067
  }
959
1068
  if (this.pos >= this.len || this.byteAt(this.pos) !== 41) {
960
1069
  return this.fail("missing )", open)
961
1070
  }
962
- this.pos += 1
1071
+ this.skip(toI32(1))
963
1072
  if (!capture) {
964
1073
  return inner
965
1074
  }
966
1075
  const g: i32 = this.node(NODE_GROUP, index, REGEX_NIL)
967
- this.kid[g] = inner
1076
+ this.setKid(g, inner)
968
1077
  return g
969
1078
  }
970
1079
 
971
1080
  /** A `\` outside a class. */
972
1081
  private parseEscape(): i32 {
973
1082
  const at: i32 = this.pos
974
- this.pos += 1
1083
+ this.skip(toI32(1))
975
1084
  if (this.pos >= this.len) {
976
1085
  return this.fail("\\ at end of pattern", at)
977
1086
  }
978
1087
  const c: i32 = this.byteAt(this.pos)
979
1088
  if (c === 100 || c === 68 || c === 119 || c === 87 || c === 115 || c === 83) {
980
- this.pos += 1
1089
+ this.skip(toI32(1))
981
1090
  this.tmpCount = 0
982
1091
  this.addSet(c)
983
1092
  // `\d`, `\w` and `\s` and their capitals are closed under case
@@ -1000,11 +1109,11 @@ class RegexParser {
1000
1109
  return this.finishClass(false)
1001
1110
  }
1002
1111
  if (c === 98) {
1003
- this.pos += 1
1112
+ this.skip(toI32(1))
1004
1113
  return this.node(NODE_ASSERT, this.icase ? ASSERT_WORD_FOLD : ASSERT_WORD, REGEX_NIL)
1005
1114
  }
1006
1115
  if (c === 66) {
1007
- this.pos += 1
1116
+ this.skip(toI32(1))
1008
1117
  return this.node(NODE_ASSERT, this.icase ? ASSERT_NOT_WORD_FOLD : ASSERT_NOT_WORD, REGEX_NIL)
1009
1118
  }
1010
1119
  if ((c >= 49 && c <= 57) || c === 107) {
@@ -1023,9 +1132,10 @@ class RegexParser {
1023
1132
  * backspace and `\-` a hyphen.
1024
1133
  */
1025
1134
  private parseCharEscape(inClass: boolean): i32 {
1026
- const at: i32 = this.pos - 1
1135
+ // The cursor is past the backslash, so this is at least 0.
1136
+ const at: i32 = toI32(toU32(this.pos) - 1)
1027
1137
  const c: i32 = this.byteAt(this.pos)
1028
- this.pos += 1
1138
+ this.skip(toI32(1))
1029
1139
  if (c === 110) {
1030
1140
  return 10
1031
1141
  }
@@ -1051,7 +1161,7 @@ class RegexParser {
1051
1161
  if (this.pos < this.len) {
1052
1162
  const l: i32 = this.byteAt(this.pos)
1053
1163
  if ((l >= 97 && l <= 122) || (l >= 65 && l <= 90)) {
1054
- this.pos += 1
1164
+ this.skip(toI32(1))
1055
1165
  return l % 32
1056
1166
  }
1057
1167
  }
@@ -1098,40 +1208,47 @@ class RegexParser {
1098
1208
 
1099
1209
  /** Exactly `n` hex digits at the cursor, or -1 with the cursor left. */
1100
1210
  private parseHex(n: i32): i32 {
1101
- if (this.pos + n > this.len) {
1211
+ if (this.ahead(n) > this.len) {
1102
1212
  return -1
1103
1213
  }
1104
- let v: i32 = 0
1214
+ // Unsigned: `n` is at most four digits, so `v` stays below 2^16.
1215
+ let v: u32 = 0
1105
1216
  let i: i32 = 0
1106
1217
  while (i < n) {
1107
- const h: i32 = regexHexValue(this.byteAt(this.pos + i))
1218
+ const h: i32 = regexHexValue(this.byteAt(this.ahead(i)))
1108
1219
  if (h < 0) {
1109
1220
  return -1
1110
1221
  }
1111
- v = v * 16 + h
1222
+ v = v * 16 + toU32(h)
1112
1223
  i += 1
1113
1224
  }
1114
- this.pos += n
1115
- return v
1225
+ this.skip(n)
1226
+ return toI32(v)
1116
1227
  }
1117
1228
 
1118
1229
  /** `\uHHHH`, a surrogate pair of them, or `\u{H…}`, the cursor after the `u`. */
1119
1230
  private parseUnicodeEscape(at: i32): i32 {
1120
1231
  if (this.pos < this.len && this.byteAt(this.pos) === 123) {
1121
- let i: i32 = this.pos + 1
1122
- let v: i32 = 0
1123
- while (i < this.len && regexHexValue(this.byteAt(i)) >= 0) {
1124
- v = v * 16 + regexHexValue(this.byteAt(i))
1125
- if (v > REGEX_MAX_CP) {
1232
+ let i: i32 = this.ahead(toI32(1))
1233
+ // Unsigned: the test fails the escape once `v` passes the last code
1234
+ // point, so it stays below 2^25.
1235
+ let v: u32 = 0
1236
+ while (i < this.len) {
1237
+ const h: i32 = regexHexValue(this.byteAt(i))
1238
+ if (h < 0) {
1239
+ break
1240
+ }
1241
+ v = v * 16 + toU32(h)
1242
+ if (v > toU32(REGEX_MAX_CP)) {
1126
1243
  return this.fail("invalid \\u escape", at)
1127
1244
  }
1128
1245
  i += 1
1129
1246
  }
1130
- if (i === this.pos + 1 || i >= this.len || this.byteAt(i) !== 125) {
1247
+ if (i === this.ahead(toI32(1)) || i >= this.len || this.byteAt(i) !== 125) {
1131
1248
  return this.fail("invalid \\u escape", at)
1132
1249
  }
1133
1250
  this.pos = i + 1
1134
- return v
1251
+ return toI32(v)
1135
1252
  }
1136
1253
  const v: i32 = this.parseHex(REGEX_HEX_UNIT)
1137
1254
  if (v < 0) {
@@ -1140,15 +1257,15 @@ class RegexParser {
1140
1257
  if (
1141
1258
  v >= 0xd800 &&
1142
1259
  v <= 0xdbff &&
1143
- this.pos + 6 <= this.len &&
1260
+ this.ahead(toI32(6)) <= this.len &&
1144
1261
  this.byteAt(this.pos) === 92 &&
1145
- this.byteAt(this.pos + 1) === 117
1262
+ this.byteAt(this.ahead(toI32(1))) === 117
1146
1263
  ) {
1147
1264
  const save: i32 = this.pos
1148
- this.pos += 2
1265
+ this.skip(toI32(2))
1149
1266
  const lo: i32 = this.parseHex(REGEX_HEX_UNIT)
1150
1267
  if (lo >= 0xdc00 && lo <= 0xdfff) {
1151
- return ((v - 0xd800) << 10) + (lo - 0xdc00) + 0x10000
1268
+ return toI32(((toU32(v) - 0xd800) << 10) + (toU32(lo) - 0xdc00) + 0x10000)
1152
1269
  }
1153
1270
  this.pos = save
1154
1271
  }
@@ -1156,14 +1273,20 @@ class RegexParser {
1156
1273
  }
1157
1274
 
1158
1275
  private addRange(lo: i32, hi: i32): void {
1159
- if (this.tmpCount < toI32(this.tmpLo.length)) {
1160
- this.tmpLo[this.tmpCount] = lo
1161
- this.tmpHi[this.tmpCount] = hi
1276
+ const n: i32 = this.tmpCount
1277
+ const los: i32[] = this.tmpLo
1278
+ const his: i32[] = this.tmpHi
1279
+ // The two lists grow together and `n` never passes them.
1280
+ if (n >= 0 && n < toI32(los.length) && n < toI32(his.length)) {
1281
+ los[n] = lo
1282
+ his[n] = hi
1162
1283
  } else {
1163
- this.tmpLo.push(lo)
1164
- this.tmpHi.push(hi)
1284
+ los.push(lo)
1285
+ his.push(hi)
1165
1286
  }
1166
- this.tmpCount += 1
1287
+ // Unsigned: each range has its slot in the lists, which hold far fewer
1288
+ // than 2^31.
1289
+ this.tmpCount = toI32(toU32(n) + 1)
1167
1290
  }
1168
1291
 
1169
1292
  /**
@@ -1173,7 +1296,8 @@ class RegexParser {
1173
1296
  */
1174
1297
  private addSet(letter: i32): void {
1175
1298
  const negate: boolean = letter === 68 || letter === 87 || letter === 83
1176
- const lower: i32 = negate ? letter + 32 : letter
1299
+ // Unsigned: a capital letter's code is far from wrapping.
1300
+ const lower: i32 = negate ? toI32(toU32(letter) + 32) : letter
1177
1301
  let set: i32 = SET_SPACE
1178
1302
  if (lower === 100) {
1179
1303
  set = SET_DIGIT
@@ -1224,24 +1348,27 @@ class RegexParser {
1224
1348
  r.push(0xfeff)
1225
1349
  r.push(0xfeff)
1226
1350
  }
1227
- if (!negate) {
1228
- let i: i32 = 0
1229
- while (i >= 0 && i + 1 < toI32(r.length)) {
1230
- this.addRange(r[i], r[i + 1])
1231
- i += 2
1232
- }
1233
- return
1234
- }
1351
+ // The pairs below are code points, so the unsigned steps between them
1352
+ // never wrap.
1235
1353
  let from: i32 = 0
1236
1354
  let i: i32 = 0
1237
- while (i >= 0 && i + 1 < toI32(r.length)) {
1355
+ while (i >= 0 && i < toI32(r.length)) {
1238
1356
  const lo: i32 = r[i]
1239
- const hi: i32 = r[i + 1]
1240
- if (lo > from) {
1241
- this.addRange(from, lo - 1)
1357
+ const at: i32 = i + 1
1358
+ if (at >= toI32(r.length)) {
1359
+ break
1242
1360
  }
1243
- from = hi + 1
1244
- i += 2
1361
+ const hi: i32 = r[at]
1362
+ if (!negate) {
1363
+ this.addRange(lo, hi)
1364
+ } else if (lo > from) {
1365
+ this.addRange(from, toI32(toU32(lo) - 1))
1366
+ }
1367
+ from = toI32(toU32(hi) + 1)
1368
+ i = at + 1
1369
+ }
1370
+ if (!negate) {
1371
+ return
1245
1372
  }
1246
1373
  if (from <= REGEX_MAX_CP) {
1247
1374
  this.addRange(from, REGEX_MAX_CP)
@@ -1256,12 +1383,12 @@ class RegexParser {
1256
1383
  * `sc`, `scx`) `=` a value, spelled as ECMA-262 spells them.
1257
1384
  */
1258
1385
  private addProperty(at: i32, negate: boolean): void {
1259
- this.pos += 1
1386
+ this.skip(toI32(1))
1260
1387
  if (this.pos >= this.len || this.byteAt(this.pos) !== 123) {
1261
1388
  this.fail("invalid property name", at)
1262
1389
  return
1263
1390
  }
1264
- const start: i32 = this.pos + 1
1391
+ const start: i32 = this.ahead(toI32(1))
1265
1392
  let eq: i32 = -1
1266
1393
  let i: i32 = start
1267
1394
  while (i < this.len && this.byteAt(i) !== 125) {
@@ -1280,7 +1407,7 @@ class RegexParser {
1280
1407
  this.pos = i + 1
1281
1408
  let entry: i32 = -1
1282
1409
  if (eq < 0) {
1283
- const name: string = this.src.slice(start, i)
1410
+ const name: string = regexSpan(this.src, start, i)
1284
1411
  entry = regexPropertyEntry(`g${name}`)
1285
1412
  if (entry < 0) {
1286
1413
  entry = regexPropertyEntry(`b${name}`)
@@ -1290,8 +1417,8 @@ class RegexParser {
1290
1417
  return
1291
1418
  }
1292
1419
  } else {
1293
- const name: string = this.src.slice(start, eq)
1294
- const value: string = this.src.slice(eq + 1, i)
1420
+ const name: string = regexSpan(this.src, start, eq)
1421
+ const value: string = regexSpan(this.src, toI32(toU32(eq) + 1), i)
1295
1422
  const script: boolean = name === "Script" || name === "sc"
1296
1423
  const extensions: boolean = name === "Script_Extensions" || name === "scx"
1297
1424
  if (name === "General_Category" || name === "gc") {
@@ -1308,49 +1435,54 @@ class RegexParser {
1308
1435
  }
1309
1436
  // A script's entry is its Script list, a comma, then its Script_Extensions list.
1310
1437
  if (extensions) {
1311
- entry = toI32(REGEX_PROPERTY_NAMES.indexOf(",", entry)) + 1
1438
+ entry = toI32(toU32(REGEX_PROPERTY_NAMES.indexOf(",", entry)) + 1)
1312
1439
  }
1313
1440
  }
1314
1441
  // An entry is the offset of its list in `REGEX_PROPERTY_DATA`, after a `~` for its complement.
1442
+ // Unsigned below: entries and offsets index the generated tables, and
1443
+ // the ranges are code points, all far below 2^31.
1315
1444
  let gaps: boolean = negate
1316
- if (toI32(REGEX_PROPERTY_NAMES.charCodeAt(entry)) === 126) {
1445
+ let u: u32 = toU32(entry)
1446
+ if (regexByteAt(REGEX_PROPERTY_NAMES, toI32(u)) === 126) {
1317
1447
  gaps = !gaps
1318
- entry += 1
1448
+ u += 1
1319
1449
  }
1320
- let offset: i32 = 0
1321
- while (
1322
- toI32(REGEX_PROPERTY_NAMES.charCodeAt(entry)) >= 48 &&
1323
- toI32(REGEX_PROPERTY_NAMES.charCodeAt(entry)) <= 57
1324
- ) {
1325
- offset = offset * 10 + toI32(REGEX_PROPERTY_NAMES.charCodeAt(entry)) - 48
1326
- entry += 1
1450
+ let offset: u32 = 0
1451
+ let d: i32 = regexByteAt(REGEX_PROPERTY_NAMES, toI32(u))
1452
+ while (d >= 48 && d <= 57) {
1453
+ offset = offset * 10 + toU32(d - 48)
1454
+ u += 1
1455
+ d = regexByteAt(REGEX_PROPERTY_NAMES, toI32(u))
1327
1456
  }
1328
- this.tablePos = offset
1457
+ this.tablePos = toI32(offset)
1329
1458
  const n: i32 = this.tableNumber(REGEX_PROPERTY_DATA)
1330
1459
  const before: i32 = this.tmpCount
1331
- let from: i32 = 0
1332
- let hi: i32 = -2
1460
+ let from: u32 = 0
1461
+ let hi: u32 = 0
1333
1462
  let k: i32 = 0
1334
1463
  while (k < n) {
1335
- const lo: i32 = hi + 2 + this.tableNumber(REGEX_PROPERTY_DATA)
1336
- hi = lo + this.tableNumber(REGEX_PROPERTY_DATA)
1464
+ // The first gap counts from -2, so that `hi + 2` starts at 0.
1465
+ const lo: u32 = (k === 0 ? toU32(0) : hi + 2) + toU32(this.tableNumber(REGEX_PROPERTY_DATA))
1466
+ hi = lo + toU32(this.tableNumber(REGEX_PROPERTY_DATA))
1337
1467
  if (!gaps) {
1338
- this.addRange(lo, hi)
1468
+ this.addRange(toI32(lo), toI32(hi))
1339
1469
  } else if (lo > from) {
1340
- this.addRange(from, lo - 1)
1470
+ this.addRange(toI32(from), toI32(lo - 1))
1341
1471
  }
1342
1472
  from = hi + 1
1343
1473
  k += 1
1344
1474
  }
1345
- if (gaps && from <= REGEX_MAX_CP) {
1346
- this.addRange(from, REGEX_MAX_CP)
1475
+ if (gaps && from <= toU32(REGEX_MAX_CP)) {
1476
+ this.addRange(toI32(from), REGEX_MAX_CP)
1347
1477
  }
1348
- this.countPropertyRanges(this.tmpCount - before, at)
1478
+ this.countPropertyRanges(toI32(toU32(this.tmpCount) - toU32(before)), at)
1349
1479
  }
1350
1480
 
1351
1481
  /** Count `n` more ranges against `REGEX_MAX_PROPERTY_RANGES`, failing at `at` past it. */
1352
1482
  private countPropertyRanges(n: i32, at: i32): void {
1353
- this.propertyRanges += n
1483
+ // Unsigned: the count fails the parse once past its limit, and `n` is
1484
+ // the ranges one escape just added.
1485
+ this.propertyRanges = toI32(toU32(this.propertyRanges) + toU32(n))
1354
1486
  if (this.propertyRanges > REGEX_MAX_PROPERTY_RANGES) {
1355
1487
  this.fail("property escapes and case folding add more than 100000 ranges", at)
1356
1488
  }
@@ -1361,13 +1493,13 @@ class RegexParser {
1361
1493
  const c: i32 = this.byteAt(this.pos)
1362
1494
  if (c === 92) {
1363
1495
  const at: i32 = this.pos
1364
- this.pos += 1
1496
+ this.skip(toI32(1))
1365
1497
  if (this.pos >= this.len) {
1366
1498
  return this.fail("\\ at end of pattern", at)
1367
1499
  }
1368
1500
  const e: i32 = this.byteAt(this.pos)
1369
1501
  if (e === 100 || e === 68 || e === 119 || e === 87 || e === 115 || e === 83) {
1370
- this.pos += 1
1502
+ this.skip(toI32(1))
1371
1503
  this.addSet(e)
1372
1504
  return -2
1373
1505
  }
@@ -1380,22 +1512,22 @@ class RegexParser {
1380
1512
  }
1381
1513
  return this.parseCharEscape(true)
1382
1514
  }
1383
- const packed: i32 = regexDecodeAt(this.src, this.pos, this.len)
1515
+ const packed: i32 = regexDecodeAt(this.src, this.pos)
1384
1516
  if (packed >> 3 >= REGEX_INVALID) {
1385
1517
  return this.fail("the pattern is not valid UTF-8", this.pos)
1386
1518
  }
1387
- this.pos += packed & 7
1519
+ this.skip(packed & 7)
1388
1520
  return packed >> 3
1389
1521
  }
1390
1522
 
1391
1523
  /** `[…]` or `[^…]`. */
1392
1524
  private parseClass(): i32 {
1393
1525
  const open: i32 = this.pos
1394
- this.pos += 1
1526
+ this.skip(toI32(1))
1395
1527
  let negated: boolean = false
1396
1528
  if (this.pos < this.len && this.byteAt(this.pos) === 94) {
1397
1529
  negated = true
1398
- this.pos += 1
1530
+ this.skip(toI32(1))
1399
1531
  }
1400
1532
  this.tmpCount = 0
1401
1533
  while (true) {
@@ -1403,7 +1535,7 @@ class RegexParser {
1403
1535
  return this.fail("missing ]", open)
1404
1536
  }
1405
1537
  if (this.byteAt(this.pos) === 93) {
1406
- this.pos += 1
1538
+ this.skip(toI32(1))
1407
1539
  break
1408
1540
  }
1409
1541
  const atAtom: i32 = this.pos
@@ -1412,7 +1544,9 @@ class RegexParser {
1412
1544
  return -1
1413
1545
  }
1414
1546
  const dash: boolean =
1415
- this.pos + 1 < this.len && this.byteAt(this.pos) === 45 && this.byteAt(this.pos + 1) !== 93
1547
+ this.ahead(toI32(1)) < this.len &&
1548
+ this.byteAt(this.pos) === 45 &&
1549
+ this.byteAt(this.ahead(toI32(1))) !== 93
1416
1550
  if (!dash) {
1417
1551
  if (lo >= 0) {
1418
1552
  this.addRange(lo, lo)
@@ -1422,7 +1556,7 @@ class RegexParser {
1422
1556
  if (lo === -2) {
1423
1557
  return this.fail("invalid class range", atAtom)
1424
1558
  }
1425
- this.pos += 1
1559
+ this.skip(toI32(1))
1426
1560
  const hi: i32 = this.parseClassAtom()
1427
1561
  if (this.failed) {
1428
1562
  return -1
@@ -1439,8 +1573,11 @@ class RegexParser {
1439
1573
  return this.fail("a class has more than 10000 ranges", open)
1440
1574
  }
1441
1575
  // A class of one code point, unnegated, is that code point.
1442
- if (!negated && this.tmpCount === 1 && this.tmpLo[0] === this.tmpHi[0]) {
1443
- return this.charNode(this.tmpLo[0])
1576
+ const tl: i32[] = this.tmpLo
1577
+ const th: i32[] = this.tmpHi
1578
+ // One range means both lists hold at least one; the length tests say so.
1579
+ if (!negated && this.tmpCount === 1 && tl.length > 0 && th.length > 0 && tl[0] === th[0]) {
1580
+ return this.charNode(tl[0])
1444
1581
  }
1445
1582
  return this.finishClass(negated)
1446
1583
  }
@@ -1448,40 +1585,41 @@ class RegexParser {
1448
1585
  /** Store the class in `tmpLo` / `tmpHi` and answer its node. */
1449
1586
  private finishClass(negated: boolean): i32 {
1450
1587
  const k: i32 = toI32(this.clsFrom.length)
1451
- const base: i32 = toI32(this.clsAscii.length)
1452
- let j: i32 = 0
1453
- while (j < 128) {
1454
- this.clsAscii.push(toU8(0))
1455
- j += 1
1456
- }
1588
+ const tl: i32[] = this.tmpLo
1589
+ const th: i32[] = this.tmpHi
1590
+ // The class's ASCII table, built here and appended to `clsAscii`.
1591
+ const set: u8[] = regexZeroBytes(128)
1457
1592
  let i: i32 = 0
1458
- while (i < this.tmpCount) {
1459
- const lo: i32 = this.tmpLo[i]
1460
- const hi: i32 = this.tmpHi[i]
1461
- let c: i32 = lo
1462
- while (c <= hi && c < 128) {
1463
- this.clsAscii[base + c] = 1
1593
+ while (i >= 0 && i < this.tmpCount && i < toI32(tl.length) && i < toI32(th.length)) {
1594
+ const hi: i32 = th[i]
1595
+ let c: i32 = tl[i]
1596
+ while (c <= hi && c >= 0 && c < toI32(set.length)) {
1597
+ set[c] = 1
1464
1598
  c += 1
1465
1599
  }
1466
1600
  i += 1
1467
1601
  }
1468
1602
  if (this.icase) {
1469
1603
  let c: i32 = 65
1470
- while (c <= 90) {
1471
- if (this.clsAscii[base + c] === 1 || this.clsAscii[base + c + 32] === 1) {
1472
- this.clsAscii[base + c] = 1
1473
- this.clsAscii[base + c + 32] = 1
1604
+ while (c <= 90 && c >= 0 && c < toI32(set.length)) {
1605
+ const other: i32 = c + 32
1606
+ if (other < toI32(set.length) && (set[c] === 1 || set[other] === 1)) {
1607
+ set[c] = 1
1608
+ set[other] = 1
1474
1609
  }
1475
1610
  c += 1
1476
1611
  }
1477
1612
  }
1478
1613
  if (negated) {
1479
1614
  let c: i32 = 0
1480
- while (c < 128) {
1481
- this.clsAscii[base + c] = this.clsAscii[base + c] === 1 ? 0 : 1
1615
+ while (c < toI32(set.length)) {
1616
+ set[c] = set[c] === 1 ? 0 : 1
1482
1617
  c += 1
1483
1618
  }
1484
1619
  }
1620
+ for (const b of set) {
1621
+ this.clsAscii.push(b)
1622
+ }
1485
1623
  let from: i32 = toI32(this.clsLo.length)
1486
1624
  let out: i32 = this.storeRanges()
1487
1625
  // Under `i`, the class closed under simple case folding above ASCII.
@@ -1504,50 +1642,68 @@ class RegexParser {
1504
1642
  * `clsHi`, sorted by start and merged, and answer where the merged run ends.
1505
1643
  */
1506
1644
  private storeRanges(): i32 {
1507
- const from: i32 = toI32(this.clsLo.length)
1645
+ const lows: i32[] = this.clsLo
1646
+ const highs: i32[] = this.clsHi
1647
+ const tl: i32[] = this.tmpLo
1648
+ const th: i32[] = this.tmpHi
1649
+ const from: i32 = toI32(lows.length)
1508
1650
  let i: i32 = 0
1509
- while (i < this.tmpCount) {
1510
- const hi: i32 = this.tmpHi[i]
1651
+ while (i >= 0 && i < this.tmpCount && i < toI32(tl.length) && i < toI32(th.length)) {
1652
+ const hi: i32 = th[i]
1511
1653
  if (hi >= 128) {
1512
- const lo: i32 = this.tmpLo[i] < 128 ? 128 : this.tmpLo[i]
1513
- this.clsLo.push(lo)
1514
- this.clsHi.push(hi)
1654
+ const lo: i32 = tl[i] < 128 ? 128 : tl[i]
1655
+ lows.push(lo)
1656
+ highs.push(hi)
1515
1657
  }
1516
1658
  i += 1
1517
1659
  }
1518
- const end: i32 = toI32(this.clsLo.length)
1660
+ const end: i32 = toI32(lows.length)
1661
+ // The tests on the lists' lengths below always hold: `from` and `end`
1662
+ // bound the run just pushed, and the run is the lists' tail.
1519
1663
  if (end - from <= REGEX_INSERTION_SORT) {
1520
- i = from + 1
1521
- while (i < end) {
1522
- const lo: i32 = this.clsLo[i]
1523
- const hi: i32 = this.clsHi[i]
1524
- let m: i32 = i
1525
- while (m > from && this.clsLo[m - 1] > lo) {
1526
- this.clsLo[m] = this.clsLo[m - 1]
1527
- this.clsHi[m] = this.clsHi[m - 1]
1528
- m -= 1
1664
+ // Unsigned: `from` is a list's length, far below 2^31.
1665
+ i = toI32(toU32(from) + 1)
1666
+ while (i >= 1 && i < end && i < toI32(lows.length) && i < toI32(highs.length)) {
1667
+ const lo: i32 = lows[i]
1668
+ const hi: i32 = highs[i]
1669
+ // `q` is the entry before the gap, which moves down past every start above `lo`.
1670
+ let q: i32 = i - 1
1671
+ while (q >= from && q >= 0 && q < toI32(lows.length) && q < toI32(highs.length) && lows[q] > lo) {
1672
+ const m: i32 = q + 1
1673
+ if (m < toI32(lows.length) && m < toI32(highs.length)) {
1674
+ lows[m] = lows[q]
1675
+ highs[m] = highs[q]
1676
+ }
1677
+ q -= 1
1678
+ }
1679
+ const m: i32 = toI32(toU32(q) + 1)
1680
+ if (m >= 0 && m < toI32(lows.length) && m < toI32(highs.length)) {
1681
+ lows[m] = lo
1682
+ highs[m] = hi
1529
1683
  }
1530
- this.clsLo[m] = lo
1531
- this.clsHi[m] = hi
1532
1684
  i += 1
1533
1685
  }
1534
1686
  } else {
1535
1687
  this.mergeSort(from, end)
1536
1688
  }
1537
- // Merge in place; what merging leaves past `out` is dead.
1689
+ // Merge in place; what merging leaves past `out` is dead. Unsigned
1690
+ // steps: `out` and the ranges' code points are far below 2^31.
1538
1691
  let out: i32 = from
1539
1692
  i = from
1540
- while (i < end) {
1541
- const lo: i32 = this.clsLo[i]
1542
- const hi: i32 = this.clsHi[i]
1543
- if (out > from && lo <= this.clsHi[out - 1] + 1) {
1544
- if (hi > this.clsHi[out - 1]) {
1545
- this.clsHi[out - 1] = hi
1693
+ while (i >= 0 && i < end && i < toI32(lows.length) && i < toI32(highs.length)) {
1694
+ const lo: i32 = lows[i]
1695
+ const hi: i32 = highs[i]
1696
+ const last: i32 = toI32(toU32(out) - 1)
1697
+ if (out > from && last >= 0 && last < toI32(highs.length) && lo <= toI32(toU32(highs[last]) + 1)) {
1698
+ if (hi > highs[last]) {
1699
+ highs[last] = hi
1546
1700
  }
1547
1701
  } else {
1548
- this.clsLo[out] = lo
1549
- this.clsHi[out] = hi
1550
- out += 1
1702
+ if (out >= 0 && out < toI32(lows.length) && out < toI32(highs.length)) {
1703
+ lows[out] = lo
1704
+ highs[out] = hi
1705
+ }
1706
+ out = toI32(toU32(out) + 1)
1551
1707
  }
1552
1708
  i += 1
1553
1709
  }
@@ -1560,36 +1716,61 @@ class RegexParser {
1560
1716
  * escapes in one, costs n log n rather than the n² insertion would.
1561
1717
  */
1562
1718
  private mergeSort(from: i32, end: i32): void {
1719
+ const lows: i32[] = this.clsLo
1720
+ const highs: i32[] = this.clsHi
1563
1721
  const n: i32 = end - from
1564
1722
  let srcLo: i32[] = []
1565
1723
  let srcHi: i32[] = []
1566
1724
  let dstLo: i32[] = []
1567
1725
  let dstHi: i32[] = []
1568
1726
  let i: i32 = from
1569
- while (i < end) {
1570
- srcLo.push(this.clsLo[i])
1571
- srcHi.push(this.clsHi[i])
1572
- dstLo.push(this.clsLo[i])
1573
- dstHi.push(this.clsHi[i])
1727
+ // `[from, end)` is inside the lists; the length tests say so where the
1728
+ // prover reads them.
1729
+ while (i >= 0 && i < end && i < toI32(lows.length) && i < toI32(highs.length)) {
1730
+ // Read before the pushes, which end what the loop's test proves.
1731
+ const lo: i32 = lows[i]
1732
+ const hi: i32 = highs[i]
1733
+ srcLo.push(lo)
1734
+ srcHi.push(hi)
1735
+ dstLo.push(lo)
1736
+ dstHi.push(hi)
1574
1737
  i += 1
1575
1738
  }
1576
- let width: i32 = 1
1577
- while (width < n) {
1578
- let left: i32 = 0
1579
- while (left < n) {
1580
- const mid: i32 = left + width < n ? left + width : n
1581
- const right: i32 = mid + width < n ? mid + width : n
1582
- let a: i32 = left
1583
- let b: i32 = mid
1584
- let o: i32 = left
1739
+ // Unsigned: `left`, `width`, `a` and `b` stay at most `n`, a list's
1740
+ // length, so their sums stay below 2^32.
1741
+ const un: u32 = toU32(n)
1742
+ let width: u32 = 1
1743
+ while (width < un) {
1744
+ let left: u32 = 0
1745
+ while (left < un) {
1746
+ let mid: u32 = left + width
1747
+ if (mid > un) {
1748
+ mid = un
1749
+ }
1750
+ let right: u32 = mid + width
1751
+ if (right > un) {
1752
+ right = un
1753
+ }
1754
+ let a: u32 = left
1755
+ let b: u32 = mid
1756
+ let o: i32 = toI32(left)
1585
1757
  // Every scratch array holds `n` entries and `a`, `b`, `o` stay below
1586
1758
  // `n`; the length tests say so where the prover reads them.
1587
- while (o >= 0 && o < right && o < toI32(dstLo.length) && o < toI32(dstHi.length)) {
1759
+ while (o >= 0 && toU32(o) < right && o < toI32(dstLo.length) && o < toI32(dstHi.length)) {
1588
1760
  let fromA: boolean = a < mid
1589
- if (fromA && b < right && a >= 0 && a < toI32(srcLo.length) && b >= 0 && b < toI32(srcLo.length)) {
1590
- fromA = srcLo[a] <= srcLo[b]
1761
+ const ia: i32 = toI32(a)
1762
+ const ib: i32 = toI32(b)
1763
+ if (
1764
+ fromA &&
1765
+ b < right &&
1766
+ ia >= 0 &&
1767
+ ia < toI32(srcLo.length) &&
1768
+ ib >= 0 &&
1769
+ ib < toI32(srcLo.length)
1770
+ ) {
1771
+ fromA = srcLo[ia] <= srcLo[ib]
1591
1772
  }
1592
- const pick: i32 = fromA ? a : b
1773
+ const pick: i32 = fromA ? ia : ib
1593
1774
  if (pick >= 0 && pick < toI32(srcLo.length) && pick < toI32(srcHi.length)) {
1594
1775
  dstLo[o] = srcLo[pick]
1595
1776
  dstHi[o] = srcHi[pick]
@@ -1613,8 +1794,11 @@ class RegexParser {
1613
1794
  }
1614
1795
  i = 0
1615
1796
  while (i < n && i < toI32(srcLo.length) && i < toI32(srcHi.length)) {
1616
- this.clsLo[from + i] = srcLo[i]
1617
- this.clsHi[from + i] = srcHi[i]
1797
+ const at: i32 = toI32(toU32(from) + toU32(i))
1798
+ if (at >= 0 && at < toI32(lows.length) && at < toI32(highs.length)) {
1799
+ lows[at] = srcLo[i]
1800
+ highs[at] = srcHi[i]
1801
+ }
1618
1802
  i += 1
1619
1803
  }
1620
1804
  }
@@ -1625,19 +1809,25 @@ class RegexParser {
1625
1809
  * missing code points, to be stored again.
1626
1810
  */
1627
1811
  private foldRanges(from: i32, out: i32): boolean {
1812
+ const lows: i32[] = this.clsLo
1813
+ const highs: i32[] = this.clsHi
1814
+ // `[from, out)` is inside the lists, which nothing below changes; the
1815
+ // length tests say so where the prover reads them. Unsigned steps: code
1816
+ // points and counts of ranges are far below 2^31.
1628
1817
  // A class closed already, holding no folding code point or all of them, leaves the fold table unread.
1629
1818
  let touches: boolean = false
1630
1819
  let covers: boolean = true
1631
1820
  let gap: i32 = 128
1632
1821
  let r0: i32 = from
1633
- while (r0 < out) {
1634
- if (!touches && regexFoldsIn(this.clsLo[r0], this.clsHi[r0])) {
1822
+ while (r0 >= 0 && r0 < out && r0 < toI32(lows.length) && r0 < toI32(highs.length)) {
1823
+ const lo: i32 = lows[r0]
1824
+ if (!touches && regexFoldsIn(lo, highs[r0])) {
1635
1825
  touches = true
1636
1826
  }
1637
- if (covers && gap < this.clsLo[r0] && regexFoldsIn(gap, this.clsLo[r0] - 1)) {
1827
+ if (covers && gap < lo && regexFoldsIn(gap, toI32(toU32(lo) - 1))) {
1638
1828
  covers = false
1639
1829
  }
1640
- gap = this.clsHi[r0] + 1
1830
+ gap = toI32(toU32(highs[r0]) + 1)
1641
1831
  r0 += 1
1642
1832
  }
1643
1833
  if (covers && gap <= REGEX_MAX_CP && regexFoldsIn(gap, REGEX_MAX_CP)) {
@@ -1647,16 +1837,18 @@ class RegexParser {
1647
1837
  return false
1648
1838
  }
1649
1839
  const end: i32 = toI32(REGEX_FOLD_DATA.length)
1650
- let added: i32 = 0
1840
+ let added: u32 = 0
1651
1841
  let r: i32 = from
1652
- while (r < out) {
1842
+ while (r >= 0 && r < out && r < toI32(lows.length) && r < toI32(highs.length)) {
1843
+ const lo: i32 = lows[r]
1844
+ const hi: i32 = highs[r]
1653
1845
  // The folding code points in the range, read on from the block that may hold its first.
1654
- let f: i32 = this.foldSeek(this.clsLo[r])
1655
- while (f <= this.clsHi[r] && this.tablePos < end) {
1846
+ let f: i32 = this.foldSeek(lo)
1847
+ while (f <= hi && this.tablePos < end) {
1656
1848
  const step: i32 = this.tableNumber(REGEX_FOLD_DATA)
1657
1849
  const at: i32 = this.tablePos
1658
1850
  let c: i32 = regexFoldTarget(f, step)
1659
- while (f >= this.clsLo[r] && c !== f) {
1851
+ while (f >= lo && c !== f) {
1660
1852
  if (!this.rangesHave(from, out, c)) {
1661
1853
  if (added === 0) {
1662
1854
  this.tmpCount = 0
@@ -1667,7 +1859,10 @@ class RegexParser {
1667
1859
  c = (step & 1) === 1 ? f : regexFoldTarget(c, this.foldStepOf(c))
1668
1860
  }
1669
1861
  this.tablePos = at
1670
- f = at < end ? f + this.tableNumber(REGEX_FOLD_DATA) : REGEX_MAX_CP + 1
1862
+ f =
1863
+ at < end
1864
+ ? toI32(toU32(f) + toU32(this.tableNumber(REGEX_FOLD_DATA)))
1865
+ : toI32(toU32(REGEX_MAX_CP) + 1)
1671
1866
  }
1672
1867
  r += 1
1673
1868
  }
@@ -1675,50 +1870,57 @@ class RegexParser {
1675
1870
  return false
1676
1871
  }
1677
1872
  r = from
1678
- while (r < out) {
1679
- this.addRange(this.clsLo[r], this.clsHi[r])
1873
+ while (r >= 0 && r < out && r < toI32(lows.length) && r < toI32(highs.length)) {
1874
+ this.addRange(lows[r], highs[r])
1680
1875
  r += 1
1681
1876
  }
1682
- this.countPropertyRanges(added, this.pos)
1877
+ this.countPropertyRanges(toI32(added), this.pos)
1683
1878
  return true
1684
1879
  }
1685
1880
 
1686
1881
  /** Whether the sorted, merged ranges `[from, out)` hold `c`. */
1687
1882
  private rangesHave(from: i32, out: i32, c: i32): boolean {
1883
+ const lows: i32[] = this.clsLo
1884
+ const highs: i32[] = this.clsHi
1688
1885
  let lo: i32 = from
1689
1886
  let hi: i32 = out
1690
1887
  while (lo < hi) {
1691
- const mid: i32 = (lo + hi) >> 1
1692
- if (this.clsHi[mid] < c) {
1888
+ // Unsigned: both ends are list indexes, so their sum does not wrap.
1889
+ const mid: i32 = toI32((toU32(lo) + toU32(hi)) >>> 1)
1890
+ // `[from, out)` is inside the lists, so this always holds.
1891
+ if (mid < 0 || mid >= toI32(highs.length)) {
1892
+ return false
1893
+ }
1894
+ if (highs[mid] < c) {
1693
1895
  lo = mid + 1
1694
1896
  } else {
1695
1897
  hi = mid
1696
1898
  }
1697
1899
  }
1698
- return lo < out && this.clsLo[lo] <= c
1900
+ return lo < out && lo >= 0 && lo < toI32(lows.length) && lows[lo] <= c
1699
1901
  }
1700
1902
  }
1701
1903
 
1702
1904
  /** Whether node `n` can match without consuming anything. */
1703
1905
  const regexNullable = (p: RegexParser, n: i32): boolean => {
1704
- const kind: i32 = p.kind[n]
1906
+ const kind: i32 = p.kindOf(n)
1705
1907
  if (kind === NODE_CHAR || kind === NODE_CLASS || kind === NODE_ANY) {
1706
1908
  return false
1707
1909
  }
1708
1910
  if (kind === NODE_GROUP) {
1709
- return regexNullable(p, p.kid[n])
1911
+ return regexNullable(p, p.kidOf(n))
1710
1912
  }
1711
1913
  if (kind === NODE_REPEAT) {
1712
- return p.na[n] === 0 || regexNullable(p, p.kid[n])
1914
+ return p.aOf(n) === 0 || regexNullable(p, p.kidOf(n))
1713
1915
  }
1714
1916
  if (kind === NODE_CAT || kind === NODE_ALT) {
1715
1917
  const all: boolean = kind === NODE_CAT
1716
- let c: i32 = p.kid[n]
1918
+ let c: i32 = p.kidOf(n)
1717
1919
  while (c !== -1) {
1718
1920
  if (regexNullable(p, c) !== all) {
1719
1921
  return !all
1720
1922
  }
1721
- c = p.next[c]
1923
+ c = p.nextOf(c)
1722
1924
  }
1723
1925
  return all
1724
1926
  }
@@ -1777,8 +1979,6 @@ const CTX_LINE: i32 = 2
1777
1979
  const CTX_OTHER: i32 = 3
1778
1980
  const CTX_EOT: i32 = 4
1779
1981
  const CTX_KINDS: i32 = 5
1780
- // The kinds, then the slot that says whether U+017F and U+212A are word characters.
1781
- const CTX_MASK_LEN: i32 = 6
1782
1982
  const CTX_SLOTS: i32 = 4
1783
1983
 
1784
1984
  /**
@@ -1787,6 +1987,119 @@ const CTX_SLOTS: i32 = 4
1787
1987
  * stops at a 0 slot), so every table it reads before writing comes from here
1788
1988
  * (#1362).
1789
1989
  */
1990
+ /**
1991
+ * Bytes `s` to `e` of `text`, as `slice` gives them. Every caller passes a
1992
+ * span inside the text that ends at or after it starts, so the "" outside
1993
+ * one is never what it reads; the test lets the copy go unclamped.
1994
+ */
1995
+ const regexSpan = (text: string, s: i32, e: i32): string => {
1996
+ if (s < 0 || e < s || e > toI32(text.length)) {
1997
+ return ""
1998
+ }
1999
+ return text.substring(s, e)
2000
+ }
2001
+
2002
+ /**
2003
+ * The string of `bytes`, a byte at a time: `String.fromBytes` keeps a range
2004
+ * check that no proof removes, and `fromCharCode` makes exactly one byte.
2005
+ */
2006
+ const regexBytesText = (bytes: u8[]): string => {
2007
+ const parts: string[] = []
2008
+ for (const b of bytes) {
2009
+ parts.push(String.fromCharCode(b))
2010
+ }
2011
+ return parts.join("")
2012
+ }
2013
+
2014
+ /**
2015
+ * The first slot of group `g`'s pair. Unsigned: the parser stops the
2016
+ * groups at `REGEX_MAX_GROUPS`.
2017
+ */
2018
+ const regexSlot = (g: i32): i32 => toI32(2 * toU32(g))
2019
+
2020
+ /**
2021
+ * Minus `e`, a negative transition entry. `~e` is `-e - 1`, which is not
2022
+ * negative, so adding 1 in u32 cannot wrap, and it gives the same value
2023
+ * under JavaScript, where a u32 sum does not wrap.
2024
+ */
2025
+ const regexNegated = (e: i32): i32 => toI32(toU32(~e) + 1)
2026
+
2027
+ /** `a[i]`, or `missing` outside `a`. */
2028
+ const regexItem = (a: i32[], i: i32, missing: i32): i32 => {
2029
+ if (i < 0 || i >= toI32(a.length)) {
2030
+ return missing
2031
+ }
2032
+ return a[i]
2033
+ }
2034
+
2035
+ /** `a[i]`, or -1 outside `a`. */
2036
+ const regexByteItem = (a: u8[], i: i32): i32 => {
2037
+ if (i < 0 || i >= toI32(a.length)) {
2038
+ return -1
2039
+ }
2040
+ return toI32(a[i])
2041
+ }
2042
+
2043
+ /** Sets `a[i]` to `v`; outside `a` it does nothing. */
2044
+ const regexPut = (a: i32[], i: i32, v: i32): void => {
2045
+ if (i >= 0 && i < toI32(a.length)) {
2046
+ a[i] = v
2047
+ }
2048
+ }
2049
+
2050
+ /** Byte `i` of `text`, or -1 outside it, which no byte test matches. */
2051
+ const regexByteAt = (text: string, i: i32): i32 => {
2052
+ if (i < 0 || i >= toI32(text.length)) {
2053
+ return -1
2054
+ }
2055
+ return toI32(text.charCodeAt(i))
2056
+ }
2057
+
2058
+ /**
2059
+ * Whether `prefix` is in `text` at byte `s`.
2060
+ */
2061
+ const regexPrefixAt = (text: string, s: i32, prefix: string): boolean => {
2062
+ const n: i32 = toI32(prefix.length)
2063
+ const len: i32 = toI32(text.length)
2064
+ let q: i32 = s
2065
+ let k: i32 = 0
2066
+ while (k < n) {
2067
+ if (q < 0 || q >= len || text.charCodeAt(q) !== prefix.charCodeAt(k)) {
2068
+ return false
2069
+ }
2070
+ q += 1
2071
+ k += 1
2072
+ }
2073
+ return true
2074
+ }
2075
+
2076
+ /** Store `ns` slots of `src` into `dst` from `base`, or as many as fit. */
2077
+ const regexStoreSlots = (dst: i32[], base: i32, src: i32[], ns: i32): void => {
2078
+ let k: i32 = 0
2079
+ while (k < ns && k < toI32(src.length)) {
2080
+ // Unsigned: `base` and `k` are not negative, and `dst` bounds the sum.
2081
+ const at: i32 = toI32(toU32(base) + toU32(k))
2082
+ if (at < 0 || at >= toI32(dst.length)) {
2083
+ return
2084
+ }
2085
+ dst[at] = src[k]
2086
+ k += 1
2087
+ }
2088
+ }
2089
+
2090
+ /** Load `ns` slots of `src` from `base` into `dst`, or as many as there are. */
2091
+ const regexLoadSlots = (dst: i32[], src: i32[], base: i32, ns: i32): void => {
2092
+ let k: i32 = 0
2093
+ while (k < ns && k < toI32(dst.length)) {
2094
+ const at: i32 = toI32(toU32(base) + toU32(k))
2095
+ if (at < 0 || at >= toI32(src.length)) {
2096
+ return
2097
+ }
2098
+ dst[k] = src[at]
2099
+ k += 1
2100
+ }
2101
+ }
2102
+
1790
2103
  const regexZeros = (n: i32): i32[] => {
1791
2104
  const a: i32[] = new Array<i32>(n)
1792
2105
  a.fill(0)
@@ -1807,39 +2120,58 @@ const regexZeroBytes = (n: i32): u8[] => {
1807
2120
  * twice.
1808
2121
  */
1809
2122
  const regexContextMask = (bits: i32): i32[] => {
1810
- const m: i32[] = regexZeros(CTX_MASK_LEN)
1811
2123
  const line: boolean = (bits & (1 << ASSERT_BEGIN_LINE)) !== 0
2124
+ let start: i32 = CTX_OTHER
1812
2125
  if ((bits & (1 << ASSERT_BEGIN_TEXT)) !== 0) {
1813
- m[CTX_START] = CTX_START
2126
+ start = CTX_START
1814
2127
  } else if (line) {
1815
- m[CTX_START] = CTX_LINE
1816
- } else {
1817
- m[CTX_START] = CTX_OTHER
2128
+ start = CTX_LINE
1818
2129
  }
1819
2130
  const word: boolean = (bits & ASSERT_WORD_BITS) !== 0
1820
- m[CTX_WORD] = word ? CTX_WORD : CTX_OTHER
1821
- m[CTX_LINE] = line ? CTX_LINE : CTX_OTHER
1822
- m[CTX_OTHER] = CTX_OTHER
1823
- m[CTX_EOT] = CTX_OTHER
1824
- m[CTX_KINDS] = (bits & ASSERT_FOLD_BITS) !== 0 ? 1 : 0
1825
- return m
2131
+ // In CTX_START, CTX_WORD, CTX_LINE, CTX_OTHER, CTX_EOT, CTX_KINDS order.
2132
+ return [
2133
+ start,
2134
+ word ? CTX_WORD : CTX_OTHER,
2135
+ line ? CTX_LINE : CTX_OTHER,
2136
+ CTX_OTHER,
2137
+ CTX_OTHER,
2138
+ (bits & ASSERT_FOLD_BITS) !== 0 ? 1 : 0,
2139
+ ]
2140
+ }
2141
+
2142
+ /**
2143
+ * Entry `kind` of a context mask. Every mask comes from `regexContextMask`
2144
+ * and every kind is a `CTX_` constant the mask has a slot for, so the
2145
+ * `CTX_OTHER` fallback, the context that asserts nothing, is never taken.
2146
+ */
2147
+ const regexMaskOf = (mask: i32[], kind: i32): i32 => {
2148
+ if (kind < 0 || kind >= toI32(mask.length)) {
2149
+ return CTX_OTHER
2150
+ }
2151
+ return mask[kind]
1826
2152
  }
1827
2153
 
1828
2154
  /** Sort `a` ascending, a bottom-up merge sort. */
1829
2155
  const regexSortPoints = (a: i32[]): void => {
2156
+ // Every slot is written before it is read; each loop tests its index
2157
+ // against `tmp`'s own length. Made before `n`: a call after it would end
2158
+ // the fact that `n` is `a`'s length.
2159
+ const tmp: i32[] = a.slice()
1830
2160
  const n: i32 = toI32(a.length)
1831
- // Not `regexZeros`: a call here would cost the proof that `n` is `a`'s
1832
- // length, and every slot is written before it is read.
1833
- const tmp: i32[] = new Array<i32>(n)
1834
- let width: i32 = 1
1835
- while (width < n) {
1836
- let lo: i32 = 0
1837
- while (lo < n) {
1838
- const mid: i32 = n - lo > width ? lo + width : n
1839
- const hi: i32 = n - mid > width ? mid + width : n
1840
- let i: i32 = lo
2161
+ // Unsigned: a run's bounds are at most the length plus a width below it,
2162
+ // and `i` and `j` stop at the run's end, so nothing here wraps.
2163
+ const un: u32 = toU32(n)
2164
+ let width: u32 = 1
2165
+ while (width < un) {
2166
+ let lo: u32 = 0
2167
+ while (lo < un) {
2168
+ const umid: u32 = un - lo > width ? lo + width : un
2169
+ const uhi: u32 = un - umid > width ? umid + width : un
2170
+ const mid: i32 = toI32(umid)
2171
+ const hi: i32 = toI32(uhi)
2172
+ let i: i32 = toI32(lo)
1841
2173
  let j: i32 = mid
1842
- let k: i32 = lo
2174
+ let k: i32 = toI32(lo)
1843
2175
  // Each index is also tested against the length it reads, `n` for `a`:
1844
2176
  // the runs end inside both arrays, and that test is what proves it.
1845
2177
  while (k >= 0 && k < hi && k < toI32(tmp.length)) {
@@ -1849,13 +2181,13 @@ const regexSortPoints = (a: i32[]): void => {
1849
2181
  tmp[k] = a[from]
1850
2182
  }
1851
2183
  if (right) {
1852
- j += 1
2184
+ j = toI32(toU32(j) + 1)
1853
2185
  } else {
1854
- i += 1
2186
+ i = toI32(toU32(i) + 1)
1855
2187
  }
1856
2188
  k += 1
1857
2189
  }
1858
- lo = hi
2190
+ lo = uhi
1859
2191
  }
1860
2192
  let k: i32 = 0
1861
2193
  while (k < n && k < toI32(tmp.length)) {
@@ -1875,9 +2207,10 @@ const regexByteScore = (b: i32): i32 => {
1875
2207
  return 100
1876
2208
  }
1877
2209
  if (b >= 97 && b <= 122) {
2210
+ const order: string = REGEX_LETTER_ORDER
1878
2211
  let i: i32 = 0
1879
- while (i < 26) {
1880
- if (toI32(REGEX_LETTER_ORDER.charCodeAt(i)) === b) {
2212
+ while (i < toI32(order.length)) {
2213
+ if (toI32(order.charCodeAt(i)) === b) {
1881
2214
  return 95 - i
1882
2215
  }
1883
2216
  i += 1
@@ -1903,8 +2236,15 @@ const REGEX_LETTER_PER_MILLE: string = "9570626055555048463331222219181716151411
1903
2236
 
1904
2237
  /** How many of every thousand bytes of English text the `i`th letter of `REGEX_LETTER_ORDER` is. */
1905
2238
  const regexLetterPerMille = (i: i32): i32 => {
1906
- const tens: i32 = toI32(REGEX_LETTER_PER_MILLE.charCodeAt(2 * i)) - 48
1907
- return tens * 10 + toI32(REGEX_LETTER_PER_MILLE.charCodeAt(2 * i + 1)) - 48
2239
+ const table: string = REGEX_LETTER_PER_MILLE
2240
+ // Unsigned: an `i` of 2^30 or more wraps negative, which the test turns
2241
+ // away. The table has a pair for every letter, so it never does.
2242
+ const at: i32 = toI32(toU32(i) * 2)
2243
+ if (i < 0 || at < 0 || at >= toI32(table.length) - 1) {
2244
+ return 0
2245
+ }
2246
+ // Two decimal digits, so the unsigned sum stays below 100.
2247
+ return toI32((toU32(table.charCodeAt(at)) - 48) * 10 + toU32(table.charCodeAt(at + 1)) - 48)
1908
2248
  }
1909
2249
 
1910
2250
  /** How many of every thousand bytes of text are ASCII byte `b`, roughly: what a scan window is chosen by. */
@@ -1913,9 +2253,10 @@ const regexBytePerMille = (b: i32): i32 => {
1913
2253
  return 170
1914
2254
  }
1915
2255
  if (b >= 97 && b <= 122) {
2256
+ const order: string = REGEX_LETTER_ORDER
1916
2257
  let i: i32 = 0
1917
- while (i < 26) {
1918
- if (toI32(REGEX_LETTER_ORDER.charCodeAt(i)) === b) {
2258
+ while (i < toI32(order.length)) {
2259
+ if (toI32(order.charCodeAt(i)) === b) {
1919
2260
  return regexLetterPerMille(i)
1920
2261
  }
1921
2262
  i += 1
@@ -1958,21 +2299,27 @@ const REGEX_SCAN_WIDTH: i32 = 3
1958
2299
  const regexScanFrom = (text: string, masks: u8[], depth: i32, off: i32, width: i32, from: i32): i32 => {
1959
2300
  const length: i32 = toI32(text.length)
1960
2301
  let i: i32 = from
1961
- while (i >= 0 && i <= length - depth) {
2302
+ while (i >= 0 && i < length && depth <= length - i) {
1962
2303
  let k: i32 = 0
1963
2304
  while (k < depth) {
1964
2305
  // The window's positions, then the ones before it, then the ones after.
1965
- let j: i32 = k
2306
+ // Unsigned, as every operand is below `REGEX_SCAN_DEPTH` and none wraps.
2307
+ let j: u32 = toU32(k)
1966
2308
  if (k < width) {
1967
- j = off + k
1968
- } else if (k < off + width) {
1969
- j = k - width
2309
+ j = toU32(off) + toU32(k)
2310
+ } else if (toU32(k) < toU32(off) + toU32(width)) {
2311
+ j = toU32(k) - toU32(width)
2312
+ }
2313
+ // `j` is below `depth`, so `at` is inside the text; the test proves it.
2314
+ const at: i32 = toI32(toU32(i) + j)
2315
+ if (at < 0 || at >= length) {
2316
+ break
1970
2317
  }
1971
- let b: i32 = toI32(text.charCodeAt(i + j))
2318
+ let b: i32 = toI32(text.charCodeAt(at))
1972
2319
  if (b > 255) {
1973
2320
  b = 255
1974
2321
  }
1975
- const slot: i32 = 32 * depth + (j << 8) + b
2322
+ const slot: i32 = toI32(toU32(depth) * 32 + (j << 8) + toU32(b))
1976
2323
  // Outside the table is never reached; the test is what proves `slot`.
1977
2324
  if (slot < 0 || slot >= toI32(masks.length) || masks[slot] === 0) {
1978
2325
  break
@@ -2006,19 +2353,19 @@ export class RegexMatch {
2006
2353
  /**
2007
2354
  * Where the match starts.
2008
2355
  *
2009
- * @returns The byte offset of the match's first byte in the searched text.
2356
+ * @returns The byte offset of the match's first byte in the searched text; -1 for a `RegexMatch` that holds no match yet.
2010
2357
  */
2011
2358
  start(): i32 {
2012
- return this.slots[0]
2359
+ return this.groupStart(REGEX_WHOLE)
2013
2360
  }
2014
2361
 
2015
2362
  /**
2016
2363
  * Where the match ends.
2017
2364
  *
2018
- * @returns The byte offset one past the match's last byte; equal to `start` for an empty match.
2365
+ * @returns The byte offset one past the match's last byte; equal to `start` for an empty match, and -1 for a `RegexMatch` that holds no match yet.
2019
2366
  */
2020
2367
  end(): i32 {
2021
- return this.slots[1]
2368
+ return this.groupEnd(REGEX_WHOLE)
2022
2369
  }
2023
2370
 
2024
2371
  /**
@@ -2046,7 +2393,22 @@ export class RegexMatch {
2046
2393
  * @returns False for a group that took no part and for a number with no group.
2047
2394
  */
2048
2395
  matched(k: i32): boolean {
2049
- return k >= 0 && k < this.groupCount() && this.slots[2 * k] >= 0
2396
+ return this.slotOf(k, false) >= 0
2397
+ }
2398
+
2399
+ /**
2400
+ * Group `k`'s start slot, or its end slot when `end` is set; -1 for a
2401
+ * number with no group. The slot index `2 * k` is computed unsigned: a `k`
2402
+ * of 2^30 or more wraps it negative, which the range test turns away as it
2403
+ * does a negative `k`.
2404
+ */
2405
+ private slotOf(k: i32, end: boolean): i32 {
2406
+ const s: i32[] = this.slots
2407
+ const at: i32 = toI32(toU32(k) * 2)
2408
+ if (k < 0 || at < 0 || at >= toI32(s.length) - 1) {
2409
+ return -1
2410
+ }
2411
+ return end ? s[at + 1] : s[at]
2050
2412
  }
2051
2413
 
2052
2414
  /**
@@ -2056,7 +2418,8 @@ export class RegexMatch {
2056
2418
  * @returns The byte offset of the group's first byte, or -1 when the group took no part or does not exist.
2057
2419
  */
2058
2420
  groupStart(k: i32): i32 {
2059
- return this.matched(k) ? this.slots[2 * k] : -1
2421
+ const at: i32 = this.slotOf(k, false)
2422
+ return at >= 0 ? at : -1
2060
2423
  }
2061
2424
 
2062
2425
  /**
@@ -2066,7 +2429,7 @@ export class RegexMatch {
2066
2429
  * @returns The byte offset one past the group's last byte, or -1 when the group took no part or does not exist.
2067
2430
  */
2068
2431
  groupEnd(k: i32): i32 {
2069
- return this.matched(k) ? this.slots[2 * k + 1] : -1
2432
+ return this.matched(k) ? this.slotOf(k, true) : -1
2070
2433
  }
2071
2434
 
2072
2435
  /**
@@ -2076,10 +2439,13 @@ export class RegexMatch {
2076
2439
  * @returns The group's text; "" when it took no part or does not exist, so test `matched` to tell that from an empty capture.
2077
2440
  */
2078
2441
  group(k: i32): string {
2079
- if (!this.matched(k)) {
2442
+ const from: i32 = this.slotOf(k, false)
2443
+ const to: i32 = this.slotOf(k, true)
2444
+ // `substring` is `slice` once the bounds are ordered and not negative.
2445
+ if (from < 0 || to < from) {
2080
2446
  return ""
2081
2447
  }
2082
- return this.input.slice(this.slots[2 * k], this.slots[2 * k + 1])
2448
+ return this.input.substring(from, to)
2083
2449
  }
2084
2450
 
2085
2451
  /**
@@ -2142,10 +2508,12 @@ export class Regex {
2142
2508
  private nDense: i32[]
2143
2509
  private nSlots: i32[]
2144
2510
  private visit: i32[]
2145
- /** The closure's stack: explore `stkA` (kind 0) or restore slot `stkA` to `stkB` (kind 1). */
2146
- private stkKind: i32[]
2147
- private stkA: i32[]
2148
- private stkB: i32[]
2511
+ /**
2512
+ * The closure's stack, three entries a frame: kind 0 explores pc `a` with
2513
+ * marks `b`; kind 1 restores slot `a` to `b`. One array, so one bound
2514
+ * covers a frame.
2515
+ */
2516
+ private stk: i32[]
2149
2517
  /** The captures of the thread being followed. */
2150
2518
  private caps: i32[]
2151
2519
  /** The best match so far. */
@@ -2308,9 +2676,7 @@ export class Regex {
2308
2676
  this.nCount = 0
2309
2677
  this.visit = []
2310
2678
  this.gen = 0
2311
- this.stkKind = []
2312
- this.stkA = []
2313
- this.stkB = []
2679
+ this.stk = []
2314
2680
  this.caps = []
2315
2681
  this.mslots = []
2316
2682
  this.rareStr = ""
@@ -2408,12 +2774,23 @@ export class Regex {
2408
2774
  return toI32(this.op.length)
2409
2775
  }
2410
2776
 
2777
+ /**
2778
+ * Point split `s` on to the next instruction and to `other`, the next
2779
+ * first when `first`. Unsigned: `s` is an instruction, and `emit` stops
2780
+ * the program at `REGEX_MAX_INSTRUCTIONS`.
2781
+ */
2782
+ private branch(s: i32, first: boolean, other: i32): void {
2783
+ const next: i32 = toI32(toU32(s) + 1)
2784
+ regexPut(this.ia, s, first ? next : other)
2785
+ regexPut(this.ib, s, first ? other : next)
2786
+ }
2787
+
2411
2788
  /** One iteration of a repeat's body: its groups cleared, then the body. */
2412
2789
  private compileIteration(p: RegexParser, r: i32, rev: boolean): void {
2413
- if (p.ne[r] > p.nd[r]) {
2414
- this.emit(OP_CLEAR, 2 * p.nd[r], 2 * p.ne[r])
2790
+ if (p.eOf(r) > p.dOf(r)) {
2791
+ this.emit(OP_CLEAR, regexSlot(p.dOf(r)), regexSlot(p.eOf(r)))
2415
2792
  }
2416
- this.compileNode(p, p.kid[r], rev)
2793
+ this.compileNode(p, p.kidOf(r), rev)
2417
2794
  }
2418
2795
 
2419
2796
  /**
@@ -2425,16 +2802,16 @@ export class Regex {
2425
2802
  if (this.tooBig) {
2426
2803
  return
2427
2804
  }
2428
- const kind: i32 = p.kind[n]
2805
+ const kind: i32 = p.kindOf(n)
2429
2806
  if (kind === NODE_EMPTY) {
2430
2807
  return
2431
2808
  }
2432
2809
  if (kind === NODE_CHAR) {
2433
- this.emit(OP_CHAR, p.na[n], p.nb[n])
2810
+ this.emit(OP_CHAR, p.aOf(n), p.bOf(n))
2434
2811
  return
2435
2812
  }
2436
2813
  if (kind === NODE_CLASS) {
2437
- this.emit(OP_CLASS, p.na[n], REGEX_NIL)
2814
+ this.emit(OP_CLASS, p.aOf(n), REGEX_NIL)
2438
2815
  return
2439
2816
  }
2440
2817
  if (kind === NODE_ANY) {
@@ -2444,16 +2821,16 @@ export class Regex {
2444
2821
  if (kind === NODE_ASSERT) {
2445
2822
  // The text and line assertions are numbered so that the low bit picks
2446
2823
  // the beginning or the end.
2447
- const a: i32 = p.na[n]
2824
+ const a: i32 = p.aOf(n)
2448
2825
  this.emit(OP_ASSERT, rev && a < ASSERT_WORD ? a ^ 1 : a, REGEX_NIL)
2449
2826
  return
2450
2827
  }
2451
2828
  if (kind === NODE_CAT) {
2452
2829
  const kids: i32[] = []
2453
- let c: i32 = p.kid[n]
2830
+ let c: i32 = p.kidOf(n)
2454
2831
  while (c !== -1) {
2455
2832
  kids.push(c)
2456
- c = p.next[c]
2833
+ c = p.nextOf(c)
2457
2834
  }
2458
2835
  const count: i32 = toI32(kids.length)
2459
2836
  let i: i32 = 0
@@ -2471,37 +2848,37 @@ export class Regex {
2471
2848
  return
2472
2849
  }
2473
2850
  if (kind === NODE_GROUP) {
2474
- this.emit(OP_SAVE, 2 * p.na[n], REGEX_NIL)
2475
- this.compileNode(p, p.kid[n], rev)
2476
- this.emit(OP_SAVE, 2 * p.na[n] + 1, REGEX_NIL)
2851
+ this.emit(OP_SAVE, regexSlot(p.aOf(n)), REGEX_NIL)
2852
+ this.compileNode(p, p.kidOf(n), rev)
2853
+ this.emit(OP_SAVE, toI32(toU32(regexSlot(p.aOf(n))) + 1), REGEX_NIL)
2477
2854
  return
2478
2855
  }
2479
2856
  if (kind === NODE_ALT) {
2480
2857
  const jumps: i32[] = []
2481
- let c: i32 = p.kid[n]
2858
+ let c: i32 = p.kidOf(n)
2482
2859
  while (c !== -1 && !this.tooBig) {
2483
- if (p.next[c] !== -1) {
2860
+ if (p.nextOf(c) !== -1) {
2484
2861
  const split: i32 = this.emit(OP_SPLIT, REGEX_NIL, REGEX_NIL)
2485
- this.ia[split] = this.pc()
2862
+ regexPut(this.ia, split, this.pc())
2486
2863
  this.compileNode(p, c, rev)
2487
2864
  jumps.push(this.emit(OP_JMP, REGEX_NIL, REGEX_NIL))
2488
- this.ib[split] = this.pc()
2865
+ regexPut(this.ib, split, this.pc())
2489
2866
  } else {
2490
2867
  this.compileNode(p, c, rev)
2491
2868
  }
2492
- c = p.next[c]
2869
+ c = p.nextOf(c)
2493
2870
  }
2494
2871
  const exit: i32 = this.pc()
2495
2872
  for (const j of jumps) {
2496
- this.ia[j] = exit
2873
+ regexPut(this.ia, j, exit)
2497
2874
  }
2498
2875
  return
2499
2876
  }
2500
2877
  // NODE_REPEAT
2501
- const min: i32 = p.na[n]
2502
- const max: i32 = p.nb[n]
2503
- const greedy: boolean = p.nc[n] === 1
2504
- if (max !== min && regexNullable(p, p.kid[n])) {
2878
+ const min: i32 = p.aOf(n)
2879
+ const max: i32 = p.bOf(n)
2880
+ const greedy: boolean = p.cOf(n) === 1
2881
+ if (max !== min && regexNullable(p, p.kidOf(n))) {
2505
2882
  this.compileEmptyCheckedRepeat(p, n, min, max, greedy, rev)
2506
2883
  return
2507
2884
  }
@@ -2511,8 +2888,7 @@ export class Regex {
2511
2888
  this.compileIteration(p, n, rev)
2512
2889
  this.emit(OP_JMP, split, REGEX_NIL)
2513
2890
  const exit: i32 = this.pc()
2514
- this.ia[split] = greedy ? split + 1 : exit
2515
- this.ib[split] = greedy ? exit : split + 1
2891
+ this.branch(split, greedy, exit)
2516
2892
  return
2517
2893
  }
2518
2894
  if (max === -1) {
@@ -2525,8 +2901,7 @@ export class Regex {
2525
2901
  const loop: i32 = this.pc()
2526
2902
  this.compileIteration(p, n, rev)
2527
2903
  const split: i32 = this.emit(OP_SPLIT, REGEX_NIL, REGEX_NIL)
2528
- this.ia[split] = greedy ? loop : split + 1
2529
- this.ib[split] = greedy ? split + 1 : loop
2904
+ this.branch(split, !greedy, loop)
2530
2905
  return
2531
2906
  }
2532
2907
  // body × min; then (SPLIT next, exit; body) × (max - min)
@@ -2550,8 +2925,7 @@ export class Regex {
2550
2925
  }
2551
2926
  const exit: i32 = this.pc()
2552
2927
  for (const s of splits) {
2553
- this.ia[s] = greedy ? s + 1 : exit
2554
- this.ib[s] = greedy ? exit : s + 1
2928
+ this.branch(s, greedy, exit)
2555
2929
  }
2556
2930
  }
2557
2931
 
@@ -2575,14 +2949,20 @@ export class Regex {
2575
2949
  greedy: boolean,
2576
2950
  rev: boolean
2577
2951
  ): void {
2578
- while (toI32(this.markOf.length) <= n) {
2579
- this.markOf.push(REGEX_NONE)
2952
+ const marks: i32[] = this.markOf
2953
+ while (toI32(marks.length) <= n) {
2954
+ marks.push(REGEX_NONE)
2955
+ }
2956
+ // The loop above makes room for `n`, a node, which is never negative.
2957
+ if (n < 0 || n >= toI32(marks.length)) {
2958
+ return
2580
2959
  }
2581
- if (this.markOf[n] < 0) {
2582
- this.markOf[n] = this.width
2583
- this.width += 1
2960
+ if (marks[n] < 0) {
2961
+ marks[n] = this.width
2962
+ // Unsigned: a slot per group end and per repeat node, far below 2^31.
2963
+ this.width = toI32(toU32(this.width) + 1)
2584
2964
  }
2585
- const k: i32 = this.markOf[n]
2965
+ const k: i32 = marks[n]
2586
2966
  let i: i32 = 0
2587
2967
  while (i < min && !this.tooBig) {
2588
2968
  // A body that emits nothing emits nothing on every copy: stop at one,
@@ -2609,8 +2989,7 @@ export class Regex {
2609
2989
  }
2610
2990
  const exit: i32 = this.pc()
2611
2991
  for (const s of splits) {
2612
- this.ia[s] = greedy ? s + 1 : exit
2613
- this.ib[s] = greedy ? exit : s + 1
2992
+ this.branch(s, greedy, exit)
2614
2993
  }
2615
2994
  }
2616
2995
 
@@ -2621,20 +3000,22 @@ export class Regex {
2621
3000
  */
2622
3001
  private compileMarked(p: RegexParser, n: i32, k: i32, rev: boolean): void {
2623
3002
  const level: i32 = this.checkDepth
3003
+ // Unsigned: the depth is at most the pattern's nesting, which the parser
3004
+ // bounds, and it goes back down only after it went up.
2624
3005
  if (level < REGEX_MAX_LEVELS && level >= this.levels) {
2625
- this.levels = level + 1
3006
+ this.levels = toI32(toU32(level) + 1)
2626
3007
  }
2627
3008
  this.emit(OP_MARK, k, level)
2628
- this.checkDepth += 1
3009
+ this.checkDepth = toI32(toU32(level) + 1)
2629
3010
  this.compileIteration(p, n, rev)
2630
- this.checkDepth -= 1
3011
+ this.checkDepth = level
2631
3012
  this.emit(OP_CHECK, k, REGEX_NIL)
2632
3013
  }
2633
3014
 
2634
3015
  /** Compile the parsed pattern and size the VM. Answers an error, or "". @internal `regexCompile` calls it. */
2635
3016
  build(p: RegexParser, root: i32): string {
2636
3017
  this.groups = p.groups
2637
- this.width = 2 * p.groups
3018
+ this.width = regexSlot(p.groups)
2638
3019
  this.names = p.names
2639
3020
  this.hasNames = p.hasNames
2640
3021
  this.clsAscii = p.clsAscii
@@ -2643,9 +3024,10 @@ export class Regex {
2643
3024
  this.clsFrom = p.clsFrom
2644
3025
  this.clsTo = p.clsTo
2645
3026
  this.clsNeg = p.clsNeg
2646
- this.emit(OP_SAVE, 2 * REGEX_WHOLE, REGEX_NIL)
3027
+ const whole: i32 = regexSlot(REGEX_WHOLE)
3028
+ this.emit(OP_SAVE, whole, REGEX_NIL)
2647
3029
  this.compileNode(p, root, false)
2648
- this.emit(OP_SAVE, 2 * REGEX_WHOLE + 1, REGEX_NIL)
3030
+ this.emit(OP_SAVE, toI32(toU32(whole) + 1), REGEX_NIL)
2649
3031
  this.emit(OP_MATCH, REGEX_NIL, REGEX_NIL)
2650
3032
  if (this.tooBig) {
2651
3033
  return "the pattern compiles to more than 100000 instructions"
@@ -2656,7 +3038,8 @@ export class Regex {
2656
3038
  // The reverse program, which the reverse DFA runs to find where a match
2657
3039
  // starts once the forward one has found where it ends. Without marks
2658
3040
  // only: a mark reorders the matches, and the reverse search reads none.
2659
- if (this.width === 2 * this.groups && 2 * count <= REGEX_MAX_INSTRUCTIONS) {
3041
+ // Unsigned: `emit` stops the program at `REGEX_MAX_INSTRUCTIONS`.
3042
+ if (this.width === regexSlot(this.groups) && 2 * toU32(count) <= toU32(REGEX_MAX_INSTRUCTIONS)) {
2660
3043
  this.revEntry = count
2661
3044
  this.compileNode(p, root, true)
2662
3045
  this.emit(OP_MATCH, REGEX_NIL, REGEX_NIL)
@@ -2664,37 +3047,46 @@ export class Regex {
2664
3047
  this.tooBig = false
2665
3048
  }
2666
3049
  const slots: i32 = this.width
2667
- if (count > REGEX_MAX_SLOT_TABLE / slots) {
3050
+ // In f64, where neither product can wrap: for whole numbers,
3051
+ // `count * slots > limit` holds exactly when `count > limit / slots`.
3052
+ if (toF64(count) * toF64(slots) > toF64(REGEX_MAX_SLOT_TABLE)) {
2668
3053
  return "too many groups for the pattern's size"
2669
3054
  }
3055
+ // Unsigned: the test above bounds `count * slots`, and `states` is at
3056
+ // most `1 << REGEX_MAX_LEVELS` beside a count `emit` bounds.
3057
+ const table: i32 = toI32(toU32(count) * toU32(slots))
2670
3058
  this.cDense = regexZeros(count)
2671
- this.cSlots = regexZeros(count * slots)
3059
+ this.cSlots = regexZeros(table)
2672
3060
  this.nDense = regexZeros(count)
2673
- this.nSlots = regexZeros(count * slots)
3061
+ this.nSlots = regexZeros(table)
2674
3062
  const states: i32 = 1 << this.levels
2675
- this.visit = regexZeros(count * states)
3063
+ this.visit = regexZeros(toI32(toU32(count) * toU32(states)))
2676
3064
  this.caps = regexZeros(slots)
2677
3065
  this.mslots = regexZeros(slots)
2678
3066
  // Each instruction is visited once per closure and set of marks, and
2679
3067
  // pushes at most one frame, or one per slot it clears.
2680
- let frames: i32 = 1
3068
+ // Unsigned: a clear's range is a run of group slots, so `frames` stays
3069
+ // within the instruction count times the slot count, far from wrapping.
3070
+ const op: i32[] = this.op
3071
+ const ia: i32[] = this.ia
3072
+ const ib: i32[] = this.ib
3073
+ let frames: u32 = 1
2681
3074
  let pc: i32 = 0
2682
- while (pc < count) {
2683
- const o: i32 = this.op[pc]
3075
+ while (pc < toI32(op.length) && pc < count) {
3076
+ const o: i32 = op[pc]
2684
3077
  if (o === OP_SPLIT || o === OP_SAVE || o === OP_MARK) {
2685
3078
  frames += 1
2686
3079
  } else if (o === OP_CLEAR) {
2687
- frames += this.ib[pc] - this.ia[pc]
3080
+ frames += toU32(regexItem(ib, pc, 0)) - toU32(regexItem(ia, pc, 0))
2688
3081
  }
2689
3082
  pc += 1
2690
3083
  }
2691
- if (frames > REGEX_MAX_SLOT_TABLE / states) {
3084
+ if (toF64(frames) * toF64(states) > toF64(REGEX_MAX_SLOT_TABLE)) {
2692
3085
  return "too many groups for the pattern's size"
2693
3086
  }
2694
- frames *= states
2695
- this.stkKind = regexZeros(frames)
2696
- this.stkA = regexZeros(frames)
2697
- this.stkB = regexZeros(frames)
3087
+ // Unsigned: the test above keeps `frames * states` within the slot
3088
+ // table's limit, and three times that far from wrapping.
3089
+ this.stk = regexZeros(toI32(frames * toU32(states) * 3))
2698
3090
  this.findPrefilters()
2699
3091
  if (this.revOk && !this.anchored && this.prefix === "") {
2700
3092
  this.findInner(p, root)
@@ -2711,14 +3103,14 @@ export class Regex {
2711
3103
  * parts before it reversed, at `innerEntry`.
2712
3104
  */
2713
3105
  private findInner(p: RegexParser, root: i32): void {
2714
- if (p.kind[root] !== NODE_CAT) {
3106
+ if (p.kindOf(root) !== NODE_CAT) {
2715
3107
  return
2716
3108
  }
2717
3109
  const kids: i32[] = []
2718
- let c: i32 = p.kid[root]
3110
+ let c: i32 = p.kidOf(root)
2719
3111
  while (c !== -1) {
2720
3112
  kids.push(c)
2721
- c = p.next[c]
3113
+ c = p.nextOf(c)
2722
3114
  }
2723
3115
  const count: i32 = toI32(kids.length)
2724
3116
  let best: i32 = 0
@@ -2728,12 +3120,14 @@ export class Regex {
2728
3120
  while (i >= 0 && i < toI32(kids.length)) {
2729
3121
  // Read before the calls below, which end what the loop's test proves.
2730
3122
  const first: i32 = kids[i]
2731
- let n: i32 = 0
2732
- while (i + n < count && this.isAsciiChar(p, kids[i + n])) {
2733
- n += 1
3123
+ // Unsigned: `end` counts up from `i` and stops at `count`.
3124
+ let end: i32 = i
3125
+ while (end < count && this.isAsciiChar(p, regexItem(kids, end, REGEX_NONE))) {
3126
+ end = toI32(toU32(end) + 1)
2734
3127
  }
3128
+ const n: i32 = toI32(toU32(end) - toU32(i))
2735
3129
  if (n > 0) {
2736
- const b: i32 = p.na[first]
3130
+ const b: i32 = p.aOf(first)
2737
3131
  const score: i32 = regexByteScore(b)
2738
3132
  if (bestLen === 0 || score < bestScore || (score === bestScore && n > bestLen)) {
2739
3133
  let j: i32 = 0
@@ -2746,7 +3140,7 @@ export class Regex {
2746
3140
  bestScore = score
2747
3141
  }
2748
3142
  }
2749
- i += n
3143
+ i = end
2750
3144
  } else {
2751
3145
  i += 1
2752
3146
  }
@@ -2757,11 +3151,13 @@ export class Regex {
2757
3151
  const bytes: u8[] = regexZeroBytes(bestLen)
2758
3152
  i = 0
2759
3153
  while (i < toI32(bytes.length)) {
2760
- bytes[i] = toU8(p.na[kids[best + i]])
3154
+ // Unsigned: the run at `best` lies within the kids.
3155
+ bytes[i] = toU8(p.aOf(regexItem(kids, toI32(toU32(best) + toU32(i)), REGEX_NONE)))
2761
3156
  i += 1
2762
3157
  }
2763
3158
  this.innerEntry = this.pc()
2764
- i = best - 1
3159
+ // The run is never the first part, so `best` is at least 1.
3160
+ i = toI32(toU32(best) - 1)
2765
3161
  while (i >= 0 && i < toI32(kids.length)) {
2766
3162
  this.compileNode(p, kids[i], true)
2767
3163
  i -= 1
@@ -2771,36 +3167,36 @@ export class Regex {
2771
3167
  this.tooBig = false
2772
3168
  return
2773
3169
  }
2774
- this.innerLit = String.fromBytes(bytes, 0, bestLen)
3170
+ this.innerLit = regexBytesText(bytes)
2775
3171
  }
2776
3172
 
2777
3173
  /** Whether node `n` is one ASCII character, matched exactly. */
2778
3174
  private isAsciiChar(p: RegexParser, n: i32): boolean {
2779
- return p.kind[n] === NODE_CHAR && p.na[n] === p.nb[n] && p.na[n] < 128
3175
+ return p.kindOf(n) === NODE_CHAR && p.aOf(n) === p.bOf(n) && p.aOf(n) < 128
2780
3176
  }
2781
3177
 
2782
3178
  /** Whether node `n` can read the ASCII byte `b`. */
2783
3179
  private canConsume(p: RegexParser, n: i32, b: i32): boolean {
2784
- const kind: i32 = p.kind[n]
3180
+ const kind: i32 = p.kindOf(n)
2785
3181
  if (kind === NODE_CHAR) {
2786
- return p.na[n] === b || p.nb[n] === b
3182
+ return p.aOf(n) === b || p.bOf(n) === b
2787
3183
  }
2788
3184
  if (kind === NODE_CLASS) {
2789
- return this.classHas(p.na[n], b)
3185
+ return this.classHas(p.aOf(n), b)
2790
3186
  }
2791
3187
  if (kind === NODE_ANY) {
2792
3188
  return true
2793
3189
  }
2794
3190
  if (kind === NODE_GROUP || kind === NODE_REPEAT) {
2795
- return this.canConsume(p, p.kid[n], b)
3191
+ return this.canConsume(p, p.kidOf(n), b)
2796
3192
  }
2797
3193
  if (kind === NODE_CAT || kind === NODE_ALT) {
2798
- let c: i32 = p.kid[n]
3194
+ let c: i32 = p.kidOf(n)
2799
3195
  while (c !== -1) {
2800
3196
  if (this.canConsume(p, c, b)) {
2801
3197
  return true
2802
3198
  }
2803
- c = p.next[c]
3199
+ c = p.nextOf(c)
2804
3200
  }
2805
3201
  }
2806
3202
  return false
@@ -2808,16 +3204,20 @@ export class Regex {
2808
3204
 
2809
3205
  /** The literal prefix, whether the pattern is only that literal, the anchor, and the first-byte set. */
2810
3206
  private findPrefilters(): void {
3207
+ const op: i32[] = this.op
3208
+ const ia: i32[] = this.ia
3209
+ const ib: i32[] = this.ib
2811
3210
  const bytes: u8[] = []
3211
+ // The program ends in `OP_MATCH`, so the walk stops inside it.
2812
3212
  let pc: i32 = 0
2813
- while (true) {
2814
- const o: i32 = this.op[pc]
2815
- const a: i32 = this.ia[pc]
3213
+ while (pc >= 0 && pc < toI32(op.length)) {
3214
+ const o: i32 = op[pc]
3215
+ const a: i32 = regexItem(ia, pc, REGEX_NONE)
2816
3216
  if (o === OP_SAVE) {
2817
3217
  pc += 1
2818
3218
  continue
2819
3219
  }
2820
- if (o === OP_CHAR && a === this.ib[pc] && !(a >= 0xd800 && a <= 0xdfff)) {
3220
+ if (o === OP_CHAR && a === regexItem(ib, pc, REGEX_NONE) && !(a >= 0xd800 && a <= 0xdfff)) {
2821
3221
  regexPushUtf8(bytes, a)
2822
3222
  pc += 1
2823
3223
  continue
@@ -2826,25 +3226,29 @@ export class Regex {
2826
3226
  }
2827
3227
  const n: i32 = toI32(bytes.length)
2828
3228
  if (n > 0) {
2829
- this.prefix = String.fromBytes(bytes, 0, n)
2830
- this.literal = this.op[pc] === OP_MATCH && this.groups === 1
3229
+ this.prefix = regexBytesText(bytes)
3230
+ this.literal = regexItem(op, pc, REGEX_NONE) === OP_MATCH && this.groups === 1
2831
3231
  let best: i32 = 1000
3232
+ let rare: i32 = 0
2832
3233
  let i: i32 = 0
2833
3234
  while (i < toI32(bytes.length)) {
2834
3235
  const b: i32 = toI32(bytes[i])
2835
3236
  if (b < 128 && regexByteScore(b) < best) {
2836
3237
  best = regexByteScore(b)
2837
- this.rareAt = i
3238
+ rare = i
2838
3239
  }
2839
3240
  i += 1
2840
3241
  }
2841
- this.rareStr = this.prefix.slice(this.rareAt, this.rareAt + 1)
3242
+ this.rareAt = rare
3243
+ // Unsigned: `rare` is a byte of the prefix.
3244
+ this.rareStr = regexSpan(this.prefix, rare, toI32(toU32(rare) + 1))
2842
3245
  }
2843
3246
  pc = 0
2844
- while (this.op[pc] === OP_SAVE) {
3247
+ while (pc >= 0 && pc < toI32(op.length) && op[pc] === OP_SAVE) {
2845
3248
  pc += 1
2846
3249
  }
2847
- this.anchored = this.op[pc] === OP_ASSERT && this.ia[pc] === ASSERT_BEGIN_TEXT
3250
+ this.anchored =
3251
+ regexItem(op, pc, REGEX_NONE) === OP_ASSERT && regexItem(ia, pc, REGEX_NONE) === ASSERT_BEGIN_TEXT
2848
3252
  if (!this.anchored) {
2849
3253
  this.findScan()
2850
3254
  }
@@ -2862,6 +3266,9 @@ export class Regex {
2862
3266
  */
2863
3267
  private findScan(): void {
2864
3268
  const count: i32 = this.pc()
3269
+ const op: i32[] = this.op
3270
+ const ia: i32[] = this.ia
3271
+ const ib: i32[] = this.ib
2865
3272
  const masks: u8[] = regexZeroBytes(32 * REGEX_SCAN_DEPTH)
2866
3273
  const odds: i32[] = regexZeros(REGEX_SCAN_DEPTH)
2867
3274
  // `seen[at]` is the depth plus one at which `at` was last walked.
@@ -2889,21 +3296,24 @@ export class Regex {
2889
3296
  }
2890
3297
  const at: i32 = work[top]
2891
3298
  work.pop()
2892
- if (at < 0 || at >= toI32(seen.length) || seen[at] === depth + 1) {
3299
+ if (at < 0 || at >= toI32(seen.length) || at >= toI32(op.length) || seen[at] === depth + 1) {
2893
3300
  continue
2894
3301
  }
2895
3302
  seen[at] = depth + 1
2896
- const o: i32 = this.op[at]
3303
+ const o: i32 = op[at]
3304
+ // A target outside the program is skipped by the test above.
3305
+ const a: i32 = regexItem(ia, at, REGEX_NONE)
3306
+ const b: i32 = regexItem(ib, at, REGEX_NONE)
2897
3307
  if (o === OP_JMP) {
2898
- work.push(this.ia[at])
3308
+ work.push(a)
2899
3309
  } else if (o === OP_SPLIT) {
2900
- work.push(this.ib[at])
2901
- work.push(this.ia[at])
3310
+ work.push(b)
3311
+ work.push(a)
2902
3312
  } else if (o === OP_SAVE || o === OP_CLEAR || o === OP_ASSERT || o === OP_MARK || o === OP_CHECK) {
2903
3313
  work.push(at + 1)
2904
3314
  } else if (o === OP_CHAR) {
2905
- const lower: i32 = this.ia[at]
2906
- const other: i32 = this.ib[at]
3315
+ const lower: i32 = a
3316
+ const other: i32 = b
2907
3317
  if (lower >= 0 && lower < toI32(set.length)) {
2908
3318
  set[lower] = 1
2909
3319
  } else {
@@ -2916,13 +3326,25 @@ export class Regex {
2916
3326
  }
2917
3327
  next.push(at + 1)
2918
3328
  } else if (o === OP_CLASS) {
2919
- const k: i32 = this.ia[at]
2920
- if (this.clsNeg[k] === 1 || this.clsTo[k] > this.clsFrom[k]) {
3329
+ // A class the tables lack, which the parser never makes, only
3330
+ // widens the scan, and a wider scan is never wrong.
3331
+ const k: i32 = a
3332
+ const neg: u8[] = this.clsNeg
3333
+ if (
3334
+ k < 0 ||
3335
+ k >= toI32(neg.length) ||
3336
+ neg[k] === 1 ||
3337
+ regexItem(this.clsTo, k, 0) > regexItem(this.clsFrom, k, 0)
3338
+ ) {
2921
3339
  wide = true
2922
3340
  }
3341
+ // Unsigned: `k` is a class, and its table is 128 bytes of `ascii`.
3342
+ const ascii: u8[] = this.clsAscii
3343
+ const base: u32 = toU32(k) * 128
2923
3344
  let c: i32 = 0
2924
3345
  while (c >= 0 && c < toI32(set.length)) {
2925
- if (this.clsAscii[k * 128 + c] === 1) {
3346
+ const idx: i32 = toI32(base + toU32(c))
3347
+ if (idx >= 0 && idx < toI32(ascii.length) && ascii[idx] === 1) {
2926
3348
  set[c] = 1
2927
3349
  }
2928
3350
  c += 1
@@ -2944,15 +3366,16 @@ export class Regex {
2944
3366
  if (ended) {
2945
3367
  break
2946
3368
  }
2947
- let odd: i32 = wide ? 100 : 0
3369
+ // Unsigned: at most 100 and a thousandth per ASCII byte.
3370
+ let odd: u32 = wide ? 100 : 0
2948
3371
  let c: i32 = 0
2949
3372
  while (c >= 0 && c < toI32(set.length)) {
2950
3373
  const row: i32 = (depth << 5) | (c & 15)
2951
3374
  // The second test always holds: it is what proves `row`.
2952
3375
  if (set[c] === 1 && row >= 0 && row < toI32(masks.length)) {
2953
3376
  const bit: i32 = toI32(1) << (c >> 4)
2954
- masks[row] |= toU8(bit)
2955
- odd += regexBytePerMille(c)
3377
+ masks[row] = masks[row] | toU8(bit)
3378
+ odd += toU32(regexBytePerMille(c))
2956
3379
  }
2957
3380
  c += 1
2958
3381
  }
@@ -2967,15 +3390,16 @@ export class Regex {
2967
3390
  }
2968
3391
  }
2969
3392
  if (depth < toI32(odds.length)) {
2970
- odds[depth] = odd > 1000 ? 1000 : odd
3393
+ odds[depth] = odd > 1000 ? 1000 : toI32(odd)
2971
3394
  }
2972
3395
  if (depth === 0 && this.prefix === "") {
2973
3396
  // The first position also feeds the VM's table, which wants a set
2974
3397
  // that leaves some byte out.
2975
- let m: i32 = 0
3398
+ // Unsigned: a count of the 128 ASCII bytes.
3399
+ let m: u32 = 0
2976
3400
  c = 0
2977
3401
  while (c >= 0 && c < toI32(set.length)) {
2978
- m += toI32(set[c])
3402
+ m += toU32(set[c])
2979
3403
  c += 1
2980
3404
  }
2981
3405
  if (!wide || m < 128) {
@@ -2990,24 +3414,26 @@ export class Regex {
2990
3414
  frontier = next
2991
3415
  }
2992
3416
  // No position, or a first one that can be inside a character: no scan.
2993
- if (depth === 0 || masks[16] !== 0) {
3417
+ if (depth === 0 || (toI32(masks.length) > 16 && masks[16] !== 0)) {
2994
3418
  return
2995
3419
  }
2996
3420
  this.scanDepth = depth
2997
3421
  // The 32-byte forms the vector window reads, then a membership table of
2998
3422
  // 256 bytes a position, which the walker and the kernel's other positions
2999
- // read a load a byte.
3000
- const both: u8[] = regexZeroBytes(288 * depth)
3423
+ // read a load a byte. Unsigned: `depth` is at most `REGEX_SCAN_DEPTH`.
3424
+ const udepth: u32 = toU32(depth)
3425
+ const vec: i32 = toI32(32 * udepth)
3426
+ const both: u8[] = regexZeroBytes(toI32(288 * udepth))
3001
3427
  let at: i32 = 0
3002
- while (at >= 0 && at < 32 * depth && at < toI32(both.length) && at < toI32(masks.length)) {
3428
+ while (at >= 0 && at < vec && at < toI32(both.length) && at < toI32(masks.length)) {
3003
3429
  both[at] = masks[at]
3004
3430
  at += 1
3005
3431
  }
3006
3432
  at = 0
3007
- while (at >= 0 && at < 256 * depth) {
3433
+ while (at >= 0 && at < toI32(256 * udepth)) {
3008
3434
  const b: i32 = at & 255
3009
3435
  const row: i32 = ((at >> 8) << 5) | ((b >> 7) << 4) | (b & 15)
3010
- const slot: i32 = 32 * depth + at
3436
+ const slot: i32 = toI32(toU32(vec) + toU32(at))
3011
3437
  if (slot >= 0 && slot < toI32(both.length) && row >= 0 && row < toI32(masks.length)) {
3012
3438
  both[slot] = toU8((toI32(masks[row]) >> ((b >> 4) & 7)) & 1)
3013
3439
  }
@@ -3024,23 +3450,26 @@ export class Regex {
3024
3450
  while (true) {
3025
3451
  all = 1
3026
3452
  best = -1
3027
- let off: i32 = 0
3028
- while (off + width <= depth) {
3029
- let odd: i32 = 1
3030
- let j: i32 = 0
3031
- while (j < width) {
3032
- odd *= odds[off + j]
3453
+ // Unsigned: offsets and widths are positions below
3454
+ // `REGEX_SCAN_DEPTH`, and a product is at most `REGEX_SCAN_WIDTH`
3455
+ // factors of at most 1000.
3456
+ let off: u32 = 0
3457
+ while (off + toU32(width) <= toU32(depth)) {
3458
+ let odd: u32 = 1
3459
+ let j: u32 = 0
3460
+ while (j < toU32(width)) {
3461
+ odd *= toU32(regexItem(odds, toI32(off + j), 0))
3033
3462
  j += 1
3034
3463
  }
3035
- if (best < 0 || odd < best) {
3036
- best = odd
3037
- this.scanOff = off
3464
+ if (best < 0 || toI32(odd) < best) {
3465
+ best = toI32(odd)
3466
+ this.scanOff = toI32(off)
3038
3467
  }
3039
3468
  off += 1
3040
3469
  }
3041
3470
  let j: i32 = 0
3042
3471
  while (j < width) {
3043
- all *= 1000
3472
+ all = toI32(toU32(all) * 1000)
3044
3473
  j += 1
3045
3474
  }
3046
3475
  if (width >= most || best < all / 256) {
@@ -3074,7 +3503,8 @@ export class Regex {
3074
3503
  while (k < 32) {
3075
3504
  const bit: i32 = toI32(1) << (c >> 4)
3076
3505
  const want: i32 = k === (c & 15) ? bit : 0
3077
- if (toI32(this.posMasks[32 * j + k]) !== want) {
3506
+ // Unsigned: the prefix fits the scan's positions, tested above.
3507
+ if (regexByteItem(this.posMasks, toI32(32 * toU32(j) + toU32(k))) !== want) {
3078
3508
  return false
3079
3509
  }
3080
3510
  k += 1
@@ -3087,27 +3517,49 @@ export class Regex {
3087
3517
  /** Whether class `k` holds code point `cp`, which is at least 0. */
3088
3518
  private classHas(k: i32, cp: i32): boolean {
3089
3519
  if (cp < 128) {
3090
- return this.clsAscii[k * 128 + cp] === 1
3520
+ // Unsigned: a class number is below the instruction limit, so its
3521
+ // 128-byte row stays inside an i32.
3522
+ const at: i32 = toI32(toU32(k) * 128 + toU32(cp))
3523
+ const ascii: u8[] = this.clsAscii
3524
+ return at >= 0 && at < toI32(ascii.length) && ascii[at] === 1
3525
+ }
3526
+ const from: i32[] = this.clsFrom
3527
+ const to: i32[] = this.clsTo
3528
+ const neg: u8[] = this.clsNeg
3529
+ // `finishClass` stores all three for every class it numbers.
3530
+ if (k < 0 || k >= toI32(from.length) || k >= toI32(to.length) || k >= toI32(neg.length)) {
3531
+ return false
3091
3532
  }
3092
- let lo: i32 = this.clsFrom[k]
3093
- let hi: i32 = this.clsTo[k]
3533
+ const los: i32[] = this.clsLo
3534
+ const his: i32[] = this.clsHi
3535
+ // Unsigned, so the midpoint's sum cannot wrap.
3536
+ let lo: u32 = toU32(from[k])
3537
+ let hi: u32 = toU32(to[k])
3094
3538
  let found: boolean = false
3095
3539
  while (lo < hi) {
3096
- const mid: i32 = (lo + hi) >> 1
3097
- if (cp < this.clsLo[mid]) {
3098
- hi = mid
3099
- } else if (cp > this.clsHi[mid]) {
3100
- lo = mid + 1
3540
+ const mid: i32 = toI32((lo + hi) >>> 1)
3541
+ // A class's ranges are inside the range tables.
3542
+ if (mid < 0 || mid >= toI32(los.length) || mid >= toI32(his.length)) {
3543
+ break
3544
+ }
3545
+ if (cp < los[mid]) {
3546
+ hi = toU32(mid)
3547
+ } else if (cp > his[mid]) {
3548
+ lo = toU32(mid) + 1
3101
3549
  } else {
3102
3550
  found = true
3103
3551
  break
3104
3552
  }
3105
3553
  }
3106
- return found !== (this.clsNeg[k] === 1)
3554
+ return found !== (neg[k] === 1)
3107
3555
  }
3108
3556
 
3109
- /** Whether assertion `kind` holds at byte `p` of `text`. */
3110
- private assertHolds(kind: i32, text: string, p: i32, len: i32): boolean {
3557
+ /** Whether assertion `kind` holds at byte `p` of `text`, which is in or at the end of it. */
3558
+ private assertHolds(kind: i32, text: string, p: i32): boolean {
3559
+ const len: i32 = toI32(text.length)
3560
+ if (p < 0 || p > len) {
3561
+ return false
3562
+ }
3111
3563
  if (kind === ASSERT_BEGIN_TEXT) {
3112
3564
  return p === 0
3113
3565
  }
@@ -3118,7 +3570,7 @@ export class Regex {
3118
3570
  if (p === 0) {
3119
3571
  return true
3120
3572
  }
3121
- const b: i32 = toI32(text.charCodeAt(p - 1))
3573
+ const b: i32 = regexByteAt(text, p - 1)
3122
3574
  if (b === 10 || b === 13) {
3123
3575
  return true
3124
3576
  }
@@ -3126,8 +3578,8 @@ export class Regex {
3126
3578
  return (
3127
3579
  (b === 0xa8 || b === 0xa9) &&
3128
3580
  p >= 3 &&
3129
- toI32(text.charCodeAt(p - 2)) === 0x80 &&
3130
- toI32(text.charCodeAt(p - 3)) === 0xe2
3581
+ regexByteAt(text, p - 2) === 0x80 &&
3582
+ regexByteAt(text, p - 3) === 0xe2
3131
3583
  )
3132
3584
  }
3133
3585
  if (kind === ASSERT_END_LINE) {
@@ -3138,17 +3590,17 @@ export class Regex {
3138
3590
  if (b === 10 || b === 13) {
3139
3591
  return true
3140
3592
  }
3141
- if (b !== 0xe2 || p + 2 >= len) {
3593
+ if (b !== 0xe2 || p >= len - 2) {
3142
3594
  return false
3143
3595
  }
3144
3596
  const b2: i32 = toI32(text.charCodeAt(p + 2))
3145
3597
  return toI32(text.charCodeAt(p + 1)) === 0x80 && (b2 === 0xa8 || b2 === 0xa9)
3146
3598
  }
3147
- let before: boolean = p > 0 && isRegexWordByte(toI32(text.charCodeAt(p - 1)))
3599
+ let before: boolean = p > 0 && isRegexWordByte(regexByteAt(text, p - 1))
3148
3600
  let after: boolean = p < len && isRegexWordByte(toI32(text.charCodeAt(p)))
3149
3601
  if (kind >= ASSERT_WORD_FOLD) {
3150
3602
  before = before || regexFoldWordBefore(text, p)
3151
- after = after || regexFoldWordAt(text, p, len)
3603
+ after = after || regexFoldWordAt(text, p)
3152
3604
  return kind === ASSERT_WORD_FOLD ? before !== after : before === after
3153
3605
  }
3154
3606
  return kind === ASSERT_WORD ? before !== after : before === after
@@ -3156,16 +3608,17 @@ export class Regex {
3156
3608
 
3157
3609
  /** Start a closure that has visited nothing, wrapping the stamps before they overflow. */
3158
3610
  private newGeneration(): void {
3159
- if (this.gen >= 2147483647) {
3160
- const count: i32 = toI32(this.visit.length)
3611
+ let g: i32 = this.gen
3612
+ if (g >= 2147483647) {
3613
+ const visit: i32[] = this.visit
3161
3614
  let pc: i32 = 0
3162
- while (pc < count) {
3163
- this.visit[pc] = 0
3615
+ while (pc < toI32(visit.length)) {
3616
+ visit[pc] = 0
3164
3617
  pc += 1
3165
3618
  }
3166
- this.gen = 0
3619
+ g = 0
3167
3620
  }
3168
- this.gen += 1
3621
+ this.gen = g + 1
3169
3622
  }
3170
3623
 
3171
3624
  /**
@@ -3179,80 +3632,111 @@ export class Regex {
3179
3632
  * thread that was here before it, since the check it meets next fails for
3180
3633
  * it alone. `(?:a*?)*` on "aa" goes on to the second `a` that way.
3181
3634
  */
3182
- private addThread(
3183
- dense: i32[],
3184
- slots: i32[],
3185
- count0: i32,
3186
- pc0: i32,
3187
- text: string,
3188
- p: i32,
3189
- len: i32,
3190
- ns: i32
3191
- ): i32 {
3635
+ private addThread(dense: i32[], slots: i32[], count0: i32, pc0: i32, text: string, p: i32, ns: i32): i32 {
3192
3636
  let count: i32 = count0
3193
- const states: i32 = 1 << this.levels
3194
- this.stkKind[0] = 0
3195
- this.stkA[0] = pc0
3196
- this.stkB[0] = 0
3197
- let top: i32 = 1
3637
+ const levels: i32 = this.levels
3638
+ const states: i32 = 1 << levels
3639
+ const op: i32[] = this.op
3640
+ const ia: i32[] = this.ia
3641
+ const ib: i32[] = this.ib
3642
+ const visit: i32[] = this.visit
3643
+ const caps: i32[] = this.caps
3644
+ const stk: i32[] = this.stk
3645
+ const gen: i32 = this.gen
3646
+ // `build` sizes the stack for every frame a closure can push, so the
3647
+ // tests on `top` below never turn a frame away.
3648
+ if (toI32(stk.length) < 3) {
3649
+ return count
3650
+ }
3651
+ stk[0] = 0
3652
+ stk[1] = pc0
3653
+ stk[2] = 0
3654
+ // Unsigned: the stack's size bounds it, as the tests on each push show.
3655
+ let top: u32 = 1
3198
3656
  while (top > 0) {
3199
3657
  top -= 1
3200
- if (this.stkKind[top] === 1) {
3201
- this.caps[this.stkA[top]] = this.stkB[top]
3658
+ const f: i32 = toI32(top * 3)
3659
+ if (f < 0 || f > toI32(stk.length) - 3) {
3660
+ break
3661
+ }
3662
+ if (stk[f] === 1) {
3663
+ const k: i32 = stk[f + 1]
3664
+ if (k >= 0 && k < toI32(caps.length)) {
3665
+ caps[k] = stk[f + 2]
3666
+ }
3202
3667
  continue
3203
3668
  }
3204
- let pc: i32 = this.stkA[top]
3205
- let marked: i32 = this.stkB[top]
3669
+ let pc: i32 = stk[f + 1]
3670
+ let marked: i32 = stk[f + 2]
3206
3671
  while (true) {
3207
- const o: i32 = this.op[pc]
3208
- const at: i32 = o >= OP_SPLIT && o !== OP_MATCH ? pc * states + marked : pc * states
3209
- if (this.visit[at] === this.gen) {
3672
+ // Every jump the compiler emits lands inside the program, and every
3673
+ // instruction has both operands, so this test never ends a thread.
3674
+ if (pc < 0 || pc >= toI32(op.length) || pc >= toI32(ia.length) || pc >= toI32(ib.length)) {
3675
+ break
3676
+ }
3677
+ const o: i32 = op[pc]
3678
+ // Unsigned: `visit` holds `states` stamps for each pc, and `marked`
3679
+ // is below `states`, so the slot is inside a table that fits an i32.
3680
+ const row: u32 = toU32(pc) * toU32(states)
3681
+ const at: i32 = toI32(o >= OP_SPLIT && o !== OP_MATCH ? row + toU32(marked) : row)
3682
+ if (at < 0 || at >= toI32(visit.length) || visit[at] === gen) {
3210
3683
  break
3211
3684
  }
3212
- this.visit[at] = this.gen
3685
+ visit[at] = gen
3213
3686
  if (o === OP_JMP) {
3214
- pc = this.ia[pc]
3687
+ pc = ia[pc]
3215
3688
  continue
3216
3689
  }
3690
+ const push: i32 = toI32(top * 3)
3217
3691
  if (o === OP_SPLIT) {
3218
- this.stkKind[top] = 0
3219
- this.stkA[top] = this.ib[pc]
3220
- this.stkB[top] = marked
3692
+ if (push < 0 || push > toI32(stk.length) - 3) {
3693
+ break
3694
+ }
3695
+ stk[push] = 0
3696
+ stk[push + 1] = ib[pc]
3697
+ stk[push + 2] = marked
3221
3698
  top += 1
3222
- pc = this.ia[pc]
3699
+ pc = ia[pc]
3223
3700
  continue
3224
3701
  }
3225
3702
  if (o === OP_SAVE || o === OP_MARK) {
3226
- const k: i32 = this.ia[pc]
3227
- if (k < ns) {
3228
- this.stkKind[top] = 1
3229
- this.stkA[top] = k
3230
- this.stkB[top] = this.caps[k]
3703
+ const k: i32 = ia[pc]
3704
+ if (k >= 0 && k < ns && k < toI32(caps.length)) {
3705
+ if (push < 0 || push > toI32(stk.length) - 3) {
3706
+ break
3707
+ }
3708
+ stk[push] = 1
3709
+ stk[push + 1] = k
3710
+ stk[push + 2] = caps[k]
3231
3711
  top += 1
3232
- this.caps[k] = p
3233
- if (o === OP_MARK && this.ib[pc] < this.levels) {
3234
- marked = marked | (1 << this.ib[pc])
3712
+ caps[k] = p
3713
+ if (o === OP_MARK && ib[pc] < levels) {
3714
+ marked = marked | (1 << ib[pc])
3235
3715
  }
3236
3716
  }
3237
3717
  pc += 1
3238
3718
  continue
3239
3719
  }
3240
3720
  if (o === OP_CLEAR) {
3241
- let k: i32 = this.ia[pc]
3242
- const end: i32 = this.ib[pc] < ns ? this.ib[pc] : ns
3243
- while (k < end) {
3244
- this.stkKind[top] = 1
3245
- this.stkA[top] = k
3246
- this.stkB[top] = this.caps[k]
3721
+ let k: i32 = ia[pc]
3722
+ const end: i32 = ib[pc] < ns ? ib[pc] : ns
3723
+ while (k >= 0 && k < end && k < toI32(caps.length)) {
3724
+ const g: i32 = toI32(top * 3)
3725
+ if (g < 0 || g > toI32(stk.length) - 3) {
3726
+ break
3727
+ }
3728
+ stk[g] = 1
3729
+ stk[g + 1] = k
3730
+ stk[g + 2] = caps[k]
3247
3731
  top += 1
3248
- this.caps[k] = -1
3732
+ caps[k] = -1
3249
3733
  k += 1
3250
3734
  }
3251
3735
  pc += 1
3252
3736
  continue
3253
3737
  }
3254
3738
  if (o === OP_ASSERT) {
3255
- if (this.assertHolds(this.ia[pc], text, p, len)) {
3739
+ if (this.assertHolds(ia[pc], text, p)) {
3256
3740
  pc += 1
3257
3741
  continue
3258
3742
  }
@@ -3261,8 +3745,8 @@ export class Regex {
3261
3745
  if (o === OP_CHECK) {
3262
3746
  // Untracked marks only reorder matches, never add or remove one,
3263
3747
  // so a search that wants no slots lets every iteration through.
3264
- const k: i32 = this.ia[pc]
3265
- if (k < ns && this.caps[k] === p) {
3748
+ const k: i32 = ia[pc]
3749
+ if (k >= 0 && k < ns && k < toI32(caps.length) && caps[k] === p) {
3266
3750
  break
3267
3751
  }
3268
3752
  pc += 1
@@ -3273,12 +3757,7 @@ export class Regex {
3273
3757
  dense[count] = pc
3274
3758
  count += 1
3275
3759
  }
3276
- const base: i32 = pc * ns
3277
- let k: i32 = 0
3278
- while (k < ns) {
3279
- slots[base + k] = this.caps[k]
3280
- k += 1
3281
- }
3760
+ regexStoreSlots(slots, toI32(toU32(pc) * toU32(ns)), caps, ns)
3282
3761
  break
3283
3762
  }
3284
3763
  }
@@ -3293,29 +3772,30 @@ export class Regex {
3293
3772
  if (this.scanSel) {
3294
3773
  return this.findPrefixScan(text, from)
3295
3774
  }
3296
- if (this.rareAt === 0) {
3297
- return toI32(text.indexOf(this.prefix, from))
3775
+ const prefix: string = this.prefix
3776
+ const rareAt: i32 = this.rareAt
3777
+ if (rareAt <= 0) {
3778
+ return toI32(text.indexOf(prefix, from))
3298
3779
  }
3299
3780
  const len: i32 = toI32(text.length)
3300
- const n: i32 = toI32(this.prefix.length)
3301
- let p: i32 = from + this.rareAt
3781
+ const n: i32 = toI32(prefix.length)
3782
+ // Unsigned: `from` is in the text and `rareAt` inside the prefix.
3783
+ let p: i32 = toI32(toU32(from) + toU32(rareAt))
3302
3784
  while (true) {
3303
3785
  const at: i32 = toI32(text.indexOf(this.rareStr, p))
3304
3786
  if (at < 0) {
3305
3787
  return -1
3306
3788
  }
3307
- const s: i32 = at - this.rareAt
3308
- if (s > len - n) {
3789
+ // `at` is at or after `p`, so `s` is at or after `from`.
3790
+ const s: i32 = at - rareAt
3791
+ if (s < 0 || s > len - n) {
3309
3792
  return -1
3310
3793
  }
3311
- let k: i32 = 0
3312
- while (k < n && text.charCodeAt(s + k) === this.prefix.charCodeAt(k)) {
3313
- k += 1
3314
- }
3315
- if (k === n) {
3794
+ if (regexPrefixAt(text, s, prefix)) {
3316
3795
  return s
3317
3796
  }
3318
- p = at + 1
3797
+ // `at` is a byte of the text, so one past it does not wrap.
3798
+ p = toI32(toU32(at) + 1)
3319
3799
  }
3320
3800
  }
3321
3801
 
@@ -3336,14 +3816,11 @@ export class Regex {
3336
3816
  if (s > len - n) {
3337
3817
  return -1
3338
3818
  }
3339
- let k: i32 = 0
3340
- while (k < n && text.charCodeAt(s + k) === this.prefix.charCodeAt(k)) {
3341
- k += 1
3342
- }
3343
- if (k >= n) {
3819
+ if (regexPrefixAt(text, s, this.prefix)) {
3344
3820
  return s
3345
3821
  }
3346
- p = s + 1
3822
+ // `s` is in the text, so one past it does not wrap.
3823
+ p = toI32(toU32(s) + 1)
3347
3824
  }
3348
3825
  return -1
3349
3826
  }
@@ -3379,17 +3856,20 @@ export class Regex {
3379
3856
  this.dReady = true
3380
3857
  const total: i32 = this.pc()
3381
3858
  const count: i32 = this.fwdCount
3859
+ const op: i32[] = this.op
3860
+ const ia: i32[] = this.ia
3861
+ const ib: i32[] = this.ib
3382
3862
  let bitsF: i32 = 0
3383
3863
  let bitsR: i32 = 0
3384
3864
  let anyNl: boolean = false
3385
3865
  let pc: i32 = 0
3386
- while (pc < total) {
3387
- const o: i32 = this.op[pc]
3866
+ while (pc < total && pc < toI32(op.length)) {
3867
+ const o: i32 = op[pc]
3388
3868
  if (o === OP_ASSERT) {
3389
3869
  if (pc < count) {
3390
- bitsF |= 1 << this.ia[pc]
3870
+ bitsF |= 1 << regexItem(ia, pc, 0)
3391
3871
  } else {
3392
- bitsR |= 1 << this.ia[pc]
3872
+ bitsR |= 1 << regexItem(ia, pc, 0)
3393
3873
  }
3394
3874
  } else if (o === OP_ANY_NL) {
3395
3875
  anyNl = true
@@ -3410,31 +3890,36 @@ export class Regex {
3410
3890
  const classes: i32 = toI32(this.clsFrom.length)
3411
3891
  const seenCls: u8[] = regexZeroBytes(classes)
3412
3892
  const points: i32[] = []
3893
+ const clsLo: i32[] = this.clsLo
3894
+ const clsHi: i32[] = this.clsHi
3895
+ // Unsigned: a code point is at most U+10FFFF, so one past it fits.
3413
3896
  pc = 0
3414
- while (pc < count) {
3415
- const o: i32 = this.op[pc]
3897
+ while (pc < count && pc < toI32(op.length)) {
3898
+ const o: i32 = op[pc]
3899
+ const a: i32 = regexItem(ia, pc, REGEX_NONE)
3900
+ const b: i32 = regexItem(ib, pc, REGEX_NONE)
3416
3901
  if (o === OP_CHAR) {
3417
- const a: i32 = this.ia[pc]
3418
- const b: i32 = this.ib[pc]
3419
3902
  if (a >= 128) {
3420
3903
  points.push(a)
3421
- points.push(a + 1)
3904
+ points.push(toI32(toU32(a) + 1))
3422
3905
  }
3423
3906
  if (b >= 128 && b !== a) {
3424
3907
  points.push(b)
3425
- points.push(b + 1)
3908
+ points.push(toI32(toU32(b) + 1))
3426
3909
  }
3427
3910
  } else if (o === OP_CLASS) {
3428
- const k: i32 = this.ia[pc]
3911
+ const k: i32 = a
3429
3912
  if (k >= 0 && k < toI32(seenCls.length) && seenCls[k] === 0) {
3430
3913
  seenCls[k] = 1
3431
- let r: i32 = this.clsFrom[k]
3432
- while (r < this.clsTo[k]) {
3433
- const hi: i32 = this.clsHi[r]
3914
+ // The class's ranges, which `finishClass` stores inside the tables.
3915
+ let r: i32 = regexItem(this.clsFrom, k, 0)
3916
+ const to: i32 = regexItem(this.clsTo, k, 0)
3917
+ while (r >= 0 && r < to && r < toI32(clsHi.length) && r < toI32(clsLo.length)) {
3918
+ const hi: i32 = clsHi[r]
3919
+ const lo: i32 = clsLo[r]
3434
3920
  if (hi >= 128) {
3435
- const lo: i32 = this.clsLo[r]
3436
3921
  points.push(lo > 128 ? lo : 128)
3437
- points.push(hi + 1)
3922
+ points.push(toI32(toU32(hi) + 1))
3438
3923
  }
3439
3924
  r += 1
3440
3925
  }
@@ -3454,37 +3939,40 @@ export class Regex {
3454
3939
  }
3455
3940
  regexSortPoints(points)
3456
3941
  const lows: i32[] = [128]
3942
+ let last: i32 = 128
3457
3943
  let i: i32 = 0
3458
3944
  while (i < toI32(points.length)) {
3459
3945
  const v: i32 = points[i]
3460
- if (v > lows[toI32(lows.length) - 1]) {
3946
+ if (v > last) {
3461
3947
  lows.push(v)
3948
+ last = v
3462
3949
  }
3463
3950
  i += 1
3464
3951
  }
3465
3952
  const wide: i32 = toI32(lows.length)
3466
- const units: i32 = 129 + wide
3953
+ // Unsigned: `wide` counts the boundaries, far below 2^31, and the test
3954
+ // below bounds `units` for the products after it.
3955
+ const units: i32 = toI32(129 + toU32(wide))
3467
3956
  if (units > DFA_MAX_UNITS) {
3468
3957
  return
3469
3958
  }
3470
- // Not `regexZeros`, for `regexSortPoints`' reason; every slot is written first.
3471
- const rep: i32[] = new Array<i32>(units)
3959
+ // Each unit's representative: the ASCII bytes, the end of the text, then
3960
+ // the first code point of each range above ASCII.
3961
+ const rep: i32[] = []
3472
3962
  let u: i32 = 0
3473
- while (u < 128 && u < toI32(rep.length)) {
3474
- rep[u] = u
3963
+ while (u < 128) {
3964
+ rep.push(u)
3475
3965
  u += 1
3476
3966
  }
3477
- rep[128] = -1
3478
- i = 0
3479
- while (i < wide && 129 + i < toI32(rep.length)) {
3480
- rep[129 + i] = lows[i]
3481
- i += 1
3967
+ rep.push(-1)
3968
+ for (const low of lows) {
3969
+ rep.push(low)
3482
3970
  }
3483
3971
  // Refine one predicate at a time: a unit's class is its old class and
3484
3972
  // whether the predicate holds for it.
3485
3973
  const cls: i32[] = regexZeros(units)
3486
3974
  const hit: u8[] = regexZeroBytes(units)
3487
- const remap: i32[] = regexZeros(2 * units)
3975
+ const remap: i32[] = regexZeros(toI32(2 * toU32(units)))
3488
3976
  let n: i32 = 1
3489
3977
  let work: i32 = 0
3490
3978
  u = 0
@@ -3500,14 +3988,15 @@ export class Regex {
3500
3988
  i += 1
3501
3989
  }
3502
3990
  pc = 0
3503
- while (pc < count) {
3504
- const o: i32 = this.op[pc]
3505
- const a: i32 = this.ia[pc]
3506
- const b: i32 = this.ib[pc]
3991
+ while (pc < count && pc < toI32(op.length)) {
3992
+ const o: i32 = op[pc]
3993
+ const a: i32 = regexItem(ia, pc, REGEX_NONE)
3994
+ const b: i32 = regexItem(ib, pc, REGEX_NONE)
3507
3995
  let use: boolean = false
3508
3996
  if (o === OP_CHAR) {
3509
3997
  if (a < 128 && b < 128) {
3510
- const key: i32 = a * 128 + b
3998
+ // Unsigned: both are ASCII here.
3999
+ const key: i32 = toI32(toU32(a) * 128 + toU32(b))
3511
4000
  // Always true for a character: the test is what proves `key`.
3512
4001
  if (key >= 0 && key < toI32(pairs.length)) {
3513
4002
  use = pairs[key] === 0
@@ -3543,7 +4032,8 @@ export class Regex {
3543
4032
  }
3544
4033
  }
3545
4034
  if (use) {
3546
- work += units
4035
+ // Unsigned: the test below stops `work` at a bound far from wrapping.
4036
+ work = toI32(toU32(work) + toU32(units))
3547
4037
  if (work > DFA_MAX_WORK) {
3548
4038
  return
3549
4039
  }
@@ -3576,40 +4066,48 @@ export class Regex {
3576
4066
  // One column past the classes for every byte above ASCII, whose entries
3577
4067
  // stay 0: the hot loop reads such a byte as a transition it cannot take
3578
4068
  // and leaves for the slow path, which decodes the character.
4069
+ // Unsigned: `n` is at most `units`, so `shift` stops far below 31.
3579
4070
  let shift: i32 = 0
3580
- while (1 << shift < n + 1) {
3581
- shift += 1
4071
+ while (1 << shift < toI32(toU32(n) + 1)) {
4072
+ shift = toI32(toU32(shift) + 1)
3582
4073
  }
3583
4074
  if (shift > DFA_MAX_SHIFT) {
3584
4075
  return
3585
4076
  }
3586
4077
  this.dShift = shift
3587
- this.dCls = regexZeros(256)
4078
+ const dcls: i32[] = regexZeros(256)
3588
4079
  u = 0
3589
- while (u < 256) {
3590
- this.dCls[u] = u < 128 && u < toI32(cls.length) ? cls[u] : n
4080
+ while (u < toI32(dcls.length)) {
4081
+ dcls[u] = u < 128 && u < toI32(cls.length) ? cls[u] : n
3591
4082
  u += 1
3592
4083
  }
3593
- this.dEot = cls[128]
4084
+ this.dCls = dcls
4085
+ // Unit 128 is the end of the text, and the units after it the ranges.
4086
+ this.dEot = regexItem(cls, 128, 0)
3594
4087
  this.dWlo = lows
3595
4088
  this.dWide = wide
3596
- this.dWcls = regexZeros(wide)
4089
+ const wcls: i32[] = regexZeros(wide)
3597
4090
  i = 0
3598
- while (i < wide && 129 + i < toI32(cls.length)) {
3599
- this.dWcls[i] = cls[129 + i]
4091
+ while (i < toI32(wcls.length)) {
4092
+ wcls[i] = regexItem(cls, toI32(129 + toU32(i)), 0)
3600
4093
  i += 1
3601
4094
  }
4095
+ this.dWcls = wcls
3602
4096
  // Each class's representative and kind: the first unit in it.
3603
4097
  this.dMaskF = regexContextMask(bitsF)
3604
4098
  this.dMaskR = regexContextMask(bitsR)
3605
4099
  this.dCtxFree =
3606
- this.dMaskF[CTX_START] === CTX_OTHER &&
3607
- this.dMaskF[CTX_WORD] === CTX_OTHER &&
3608
- this.dMaskF[CTX_LINE] === CTX_OTHER
3609
- this.dRep = regexZeros(n)
3610
- this.dNextK = regexZeros(n)
3611
- this.dCtxF = regexZeros(n)
3612
- this.dCtxR = regexZeros(n)
4100
+ regexMaskOf(this.dMaskF, CTX_START) === CTX_OTHER &&
4101
+ regexMaskOf(this.dMaskF, CTX_WORD) === CTX_OTHER &&
4102
+ regexMaskOf(this.dMaskF, CTX_LINE) === CTX_OTHER
4103
+ const dRep: i32[] = regexZeros(n)
4104
+ const dNextK: i32[] = regexZeros(n)
4105
+ const dCtxF: i32[] = regexZeros(n)
4106
+ const dCtxR: i32[] = regexZeros(n)
4107
+ this.dRep = dRep
4108
+ this.dNextK = dNextK
4109
+ this.dCtxF = dCtxF
4110
+ this.dCtxR = dCtxR
3613
4111
  const done: u8[] = regexZeroBytes(n)
3614
4112
  u = 0
3615
4113
  while (u < toI32(cls.length) && u < toI32(rep.length)) {
@@ -3617,7 +4115,8 @@ export class Regex {
3617
4115
  const r: i32 = rep[u]
3618
4116
  if (c >= 0 && c < toI32(done.length) && done[c] === 0) {
3619
4117
  done[c] = 1
3620
- this.dRep[c] = r
4118
+ // All five tables hold `n` classes.
4119
+ regexPut(dRep, c, r)
3621
4120
  let kind: i32 = CTX_OTHER
3622
4121
  if (r < 0) {
3623
4122
  kind = CTX_EOT
@@ -3626,16 +4125,17 @@ export class Regex {
3626
4125
  } else if ((r < 128 && isRegexWordByte(r)) || (wordFold && (r === 0x17f || r === 0x212a))) {
3627
4126
  kind = CTX_WORD
3628
4127
  }
3629
- this.dNextK[c] = kind
3630
- this.dCtxF[c] = this.dMaskF[kind]
3631
- this.dCtxR[c] = this.dMaskR[kind]
4128
+ regexPut(dNextK, c, kind)
4129
+ regexPut(dCtxF, c, regexMaskOf(this.dMaskF, kind))
4130
+ regexPut(dCtxR, c, regexMaskOf(this.dMaskR, kind))
3632
4131
  }
3633
4132
  u += 1
3634
4133
  }
3635
4134
  this.dVisit = regexZeros(total)
3636
4135
  this.dLeaf = regexZeros(total)
3637
4136
  this.dBuf = regexZeros(total)
3638
- this.dStk = regexZeros(total + 1)
4137
+ // Unsigned: `emit` bounds the program at `REGEX_MAX_INSTRUCTIONS`.
4138
+ this.dStk = regexZeros(toI32(toU32(total) + 1))
3639
4139
  this.dMax = DFA_MAX_TABLE >> shift < DFA_MAX_STATES ? DFA_MAX_TABLE >> shift : DFA_MAX_STATES
3640
4140
  this.dCap = this.dMax < DFA_FIRST_STATES ? this.dMax : DFA_FIRST_STATES
3641
4141
  this.dTrans = regexZeros(this.dCap << shift)
@@ -3643,7 +4143,8 @@ export class Regex {
3643
4143
  this.dLen = regexZeros(this.dCap)
3644
4144
  this.dFlags = regexZeros(this.dCap)
3645
4145
  this.dHash = regexZeros(this.dHashSize(this.dCap))
3646
- this.dPoolMax = 2 * total > DFA_MIN_POOL ? 2 * total : DFA_MIN_POOL
4146
+ const pool: i32 = toI32(2 * toU32(total))
4147
+ this.dPoolMax = pool > DFA_MIN_POOL ? pool : DFA_MIN_POOL
3647
4148
  this.dPool = regexZeros(DFA_FIRST_POOL)
3648
4149
  this.dPoolLen = 0
3649
4150
  // Row 0 is never a state, so a plain transition, the next row's offset,
@@ -3661,20 +4162,22 @@ export class Regex {
3661
4162
  * classes in order of first appearance. Answers how many there are.
3662
4163
  */
3663
4164
  private dRefine(cls: i32[], hit: u8[], units: i32, n: i32, remap: i32[]): i32 {
4165
+ // Unsigned: classes number at most the code units, far below 2^31, and
4166
+ // `next` counts new ones.
3664
4167
  let k: i32 = 0
3665
- while (k < 2 * n && k < toI32(remap.length)) {
4168
+ while (k < toI32(2 * toU32(n)) && k < toI32(remap.length)) {
3666
4169
  remap[k] = -1
3667
4170
  k += 1
3668
4171
  }
3669
4172
  let next: i32 = 0
3670
4173
  let u: i32 = 0
3671
4174
  while (u < units && u < toI32(cls.length) && u < toI32(hit.length)) {
3672
- const key: i32 = cls[u] * 2 + toI32(hit[u])
4175
+ const key: i32 = toI32(toU32(cls[u]) * 2 + toU32(hit[u]))
3673
4176
  // Always true: a class is below `n`. The test is what proves `key`.
3674
4177
  if (key >= 0 && key < toI32(remap.length)) {
3675
4178
  if (remap[key] < 0) {
3676
4179
  remap[key] = next
3677
- next += 1
4180
+ next = toI32(toU32(next) + 1)
3678
4181
  }
3679
4182
  cls[u] = remap[key]
3680
4183
  }
@@ -3685,29 +4188,40 @@ export class Regex {
3685
4188
 
3686
4189
  /** The smallest power of two that holds `cap` states at most half full. */
3687
4190
  private dHashSize(cap: i32): i32 {
3688
- let size: i32 = 1
3689
- while (size < 2 * cap) {
4191
+ // Unsigned: `cap` is at most `dMax`, a state budget far below 2^30.
4192
+ let size: u32 = 1
4193
+ while (size < 2 * toU32(cap)) {
3690
4194
  size *= 2
3691
4195
  }
3692
- return size
4196
+ return toI32(size)
3693
4197
  }
3694
4198
 
3695
4199
  /** The class of code point `cp`, which is above ASCII. */
3696
4200
  private dWideClass(cp: i32): i32 {
3697
- if (this.dWide === 1) {
3698
- return this.dWcls[0]
3699
- }
4201
+ const wlo: i32[] = this.dWlo
4202
+ const wcls: i32[] = this.dWcls
4203
+ // `dfaInit` makes at least one wide class and `dWide` entries in each
4204
+ // array, so the tests on `lo` and `mid` below always hold and the class
4205
+ // 0 answer is never reached.
3700
4206
  let lo: i32 = 0
3701
- let hi: i32 = this.dWide - 1
4207
+ let hi: i32 = toI32(toU32(this.dWide) - 1)
3702
4208
  while (lo < hi) {
3703
- const mid: i32 = (lo + hi + 1) >> 1
3704
- if (this.dWlo[mid] <= cp) {
4209
+ // Both are indexes into the arrays, so the sum cannot wrap; `mid` is
4210
+ // above `lo`, so `mid - 1` is not negative.
4211
+ const mid: i32 = toI32((toU32(lo) + toU32(hi) + 1) >>> 1)
4212
+ if (mid < 0 || mid >= toI32(wlo.length)) {
4213
+ return 0
4214
+ }
4215
+ if (wlo[mid] <= cp) {
3705
4216
  lo = mid
3706
4217
  } else {
3707
- hi = mid - 1
4218
+ hi = toI32(toU32(mid) - 1)
3708
4219
  }
3709
4220
  }
3710
- return this.dWcls[lo]
4221
+ if (lo < 0 || lo >= toI32(wcls.length)) {
4222
+ return 0
4223
+ }
4224
+ return wcls[lo]
3711
4225
  }
3712
4226
 
3713
4227
  /** Whether assertion `kind` holds between a character of kind `ctx` and one of kind `nk`. */
@@ -3735,26 +4249,54 @@ export class Regex {
3735
4249
  * new length.
3736
4250
  */
3737
4251
  private dClose(pc0: i32, ctx: i32, nk: i32, n0: i32): i32 {
4252
+ // Every jump lands inside the program, and `dStk` and `dLeaf` have room
4253
+ // for every instruction, each pushed or kept once per generation: no
4254
+ // test below fails, and the -1 that gives the step up is never answered.
4255
+ const stk: i32[] = this.dStk
4256
+ const visit: i32[] = this.dVisit
4257
+ const leaf: i32[] = this.dLeaf
4258
+ const op: i32[] = this.op
4259
+ const ia: i32[] = this.ia
4260
+ const ib: i32[] = this.ib
4261
+ const gen: i32 = this.dGen
3738
4262
  let n: i32 = n0
3739
- this.dStk[0] = pc0
4263
+ if (toI32(stk.length) < 1) {
4264
+ return -1
4265
+ }
4266
+ stk[0] = pc0
3740
4267
  let top: i32 = 1
3741
4268
  while (top > 0) {
3742
4269
  top -= 1
3743
- let pc: i32 = this.dStk[top]
4270
+ if (top >= toI32(stk.length)) {
4271
+ return -1
4272
+ }
4273
+ let pc: i32 = stk[top]
3744
4274
  while (true) {
3745
- if (this.dVisit[pc] === this.dGen) {
4275
+ if (
4276
+ pc < 0 ||
4277
+ pc >= toI32(visit.length) ||
4278
+ pc >= toI32(op.length) ||
4279
+ pc >= toI32(ia.length) ||
4280
+ pc >= toI32(ib.length)
4281
+ ) {
4282
+ return -1
4283
+ }
4284
+ if (visit[pc] === gen) {
3746
4285
  break
3747
4286
  }
3748
- this.dVisit[pc] = this.dGen
3749
- const o: i32 = this.op[pc]
4287
+ visit[pc] = gen
4288
+ const o: i32 = op[pc]
3750
4289
  if (o === OP_JMP) {
3751
- pc = this.ia[pc]
4290
+ pc = ia[pc]
3752
4291
  continue
3753
4292
  }
3754
4293
  if (o === OP_SPLIT) {
3755
- this.dStk[top] = this.ib[pc]
4294
+ if (top >= toI32(stk.length)) {
4295
+ return -1
4296
+ }
4297
+ stk[top] = ib[pc]
3756
4298
  top += 1
3757
- pc = this.ia[pc]
4299
+ pc = ia[pc]
3758
4300
  continue
3759
4301
  }
3760
4302
  if (o === OP_SAVE || o === OP_MARK || o === OP_CLEAR || o === OP_CHECK) {
@@ -3762,13 +4304,16 @@ export class Regex {
3762
4304
  continue
3763
4305
  }
3764
4306
  if (o === OP_ASSERT) {
3765
- if (this.dAssert(this.ia[pc], ctx, nk)) {
4307
+ if (this.dAssert(ia[pc], ctx, nk)) {
3766
4308
  pc += 1
3767
4309
  continue
3768
4310
  }
3769
4311
  break
3770
4312
  }
3771
- this.dLeaf[n] = pc
4313
+ if (n < 0 || n >= toI32(leaf.length)) {
4314
+ return -1
4315
+ }
4316
+ leaf[n] = pc
3772
4317
  n += 1
3773
4318
  break
3774
4319
  }
@@ -3781,58 +4326,97 @@ export class Regex {
3781
4326
  * read at byte `p`. Answers the entry as stored, or 0 when the cache gave up.
3782
4327
  */
3783
4328
  private dStep(off: i32, c: i32, p: i32): i32 {
4329
+ // A state's offset and a class are always in range, and `dClose` keeps
4330
+ // only leaves inside the program and `dBuf` has room for each: the 0
4331
+ // that gives the search up to the NFA for a value outside is never
4332
+ // answered.
3784
4333
  const id: i32 = off >> this.dShift
3785
- const start: i32 = this.dStart[id]
3786
- const len: i32 = this.dLen[id]
3787
- const flags: i32 = this.dFlags[id]
4334
+ const starts: i32[] = this.dStart
4335
+ const lens: i32[] = this.dLen
4336
+ const flagsOf: i32[] = this.dFlags
4337
+ const nextK: i32[] = this.dNextK
4338
+ if (
4339
+ id < 0 ||
4340
+ id >= toI32(starts.length) ||
4341
+ id >= toI32(lens.length) ||
4342
+ id >= toI32(flagsOf.length) ||
4343
+ c < 0 ||
4344
+ c >= toI32(nextK.length)
4345
+ ) {
4346
+ return 0
4347
+ }
4348
+ const start: i32 = starts[id]
4349
+ const len: i32 = lens[id]
4350
+ const flags: i32 = flagsOf[id]
3788
4351
  const restart: boolean = (flags & DFA_RESTART) !== 0
3789
4352
  const rev: boolean = (flags & DFA_REV) !== 0
3790
4353
  const ctx: i32 = (flags >> 1) & 3
3791
- const nk: i32 = this.dNextK[c]
4354
+ const nk: i32 = nextK[c]
3792
4355
  const epoch: i32 = this.dEpoch
3793
4356
  if (this.dGen >= 2147483647) {
3794
- let k: i32 = 0
3795
- while (k < toI32(this.dVisit.length)) {
3796
- this.dVisit[k] = 0
3797
- k += 1
3798
- }
4357
+ this.dVisit.fill(0)
3799
4358
  this.dGen = 0
3800
4359
  }
3801
- this.dGen += 1
4360
+ // Reset just above when it reaches the i32 maximum, so it cannot wrap.
4361
+ this.dGen = toI32(toU32(this.dGen) + 1)
4362
+ const pool: i32[] = this.dPool
3802
4363
  let n: i32 = 0
3803
4364
  let i: i32 = 0
3804
4365
  while (i < len) {
3805
- n = this.dClose(this.dPool[start + i], ctx, nk, n)
4366
+ const at: i32 = toI32(toU32(start) + toU32(i))
4367
+ if (at < 0 || at >= toI32(pool.length)) {
4368
+ return 0
4369
+ }
4370
+ n = this.dClose(pool[at], ctx, nk, n)
4371
+ if (n < 0) {
4372
+ return 0
4373
+ }
3806
4374
  i += 1
3807
4375
  }
3808
4376
  if (restart) {
3809
4377
  n = this.dClose(REGEX_ENTRY, ctx, nk, n)
4378
+ if (n < 0) {
4379
+ return 0
4380
+ }
3810
4381
  }
4382
+ const leaf: i32[] = this.dLeaf
4383
+ const op: i32[] = this.op
4384
+ const ia: i32[] = this.ia
4385
+ const ib: i32[] = this.ib
3811
4386
  let matched: boolean = false
3812
4387
  let entry: i32 = DFA_KNOWN | DFA_DEAD
3813
4388
  let to: i32 = 0
3814
4389
  if (c === this.dEot) {
3815
4390
  i = 0
3816
- while (i < n) {
3817
- if (this.op[this.dLeaf[i]] === OP_MATCH) {
4391
+ while (i < n && i < toI32(leaf.length)) {
4392
+ const pc: i32 = leaf[i]
4393
+ if (pc >= 0 && pc < toI32(op.length) && op[pc] === OP_MATCH) {
3818
4394
  matched = true
3819
4395
  break
3820
4396
  }
3821
4397
  i += 1
3822
4398
  }
3823
4399
  } else {
3824
- const r: i32 = this.dRep[c]
4400
+ const dRep: i32[] = this.dRep
4401
+ if (c >= toI32(dRep.length)) {
4402
+ return 0
4403
+ }
4404
+ const r: i32 = dRep[c]
4405
+ const buf: i32[] = this.dBuf
3825
4406
  let nb: i32 = 0
3826
4407
  i = 0
3827
- while (i < n) {
3828
- const pc: i32 = this.dLeaf[i]
4408
+ while (i < n && i < toI32(leaf.length)) {
4409
+ const pc: i32 = leaf[i]
3829
4410
  i += 1
3830
- const o: i32 = this.op[pc]
4411
+ if (pc < 0 || pc >= toI32(op.length) || pc >= toI32(ia.length) || pc >= toI32(ib.length)) {
4412
+ return 0
4413
+ }
4414
+ const o: i32 = op[pc]
3831
4415
  let hit: boolean = false
3832
4416
  if (o === OP_CHAR) {
3833
- hit = r === this.ia[pc] || r === this.ib[pc]
4417
+ hit = r === ia[pc] || r === ib[pc]
3834
4418
  } else if (o === OP_CLASS) {
3835
- hit = this.classHas(this.ia[pc], r)
4419
+ hit = this.classHas(ia[pc], r)
3836
4420
  } else if (o === OP_ANY) {
3837
4421
  hit = true
3838
4422
  } else if (o === OP_ANY_NL) {
@@ -3845,13 +4429,21 @@ export class Regex {
3845
4429
  }
3846
4430
  }
3847
4431
  if (hit) {
3848
- this.dBuf[nb] = pc + 1
4432
+ if (nb < 0 || nb >= toI32(buf.length)) {
4433
+ return 0
4434
+ }
4435
+ // `pc` is inside the program, so one more cannot wrap.
4436
+ buf[nb] = toI32(toU32(pc) + 1)
3849
4437
  nb += 1
3850
4438
  }
3851
4439
  }
3852
4440
  const restart2: boolean = restart && !matched
3853
4441
  if (nb > 0 || restart2) {
3854
- const ctx2: i32 = rev ? this.dCtxR[c] : this.dCtxF[c]
4442
+ const ctxOf: i32[] = rev ? this.dCtxR : this.dCtxF
4443
+ if (c >= toI32(ctxOf.length)) {
4444
+ return 0
4445
+ }
4446
+ const ctx2: i32 = ctxOf[c]
3855
4447
  let flags2: i32 = ctx2 << 1
3856
4448
  if (restart2) {
3857
4449
  flags2 |= DFA_RESTART
@@ -3876,23 +4468,34 @@ export class Regex {
3876
4468
  if (to !== 0 && entry === ((to << 4) | DFA_KNOWN)) {
3877
4469
  entry = to
3878
4470
  } else {
3879
- entry = -entry
4471
+ // A state shifted left by 4 with its flags: negated in u32, where it
4472
+ // cannot overflow.
4473
+ entry = toI32(0 - toU32(entry))
3880
4474
  }
3881
- if (epoch === this.dEpoch) {
3882
- this.dTrans[off + c] = entry
4475
+ // The table is a cache: a slot outside it would only go unremembered.
4476
+ const trans: i32[] = this.dTrans
4477
+ const at: i32 = toI32(toU32(off) + toU32(c))
4478
+ if (epoch === this.dEpoch && at >= 0 && at < toI32(trans.length)) {
4479
+ trans[at] = entry
3883
4480
  }
3884
4481
  return entry
3885
4482
  }
3886
4483
 
3887
4484
  /** The hash of a state's flags and its `n` instructions in `src` from `at`. */
3888
4485
  private dHashOf(src: i32[], at: i32, n: i32, flags: i32): i32 {
3889
- let h: i32 = flags
4486
+ // In u32 the low bits the mask keeps are those of the i32 sum.
4487
+ let h: u32 = toU32(flags)
3890
4488
  let i: i32 = 0
3891
4489
  while (i < n) {
3892
- h = (h * 31 + src[at + i]) & DFA_HASH_MASK
4490
+ // Callers hash a state's slots, which are inside `src`.
4491
+ const k: i32 = toI32(toU32(at) + toU32(i))
4492
+ if (k < 0 || k >= toI32(src.length)) {
4493
+ break
4494
+ }
4495
+ h = (h * 31 + toU32(src[k])) & toU32(DFA_HASH_MASK)
3893
4496
  i += 1
3894
4497
  }
3895
- return h
4498
+ return toI32(h)
3896
4499
  }
3897
4500
 
3898
4501
  /**
@@ -3901,15 +4504,39 @@ export class Regex {
3901
4504
  * cleared too recently, at byte `p`, to clear again.
3902
4505
  */
3903
4506
  private dIntern(n: i32, flags: i32, p: i32): i32 {
3904
- const mask: i32 = toI32(this.dHash.length) - 1
3905
- const at: i32 = 0
3906
- let slot: i32 = this.dHashOf(this.dBuf, at, n, flags) & mask
3907
- while (this.dHash[slot] !== 0) {
3908
- const id: i32 = this.dHash[slot] - 1
3909
- if (this.dFlags[id] === flags && this.dLen[id] === n) {
3910
- const start: i32 = this.dStart[id]
4507
+ // The table's arrays are sized together in `dfaInit` and `dGrow`: a
4508
+ // hash slot is masked into `dHash`, every id in it is a state, a state
4509
+ // has room in `dPool`, and `n` is at most `dBuf`'s length. The -1 that
4510
+ // gives the search up to the NFA for a value outside is never answered.
4511
+ const hash: i32[] = this.dHash
4512
+ const buf: i32[] = this.dBuf
4513
+ const flagsOf: i32[] = this.dFlags
4514
+ const lens: i32[] = this.dLen
4515
+ const starts: i32[] = this.dStart
4516
+ if (n < 0 || n > toI32(buf.length)) {
4517
+ return -1
4518
+ }
4519
+ const mask: i32 = toI32(hash.length) - 1
4520
+ let slot: i32 = this.dHashOf(buf, toI32(0), n, flags) & mask
4521
+ while (slot >= 0 && slot < toI32(hash.length) && hash[slot] !== 0) {
4522
+ // Slots hold an id plus one.
4523
+ const id: i32 = toI32(toU32(hash[slot]) - 1)
4524
+ if (
4525
+ id >= 0 &&
4526
+ id < toI32(flagsOf.length) &&
4527
+ id < toI32(lens.length) &&
4528
+ id < toI32(starts.length) &&
4529
+ flagsOf[id] === flags &&
4530
+ lens[id] === n
4531
+ ) {
4532
+ const start: i32 = starts[id]
4533
+ const pool: i32[] = this.dPool
3911
4534
  let k: i32 = 0
3912
- while (k < n && this.dPool[start + k] === this.dBuf[k]) {
4535
+ while (k < n && k < toI32(buf.length)) {
4536
+ const q: i32 = toI32(toU32(start) + toU32(k))
4537
+ if (q < 0 || q >= toI32(pool.length) || pool[q] !== buf[k]) {
4538
+ break
4539
+ }
3913
4540
  k += 1
3914
4541
  }
3915
4542
  if (k === n) {
@@ -3918,6 +4545,9 @@ export class Regex {
3918
4545
  }
3919
4546
  slot = (slot + 1) & mask
3920
4547
  }
4548
+ if (slot < 0 || slot >= toI32(hash.length)) {
4549
+ return -1
4550
+ }
3921
4551
  if (this.dCount === this.dCap) {
3922
4552
  if (this.dCap < this.dMax) {
3923
4553
  this.dGrow()
@@ -3926,7 +4556,11 @@ export class Regex {
3926
4556
  }
3927
4557
  return this.dIntern(n, flags, p)
3928
4558
  }
3929
- if (this.dPoolLen + n > toI32(this.dPool.length)) {
4559
+ // The pool's fill and a state's size are both below `dPoolMax`, a few
4560
+ // times the program's size: their sum, and the doubling that reaches
4561
+ // it, stay far below 2^31.
4562
+ const need: u32 = toU32(this.dPoolLen) + toU32(n)
4563
+ if (need > toU32(this.dPool.length)) {
3930
4564
  const size: i32 = toI32(this.dPool.length)
3931
4565
  if (size >= this.dPoolMax) {
3932
4566
  if (!this.dFlush(p)) {
@@ -3934,54 +4568,75 @@ export class Regex {
3934
4568
  }
3935
4569
  return this.dIntern(n, flags, p)
3936
4570
  }
3937
- let grown: i32 = 2 * size
3938
- while (grown < this.dPoolLen + n) {
4571
+ let grown: u32 = toU32(size) * 2
4572
+ while (grown < need) {
3939
4573
  grown *= 2
3940
4574
  }
3941
- if (grown > this.dPoolMax) {
3942
- grown = this.dPoolMax
4575
+ if (grown > toU32(this.dPoolMax)) {
4576
+ grown = toU32(this.dPoolMax)
3943
4577
  }
3944
- const pool: i32[] = regexZeros(grown)
4578
+ const pool: i32[] = regexZeros(toI32(grown))
4579
+ const old: i32[] = this.dPool
3945
4580
  let k: i32 = 0
3946
- while (k < this.dPoolLen && k < toI32(pool.length)) {
3947
- pool[k] = this.dPool[k]
4581
+ while (k < this.dPoolLen && k < toI32(pool.length) && k < toI32(old.length)) {
4582
+ pool[k] = old[k]
3948
4583
  k += 1
3949
4584
  }
3950
4585
  this.dPool = pool
3951
4586
  }
3952
4587
  const id: i32 = this.dCount
3953
- this.dCount += 1
3954
- this.dStart[id] = this.dPoolLen
3955
- this.dLen[id] = n
3956
- this.dFlags[id] = flags
4588
+ const base: i32 = this.dPoolLen
4589
+ const pool: i32[] = this.dPool
4590
+ if (
4591
+ id < 0 ||
4592
+ id >= toI32(starts.length) ||
4593
+ id >= toI32(lens.length) ||
4594
+ id >= toI32(flagsOf.length) ||
4595
+ base < 0 ||
4596
+ toU32(base) + toU32(n) > toU32(pool.length)
4597
+ ) {
4598
+ return -1
4599
+ }
4600
+ this.dCount = id + 1
4601
+ starts[id] = base
4602
+ lens[id] = n
4603
+ flagsOf[id] = flags
3957
4604
  let k: i32 = 0
3958
- while (k < n) {
3959
- this.dPool[this.dPoolLen + k] = this.dBuf[k]
4605
+ while (k < n && k < toI32(buf.length)) {
4606
+ // Inside the pool: checked above.
4607
+ const q: i32 = toI32(toU32(base) + toU32(k))
4608
+ if (q < 0 || q >= toI32(pool.length)) {
4609
+ break
4610
+ }
4611
+ pool[q] = buf[k]
3960
4612
  k += 1
3961
4613
  }
3962
- this.dPoolLen += n
4614
+ this.dPoolLen = toI32(toU32(base) + toU32(n))
3963
4615
  const off: i32 = id << this.dShift
3964
4616
  const row: i32 = 1 << this.dShift
3965
- k = 0
3966
- while (k < row) {
3967
- this.dTrans[off + k] = 0
3968
- k += 1
4617
+ // A state's row ends inside the table, which holds `dCap` rows.
4618
+ this.dTrans.fill(0, off, toI32(toU32(off) + toU32(row)))
4619
+ // In `hash`, checked after the probe; the calls since then cost the proof.
4620
+ if (slot >= 0 && slot < toI32(hash.length)) {
4621
+ hash[slot] = toI32(toU32(id) + 1)
3969
4622
  }
3970
- this.dHash[slot] = id + 1
3971
4623
  return off
3972
4624
  }
3973
4625
 
3974
4626
  /** Double the state table, up to `dMax` states. */
3975
4627
  private dGrow(): void {
3976
- let cap: i32 = 2 * this.dCap
4628
+ // The cache holds at most `dMax` states, far below 2^30: doubling it
4629
+ // cannot wrap.
4630
+ let cap: i32 = toI32(toU32(this.dCap) * 2)
3977
4631
  if (cap > this.dMax) {
3978
4632
  cap = this.dMax
3979
4633
  }
3980
4634
  const used: i32 = this.dCount << this.dShift
3981
4635
  const trans: i32[] = regexZeros(cap << this.dShift)
4636
+ const old: i32[] = this.dTrans
3982
4637
  let k: i32 = 0
3983
- while (k < used && k < toI32(trans.length)) {
3984
- trans[k] = this.dTrans[k]
4638
+ while (k < used && k < toI32(trans.length) && k < toI32(old.length)) {
4639
+ trans[k] = old[k]
3985
4640
  k += 1
3986
4641
  }
3987
4642
  const start: i32[] = regexZeros(cap)
@@ -3991,11 +4646,22 @@ export class Regex {
3991
4646
  const mask: i32 = toI32(hash.length) - 1
3992
4647
  // Every length test below always holds (`dCount` is at most `cap`, and
3993
4648
  // `& mask` keeps a slot within `hash`): each is what proves its index.
4649
+ const oldStart: i32[] = this.dStart
4650
+ const oldLen: i32[] = this.dLen
4651
+ const oldFlags: i32[] = this.dFlags
3994
4652
  k = 1
3995
- while (k < this.dCount && k < toI32(start.length) && k < toI32(len.length) && k < toI32(flags.length)) {
3996
- const at: i32 = this.dStart[k]
3997
- const size: i32 = this.dLen[k]
3998
- const kind: i32 = this.dFlags[k]
4653
+ while (
4654
+ k < this.dCount &&
4655
+ k < toI32(start.length) &&
4656
+ k < toI32(len.length) &&
4657
+ k < toI32(flags.length) &&
4658
+ k < toI32(oldStart.length) &&
4659
+ k < toI32(oldLen.length) &&
4660
+ k < toI32(oldFlags.length)
4661
+ ) {
4662
+ const at: i32 = oldStart[k]
4663
+ const size: i32 = oldLen[k]
4664
+ const kind: i32 = oldFlags[k]
3999
4665
  start[k] = at
4000
4666
  len[k] = size
4001
4667
  flags[k] = kind
@@ -4023,33 +4689,38 @@ export class Regex {
4023
4689
  * cleared.
4024
4690
  */
4025
4691
  private dFlush(p: i32): boolean {
4026
- this.dFlushes += 1
4027
- const moved: i32 = p > this.dPos ? p - this.dPos : this.dPos - p
4028
- if (this.dFlushes > DFA_FREE_FLUSHES && moved < DFA_BYTES_PER_STATE * this.dMax) {
4692
+ // Only whether the count passed `DFA_FREE_FLUSHES` is read, so it stops
4693
+ // one past it and cannot wrap.
4694
+ const flushes: i32 = this.dFlushes
4695
+ this.dFlushes = flushes > DFA_FREE_FLUSHES ? flushes : toI32(toU32(flushes) + 1)
4696
+ const moved: f64 = Math.abs(toF64(p) - toF64(this.dPos))
4697
+ if (this.dFlushes > DFA_FREE_FLUSHES && moved < toF64(DFA_BYTES_PER_STATE) * toF64(this.dMax)) {
4029
4698
  return false
4030
4699
  }
4031
4700
  this.dPos = p
4032
4701
  this.dCount = 1
4033
4702
  this.dPoolLen = 0
4034
- let k: i32 = 0
4035
- while (k < toI32(this.dHash.length)) {
4036
- this.dHash[k] = 0
4037
- k += 1
4038
- }
4039
- k = 0
4040
- while (k < toI32(this.dStartOff.length)) {
4041
- this.dStartOff[k] = 0
4042
- k += 1
4043
- }
4703
+ this.dHash.fill(0)
4704
+ this.dStartOff.fill(0)
4044
4705
  this.dPrefOff = 0
4045
- this.dEpoch = (this.dEpoch + 1) & DFA_EPOCH_MASK
4706
+ // Masked, so wrapping past 2^32 is harmless.
4707
+ this.dEpoch = toI32((toU32(this.dEpoch) + 1) & toU32(DFA_EPOCH_MASK))
4046
4708
  return true
4047
4709
  }
4048
4710
 
4049
4711
  /** The start state for `mode` after a character of kind `ctx`, or -1. */
4050
4712
  private dStartAt(mode: i32, ctx: i32, p: i32): i32 {
4051
- const k: i32 = mode * CTX_SLOTS + ctx
4052
- const cached: i32 = this.dStartOff[k]
4713
+ // Modes and contexts are small constants, so the slot cannot wrap, and
4714
+ // `dStartOff` has one per pair and `dBuf` room for a state: the -1 for a
4715
+ // slot outside either, which hands the search to the NFA, is never
4716
+ // answered.
4717
+ const k: i32 = toI32(toU32(mode) * toU32(CTX_SLOTS) + toU32(ctx))
4718
+ const startOff: i32[] = this.dStartOff
4719
+ const buf: i32[] = this.dBuf
4720
+ if (k < 0 || k >= toI32(startOff.length) || toI32(buf.length) < 1) {
4721
+ return -1
4722
+ }
4723
+ const cached: i32 = startOff[k]
4053
4724
  if (cached > 0) {
4054
4725
  return cached - 1
4055
4726
  }
@@ -4058,16 +4729,19 @@ export class Regex {
4058
4729
  if (mode === DFA_UNANCHORED) {
4059
4730
  flags |= DFA_RESTART
4060
4731
  } else if (mode === DFA_ANCHORED) {
4061
- this.dBuf[0] = REGEX_ENTRY
4732
+ buf[0] = REGEX_ENTRY
4062
4733
  n = 1
4063
4734
  } else {
4064
- this.dBuf[0] = mode === DFA_INNER ? this.innerEntry : this.revEntry
4735
+ buf[0] = mode === DFA_INNER ? this.innerEntry : this.revEntry
4065
4736
  n = 1
4066
4737
  flags |= DFA_REV
4067
4738
  }
4068
4739
  const off: i32 = this.dIntern(n, flags, p)
4069
- if (off >= 0) {
4070
- this.dStartOff[k] = off + 1
4740
+ // `dIntern` can flush the cache, which clears `dStartOff` in place.
4741
+ const after: i32[] = this.dStartOff
4742
+ if (off >= 0 && k < toI32(after.length)) {
4743
+ // A state's offset is inside the transition table: one more cannot wrap.
4744
+ after[k] = toI32(toU32(off) + 1)
4071
4745
  }
4072
4746
  return off
4073
4747
  }
@@ -4078,14 +4752,31 @@ export class Regex {
4078
4752
  * `dPrefFast` goes off.
4079
4753
  */
4080
4754
  private dAfterPrefix(p: i32): i32 {
4081
- let off: i32 = this.dStartAt(DFA_UNANCHORED, this.dMaskF[CTX_OTHER], p)
4082
- const n: i32 = toI32(this.prefix.length)
4755
+ let off: i32 = this.dStartAt(DFA_UNANCHORED, regexMaskOf(this.dMaskF, CTX_OTHER), p)
4756
+ const prefix: string = this.prefix
4757
+ const cls: i32[] = this.dCls
4758
+ const n: i32 = toI32(prefix.length)
4083
4759
  let k: i32 = 0
4084
4760
  while (off >= 0 && k < n) {
4085
- const c: i32 = this.dCls[toI32(this.prefix.charCodeAt(k))]
4086
- let e: i32 = this.dTrans[off + c]
4761
+ // `dCls` has an entry per byte and a state's row one per class, as in
4762
+ // `dfaForward`; a slot outside either ends the fast path like a step
4763
+ // that is not a plain transition.
4764
+ const b: i32 = toI32(prefix.charCodeAt(k))
4765
+ if (b >= toI32(cls.length)) {
4766
+ off = -1
4767
+ break
4768
+ }
4769
+ const c: i32 = cls[b]
4770
+ const trans: i32[] = this.dTrans
4771
+ const at: i32 = toI32(toU32(off) + toU32(c))
4772
+ if (at < 0 || at >= toI32(trans.length)) {
4773
+ off = -1
4774
+ break
4775
+ }
4776
+ let e: i32 = trans[at]
4087
4777
  if (e === 0) {
4088
- e = this.dStep(off, c, p + k)
4778
+ // The prefix is in the text at `p`, so `p + k` cannot wrap.
4779
+ e = this.dStep(off, c, toI32(toU32(p) + toU32(k)))
4089
4780
  }
4090
4781
  off = e > 0 ? e : -1
4091
4782
  k += 1
@@ -4094,7 +4785,7 @@ export class Regex {
4094
4785
  this.dPrefFast = false
4095
4786
  return -1
4096
4787
  }
4097
- this.dPrefOff = off + 1
4788
+ this.dPrefOff = toI32(toU32(off) + 1)
4098
4789
  return off
4099
4790
  }
4100
4791
 
@@ -4104,7 +4795,7 @@ export class Regex {
4104
4795
  if (p <= 0) {
4105
4796
  kind = CTX_START
4106
4797
  } else {
4107
- const b: i32 = toI32(text.charCodeAt(p - 1))
4798
+ const b: i32 = regexByteAt(text, p - 1)
4108
4799
  if (b === 10 || b === 13) {
4109
4800
  kind = CTX_LINE
4110
4801
  } else if (isRegexWordByte(b)) {
@@ -4112,17 +4803,16 @@ export class Regex {
4112
4803
  } else if (b >= 0x80) {
4113
4804
  if (
4114
4805
  (b === 0xa8 || b === 0xa9) &&
4115
- p >= 3 &&
4116
- toI32(text.charCodeAt(p - 2)) === 0x80 &&
4117
- toI32(text.charCodeAt(p - 3)) === 0xe2
4806
+ regexByteAt(text, p - 2) === 0x80 &&
4807
+ regexByteAt(text, p - 3) === 0xe2
4118
4808
  ) {
4119
4809
  kind = CTX_LINE
4120
- } else if (this.dMaskF[CTX_KINDS] !== 0 && regexFoldWordBefore(text, p)) {
4810
+ } else if (regexMaskOf(this.dMaskF, CTX_KINDS) !== 0 && regexFoldWordBefore(text, p)) {
4121
4811
  kind = CTX_WORD
4122
4812
  }
4123
4813
  }
4124
4814
  }
4125
- return this.dMaskF[kind]
4815
+ return regexMaskOf(this.dMaskF, kind)
4126
4816
  }
4127
4817
 
4128
4818
  /** The kind of the character at byte `p`, as the reverse program tells kinds apart. */
@@ -4132,22 +4822,28 @@ export class Regex {
4132
4822
  if (p >= len) {
4133
4823
  kind = CTX_START
4134
4824
  } else {
4135
- const b: i32 = toI32(text.charCodeAt(p))
4825
+ const b: i32 = regexByteAt(text, p)
4136
4826
  if (b === 10 || b === 13) {
4137
4827
  kind = CTX_LINE
4138
4828
  } else if (isRegexWordByte(b)) {
4139
4829
  kind = CTX_WORD
4140
- } else if (b === 0xe2 && p + 2 < len && toI32(text.charCodeAt(p + 1)) === 0x80) {
4141
- const b2: i32 = toI32(text.charCodeAt(p + 2))
4830
+ } else if (b === 0xe2 && regexByteAt(text, p + 1) === 0x80) {
4831
+ // `b` was read, so `p` is in the text and `p + 2` cannot wrap.
4832
+ const b2: i32 = regexByteAt(text, toI32(toU32(p) + 2))
4142
4833
  if (b2 === 0xa8 || b2 === 0xa9) {
4143
4834
  kind = CTX_LINE
4144
4835
  }
4145
4836
  }
4146
- if (kind === CTX_OTHER && b >= 0x80 && this.dMaskR[CTX_KINDS] !== 0 && regexFoldWordAt(text, p, len)) {
4837
+ if (
4838
+ kind === CTX_OTHER &&
4839
+ b >= 0x80 &&
4840
+ regexMaskOf(this.dMaskR, CTX_KINDS) !== 0 &&
4841
+ regexFoldWordAt(text, p)
4842
+ ) {
4147
4843
  kind = CTX_WORD
4148
4844
  }
4149
4845
  }
4150
- return this.dMaskR[kind]
4846
+ return regexMaskOf(this.dMaskR, kind)
4151
4847
  }
4152
4848
 
4153
4849
  /**
@@ -4169,15 +4865,21 @@ export class Regex {
4169
4865
  return DFA_NONE
4170
4866
  }
4171
4867
  if (this.dPrefFast) {
4172
- // The prefix is at `p`: start past it.
4173
- off = this.dPrefOff > 0 ? this.dPrefOff - 1 : this.dAfterPrefix(p)
4868
+ // The prefix is at `p`: start past it. `p` and the prefix are in
4869
+ // the text, so the sum cannot wrap.
4870
+ const prefOff: i32 = this.dPrefOff
4871
+ off = prefOff > 0 ? prefOff - 1 : this.dAfterPrefix(p)
4174
4872
  if (off >= 0) {
4175
- p += toI32(this.prefix.length)
4873
+ p = toI32(toU32(p) + toU32(this.prefix.length))
4176
4874
  }
4177
4875
  }
4178
4876
  }
4179
4877
  if (off < 0) {
4180
- off = this.dStartAt(mode, this.dCtxFree ? this.dMaskF[CTX_OTHER] : this.ctxBefore(text, p), p)
4878
+ off = this.dStartAt(
4879
+ mode,
4880
+ this.dCtxFree ? regexMaskOf(this.dMaskF, CTX_OTHER) : this.ctxBefore(text, p),
4881
+ p
4882
+ )
4181
4883
  if (off < 0) {
4182
4884
  return DFA_GAVE_UP
4183
4885
  }
@@ -4193,16 +4895,19 @@ export class Regex {
4193
4895
  if (e > 0) {
4194
4896
  off = e
4195
4897
  p += 1
4196
- } else if (e === 0 || (-e & (DFA_DEAD | DFA_IDLE)) !== 0) {
4197
- break
4198
4898
  } else {
4899
+ // A negative entry is minus a state and its flags.
4900
+ const neg: i32 = regexNegated(e)
4901
+ if (e === 0 || (neg & (DFA_DEAD | DFA_IDLE)) !== 0) {
4902
+ break
4903
+ }
4199
4904
  // A match ends before this byte and the search goes on.
4200
4905
  if (first) {
4201
4906
  this.dStop = p
4202
4907
  return p
4203
4908
  }
4204
4909
  end = p
4205
- off = -e >> 4
4910
+ off = neg >> 4
4206
4911
  p += 1
4207
4912
  }
4208
4913
  }
@@ -4214,12 +4919,19 @@ export class Regex {
4214
4919
  // `dCls` has an entry for every byte.
4215
4920
  c = uncheckedGet(cls, b)
4216
4921
  } else {
4217
- const packed: i32 = regexDecodeAt(text, p, len)
4922
+ const packed: i32 = regexDecodeAt(text, p)
4218
4923
  c = this.dWideClass(packed >> 3)
4219
4924
  w = packed & 7
4220
4925
  }
4221
4926
  }
4222
- let e: i32 = trans[off + c]
4927
+ // States and classes are both non-negative, and a state's row holds
4928
+ // every class, so the slot is in the table; were it not, giving up
4929
+ // hands the search to the NFA.
4930
+ const at: i32 = toI32(toU32(off) + toU32(c))
4931
+ if (at < 0 || at >= toI32(trans.length)) {
4932
+ return DFA_GAVE_UP
4933
+ }
4934
+ let e: i32 = trans[at]
4223
4935
  if (e === 0) {
4224
4936
  e = this.dStep(off, c, p)
4225
4937
  if (e === 0) {
@@ -4227,16 +4939,18 @@ export class Regex {
4227
4939
  }
4228
4940
  trans = this.dTrans
4229
4941
  }
4942
+ // Past here `p` is before the end and `w` is at most 4, so `p + w`
4943
+ // cannot wrap.
4230
4944
  if (e > 0) {
4231
4945
  if (p >= len) {
4232
4946
  this.dStop = p
4233
4947
  return end
4234
4948
  }
4235
4949
  off = e
4236
- p += w
4950
+ p = toI32(toU32(p) + toU32(w))
4237
4951
  continue
4238
4952
  }
4239
- e = -e
4953
+ e = regexNegated(e)
4240
4954
  if ((e & DFA_MATCH) !== 0) {
4241
4955
  if (first) {
4242
4956
  this.dStop = p
@@ -4249,7 +4963,7 @@ export class Regex {
4249
4963
  return end
4250
4964
  }
4251
4965
  off = e >> 4
4252
- p += w
4966
+ p = toI32(toU32(p) + toU32(w))
4253
4967
  if ((e & DFA_IDLE) !== 0) {
4254
4968
  // No thread is alive and no match has been seen: skip to where the
4255
4969
  // next one can start. The state reached is the start state there
@@ -4266,9 +4980,11 @@ export class Regex {
4266
4980
  trans = this.dTrans
4267
4981
  }
4268
4982
  p = q
4269
- if (this.dPrefOff > 0 && this.dPrefFast) {
4270
- off = this.dPrefOff - 1
4271
- p = q + toI32(this.prefix.length)
4983
+ const prefOff: i32 = this.dPrefOff
4984
+ if (prefOff > 0 && this.dPrefFast) {
4985
+ // The prefix is at `q`, inside the text: the sum cannot wrap.
4986
+ off = prefOff - 1
4987
+ p = toI32(toU32(q) + toU32(this.prefix.length))
4272
4988
  }
4273
4989
  }
4274
4990
  }
@@ -4282,6 +4998,10 @@ export class Regex {
4282
4998
  */
4283
4999
  private dfaReverse(text: string, from: i32, e: i32, mode: i32): i32 {
4284
5000
  const len: i32 = toI32(text.length)
5001
+ // Callers search a span of the text; any other is left to the NFA.
5002
+ if (from < 0 || from > e || e > len) {
5003
+ return DFA_GAVE_UP
5004
+ }
4285
5005
  let p: i32 = e
4286
5006
  let off: i32 = this.dStartAt(mode, this.ctxAt(text, e), e)
4287
5007
  if (off < 0) {
@@ -4291,28 +5011,38 @@ export class Regex {
4291
5011
  let trans: i32[] = this.dTrans
4292
5012
  const cls: i32[] = this.dCls
4293
5013
  while (true) {
4294
- while (p > from) {
4295
- const b: i32 = toI32(text.charCodeAt(p - 1))
5014
+ // `q` is the byte before `p`. `p` only falls from `e`, so `q` stays
5015
+ // below `len` and above -2: the tests on it stand in for the bounds
5016
+ // check its read needs, and `q - 1` and `q + 1` cannot wrap.
5017
+ let q: i32 = toI32(toU32(p) - 1)
5018
+ while (q >= from && q >= 0 && q < len) {
5019
+ const b: i32 = toI32(text.charCodeAt(q))
4296
5020
  const t: i32 = uncheckedGet(trans, off | uncheckedGet(cls, b))
4297
5021
  if (t > 0) {
4298
5022
  off = t
4299
- p -= 1
4300
- } else if (t === 0 || (-t & DFA_DEAD) !== 0) {
4301
- break
5023
+ q -= 1
4302
5024
  } else {
4303
- if ((-t & DFA_MATCH) !== 0) {
4304
- s = p
5025
+ // Minus a state and its flags, as in `dfaForward`.
5026
+ const neg: i32 = regexNegated(t)
5027
+ if (t === 0 || (neg & DFA_DEAD) !== 0) {
5028
+ break
5029
+ }
5030
+ if ((neg & DFA_MATCH) !== 0) {
5031
+ s = q + 1
4305
5032
  }
4306
- off = -t >> 4
4307
- p -= 1
5033
+ off = neg >> 4
5034
+ q -= 1
4308
5035
  }
4309
5036
  }
5037
+ // `q` is -1 at the least, where `p` is 0: in u32 -1 would wrap, which
5038
+ // JavaScript's numbers do not.
5039
+ p = q < 0 ? 0 : toI32(toU32(q) + 1)
4310
5040
  // The character before `p`. At `from` it is read only for its kind,
4311
5041
  // so it is decoded from the start of the text.
4312
5042
  let c: i32 = this.dEot
4313
5043
  let w: i32 = 1
4314
- if (p > 0) {
4315
- const b: i32 = toI32(text.charCodeAt(p - 1))
5044
+ if (q >= 0 && q < len) {
5045
+ const b: i32 = toI32(text.charCodeAt(q))
4316
5046
  if (b < 128) {
4317
5047
  // `dCls` has an entry for every byte.
4318
5048
  c = uncheckedGet(cls, b)
@@ -4320,10 +5050,15 @@ export class Regex {
4320
5050
  const lim: i32 = p > from ? from : 0
4321
5051
  let cp: i32 = REGEX_INVALID + b
4322
5052
  let k: i32 = 2
4323
- while (k <= 4 && p - k >= lim) {
4324
- const lead: i32 = toI32(text.charCodeAt(p - k))
5053
+ while (k <= 4) {
5054
+ // Below zero, `at` wraps to a negative value under `lim`.
5055
+ const at: i32 = toI32(toU32(p) - toU32(k))
5056
+ if (at < lim) {
5057
+ break
5058
+ }
5059
+ const lead: i32 = regexByteAt(text, at)
4325
5060
  if (lead >= 0xc2) {
4326
- const packed: i32 = regexDecodeAt(text, p - k, len)
5061
+ const packed: i32 = regexDecodeAt(text, at)
4327
5062
  if ((packed & 7) === k) {
4328
5063
  cp = packed >> 3
4329
5064
  w = k
@@ -4338,7 +5073,12 @@ export class Regex {
4338
5073
  c = this.dWideClass(cp)
4339
5074
  }
4340
5075
  }
4341
- let t: i32 = trans[off + c]
5076
+ // As in `dfaForward`: the slot is in the table.
5077
+ const at: i32 = toI32(toU32(off) + toU32(c))
5078
+ if (at < 0 || at >= toI32(trans.length)) {
5079
+ return DFA_GAVE_UP
5080
+ }
5081
+ let t: i32 = trans[at]
4342
5082
  if (t === 0) {
4343
5083
  t = this.dStep(off, c, p)
4344
5084
  if (t === 0) {
@@ -4346,15 +5086,17 @@ export class Regex {
4346
5086
  }
4347
5087
  trans = this.dTrans
4348
5088
  }
5089
+ // Past here `p` is above `from`, and the `w` bytes before it were
5090
+ // decoded at or after `from`, so `p - w` cannot wrap.
4349
5091
  if (t > 0) {
4350
5092
  if (p <= from) {
4351
5093
  return s
4352
5094
  }
4353
5095
  off = t
4354
- p -= w
5096
+ p = toI32(toU32(p) - toU32(w))
4355
5097
  continue
4356
5098
  }
4357
- t = -t
5099
+ t = regexNegated(t)
4358
5100
  if ((t & DFA_MATCH) !== 0) {
4359
5101
  s = p
4360
5102
  }
@@ -4362,7 +5104,7 @@ export class Regex {
4362
5104
  return s
4363
5105
  }
4364
5106
  off = t >> 4
4365
- p -= w
5107
+ p = toI32(toU32(p) - toU32(w))
4366
5108
  }
4367
5109
  }
4368
5110
 
@@ -4389,7 +5131,7 @@ export class Regex {
4389
5131
  if (i < 0) {
4390
5132
  return DFA_NONE
4391
5133
  }
4392
- this.innerGain += toF64(i + 1 - lower)
5134
+ this.innerGain += toF64(i) + 1.0 - toF64(lower)
4393
5135
  const s: i32 = this.dfaReverse(text, lower, i, DFA_INNER)
4394
5136
  if (s === DFA_GAVE_UP) {
4395
5137
  return DFA_GAVE_UP
@@ -4400,12 +5142,13 @@ export class Regex {
4400
5142
  this.dBegin = s
4401
5143
  return e
4402
5144
  }
4403
- this.innerWaste += toF64(this.dStop - i)
5145
+ this.innerWaste += toF64(this.dStop) - toF64(i)
4404
5146
  if (this.innerWaste > 2.0 * this.innerGain + DFA_INNER_SLACK) {
4405
5147
  this.innerLit = ""
4406
5148
  }
4407
5149
  }
4408
- lower = i + 1
5150
+ // `i` is a byte of the text, so the next one cannot wrap.
5151
+ lower = toI32(toU32(i) + 1)
4409
5152
  }
4410
5153
  // No match starts before `lower`.
4411
5154
  const e: i32 = this.dfaForward(text, lower, first, false)
@@ -4433,8 +5176,8 @@ export class Regex {
4433
5176
  return false
4434
5177
  }
4435
5178
  if (want >= REGEX_WANT_BOUNDS) {
4436
- this.mslots[0] = at
4437
- this.mslots[1] = at + toI32(this.prefix.length)
5179
+ // The prefix found at `at` ends inside the text.
5180
+ this.setBounds(at, toI32(toU32(at) + toU32(this.prefix.length)))
4438
5181
  }
4439
5182
  return true
4440
5183
  }
@@ -4448,8 +5191,7 @@ export class Regex {
4448
5191
  }
4449
5192
  if (e >= 0 && want <= REGEX_WANT_BOUNDS) {
4450
5193
  if (want > 0) {
4451
- this.mslots[0] = this.dBegin
4452
- this.mslots[1] = e
5194
+ this.setBounds(this.dBegin, e)
4453
5195
  }
4454
5196
  return true
4455
5197
  }
@@ -4470,8 +5212,7 @@ export class Regex {
4470
5212
  if (e >= 0) {
4471
5213
  const s: i32 = this.dfaReverse(text, from, e, DFA_REVERSE)
4472
5214
  if (s >= 0 && want <= REGEX_WANT_BOUNDS) {
4473
- this.mslots[0] = s
4474
- this.mslots[1] = e
5215
+ this.setBounds(s, e)
4475
5216
  return true
4476
5217
  }
4477
5218
  if (s >= 0) {
@@ -4483,6 +5224,18 @@ export class Regex {
4483
5224
  return this.vm(text, from, want, false)
4484
5225
  }
4485
5226
 
5227
+ /**
5228
+ * Set the match's bounds, group 0's slots. `build` sizes `mslots` for
5229
+ * them, so the test never drops a match.
5230
+ */
5231
+ private setBounds(s: i32, e: i32): void {
5232
+ const m: i32[] = this.mslots
5233
+ if (toI32(m.length) >= 2) {
5234
+ m[0] = s
5235
+ m[1] = e
5236
+ }
5237
+ }
5238
+
4486
5239
  /**
4487
5240
  * The VM: whether the pattern matches `text` at or after byte `from`, and
4488
5241
  * the leftmost-first match's first `want` slots in `mslots`. `want` is 0 to
@@ -4491,16 +5244,23 @@ export class Regex {
4491
5244
  * depends on them. `pinned` tries only a match that starts at `from`.
4492
5245
  */
4493
5246
  private vm(text: string, from: i32, want: i32, pinned: boolean): boolean {
4494
- const ns: i32 = want > 0 && this.width > 2 * this.groups ? this.width : want
5247
+ const ns: i32 = want > 0 && this.width > this.groupSlots() ? this.width : want
4495
5248
  const len: i32 = toI32(text.length)
4496
5249
  const anchored: boolean = this.anchored || pinned
4497
5250
  const hasPrefix: boolean = this.prefix !== ""
4498
5251
  const hasScan: boolean = this.scanSel
4499
- const hasSet: boolean = toI32(this.firstSet.length) > 0
5252
+ const firstSet: u8[] = this.firstSet
5253
+ const hasSet: boolean = toI32(firstSet.length) > 0
5254
+ const op: i32[] = this.op
5255
+ const ia: i32[] = this.ia
5256
+ const ib: i32[] = this.ib
5257
+ const clsAscii: u8[] = this.clsAscii
5258
+ const caps: i32[] = this.caps
5259
+ const mslots: i32[] = this.mslots
4500
5260
  this.cCount = 0
4501
5261
  let matched: boolean = false
4502
5262
  let p: i32 = from
4503
- while (true) {
5263
+ while (p >= 0) {
4504
5264
  if (!matched && (p === from || !anchored)) {
4505
5265
  if (this.cCount === 0) {
4506
5266
  if (!anchored) {
@@ -4521,7 +5281,7 @@ export class Regex {
4521
5281
  // before any byte inside one.
4522
5282
  while (p >= 0 && p < len) {
4523
5283
  const b: i32 = toI32(text.charCodeAt(p))
4524
- if (b >= 128 ? this.firstWide : this.firstSet[b] === 1) {
5284
+ if (b >= 128 ? this.firstWide : b < toI32(firstSet.length) && firstSet[b] === 1) {
4525
5285
  break
4526
5286
  }
4527
5287
  p += 1
@@ -4535,17 +5295,18 @@ export class Regex {
4535
5295
  this.newGeneration()
4536
5296
  }
4537
5297
  let k: i32 = 0
4538
- while (k < ns) {
4539
- this.caps[k] = -1
5298
+ while (k < ns && k < toI32(caps.length)) {
5299
+ caps[k] = -1
4540
5300
  k += 1
4541
5301
  }
4542
- this.cCount = this.addThread(this.cDense, this.cSlots, this.cCount, REGEX_ENTRY, text, p, len, ns)
5302
+ this.cCount = this.addThread(this.cDense, this.cSlots, this.cCount, REGEX_ENTRY, text, p, ns)
4543
5303
  }
4544
5304
  if (this.cCount === 0) {
4545
5305
  if (matched || anchored || p >= len) {
4546
5306
  break
4547
5307
  }
4548
- p += regexWidthAt(text, p, len)
5308
+ // `p` is inside the text and a character is at most 4 bytes.
5309
+ p = toI32(toU32(p) + toU32(regexWidthAt(text, p)))
4549
5310
  continue
4550
5311
  }
4551
5312
  let cp: i32 = -1
@@ -4553,26 +5314,36 @@ export class Regex {
4553
5314
  if (p >= 0 && p < len) {
4554
5315
  cp = toI32(text.charCodeAt(p))
4555
5316
  if (cp >= 128) {
4556
- const packed: i32 = regexDecodeAt(text, p, len)
5317
+ const packed: i32 = regexDecodeAt(text, p)
4557
5318
  cp = packed >> 3
4558
5319
  w = packed & 7
4559
5320
  }
4560
5321
  }
4561
5322
  this.newGeneration()
4562
5323
  this.nCount = 0
5324
+ const cDense: i32[] = this.cDense
5325
+ const cSlots: i32[] = this.cSlots
5326
+ const cCount: i32 = this.cCount
4563
5327
  let i: i32 = 0
4564
- while (i < this.cCount) {
4565
- const pc: i32 = this.cDense[i]
5328
+ while (i < cCount && i < toI32(cDense.length)) {
5329
+ const pc: i32 = cDense[i]
4566
5330
  i += 1
4567
- const o: i32 = this.op[pc]
5331
+ // A list holds only pcs of the program, each with both operands.
5332
+ if (pc < 0 || pc >= toI32(op.length) || pc >= toI32(ia.length) || pc >= toI32(ib.length)) {
5333
+ continue
5334
+ }
5335
+ const o: i32 = op[pc]
4568
5336
  let hit: boolean = false
4569
5337
  if (o === OP_CHAR) {
4570
- hit = cp === this.ia[pc] || cp === this.ib[pc]
5338
+ hit = cp === ia[pc] || cp === ib[pc]
4571
5339
  } else if (o === OP_CLASS) {
4572
5340
  if (cp >= 0 && cp < 128) {
4573
- hit = this.clsAscii[this.ia[pc] * 128 + cp] === 1
5341
+ // Unsigned: a class number is below the instruction limit, so its
5342
+ // 128-byte row stays inside an i32.
5343
+ const at: i32 = toI32(toU32(ia[pc]) * 128 + toU32(cp))
5344
+ hit = at >= 0 && at < toI32(clsAscii.length) && clsAscii[at] === 1
4574
5345
  } else {
4575
- hit = cp >= 0 && this.classHas(this.ia[pc], cp)
5346
+ hit = cp >= 0 && this.classHas(ia[pc], cp)
4576
5347
  }
4577
5348
  } else if (o === OP_ANY_NL) {
4578
5349
  hit = cp >= 0 && cp !== 10 && cp !== 13 && cp !== 0x2028 && cp !== 0x2029
@@ -4582,24 +5353,25 @@ export class Regex {
4582
5353
  if (ns === 0) {
4583
5354
  return true
4584
5355
  }
4585
- const base: i32 = pc * ns
4586
- let k: i32 = 0
4587
- while (k < ns) {
4588
- this.mslots[k] = this.cSlots[base + k]
4589
- k += 1
4590
- }
5356
+ // Unsigned: the slot table holds `ns` slots for every pc.
5357
+ regexLoadSlots(mslots, cSlots, toI32(toU32(pc) * toU32(ns)), ns)
4591
5358
  matched = true
4592
5359
  // Every thread after this one has lower priority.
4593
5360
  break
4594
5361
  }
4595
5362
  if (hit) {
4596
- const base: i32 = pc * ns
4597
- let k: i32 = 0
4598
- while (k < ns) {
4599
- this.caps[k] = this.cSlots[base + k]
4600
- k += 1
4601
- }
4602
- this.nCount = this.addThread(this.nDense, this.nSlots, this.nCount, pc + 1, text, p + w, len, ns)
5363
+ regexLoadSlots(caps, cSlots, toI32(toU32(pc) * toU32(ns)), ns)
5364
+ // A hit read a character at `p`, inside the text; `pc` is below
5365
+ // the program's length.
5366
+ this.nCount = this.addThread(
5367
+ this.nDense,
5368
+ this.nSlots,
5369
+ this.nCount,
5370
+ pc + 1,
5371
+ text,
5372
+ toI32(toU32(p) + toU32(w)),
5373
+ ns
5374
+ )
4603
5375
  }
4604
5376
  }
4605
5377
  const d: i32[] = this.cDense
@@ -4612,7 +5384,8 @@ export class Regex {
4612
5384
  if (p >= len) {
4613
5385
  break
4614
5386
  }
4615
- p += w
5387
+ // `p` is inside the text and `w` the width of its character.
5388
+ p = toI32(toU32(p) + toU32(w))
4616
5389
  }
4617
5390
  return matched
4618
5391
  }
@@ -4638,16 +5411,20 @@ export class Regex {
4638
5411
  * @returns True when there was a match.
4639
5412
  */
4640
5413
  exec(text: string, from: i32, m: RegexMatch): boolean {
4641
- const ns: i32 = 2 * this.groups
5414
+ const ns: i32 = this.groupSlots()
4642
5415
  if (!this.run(text, from, ns)) {
4643
5416
  return false
4644
5417
  }
4645
5418
  if (toI32(m.slots.length) !== ns) {
4646
5419
  m.slots = regexZeros(ns)
4647
5420
  }
5421
+ // `dst` now holds `ns` slots and `src` at least as many (marks follow the
5422
+ // groups' slots), so the loop copies exactly the groups' slots.
5423
+ const dst: i32[] = m.slots
5424
+ const src: i32[] = this.mslots
4648
5425
  let k: i32 = 0
4649
- while (k < ns) {
4650
- m.slots[k] = this.mslots[k]
5426
+ while (k < toI32(dst.length) && k < toI32(src.length)) {
5427
+ dst[k] = src[k]
4651
5428
  k += 1
4652
5429
  }
4653
5430
  m.input = text
@@ -4682,14 +5459,14 @@ export class Regex {
4682
5459
  const len: i32 = toI32(text.length)
4683
5460
  let pos: i32 = 0
4684
5461
  while (pos <= len && this.run(text, pos, REGEX_WANT_BOUNDS)) {
4685
- const s: i32 = this.mslots[0]
4686
- const e: i32 = this.mslots[1]
4687
- out.push(text.slice(s, e))
5462
+ const s: i32 = this.slotOf(toI32(0))
5463
+ const e: i32 = this.slotOf(toI32(1))
5464
+ out.push(regexSpan(text, s, e))
4688
5465
  if (e === s) {
4689
5466
  if (e >= len) {
4690
5467
  break
4691
5468
  }
4692
- pos = e + regexWidthAt(text, e, len)
5469
+ pos = this.pastEmpty(text, e)
4693
5470
  } else {
4694
5471
  pos = e
4695
5472
  }
@@ -4705,17 +5482,20 @@ export class Regex {
4705
5482
  */
4706
5483
  count(text: string): i32 {
4707
5484
  const len: i32 = toI32(text.length)
4708
- let n: i32 = 0
5485
+ // Unsigned: a match is counted at most once a position and once at the
5486
+ // end, so the count stays within an i32.
5487
+ let n: u32 = 0
4709
5488
  let pos: i32 = 0
4710
5489
  if (this.literal && this.prefix !== "") {
4711
- const n0: i32 = toI32(this.prefix.length)
5490
+ const n0: u32 = toU32(this.prefix.length)
4712
5491
  while (true) {
4713
5492
  const at: i32 = this.findPrefix(text, pos)
4714
5493
  if (at < 0) {
4715
- return n
5494
+ return toI32(n)
4716
5495
  }
4717
5496
  n += 1
4718
- pos = at + n0
5497
+ // The prefix found at `at` ends inside the text; unsigned, nothing wraps.
5498
+ pos = toI32(toU32(at) + n0)
4719
5499
  }
4720
5500
  }
4721
5501
  if (!this.dReady) {
@@ -4723,7 +5503,7 @@ export class Regex {
4723
5503
  }
4724
5504
  // A match that cannot be empty ends past where its search started, so
4725
5505
  // the forward DFA alone counts them; marks could move the end.
4726
- if (this.dOk && !this.literal && !this.nullable && this.width === 2 * this.groups) {
5506
+ if (this.dOk && !this.literal && !this.nullable && this.width === this.groupSlots()) {
4727
5507
  while (pos <= len) {
4728
5508
  let e: i32 = DFA_NONE
4729
5509
  if (this.innerLit !== "") {
@@ -4732,7 +5512,7 @@ export class Regex {
4732
5512
  e = this.dfaForward(text, pos, false, false)
4733
5513
  }
4734
5514
  if (e === DFA_NONE) {
4735
- return n
5515
+ return toI32(n)
4736
5516
  }
4737
5517
  if (e === DFA_GAVE_UP) {
4738
5518
  break
@@ -4743,18 +5523,18 @@ export class Regex {
4743
5523
  }
4744
5524
  while (pos <= len && this.run(text, pos, REGEX_WANT_BOUNDS)) {
4745
5525
  n += 1
4746
- const s: i32 = this.mslots[0]
4747
- const e: i32 = this.mslots[1]
5526
+ const s: i32 = this.slotOf(toI32(0))
5527
+ const e: i32 = this.slotOf(toI32(1))
4748
5528
  if (e === s) {
4749
5529
  if (e >= len) {
4750
5530
  break
4751
5531
  }
4752
- pos = e + regexWidthAt(text, e, len)
5532
+ pos = this.pastEmpty(text, e)
4753
5533
  } else {
4754
5534
  pos = e
4755
5535
  }
4756
5536
  }
4757
- return n
5537
+ return toI32(n)
4758
5538
  }
4759
5539
 
4760
5540
  /**
@@ -4786,11 +5566,12 @@ export class Regex {
4786
5566
  const len: i32 = toI32(text.length)
4787
5567
  // Track every group only when the template can name one.
4788
5568
  let ns: i32 = 2
5569
+ const n: i32 = toI32(replacement.length)
4789
5570
  let i: i32 = toI32(replacement.indexOf("$"))
4790
- while (i >= 0 && i + 1 < toI32(replacement.length)) {
5571
+ while (i >= 0 && i < n - 1) {
4791
5572
  const c: i32 = toI32(replacement.charCodeAt(i + 1))
4792
5573
  if ((c >= 48 && c <= 57) || c === 60) {
4793
- ns = 2 * this.groups
5574
+ ns = this.groupSlots()
4794
5575
  break
4795
5576
  }
4796
5577
  i = toI32(replacement.indexOf("$", i + 2))
@@ -4799,9 +5580,9 @@ export class Regex {
4799
5580
  let last: i32 = 0
4800
5581
  let pos: i32 = 0
4801
5582
  while (pos <= len && this.run(text, pos, ns)) {
4802
- const s: i32 = this.mslots[0]
4803
- const e: i32 = this.mslots[1]
4804
- parts.push(text.slice(last, s))
5583
+ const s: i32 = this.slotOf(toI32(0))
5584
+ const e: i32 = this.slotOf(toI32(1))
5585
+ parts.push(regexSpan(text, last, s))
4805
5586
  this.expand(replacement, text, ns, parts)
4806
5587
  last = e
4807
5588
  if (!all) {
@@ -4811,7 +5592,7 @@ export class Regex {
4811
5592
  if (e >= len) {
4812
5593
  break
4813
5594
  }
4814
- pos = e + regexWidthAt(text, e, len)
5595
+ pos = this.pastEmpty(text, e)
4815
5596
  } else {
4816
5597
  pos = e
4817
5598
  }
@@ -4819,25 +5600,73 @@ export class Regex {
4819
5600
  if (toI32(parts.length) === 0) {
4820
5601
  return text
4821
5602
  }
4822
- parts.push(text.slice(last, len))
5603
+ parts.push(regexSpan(text, last, len))
4823
5604
  return parts.join("")
4824
5605
  }
4825
5606
 
5607
+ /** Two slots a group; `REGEX_MAX_GROUPS` keeps the product far from wrapping. */
5608
+ private groupSlots(): i32 {
5609
+ return toI32(toU32(this.groups) * 2)
5610
+ }
5611
+
5612
+ /**
5613
+ * Slot `k` of the match the last `run` found. `build` sizes `mslots` to
5614
+ * every slot, group 0's included, before any run, so the -1 outside it,
5615
+ * that of a group that took no part, is never what a caller reads.
5616
+ */
5617
+ private slotOf(k: i32): i32 {
5618
+ const s: i32[] = this.mslots
5619
+ if (k < 0 || k >= toI32(s.length)) {
5620
+ return -1
5621
+ }
5622
+ return s[k]
5623
+ }
5624
+
5625
+ /**
5626
+ * Where the search resumes after an empty match at `e`, which is inside
5627
+ * the text: one character on. Unsigned, as a character's width is at
5628
+ * most 4 and the sum stays at or below the text's length.
5629
+ */
5630
+ private pastEmpty(text: string, e: i32): i32 {
5631
+ return toI32(toU32(e) + toU32(regexWidthAt(text, e)))
5632
+ }
5633
+
5634
+ /**
5635
+ * The text of the group `rep` names from byte `from` to `to`, or "". The
5636
+ * caller finds `to` at or after `from`, inside `rep`.
5637
+ */
5638
+ private namedText(text: string, ns: i32, rep: string, from: i32, to: i32): string {
5639
+ if (from < 0 || to < from || to > toI32(rep.length)) {
5640
+ return ""
5641
+ }
5642
+ return this.slotText(text, ns, this.groupIndex(rep.substring(from, to)))
5643
+ }
5644
+
4826
5645
  /** Group `k` of the match in `mslots`, or "". */
4827
5646
  private slotText(text: string, ns: i32, k: i32): string {
4828
- if (k < 0 || 2 * k + 1 >= ns || this.mslots[2 * k] < 0) {
5647
+ // `2 * k` unsigned: a `k` of 2^30 or more wraps it negative, which the
5648
+ // test turns away as it does a negative `k`; an even `at` plus one
5649
+ // cannot wrap.
5650
+ const at: i32 = toI32(toU32(k) * 2)
5651
+ const end: i32 = toI32(toU32(at) + 1)
5652
+ if (k < 0 || at < 0 || end >= ns) {
5653
+ return ""
5654
+ }
5655
+ const from: i32 = this.slotOf(at)
5656
+ if (from < 0) {
4829
5657
  return ""
4830
5658
  }
4831
- return text.slice(this.mslots[2 * k], this.mslots[2 * k + 1])
5659
+ return regexSpan(text, from, this.slotOf(end))
4832
5660
  }
4833
5661
 
4834
5662
  /** Append the replacement template, expanded for the match in `mslots`, to `parts`. */
4835
5663
  private expand(rep: string, text: string, ns: i32, parts: string[]): void {
4836
5664
  const n: i32 = toI32(rep.length)
4837
- const groupsAfterZero: i32 = this.groups - 1
5665
+ // Group 0 is always there, so this is not negative.
5666
+ const groupsAfterZero: i32 = toI32(toU32(this.groups) - 1)
4838
5667
  let lit: i32 = 0
4839
5668
  let i: i32 = toI32(rep.indexOf("$"))
4840
- while (i >= 0 && i + 1 < n) {
5669
+ while (i >= 0 && i < n - 1) {
4841
5670
  const c: i32 = toI32(rep.charCodeAt(i + 1))
4842
5671
  let used: i32 = 0
4843
5672
  let value: string = ""
@@ -4848,10 +5677,10 @@ export class Regex {
4848
5677
  value = this.slotText(text, ns, REGEX_WHOLE)
4849
5678
  used = 2
4850
5679
  } else if (c === 96) {
4851
- value = text.slice(0, this.mslots[0])
5680
+ value = text.substring(0, this.slotOf(toI32(0)))
4852
5681
  used = 2
4853
5682
  } else if (c === 39) {
4854
- value = text.slice(this.mslots[1], toI32(text.length))
5683
+ value = text.substring(this.slotOf(toI32(1)), toI32(text.length))
4855
5684
  used = 2
4856
5685
  } else if (c >= 48 && c <= 57) {
4857
5686
  const one: i32 = c - 48
@@ -4866,20 +5695,21 @@ export class Regex {
4866
5695
  } else if (c === 60 && this.hasNames) {
4867
5696
  const close: i32 = toI32(rep.indexOf(">", i + 2))
4868
5697
  if (close >= 0) {
4869
- value = this.slotText(text, ns, this.groupIndex(rep.slice(i + 2, close)))
4870
- used = close + 1 - i
5698
+ // `close` is at or after `i + 2`, so the unsigned width cannot wrap.
5699
+ value = this.namedText(text, ns, rep, i + 2, close)
5700
+ used = toI32(toU32(close) + 1 - toU32(i))
4871
5701
  }
4872
5702
  }
4873
5703
  if (used === 0) {
4874
5704
  i = toI32(rep.indexOf("$", i + 1))
4875
5705
  continue
4876
5706
  }
4877
- parts.push(rep.slice(lit, i))
5707
+ parts.push(regexSpan(rep, lit, i))
4878
5708
  parts.push(value)
4879
- lit = i + used
5709
+ lit = toI32(toU32(i) + toU32(used))
4880
5710
  i = toI32(rep.indexOf("$", lit))
4881
5711
  }
4882
- parts.push(rep.slice(lit, n))
5712
+ parts.push(regexSpan(rep, lit, n))
4883
5713
  }
4884
5714
 
4885
5715
  /**
@@ -4895,7 +5725,7 @@ export class Regex {
4895
5725
  split(text: string): string[] {
4896
5726
  const len: i32 = toI32(text.length)
4897
5727
  const out: string[] = []
4898
- const ns: i32 = 2 * this.groups
5728
+ const ns: i32 = this.groupSlots()
4899
5729
  if (len === 0) {
4900
5730
  if (!this.run(text, REGEX_TEXT_START, REGEX_WANT_NONE)) {
4901
5731
  out.push(text)
@@ -4905,16 +5735,16 @@ export class Regex {
4905
5735
  let p: i32 = 0
4906
5736
  let q: i32 = 0
4907
5737
  while (q < len && this.run(text, q, ns)) {
4908
- const s: i32 = this.mslots[0]
4909
- const e: i32 = this.mslots[1]
5738
+ const s: i32 = this.slotOf(toI32(0))
5739
+ const e: i32 = this.slotOf(toI32(1))
4910
5740
  if (s >= len) {
4911
5741
  break
4912
5742
  }
4913
5743
  if (e === p) {
4914
- q = s + regexWidthAt(text, s, len)
5744
+ q = this.pastEmpty(text, s)
4915
5745
  continue
4916
5746
  }
4917
- out.push(text.slice(p, s))
5747
+ out.push(regexSpan(text, p, s))
4918
5748
  let k: i32 = 1
4919
5749
  while (k < this.groups) {
4920
5750
  out.push(this.slotText(text, ns, k))
@@ -4923,7 +5753,7 @@ export class Regex {
4923
5753
  p = e
4924
5754
  q = p
4925
5755
  }
4926
- out.push(text.slice(p, len))
5756
+ out.push(regexSpan(text, p, len))
4927
5757
  return out
4928
5758
  }
4929
5759
  }
@@ -5008,7 +5838,7 @@ export const regexEscape = (text: string): string => {
5008
5838
  c === 124 ||
5009
5839
  c === 47
5010
5840
  ) {
5011
- parts.push(text.slice(lit, i))
5841
+ parts.push(regexSpan(text, lit, i))
5012
5842
  parts.push("\\")
5013
5843
  lit = i
5014
5844
  }
@@ -5017,6 +5847,6 @@ export const regexEscape = (text: string): string => {
5017
5847
  if (parts.length === 0) {
5018
5848
  return text
5019
5849
  }
5020
- parts.push(text.slice(lit, n))
5850
+ parts.push(regexSpan(text, lit, n))
5021
5851
  return parts.join("")
5022
5852
  }