@amritk/nish-x86_64-linux 0.34.1 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/INSTALL.md +22 -13
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +86 -16
  4. package/package.json +1 -1
  5. package/runtime/async.d.mts +49 -0
  6. package/runtime/async.mjs +27 -0
  7. package/runtime/nish.d.ts +4681 -427
  8. package/runtime/nish.h +6 -2
  9. package/runtime/nish.mjs +4 -0
  10. package/runtime/runtime-net.c +22 -0
  11. package/runtime/runtime-shm.c +93 -2
  12. package/runtime/vite.d.ts +30 -0
  13. package/runtime/vite.mjs +150 -0
  14. package/scripts/build.sh +108 -13
  15. package/std/README.md +131 -131
  16. package/std/base64.ts +34 -5
  17. package/std/bytes.ts +258 -42
  18. package/std/cli-args.ts +296 -70
  19. package/std/cli-flags.ts +184 -19
  20. package/std/cli.ts +475 -54
  21. package/std/collections.ts +121 -23
  22. package/std/compress/checksum.ts +44 -14
  23. package/std/compress/deflate.ts +224 -111
  24. package/std/compress/gzip.ts +217 -53
  25. package/std/compress/zlib.ts +63 -7
  26. package/std/crypto/aes.ts +80 -9
  27. package/std/crypto/base64url.ts +22 -0
  28. package/std/crypto/bcrypt-batch.ts +88 -21
  29. package/std/crypto/bcrypt.ts +60 -0
  30. package/std/crypto/chacha20poly1305.ts +53 -0
  31. package/std/crypto/ct.ts +32 -13
  32. package/std/crypto/ed25519.ts +47 -0
  33. package/std/crypto/hkdf.ts +198 -13
  34. package/std/crypto/hmac.ts +235 -31
  35. package/std/crypto/jwt.ts +192 -63
  36. package/std/crypto/p256.ts +159 -16
  37. package/std/crypto/p384.ts +28 -3
  38. package/std/crypto/pki-roots.ts +32 -2
  39. package/std/crypto/pki.ts +81 -15
  40. package/std/crypto/rsa.ts +33 -3
  41. package/std/crypto/sha1.ts +3 -0
  42. package/std/crypto/sha256.ts +24 -1
  43. package/std/crypto/sha512.ts +99 -14
  44. package/std/crypto/x25519.ts +152 -14
  45. package/std/crypto/x509-random.ts +10 -0
  46. package/std/crypto/x509.ts +67 -7
  47. package/std/json-equal.ts +19 -0
  48. package/std/json-format.ts +112 -13
  49. package/std/json-number.ts +64 -14
  50. package/std/json-pointer.ts +23 -1
  51. package/std/json-tokenizer.ts +100 -29
  52. package/std/json-typed.ts +713 -66
  53. package/std/json-value.ts +400 -124
  54. package/std/json.ts +138 -10
  55. package/std/map.ts +25 -7
  56. package/std/math.ts +36 -4
  57. package/std/net/buffer-pool.ts +42 -11
  58. package/std/net/dns-config.ts +109 -23
  59. package/std/net/dns-message.ts +62 -7
  60. package/std/net/dns-resolver.ts +247 -53
  61. package/std/net/dns.ts +203 -35
  62. package/std/net/fetch.ts +151 -61
  63. package/std/net/handler.ts +278 -80
  64. package/std/net/hpack.ts +262 -52
  65. package/std/net/http-compress.ts +125 -15
  66. package/std/net/http-fields.ts +85 -9
  67. package/std/net/http1-client-codec.ts +100 -19
  68. package/std/net/http1-codec.ts +297 -77
  69. package/std/net/http1-server.ts +430 -94
  70. package/std/net/http2-frame.ts +272 -35
  71. package/std/net/http2-tls.ts +135 -34
  72. package/std/net/http2.ts +247 -106
  73. package/std/net/http3-frame.ts +154 -17
  74. package/std/net/http3-server.ts +275 -101
  75. package/std/net/http3.ts +307 -115
  76. package/std/net/outbound.ts +37 -3
  77. package/std/net/postgres-carrier.ts +144 -8
  78. package/std/net/postgres.ts +801 -112
  79. package/std/net/prefork.ts +164 -31
  80. package/std/net/qpack.ts +105 -18
  81. package/std/net/quic-conn-ack.ts +59 -12
  82. package/std/net/quic-conn-cid.ts +124 -27
  83. package/std/net/quic-conn-params.ts +70 -10
  84. package/std/net/quic-datagram.ts +43 -3
  85. package/std/net/quic-frame.ts +286 -12
  86. package/std/net/quic-listener.ts +101 -32
  87. package/std/net/quic-packet.ts +302 -21
  88. package/std/net/quic-recovery.ts +264 -52
  89. package/std/net/quic-stream.ts +353 -91
  90. package/std/net/quic.ts +1124 -201
  91. package/std/net/router.ts +87 -40
  92. package/std/net/tls/client.ts +166 -51
  93. package/std/net/tls/codec.ts +413 -56
  94. package/std/net/tls/record-client.ts +110 -55
  95. package/std/net/tls/record-server.ts +159 -59
  96. package/std/net/tls/record.ts +109 -12
  97. package/std/net/tls/schedule.ts +235 -26
  98. package/std/net/tls/ticket.ts +159 -40
  99. package/std/net/tls/verify.ts +49 -3
  100. package/std/net/tls-tcp.ts +250 -45
  101. package/std/net/tls.ts +221 -59
  102. package/std/net/upstream.ts +81 -45
  103. package/std/net/url.ts +97 -13
  104. package/std/net/websocket.ts +122 -12
  105. package/std/net/webtransport.ts +220 -89
  106. package/std/pair.ts +10 -2
  107. package/std/random.ts +34 -9
  108. package/std/regex.ts +352 -198
  109. package/std/region.ts +70 -15
  110. package/std/secret.ts +52 -11
  111. package/std/sort.ts +17 -2
  112. package/std/sqlite.ts +256 -35
  113. package/std/sync/ring.ts +430 -40
  114. package/std/sync/shm.ts +222 -15
  115. package/std/term-io.ts +137 -64
  116. package/std/term-pixels.ts +144 -19
  117. package/std/term-probe.ts +66 -25
  118. package/std/term-rate.ts +128 -57
  119. package/std/term.ts +332 -121
  120. package/std/testing.ts +130 -32
  121. package/std/text.ts +73 -7
  122. package/std/threads.ts +229 -41
  123. package/std/yaml-json.ts +94 -82
  124. package/std/yaml-lines.ts +47 -15
  125. package/std/yaml-write.ts +24 -16
  126. package/std/yaml.ts +764 -384
package/std/regex.ts CHANGED
@@ -151,19 +151,25 @@ const REGEX_FOLD_INDEX: string =
151
151
  "005E0002006?001C00700034007@004F00810065008A007500910085009C009500:400:500:E00;500;500<500;E00=500<600>800<G00?B00=700@L00=I00B100>@00C200?000D300?A00E300@100F500@A00G500A200H600AB00I600B800JF00BK00L400CE00N600KA010100L@011<00M0012M00MA014>00N1016000NA017@00O4018C00OD019F010601;1010F01<A011601>1011F01?C012601A6012F01BG013601D3013F01E3014>01F3014N01G3015>01H3015N01I3016>01J4016N01K5017>01L5017N01M5018>01N5018N01O5019>0205019O021C01:?023301;9024C01;I02630452027G045B029G046202;G046J02=G047:02?G047J02AG04M<02D404ML02FD04N<02I404NL02KD04O<02N4074002OH074E031C0755033C075E035C07@4037?07@D038?07A4039?07AD03:?07B403;?07BD03<?07C403=@07CD03>@07D403?@07DD03@@07E<03AC07EL03BC07F<03CC07FL03DC07G<03EC07GL03FC07H<03GK07I003I107I@03J907J003KA07JI03LK07K<03N307KL03O?07L>040I07LN042107M>043907N<044F07O<046308;5047F08;E0496095I04:E096904<5096I04=E097904?50;0?04@G0;0O04B70;1?04CG0;1O04E70;2?04FG0;2O04H70;3?04IL0;4804K80;4H04L80;5804M80;5H04N80;6804O80;6H05080;7B05180;8>05340;8N055419B6056J19BF057M19C6058M19D8059M19DH05:M19I>05;N19J005<N19J@05=N19K005>N19KI05?N19L<05A119LO05B519M@05CD19N005DO19NF05F91:KF05H11:L605JA1:LF05M11:M605OA1:MF06211OI506471OIE065G1OJ;06772100068I210@06:9211006;I211@06=9212006>I215@06@:216006AJ216@06C:217406DJ217D06F:21;H06GK21<906I;21<L06JK21==06L;234206MM234B06O=2352070M235B072=236?073M236O075=237?076M23:L078>23;F079N265007;@265@07=0266007>@266@07@02KB007AB2KB@07C22KC007DB2KC@07F23J8007GD3J8@07I43J9007JD3J9@07L43J:007MD"
152
152
  // gen-regex-unicode:end
153
153
 
154
- /** The most instructions a compiled pattern may have. */
154
+ /** The most instructions a compiled pattern may have; `regexCompile` refuses a bigger program. */
155
155
  export const REGEX_MAX_INSTRUCTIONS: i32 = 100000
156
- /** The largest count `{n}`, `{n,}` or `{n,m}` may give. */
156
+ /** The largest count `{n}`, `{n,}` or `{n,m}` may give; a larger one is a compile error. */
157
157
  export const REGEX_MAX_REPEAT: i32 = 1000
158
- /** How deeply groups may nest. */
158
+ /** How deeply groups may nest; a deeper pattern is a compile error. */
159
159
  export const REGEX_MAX_DEPTH: i32 = 250
160
160
  /** The most entries one thread list's capture table may have: instructions × slots per thread. */
161
161
  const REGEX_MAX_SLOT_TABLE: i32 = 4194304
162
- /** The most capturing groups a pattern may have, past group 0; a name is checked against each. */
162
+ /**
163
+ * The most capturing groups a pattern may have, past group 0; one more is a
164
+ * compile error. A name is checked against each.
165
+ */
163
166
  export const REGEX_MAX_GROUPS: i32 = 1024
164
- /** The most ranges one class may list; the class sorts them by insertion. */
167
+ /** The most ranges one class may list; one more is a compile error. */
165
168
  export const REGEX_MAX_CLASS_RANGES: i32 = 10000
166
- /** Most ranges the property escapes and case folding of one pattern may add to its classes. */
169
+ /**
170
+ * The most ranges the property escapes and case folding of one pattern may
171
+ * add to its classes; one more is a compile error.
172
+ */
167
173
  export const REGEX_MAX_PROPERTY_RANGES: i32 = 100000
168
174
  /** Most ranges a class sorts by insertion; a longer one is merge-sorted. */
169
175
  const REGEX_INSERTION_SORT: i32 = 64
@@ -462,47 +468,69 @@ const regexPushUtf8 = (out: u8[], cp: i32): void => {
462
468
  */
463
469
  class RegexParser {
464
470
  // Widest first, so the alignment rules insert no padding.
465
- src: string
471
+ private src: string
472
+ /** @internal `regexCompile` puts it in the compile error. */
466
473
  error: string
467
474
 
475
+ /** @internal `Regex` compiles the parser's tree from it. */
468
476
  kind: i32[]
477
+ /** @internal `Regex` compiles the parser's tree from it. */
469
478
  na: i32[]
479
+ /** @internal `Regex` compiles the parser's tree from it. */
470
480
  nb: i32[]
481
+ /** @internal `Regex` compiles the parser's tree from it. */
471
482
  nc: i32[]
483
+ /** @internal `Regex` compiles the parser's tree from it. */
472
484
  nd: i32[]
485
+ /** @internal `Regex` compiles the parser's tree from it. */
473
486
  ne: i32[]
487
+ /** @internal `Regex` compiles the parser's tree from it. */
474
488
  kid: i32[]
489
+ /** @internal `Regex` compiles the parser's tree from it. */
475
490
  next: i32[]
476
491
 
492
+ /** @internal `Regex.build` copies the group names from it. */
477
493
  names: string[]
478
494
 
495
+ /** @internal `Regex.build` copies the parsed classes from it. */
479
496
  clsAscii: u8[]
497
+ /** @internal `Regex.build` copies the parsed classes from it. */
480
498
  clsLo: i32[]
499
+ /** @internal `Regex.build` copies the parsed classes from it. */
481
500
  clsHi: i32[]
501
+ /** @internal `Regex.build` copies the parsed classes from it. */
482
502
  clsFrom: i32[]
503
+ /** @internal `Regex.build` copies the parsed classes from it. */
483
504
  clsTo: i32[]
505
+ /** @internal `Regex.build` copies the parsed classes from it. */
484
506
  clsNeg: u8[]
485
507
  /** The class being parsed: its ranges, unsorted, `tmpCount` of them. */
486
- tmpLo: i32[]
487
- tmpHi: i32[]
508
+ private tmpLo: i32[]
509
+ private tmpHi: i32[]
510
+ /** @internal `regexCompile` compares `pos` with it to find a stray `)`. */
488
511
  len: i32
512
+ /** @internal `regexCompile` reads it to find a stray `)`. */
489
513
  pos: i32
514
+ /** @internal `regexCompile` reads where the parse failed. */
490
515
  errorAt: i32
491
- /** Capture groups, the whole match (group 0) included. */
516
+ /** Capture groups, the whole match (group 0) included. @internal `Regex.build` copies it. */
492
517
  groups: i32
493
- tmpCount: i32
518
+ private tmpCount: i32
494
519
  /** What `parseBraces` read. */
495
- qMin: i32
496
- qMax: i32
520
+ private qMin: i32
521
+ private qMax: i32
497
522
  /** Where `tableNumber` reads next. */
498
- tablePos: i32
523
+ private tablePos: i32
499
524
  /** The ranges property escapes and case folding have added, against `REGEX_MAX_PROPERTY_RANGES`. */
500
- propertyRanges: i32
525
+ private propertyRanges: i32
501
526
 
502
- icase: boolean
503
- multiline: boolean
527
+ private icase: boolean
528
+ private multiline: boolean
529
+ /** @internal `Regex.compileNode` reads it to compile `.`. */
504
530
  dotAll: boolean
531
+ /** @internal `regexCompile` reads it after the parse. */
505
532
  failed: boolean
533
+ /** @internal `Regex.build` copies it. */
506
534
  hasNames: boolean
507
535
 
508
536
  constructor(src: string, icase: boolean, multiline: boolean, dotAll: boolean) {
@@ -541,6 +569,7 @@ class RegexParser {
541
569
  this.propertyRanges = 0
542
570
  }
543
571
 
572
+ /** @internal `regexCompile` calls it for a stray `)`. */
544
573
  fail(message: string, at: i32): i32 {
545
574
  if (!this.failed) {
546
575
  this.failed = true
@@ -550,11 +579,11 @@ class RegexParser {
550
579
  return -1
551
580
  }
552
581
 
553
- byteAt(i: i32): i32 {
582
+ private byteAt(i: i32): i32 {
554
583
  return toI32(this.src.charCodeAt(i))
555
584
  }
556
585
 
557
- node(kind: i32, a: i32, b: i32): i32 {
586
+ private node(kind: i32, a: i32, b: i32): i32 {
558
587
  const n: i32 = toI32(this.kind.length)
559
588
  this.kind.push(kind)
560
589
  this.na.push(a)
@@ -571,7 +600,7 @@ class RegexParser {
571
600
  * A node for code point `cp`, with its other case under `i`: one other
572
601
  * case is a character's second operand, more make a class of them all.
573
602
  */
574
- charNode(cp: i32): i32 {
603
+ private charNode(cp: i32): i32 {
575
604
  if (!this.icase) {
576
605
  return this.node(NODE_CHAR, cp, cp)
577
606
  }
@@ -600,7 +629,7 @@ class RegexParser {
600
629
  }
601
630
 
602
631
  /** The number at `tablePos` in `table`, a generated table's base-32 digits, `tablePos` moved past it. */
603
- tableNumber(table: string): i32 {
632
+ private tableNumber(table: string): i32 {
604
633
  let v: i32 = 0
605
634
  let d: i32 = toI32(table.charCodeAt(this.tablePos)) - 48
606
635
  this.tablePos += 1
@@ -617,7 +646,7 @@ class RegexParser {
617
646
  * block starting at or before it, or the first. `tablePos` is left at that
618
647
  * member's step.
619
648
  */
620
- foldSeek(cp: i32): i32 {
649
+ private foldSeek(cp: i32): i32 {
621
650
  let a: i32 = 0
622
651
  let b: i32 = toI32(REGEX_FOLD_INDEX.length) >> 3
623
652
  while (b - a > 1) {
@@ -633,7 +662,7 @@ class RegexParser {
633
662
  }
634
663
 
635
664
  /** `cp`'s step in `REGEX_FOLD_DATA`, for `regexFoldTarget`, or -1 if it folds to nothing. */
636
- foldStepOf(cp: i32): i32 {
665
+ private foldStepOf(cp: i32): i32 {
637
666
  let c: i32 = this.foldSeek(cp)
638
667
  const end: i32 = toI32(REGEX_FOLD_DATA.length)
639
668
  while (c < cp && this.tablePos < end) {
@@ -645,7 +674,7 @@ class RegexParser {
645
674
  return c === cp && this.tablePos < end ? this.tableNumber(REGEX_FOLD_DATA) : -1
646
675
  }
647
676
 
648
- /** `alternative ('|' alternative)*`. */
677
+ /** `alternative ('|' alternative)*`. @internal `regexCompile` starts the parse with it. */
649
678
  parseAlt(depth: i32): i32 {
650
679
  if (depth > REGEX_MAX_DEPTH) {
651
680
  return this.fail("groups nest too deeply", this.pos)
@@ -673,7 +702,7 @@ class RegexParser {
673
702
  }
674
703
 
675
704
  /** The terms up to a `|`, a `)` or the end. */
676
- parseSeq(depth: i32): i32 {
705
+ private parseSeq(depth: i32): i32 {
677
706
  let first: i32 = -1
678
707
  let last: i32 = -1
679
708
  let count: i32 = 0
@@ -706,7 +735,7 @@ class RegexParser {
706
735
  }
707
736
 
708
737
  /** An atom and at most one quantifier. */
709
- parseTerm(depth: i32): i32 {
738
+ private parseTerm(depth: i32): i32 {
710
739
  const groupsBefore: i32 = this.groups
711
740
  const atomAt: i32 = this.pos
712
741
  const atom: i32 = this.parseAtom(depth)
@@ -765,7 +794,7 @@ class RegexParser {
765
794
  * in `qMin` / `qMax` (-1 unbounded) with the cursor past it. Anything else
766
795
  * leaves the cursor where it was.
767
796
  */
768
- parseBraces(): boolean {
797
+ private parseBraces(): boolean {
769
798
  let i: i32 = this.pos + 1
770
799
  const start: i32 = this.pos
771
800
  let min: i32 = 0
@@ -814,7 +843,7 @@ class RegexParser {
814
843
  return true
815
844
  }
816
845
 
817
- parseAtom(depth: i32): i32 {
846
+ private parseAtom(depth: i32): i32 {
818
847
  const c: i32 = this.byteAt(this.pos)
819
848
  if (c === 40) {
820
849
  return this.parseGroup(depth)
@@ -861,7 +890,7 @@ class RegexParser {
861
890
  return this.charNode(packed >> 3)
862
891
  }
863
892
 
864
- parseGroup(depth: i32): i32 {
893
+ private parseGroup(depth: i32): i32 {
865
894
  const open: i32 = this.pos
866
895
  this.pos += 1
867
896
  let capture: boolean = true
@@ -929,7 +958,7 @@ class RegexParser {
929
958
  }
930
959
 
931
960
  /** A `\` outside a class. */
932
- parseEscape(): i32 {
961
+ private parseEscape(): i32 {
933
962
  const at: i32 = this.pos
934
963
  this.pos += 1
935
964
  if (this.pos >= this.len) {
@@ -982,7 +1011,7 @@ class RegexParser {
982
1011
  * `\`. Shared by escapes inside and outside a class; inside one, `\b` is a
983
1012
  * backspace and `\-` a hyphen.
984
1013
  */
985
- parseCharEscape(inClass: boolean): i32 {
1014
+ private parseCharEscape(inClass: boolean): i32 {
986
1015
  const at: i32 = this.pos - 1
987
1016
  const c: i32 = this.byteAt(this.pos)
988
1017
  this.pos += 1
@@ -1057,7 +1086,7 @@ class RegexParser {
1057
1086
  }
1058
1087
 
1059
1088
  /** Exactly `n` hex digits at the cursor, or -1 with the cursor left. */
1060
- parseHex(n: i32): i32 {
1089
+ private parseHex(n: i32): i32 {
1061
1090
  if (this.pos + n > this.len) {
1062
1091
  return -1
1063
1092
  }
@@ -1076,7 +1105,7 @@ class RegexParser {
1076
1105
  }
1077
1106
 
1078
1107
  /** `\uHHHH`, a surrogate pair of them, or `\u{H…}`, the cursor after the `u`. */
1079
- parseUnicodeEscape(at: i32): i32 {
1108
+ private parseUnicodeEscape(at: i32): i32 {
1080
1109
  if (this.pos < this.len && this.byteAt(this.pos) === 123) {
1081
1110
  let i: i32 = this.pos + 1
1082
1111
  let v: i32 = 0
@@ -1115,7 +1144,7 @@ class RegexParser {
1115
1144
  return v
1116
1145
  }
1117
1146
 
1118
- addRange(lo: i32, hi: i32): void {
1147
+ private addRange(lo: i32, hi: i32): void {
1119
1148
  if (this.tmpCount < toI32(this.tmpLo.length)) {
1120
1149
  this.tmpLo[this.tmpCount] = lo
1121
1150
  this.tmpHi[this.tmpCount] = hi
@@ -1131,7 +1160,7 @@ class RegexParser {
1131
1160
  * escape's letter — to the class being parsed. A capital adds the complement, the
1132
1161
  * gaps between the set's sorted ranges.
1133
1162
  */
1134
- addSet(letter: i32): void {
1163
+ private addSet(letter: i32): void {
1135
1164
  const negate: boolean = letter === 68 || letter === 87 || letter === 83
1136
1165
  const lower: i32 = negate ? letter + 32 : letter
1137
1166
  let set: i32 = SET_SPACE
@@ -1215,7 +1244,7 @@ class RegexParser {
1215
1244
  * alone, or `General_Category`, `Script` or `Script_Extensions` (or `gc`,
1216
1245
  * `sc`, `scx`) `=` a value, spelled as ECMA-262 spells them.
1217
1246
  */
1218
- addProperty(at: i32, negate: boolean): void {
1247
+ private addProperty(at: i32, negate: boolean): void {
1219
1248
  this.pos += 1
1220
1249
  if (this.pos >= this.len || this.byteAt(this.pos) !== 123) {
1221
1250
  this.fail("invalid property name", at)
@@ -1309,7 +1338,7 @@ class RegexParser {
1309
1338
  }
1310
1339
 
1311
1340
  /** Count `n` more ranges against `REGEX_MAX_PROPERTY_RANGES`, failing at `at` past it. */
1312
- countPropertyRanges(n: i32, at: i32): void {
1341
+ private countPropertyRanges(n: i32, at: i32): void {
1313
1342
  this.propertyRanges += n
1314
1343
  if (this.propertyRanges > REGEX_MAX_PROPERTY_RANGES) {
1315
1344
  this.fail("property escapes and case folding add more than 100000 ranges", at)
@@ -1317,7 +1346,7 @@ class RegexParser {
1317
1346
  }
1318
1347
 
1319
1348
  /** One member of a class: a code point, or -2 once a set escape has added its ranges. */
1320
- parseClassAtom(): i32 {
1349
+ private parseClassAtom(): i32 {
1321
1350
  const c: i32 = this.byteAt(this.pos)
1322
1351
  if (c === 92) {
1323
1352
  const at: i32 = this.pos
@@ -1349,7 +1378,7 @@ class RegexParser {
1349
1378
  }
1350
1379
 
1351
1380
  /** `[…]` or `[^…]`. */
1352
- parseClass(): i32 {
1381
+ private parseClass(): i32 {
1353
1382
  const open: i32 = this.pos
1354
1383
  this.pos += 1
1355
1384
  let negated: boolean = false
@@ -1406,7 +1435,7 @@ class RegexParser {
1406
1435
  }
1407
1436
 
1408
1437
  /** Store the class in `tmpLo` / `tmpHi` and answer its node. */
1409
- finishClass(negated: boolean): i32 {
1438
+ private finishClass(negated: boolean): i32 {
1410
1439
  const k: i32 = toI32(this.clsFrom.length)
1411
1440
  const base: i32 = toI32(this.clsAscii.length)
1412
1441
  let j: i32 = 0
@@ -1463,7 +1492,7 @@ class RegexParser {
1463
1492
  * Append the non-ASCII part of the class being parsed to `clsLo` /
1464
1493
  * `clsHi`, sorted by start and merged, and answer where the merged run ends.
1465
1494
  */
1466
- storeRanges(): i32 {
1495
+ private storeRanges(): i32 {
1467
1496
  const from: i32 = toI32(this.clsLo.length)
1468
1497
  let i: i32 = 0
1469
1498
  while (i < this.tmpCount) {
@@ -1519,7 +1548,7 @@ class RegexParser {
1519
1548
  * scratch copy, so that a class of many ranges, such as a few property
1520
1549
  * escapes in one, costs n log n rather than the n² insertion would.
1521
1550
  */
1522
- mergeSort(from: i32, end: i32): void {
1551
+ private mergeSort(from: i32, end: i32): void {
1523
1552
  const n: i32 = end - from
1524
1553
  let srcLo: i32[] = []
1525
1554
  let srcHi: i32[] = []
@@ -1584,7 +1613,7 @@ class RegexParser {
1584
1613
  * folds to; if they do, the class being parsed becomes them and the
1585
1614
  * missing code points, to be stored again.
1586
1615
  */
1587
- foldRanges(from: i32, out: i32): boolean {
1616
+ private foldRanges(from: i32, out: i32): boolean {
1588
1617
  // A class closed already, holding no folding code point or all of them, leaves the fold table unread.
1589
1618
  let touches: boolean = false
1590
1619
  let covers: boolean = true
@@ -1644,7 +1673,7 @@ class RegexParser {
1644
1673
  }
1645
1674
 
1646
1675
  /** Whether the sorted, merged ranges `[from, out)` hold `c`. */
1647
- rangesHave(from: i32, out: i32, c: i32): boolean {
1676
+ private rangesHave(from: i32, out: i32, c: i32): boolean {
1648
1677
  let lo: i32 = from
1649
1678
  let hi: i32 = out
1650
1679
  while (lo < hi) {
@@ -1928,55 +1957,92 @@ const regexScanFrom = (text: string, masks: u8[], depth: i32, off: i32, width: i
1928
1957
 
1929
1958
  /** One match: the input it was found in and the byte offsets of every group. */
1930
1959
  export class RegexMatch {
1931
- /** The text that was searched. */
1960
+ /** The text that was searched. @internal `Regex.exec` fills it in. */
1932
1961
  input: string
1933
- /** `start, end` of group 0, then of each group in order; -1 for a group that took no part. */
1962
+ /** `start, end` of group 0, then of each group in order; -1 for a group that took no part. @internal `Regex.exec` fills it in. */
1934
1963
  slots: i32[]
1935
- /** Each group's name, "" for an unnamed one. */
1964
+ /** Each group's name, "" for an unnamed one. @internal `Regex.exec` fills it in. */
1936
1965
  names: string[]
1937
1966
 
1967
+ /** An empty match with no groups, for `Regex.exec` to fill in. */
1938
1968
  constructor() {
1939
1969
  this.input = ""
1940
1970
  this.slots = []
1941
1971
  this.names = []
1942
1972
  }
1943
1973
 
1944
- /** Where the match starts. */
1974
+ /**
1975
+ * Where the match starts.
1976
+ *
1977
+ * @returns The byte offset of the match's first byte in the searched text.
1978
+ */
1945
1979
  start(): i32 {
1946
1980
  return this.slots[0]
1947
1981
  }
1948
1982
 
1949
- /** Where the match ends, one past its last byte. */
1983
+ /**
1984
+ * Where the match ends.
1985
+ *
1986
+ * @returns The byte offset one past the match's last byte; equal to `start` for an empty match.
1987
+ */
1950
1988
  end(): i32 {
1951
1989
  return this.slots[1]
1952
1990
  }
1953
1991
 
1954
- /** The matched text. */
1992
+ /**
1993
+ * The matched text.
1994
+ *
1995
+ * @returns The bytes from `start` to `end` of the searched text; "" for an empty match.
1996
+ */
1955
1997
  text(): string {
1956
1998
  return this.group(REGEX_WHOLE)
1957
1999
  }
1958
2000
 
1959
- /** How many groups there are, the whole match (group 0) included. */
2001
+ /**
2002
+ * How many groups the pattern has.
2003
+ *
2004
+ * @returns The count with the whole match (group 0) included, so a pattern with no groups gives 1.
2005
+ */
1960
2006
  groupCount(): i32 {
1961
2007
  return toI32(this.slots.length) / 2
1962
2008
  }
1963
2009
 
1964
- /** Whether group `k` took part in the match. */
2010
+ /**
2011
+ * Whether group `k` took part in the match.
2012
+ *
2013
+ * @param k The group's number, 0 for the whole match.
2014
+ * @returns False for a group that took no part and for a number with no group.
2015
+ */
1965
2016
  matched(k: i32): boolean {
1966
2017
  return k >= 0 && k < this.groupCount() && this.slots[2 * k] >= 0
1967
2018
  }
1968
2019
 
1969
- /** Where group `k` starts, or -1. */
2020
+ /**
2021
+ * Where group `k` starts.
2022
+ *
2023
+ * @param k The group's number, 0 for the whole match.
2024
+ * @returns The byte offset of the group's first byte, or -1 when the group took no part or does not exist.
2025
+ */
1970
2026
  groupStart(k: i32): i32 {
1971
2027
  return this.matched(k) ? this.slots[2 * k] : -1
1972
2028
  }
1973
2029
 
1974
- /** Where group `k` ends, or -1. */
2030
+ /**
2031
+ * Where group `k` ends.
2032
+ *
2033
+ * @param k The group's number, 0 for the whole match.
2034
+ * @returns The byte offset one past the group's last byte, or -1 when the group took no part or does not exist.
2035
+ */
1975
2036
  groupEnd(k: i32): i32 {
1976
2037
  return this.matched(k) ? this.slots[2 * k + 1] : -1
1977
2038
  }
1978
2039
 
1979
- /** Group `k`'s text: "" when it took no part, so test `matched` to tell the two apart. */
2040
+ /**
2041
+ * Group `k`'s text.
2042
+ *
2043
+ * @param k The group's number, 0 for the whole match.
2044
+ * @returns The group's text; "" when it took no part or does not exist, so test `matched` to tell that from an empty capture.
2045
+ */
1980
2046
  group(k: i32): string {
1981
2047
  if (!this.matched(k)) {
1982
2048
  return ""
@@ -1984,7 +2050,12 @@ export class RegexMatch {
1984
2050
  return this.input.slice(this.slots[2 * k], this.slots[2 * k + 1])
1985
2051
  }
1986
2052
 
1987
- /** The text of the group named `name`; "" when there is none or it took no part. */
2053
+ /**
2054
+ * The text of a named group.
2055
+ *
2056
+ * @param name The name the pattern gave the group with `(?<name>...)`.
2057
+ * @returns The group's text; "" when no group has that name or it took no part.
2058
+ */
1988
2059
  named(name: string): string {
1989
2060
  return this.group(toI32(this.names.indexOf(name)))
1990
2061
  }
@@ -2001,167 +2072,175 @@ export class Regex {
2001
2072
  /** The flags as written. */
2002
2073
  readonly flags: string
2003
2074
  /** Each repeat node's mark slot, or -1 before it has one. */
2004
- markOf: i32[]
2005
- names: string[]
2075
+ private markOf: i32[]
2076
+ private names: string[]
2006
2077
 
2007
- op: i32[]
2008
- ia: i32[]
2009
- ib: i32[]
2078
+ private op: i32[]
2079
+ private ia: i32[]
2080
+ private ib: i32[]
2010
2081
 
2011
- clsAscii: u8[]
2012
- clsLo: i32[]
2013
- clsHi: i32[]
2014
- clsFrom: i32[]
2015
- clsTo: i32[]
2016
- clsNeg: u8[]
2082
+ private clsAscii: u8[]
2083
+ private clsLo: i32[]
2084
+ private clsHi: i32[]
2085
+ private clsFrom: i32[]
2086
+ private clsTo: i32[]
2087
+ private clsNeg: u8[]
2017
2088
 
2018
2089
  /** The literal every match starts with, or "". */
2019
- prefix: string
2090
+ private prefix: string
2020
2091
  /** The prefix's rarest byte, which `findPrefix` searches for, at `rareAt`. */
2021
- rareStr: string
2092
+ private rareStr: string
2022
2093
  /** A literal inside every match that the part before it cannot read, or "": see `dfaInner`. */
2023
- innerLit: string
2094
+ private innerLit: string
2024
2095
  /** Which ASCII bytes a match can start with, or empty when any byte can start one. */
2025
- firstSet: u8[]
2096
+ private firstSet: u8[]
2026
2097
  /**
2027
2098
  * The byte sets of a match's first `scanDepth` positions, 32 bytes each in
2028
2099
  * `regexScanFrom`'s layout. Every position but the last is ASCII only, so
2029
2100
  * each is one byte of the text.
2030
2101
  */
2031
- posMasks: u8[]
2102
+ private posMasks: u8[]
2032
2103
 
2033
2104
  // Two lists of threads, each with a capture table indexed by pc. A list
2034
2105
  // holds only the threads that wait for a character or have matched, in
2035
2106
  // priority order; the instructions a closure has passed through are in
2036
2107
  // `visit`, stamped with the closure's generation `gen`.
2037
- cDense: i32[]
2038
- cSlots: i32[]
2039
- nDense: i32[]
2040
- nSlots: i32[]
2041
- visit: i32[]
2108
+ private cDense: i32[]
2109
+ private cSlots: i32[]
2110
+ private nDense: i32[]
2111
+ private nSlots: i32[]
2112
+ private visit: i32[]
2042
2113
  /** The closure's stack: explore `stkA` (kind 0) or restore slot `stkA` to `stkB` (kind 1). */
2043
- stkKind: i32[]
2044
- stkA: i32[]
2045
- stkB: i32[]
2114
+ private stkKind: i32[]
2115
+ private stkA: i32[]
2116
+ private stkB: i32[]
2046
2117
  /** The captures of the thread being followed. */
2047
- caps: i32[]
2118
+ private caps: i32[]
2048
2119
  /** The best match so far. */
2049
- mslots: i32[]
2120
+ private mslots: i32[]
2050
2121
 
2051
2122
  // The lazy DFA, built on first use by `dfaInit`. State `id` lives at
2052
2123
  // offset `id << dShift` of `dTrans`, one transition per byte class; its
2053
2124
  // instructions are `dPool[dStart[id] .. + dLen[id])` and `dHash` finds a
2054
2125
  // state by them, holding `id + 1`.
2055
- dTrans: i32[]
2056
- dPool: i32[]
2057
- dStart: i32[]
2058
- dLen: i32[]
2059
- dFlags: i32[]
2060
- dHash: i32[]
2126
+ private dTrans: i32[]
2127
+ private dPool: i32[]
2128
+ private dStart: i32[]
2129
+ private dLen: i32[]
2130
+ private dFlags: i32[]
2131
+ private dHash: i32[]
2061
2132
  // Scratch for one step: the closure's stamps, leaves and stack, and the
2062
2133
  // next state's instructions.
2063
- dVisit: i32[]
2064
- dLeaf: i32[]
2065
- dBuf: i32[]
2066
- dStk: i32[]
2134
+ private dVisit: i32[]
2135
+ private dLeaf: i32[]
2136
+ private dBuf: i32[]
2137
+ private dStk: i32[]
2067
2138
  /** The class of each ASCII byte; every byte above it maps to the spare column, never filled. */
2068
- dCls: i32[]
2139
+ private dCls: i32[]
2069
2140
  /** The classes of the code points from `dWlo[i]` up to the next: every one above ASCII. */
2070
- dWlo: i32[]
2071
- dWcls: i32[]
2141
+ private dWlo: i32[]
2142
+ private dWcls: i32[]
2072
2143
  /** A code point of each class, or -1 for the end of the text, and its kind. */
2073
- dRep: i32[]
2074
- dNextK: i32[]
2144
+ private dRep: i32[]
2145
+ private dNextK: i32[]
2075
2146
  /** The context a class leaves behind, read forward and in reverse. */
2076
- dCtxF: i32[]
2077
- dCtxR: i32[]
2078
- dMaskF: i32[]
2079
- dMaskR: i32[]
2147
+ private dCtxF: i32[]
2148
+ private dCtxR: i32[]
2149
+ private dMaskF: i32[]
2150
+ private dMaskR: i32[]
2080
2151
  /** Each start state's offset + 1, by mode and context, or 0. */
2081
- dStartOff: i32[]
2152
+ private dStartOff: i32[]
2082
2153
  /** The unanchored start state after the prefix (see `dPrefFast`): its offset + 1, or 0 until it is computed. */
2083
- dPrefOff: i32
2154
+ private dPrefOff: i32
2084
2155
  /** Bytes the inner search read in vain past its literal, and bytes it moved past: see `dfaInner`. */
2085
- innerWaste: f64
2086
- innerGain: f64
2156
+ private innerWaste: f64
2157
+ private innerGain: f64
2087
2158
 
2088
2159
  /** Capture groups, the whole match included. */
2089
2160
  groups: i32
2090
2161
  /** Slots a thread carries: two per group, then one mark per repeat that can iterate empty. */
2091
- width: i32
2162
+ private width: i32
2092
2163
  /**
2093
2164
  * How deep the marked iterations nest, up to `REGEX_MAX_LEVELS`, so a
2094
2165
  * closure tells `1 << levels` sets of them apart; `checkDepth` is the
2095
2166
  * nesting where the compiler is.
2096
2167
  */
2097
- levels: i32
2098
- checkDepth: i32
2099
- cCount: i32
2100
- nCount: i32
2101
- gen: i32
2168
+ private levels: i32
2169
+ private checkDepth: i32
2170
+ private cCount: i32
2171
+ private nCount: i32
2172
+ private gen: i32
2102
2173
  /** The forward program's length; the reverse one follows it, at `revEntry`. */
2103
- fwdCount: i32
2104
- revEntry: i32
2174
+ private fwdCount: i32
2175
+ private revEntry: i32
2105
2176
  /** The reverse of the part before `innerLit`, which the inner search runs back from it. */
2106
- innerEntry: i32
2177
+ private innerEntry: i32
2107
2178
  /** Where `dfaForward` stopped reading, and where `dfaInner`'s match starts. */
2108
- dStop: i32
2109
- dBegin: i32
2179
+ private dStop: i32
2180
+ private dBegin: i32
2110
2181
  /** Where `rareStr` is in `prefix`. */
2111
- rareAt: i32
2112
- scanDepth: i32
2113
- scanWidth: i32
2114
- scanOff: i32
2115
- dShift: i32
2116
- dCount: i32
2117
- dCap: i32
2118
- dMax: i32
2119
- dPoolLen: i32
2120
- dPoolMax: i32
2121
- dGen: i32
2182
+ private rareAt: i32
2183
+ private scanDepth: i32
2184
+ private scanWidth: i32
2185
+ private scanOff: i32
2186
+ private dShift: i32
2187
+ private dCount: i32
2188
+ private dCap: i32
2189
+ private dMax: i32
2190
+ private dPoolLen: i32
2191
+ private dPoolMax: i32
2192
+ private dGen: i32
2122
2193
  /** Bumped by every flush, so a step can tell its row was cleared under it. */
2123
- dEpoch: i32
2124
- dFlushes: i32
2194
+ private dEpoch: i32
2195
+ private dFlushes: i32
2125
2196
  /** Where the last flush happened. */
2126
- dPos: i32
2197
+ private dPos: i32
2127
2198
  /** The class of the end of the text, and how many intervals above ASCII there are. */
2128
- dEot: i32
2129
- dWide: i32
2199
+ private dEot: i32
2200
+ private dWide: i32
2130
2201
 
2131
- hasNames: boolean
2132
- tooBig: boolean
2202
+ private hasNames: boolean
2203
+ private tooBig: boolean
2133
2204
  /** The pattern is `prefix` and nothing else. */
2134
- literal: boolean
2205
+ private literal: boolean
2135
2206
  /** The pattern starts with `^` outside multiline mode. */
2136
- anchored: boolean
2207
+ private anchored: boolean
2137
2208
  /** Whether a byte above ASCII can start a match, when `firstSet` is not empty. */
2138
- firstWide: boolean
2209
+ private firstWide: boolean
2139
2210
  /** The pattern can match the empty string. */
2140
- nullable: boolean
2211
+ private nullable: boolean
2141
2212
  /** The reverse program was compiled. */
2142
- revOk: boolean
2213
+ private revOk: boolean
2143
2214
  /** `dfaInit` has run, and whether the DFA may be used. */
2144
- dReady: boolean
2145
- dOk: boolean
2215
+ private dReady: boolean
2216
+ private dOk: boolean
2146
2217
  /** An idle state skips ahead with the prefilter. */
2147
- dSkip: boolean
2218
+ private dSkip: boolean
2148
2219
  /** The scan's window is rare enough in text to search with. */
2149
- scanSel: boolean
2220
+ private scanSel: boolean
2150
2221
  /** The scan's first positions are exactly the prefix's bytes, all ASCII: see `prefixIsScan`. */
2151
- prefixExact: boolean
2222
+ private prefixExact: boolean
2152
2223
  /**
2153
2224
  * Whether no forward assertion reads the character before it, so every
2154
2225
  * forward state after a character holds the same context and a state with
2155
2226
  * no thread left is the unanchored start state wherever the text resumes.
2156
2227
  */
2157
- dCtxFree: boolean
2228
+ private dCtxFree: boolean
2158
2229
  /**
2159
2230
  * Whether a match can start only where the ASCII `prefix` is and no
2160
2231
  * assertion reads the context, so the unanchored start state after the
2161
2232
  * prefix's bytes is one state, at `dPrefOff`.
2162
2233
  */
2163
- dPrefFast: boolean
2234
+ private dPrefFast: boolean
2164
2235
 
2236
+ /**
2237
+ * @internal Built only by `regexCompile`, which a program calls instead.
2238
+ *
2239
+ * A pattern not compiled yet: `regexCompile` builds its program.
2240
+ *
2241
+ * @param source The pattern as written, kept for `source`.
2242
+ * @param flags The flags as written, kept for `flags`.
2243
+ */
2165
2244
  constructor(source: string, flags: string) {
2166
2245
  this.source = source
2167
2246
  this.flags = flags
@@ -2259,12 +2338,21 @@ export class Regex {
2259
2338
  this.prefixExact = false
2260
2339
  }
2261
2340
 
2262
- /** How many groups there are, the whole match (group 0) included. */
2341
+ /**
2342
+ * How many groups the pattern has.
2343
+ *
2344
+ * @returns The count with the whole match (group 0) included, so a pattern with no groups gives 1.
2345
+ */
2263
2346
  groupCount(): i32 {
2264
2347
  return this.groups
2265
2348
  }
2266
2349
 
2267
- /** The number of the group named `name`, or -1. */
2350
+ /**
2351
+ * The number of a named group.
2352
+ *
2353
+ * @param name The name the pattern gave the group with `(?<name>...)`.
2354
+ * @returns The group's number, for `RegexMatch.group`; -1 when no group has that name or `name` is "".
2355
+ */
2268
2356
  groupIndex(name: string): i32 {
2269
2357
  if (name === "") {
2270
2358
  return -1
@@ -2272,7 +2360,7 @@ export class Regex {
2272
2360
  return toI32(this.names.indexOf(name))
2273
2361
  }
2274
2362
 
2275
- emit(op: i32, a: i32, b: i32): i32 {
2363
+ private emit(op: i32, a: i32, b: i32): i32 {
2276
2364
  const pc: i32 = toI32(this.op.length)
2277
2365
  if (pc >= REGEX_MAX_INSTRUCTIONS) {
2278
2366
  this.tooBig = true
@@ -2284,12 +2372,12 @@ export class Regex {
2284
2372
  return pc
2285
2373
  }
2286
2374
 
2287
- pc(): i32 {
2375
+ private pc(): i32 {
2288
2376
  return toI32(this.op.length)
2289
2377
  }
2290
2378
 
2291
2379
  /** One iteration of a repeat's body: its groups cleared, then the body. */
2292
- compileIteration(p: RegexParser, r: i32, rev: boolean): void {
2380
+ private compileIteration(p: RegexParser, r: i32, rev: boolean): void {
2293
2381
  if (p.ne[r] > p.nd[r]) {
2294
2382
  this.emit(OP_CLEAR, 2 * p.nd[r], 2 * p.ne[r])
2295
2383
  }
@@ -2301,7 +2389,7 @@ export class Regex {
2301
2389
  * end: a concatenation's parts come last first and each assertion trades
2302
2390
  * places with its mirror, `^` with `$`. The reverse DFA runs that program.
2303
2391
  */
2304
- compileNode(p: RegexParser, n: i32, rev: boolean): void {
2392
+ private compileNode(p: RegexParser, n: i32, rev: boolean): void {
2305
2393
  if (this.tooBig) {
2306
2394
  return
2307
2395
  }
@@ -2447,7 +2535,14 @@ export class Regex {
2447
2535
  * body × min; L: SPLIT M, exit; M: MARK k; body; CHECK k; JMP L (unbounded)
2448
2536
  * body × min; (SPLIT M, exit; M: MARK k; body; CHECK k) × (max - min)
2449
2537
  */
2450
- compileEmptyCheckedRepeat(p: RegexParser, n: i32, min: i32, max: i32, greedy: boolean, rev: boolean): void {
2538
+ private compileEmptyCheckedRepeat(
2539
+ p: RegexParser,
2540
+ n: i32,
2541
+ min: i32,
2542
+ max: i32,
2543
+ greedy: boolean,
2544
+ rev: boolean
2545
+ ): void {
2451
2546
  while (toI32(this.markOf.length) <= n) {
2452
2547
  this.markOf.push(REGEX_NONE)
2453
2548
  }
@@ -2492,7 +2587,7 @@ export class Regex {
2492
2587
  * check that it moved on. The mark records how deep it nests, which the VM
2493
2588
  * keys its closure's stamps by.
2494
2589
  */
2495
- compileMarked(p: RegexParser, n: i32, k: i32, rev: boolean): void {
2590
+ private compileMarked(p: RegexParser, n: i32, k: i32, rev: boolean): void {
2496
2591
  const level: i32 = this.checkDepth
2497
2592
  if (level < REGEX_MAX_LEVELS && level >= this.levels) {
2498
2593
  this.levels = level + 1
@@ -2504,7 +2599,7 @@ export class Regex {
2504
2599
  this.emit(OP_CHECK, k, REGEX_NIL)
2505
2600
  }
2506
2601
 
2507
- /** Compile the parsed pattern and size the VM. Answers an error, or "". */
2602
+ /** Compile the parsed pattern and size the VM. Answers an error, or "". @internal `regexCompile` calls it. */
2508
2603
  build(p: RegexParser, root: i32): string {
2509
2604
  this.groups = p.groups
2510
2605
  this.width = 2 * p.groups
@@ -2583,7 +2678,7 @@ export class Regex {
2583
2678
  * run with the rarest first byte, the longest of those, and compiles the
2584
2679
  * parts before it reversed, at `innerEntry`.
2585
2680
  */
2586
- findInner(p: RegexParser, root: i32): void {
2681
+ private findInner(p: RegexParser, root: i32): void {
2587
2682
  if (p.kind[root] !== NODE_CAT) {
2588
2683
  return
2589
2684
  }
@@ -2648,12 +2743,12 @@ export class Regex {
2648
2743
  }
2649
2744
 
2650
2745
  /** Whether node `n` is one ASCII character, matched exactly. */
2651
- isAsciiChar(p: RegexParser, n: i32): boolean {
2746
+ private isAsciiChar(p: RegexParser, n: i32): boolean {
2652
2747
  return p.kind[n] === NODE_CHAR && p.na[n] === p.nb[n] && p.na[n] < 128
2653
2748
  }
2654
2749
 
2655
2750
  /** Whether node `n` can read the ASCII byte `b`. */
2656
- canConsume(p: RegexParser, n: i32, b: i32): boolean {
2751
+ private canConsume(p: RegexParser, n: i32, b: i32): boolean {
2657
2752
  const kind: i32 = p.kind[n]
2658
2753
  if (kind === NODE_CHAR) {
2659
2754
  return p.na[n] === b || p.nb[n] === b
@@ -2680,7 +2775,7 @@ export class Regex {
2680
2775
  }
2681
2776
 
2682
2777
  /** The literal prefix, whether the pattern is only that literal, the anchor, and the first-byte set. */
2683
- findPrefilters(): void {
2778
+ private findPrefilters(): void {
2684
2779
  const bytes: u8[] = []
2685
2780
  let pc: i32 = 0
2686
2781
  while (true) {
@@ -2733,7 +2828,7 @@ export class Regex {
2733
2828
  * a character above ASCII can start, since the next byte of the text may be
2734
2829
  * inside that character.
2735
2830
  */
2736
- findScan(): void {
2831
+ private findScan(): void {
2737
2832
  const count: i32 = this.pc()
2738
2833
  const masks: u8[] = new Array<u8>(32 * REGEX_SCAN_DEPTH)
2739
2834
  const odds: i32[] = new Array<i32>(REGEX_SCAN_DEPTH)
@@ -2932,7 +3027,7 @@ export class Regex {
2932
3027
  * else. The positions after them hold for every match too, so a start the
2933
3028
  * scan finds is then the prefix's next occurrence that can begin a match.
2934
3029
  */
2935
- prefixIsScan(): boolean {
3030
+ private prefixIsScan(): boolean {
2936
3031
  const n: i32 = toI32(this.prefix.length)
2937
3032
  if (n === 0 || n > this.scanDepth) {
2938
3033
  return false
@@ -2958,7 +3053,7 @@ export class Regex {
2958
3053
  }
2959
3054
 
2960
3055
  /** Whether class `k` holds code point `cp`, which is at least 0. */
2961
- classHas(k: i32, cp: i32): boolean {
3056
+ private classHas(k: i32, cp: i32): boolean {
2962
3057
  if (cp < 128) {
2963
3058
  return this.clsAscii[k * 128 + cp] === 1
2964
3059
  }
@@ -2980,7 +3075,7 @@ export class Regex {
2980
3075
  }
2981
3076
 
2982
3077
  /** Whether assertion `kind` holds at byte `p` of `text`. */
2983
- assertHolds(kind: i32, text: string, p: i32, len: i32): boolean {
3078
+ private assertHolds(kind: i32, text: string, p: i32, len: i32): boolean {
2984
3079
  if (kind === ASSERT_BEGIN_TEXT) {
2985
3080
  return p === 0
2986
3081
  }
@@ -3028,7 +3123,7 @@ export class Regex {
3028
3123
  }
3029
3124
 
3030
3125
  /** Start a closure that has visited nothing, wrapping the stamps before they overflow. */
3031
- newGeneration(): void {
3126
+ private newGeneration(): void {
3032
3127
  if (this.gen >= 2147483647) {
3033
3128
  const count: i32 = toI32(this.visit.length)
3034
3129
  let pc: i32 = 0
@@ -3052,7 +3147,16 @@ export class Regex {
3052
3147
  * thread that was here before it, since the check it meets next fails for
3053
3148
  * it alone. `(?:a*?)*` on "aa" goes on to the second `a` that way.
3054
3149
  */
3055
- addThread(dense: i32[], slots: i32[], count0: i32, pc0: i32, text: string, p: i32, len: i32, ns: i32): i32 {
3150
+ private addThread(
3151
+ dense: i32[],
3152
+ slots: i32[],
3153
+ count0: i32,
3154
+ pc0: i32,
3155
+ text: string,
3156
+ p: i32,
3157
+ len: i32,
3158
+ ns: i32
3159
+ ): i32 {
3056
3160
  let count: i32 = count0
3057
3161
  const states: i32 = 1 << this.levels
3058
3162
  this.stkKind[0] = 0
@@ -3153,7 +3257,7 @@ export class Regex {
3153
3257
  * The first match of `prefix` at or after `from`, or -1: a search for its
3154
3258
  * rarest byte, then a comparison of the bytes around it.
3155
3259
  */
3156
- findPrefix(text: string, from: i32): i32 {
3260
+ private findPrefix(text: string, from: i32): i32 {
3157
3261
  if (this.scanSel) {
3158
3262
  return this.findPrefixScan(text, from)
3159
3263
  }
@@ -3188,7 +3292,7 @@ export class Regex {
3188
3292
  * alone. When `prefixExact` the scan's positions are the prefix's bytes and
3189
3293
  * a start it finds is a match.
3190
3294
  */
3191
- findPrefixScan(text: string, from: i32): i32 {
3295
+ private findPrefixScan(text: string, from: i32): i32 {
3192
3296
  const len: i32 = toI32(text.length)
3193
3297
  const n: i32 = toI32(this.prefix.length)
3194
3298
  let p: i32 = from
@@ -3216,7 +3320,7 @@ export class Regex {
3216
3320
  * The first index at or after `from`, which is at least 0, whose bytes pass
3217
3321
  * every position of `posMasks`, or -1. No match starts anywhere it skips.
3218
3322
  */
3219
- scanTo(text: string, from: i32): i32 {
3323
+ private scanTo(text: string, from: i32): i32 {
3220
3324
  if (from > toI32(text.length)) {
3221
3325
  return -1
3222
3326
  }
@@ -3224,7 +3328,7 @@ export class Regex {
3224
3328
  }
3225
3329
 
3226
3330
  /** Where the next match can start at or after `p`, by the prefilter, or -1. */
3227
- dSkipTo(text: string, p: i32): i32 {
3331
+ private dSkipTo(text: string, p: i32): i32 {
3228
3332
  if (this.prefix !== "") {
3229
3333
  return this.findPrefix(text, p)
3230
3334
  }
@@ -3239,7 +3343,7 @@ export class Regex {
3239
3343
  * it does not. Leaves `dOk` false when the split or the table would be too
3240
3344
  * big, and the VM does all the work.
3241
3345
  */
3242
- dfaInit(): void {
3346
+ private dfaInit(): void {
3243
3347
  this.dReady = true
3244
3348
  const total: i32 = this.pc()
3245
3349
  const count: i32 = this.fwdCount
@@ -3523,7 +3627,7 @@ export class Regex {
3523
3627
  * Split each of the `n` classes of `cls` by `hit`, numbering the new
3524
3628
  * classes in order of first appearance. Answers how many there are.
3525
3629
  */
3526
- dRefine(cls: i32[], hit: u8[], units: i32, n: i32, remap: i32[]): i32 {
3630
+ private dRefine(cls: i32[], hit: u8[], units: i32, n: i32, remap: i32[]): i32 {
3527
3631
  let k: i32 = 0
3528
3632
  while (k < 2 * n && k < toI32(remap.length)) {
3529
3633
  remap[k] = -1
@@ -3547,7 +3651,7 @@ export class Regex {
3547
3651
  }
3548
3652
 
3549
3653
  /** The smallest power of two that holds `cap` states at most half full. */
3550
- dHashSize(cap: i32): i32 {
3654
+ private dHashSize(cap: i32): i32 {
3551
3655
  let size: i32 = 1
3552
3656
  while (size < 2 * cap) {
3553
3657
  size *= 2
@@ -3556,7 +3660,7 @@ export class Regex {
3556
3660
  }
3557
3661
 
3558
3662
  /** The class of code point `cp`, which is above ASCII. */
3559
- dWideClass(cp: i32): i32 {
3663
+ private dWideClass(cp: i32): i32 {
3560
3664
  if (this.dWide === 1) {
3561
3665
  return this.dWcls[0]
3562
3666
  }
@@ -3574,7 +3678,7 @@ export class Regex {
3574
3678
  }
3575
3679
 
3576
3680
  /** Whether assertion `kind` holds between a character of kind `ctx` and one of kind `nk`. */
3577
- dAssert(kind: i32, ctx: i32, nk: i32): boolean {
3681
+ private dAssert(kind: i32, ctx: i32, nk: i32): boolean {
3578
3682
  if (kind === ASSERT_BEGIN_TEXT) {
3579
3683
  return ctx === CTX_START
3580
3684
  }
@@ -3597,7 +3701,7 @@ export class Regex {
3597
3701
  * wait for a character or match to `dLeaf` in priority order. Answers the
3598
3702
  * new length.
3599
3703
  */
3600
- dClose(pc0: i32, ctx: i32, nk: i32, n0: i32): i32 {
3704
+ private dClose(pc0: i32, ctx: i32, nk: i32, n0: i32): i32 {
3601
3705
  let n: i32 = n0
3602
3706
  this.dStk[0] = pc0
3603
3707
  let top: i32 = 1
@@ -3643,7 +3747,7 @@ export class Regex {
3643
3747
  * Compute and record the transition from the state at `off` on class `c`,
3644
3748
  * read at byte `p`. Answers the entry as stored, or 0 when the cache gave up.
3645
3749
  */
3646
- dStep(off: i32, c: i32, p: i32): i32 {
3750
+ private dStep(off: i32, c: i32, p: i32): i32 {
3647
3751
  const id: i32 = off >> this.dShift
3648
3752
  const start: i32 = this.dStart[id]
3649
3753
  const len: i32 = this.dLen[id]
@@ -3748,7 +3852,7 @@ export class Regex {
3748
3852
  }
3749
3853
 
3750
3854
  /** The hash of a state's flags and its `n` instructions in `src` from `at`. */
3751
- dHashOf(src: i32[], at: i32, n: i32, flags: i32): i32 {
3855
+ private dHashOf(src: i32[], at: i32, n: i32, flags: i32): i32 {
3752
3856
  let h: i32 = flags
3753
3857
  let i: i32 = 0
3754
3858
  while (i < n) {
@@ -3763,7 +3867,7 @@ export class Regex {
3763
3867
  * `dBuf`, made if it is new. Answers -1 when the cache is full and was
3764
3868
  * cleared too recently, at byte `p`, to clear again.
3765
3869
  */
3766
- dIntern(n: i32, flags: i32, p: i32): i32 {
3870
+ private dIntern(n: i32, flags: i32, p: i32): i32 {
3767
3871
  const mask: i32 = toI32(this.dHash.length) - 1
3768
3872
  const at: i32 = 0
3769
3873
  let slot: i32 = this.dHashOf(this.dBuf, at, n, flags) & mask
@@ -3835,7 +3939,7 @@ export class Regex {
3835
3939
  }
3836
3940
 
3837
3941
  /** Double the state table, up to `dMax` states. */
3838
- dGrow(): void {
3942
+ private dGrow(): void {
3839
3943
  let cap: i32 = 2 * this.dCap
3840
3944
  if (cap > this.dMax) {
3841
3945
  cap = this.dMax
@@ -3885,7 +3989,7 @@ export class Regex {
3885
3989
  * state for nearly every byte and the VM is cheaper. Answers whether it
3886
3990
  * cleared.
3887
3991
  */
3888
- dFlush(p: i32): boolean {
3992
+ private dFlush(p: i32): boolean {
3889
3993
  this.dFlushes += 1
3890
3994
  const moved: i32 = p > this.dPos ? p - this.dPos : this.dPos - p
3891
3995
  if (this.dFlushes > DFA_FREE_FLUSHES && moved < DFA_BYTES_PER_STATE * this.dMax) {
@@ -3910,7 +4014,7 @@ export class Regex {
3910
4014
  }
3911
4015
 
3912
4016
  /** The start state for `mode` after a character of kind `ctx`, or -1. */
3913
- dStartAt(mode: i32, ctx: i32, p: i32): i32 {
4017
+ private dStartAt(mode: i32, ctx: i32, p: i32): i32 {
3914
4018
  const k: i32 = mode * CTX_SLOTS + ctx
3915
4019
  const cached: i32 = this.dStartOff[k]
3916
4020
  if (cached > 0) {
@@ -3940,7 +4044,7 @@ export class Regex {
3940
4044
  * `p`, or -1 when a step on the way is not a plain transition; then
3941
4045
  * `dPrefFast` goes off.
3942
4046
  */
3943
- dAfterPrefix(p: i32): i32 {
4047
+ private dAfterPrefix(p: i32): i32 {
3944
4048
  let off: i32 = this.dStartAt(DFA_UNANCHORED, this.dMaskF[CTX_OTHER], p)
3945
4049
  const n: i32 = toI32(this.prefix.length)
3946
4050
  let k: i32 = 0
@@ -3962,7 +4066,7 @@ export class Regex {
3962
4066
  }
3963
4067
 
3964
4068
  /** The kind of the character before byte `p`, as the forward program tells kinds apart. */
3965
- ctxBefore(text: string, p: i32): i32 {
4069
+ private ctxBefore(text: string, p: i32): i32 {
3966
4070
  let kind: i32 = CTX_OTHER
3967
4071
  if (p <= 0) {
3968
4072
  kind = CTX_START
@@ -3989,7 +4093,7 @@ export class Regex {
3989
4093
  }
3990
4094
 
3991
4095
  /** The kind of the character at byte `p`, as the reverse program tells kinds apart. */
3992
- ctxAt(text: string, p: i32): i32 {
4096
+ private ctxAt(text: string, p: i32): i32 {
3993
4097
  const len: i32 = toI32(text.length)
3994
4098
  let kind: i32 = CTX_OTHER
3995
4099
  if (p >= len) {
@@ -4020,7 +4124,7 @@ export class Regex {
4020
4124
  * `DFA_NONE` or `DFA_GAVE_UP` otherwise, and leaves where it stopped
4021
4125
  * reading in `dStop`.
4022
4126
  */
4023
- dfaForward(text: string, from: i32, first: boolean, pinned: boolean): i32 {
4127
+ private dfaForward(text: string, from: i32, first: boolean, pinned: boolean): i32 {
4024
4128
  const len: i32 = toI32(text.length)
4025
4129
  const mode: i32 = this.anchored || pinned ? DFA_ANCHORED : DFA_UNANCHORED
4026
4130
  let p: i32 = from
@@ -4143,7 +4247,7 @@ export class Regex {
4143
4247
  * `innerLit` does when the literal is at `e`. Answers `DFA_NONE` or
4144
4248
  * `DFA_GAVE_UP` otherwise.
4145
4249
  */
4146
- dfaReverse(text: string, from: i32, e: i32, mode: i32): i32 {
4250
+ private dfaReverse(text: string, from: i32, e: i32, mode: i32): i32 {
4147
4251
  const len: i32 = toI32(text.length)
4148
4252
  let p: i32 = e
4149
4253
  let off: i32 = this.dStartAt(mode, this.ctxAt(text, e), e)
@@ -4245,7 +4349,7 @@ export class Regex {
4245
4349
  * the regex: past twice those and a slack, the inner search is dropped for
4246
4350
  * the plain one, which keeps the cost linear.
4247
4351
  */
4248
- dfaInner(text: string, from: i32, first: boolean): i32 {
4352
+ private dfaInner(text: string, from: i32, first: boolean): i32 {
4249
4353
  let lower: i32 = from
4250
4354
  while (this.innerLit !== "") {
4251
4355
  const i: i32 = toI32(text.indexOf(this.innerLit, lower))
@@ -4285,7 +4389,7 @@ export class Regex {
4285
4389
  * whether there is one and where it ends, the reverse DFA where it starts,
4286
4390
  * and the VM, from that start, the groups.
4287
4391
  */
4288
- run(text: string, from: i32, want: i32): boolean {
4392
+ private run(text: string, from: i32, want: i32): boolean {
4289
4393
  const len: i32 = toI32(text.length)
4290
4394
  if (from < 0 || from > len) {
4291
4395
  return false
@@ -4353,7 +4457,7 @@ export class Regex {
4353
4457
  * Any slots at all bring the marks along, because which match comes first
4354
4458
  * depends on them. `pinned` tries only a match that starts at `from`.
4355
4459
  */
4356
- vm(text: string, from: i32, want: i32, pinned: boolean): boolean {
4460
+ private vm(text: string, from: i32, want: i32, pinned: boolean): boolean {
4357
4461
  const ns: i32 = want > 0 && this.width > 2 * this.groups ? this.width : want
4358
4462
  const len: i32 = toI32(text.length)
4359
4463
  const anchored: boolean = this.anchored || pinned
@@ -4480,7 +4584,12 @@ export class Regex {
4480
4584
  return matched
4481
4585
  }
4482
4586
 
4483
- /** Whether the pattern matches anywhere in `text`. */
4587
+ /**
4588
+ * Whether the pattern matches anywhere in `text`.
4589
+ *
4590
+ * @param text The text to search.
4591
+ * @returns True when there is at least one match.
4592
+ */
4484
4593
  test(text: string): boolean {
4485
4594
  return this.run(text, REGEX_TEXT_START, REGEX_WANT_NONE)
4486
4595
  }
@@ -4489,6 +4598,11 @@ export class Regex {
4489
4598
  * Search `text` from byte `from` and fill `m` with the first match and
4490
4599
  * every group. Answers whether there was one; `m` is untouched when not.
4491
4600
  * Reusing one `RegexMatch` makes a loop of searches allocate nothing.
4601
+ *
4602
+ * @param text The text to search.
4603
+ * @param from The byte offset to start searching at; a match may start there or later.
4604
+ * @param m Where the match goes; overwritten on a match, left as it was otherwise.
4605
+ * @returns True when there was a match.
4492
4606
  */
4493
4607
  exec(text: string, from: i32, m: RegexMatch): boolean {
4494
4608
  const ns: i32 = 2 * this.groups
@@ -4508,7 +4622,13 @@ export class Regex {
4508
4622
  return true
4509
4623
  }
4510
4624
 
4511
- /** The first match at or after byte `from`, or null. */
4625
+ /**
4626
+ * The first match at or after byte `from`.
4627
+ *
4628
+ * @param text The text to search.
4629
+ * @param from The byte offset to start searching at; a match may start there or later.
4630
+ * @returns A new match, or null when there is none.
4631
+ */
4512
4632
  find(text: string, from: i32): RegexMatch | null {
4513
4633
  const m: RegexMatch = new RegexMatch()
4514
4634
  if (this.exec(text, from, m)) {
@@ -4517,7 +4637,13 @@ export class Regex {
4517
4637
  return null
4518
4638
  }
4519
4639
 
4520
- /** The text of every match, left to right, none overlapping. */
4640
+ /**
4641
+ * The text of every match, left to right, none overlapping. After an empty
4642
+ * match the search resumes one character on.
4643
+ *
4644
+ * @param text The text to search.
4645
+ * @returns Each match's text in order; empty when nothing matches.
4646
+ */
4521
4647
  findAll(text: string): string[] {
4522
4648
  const out: string[] = []
4523
4649
  const len: i32 = toI32(text.length)
@@ -4538,7 +4664,12 @@ export class Regex {
4538
4664
  return out
4539
4665
  }
4540
4666
 
4541
- /** How many matches `findAll` would answer. */
4667
+ /**
4668
+ * How many matches `findAll` would answer, without making their strings.
4669
+ *
4670
+ * @param text The text to search.
4671
+ * @returns The number of non-overlapping matches.
4672
+ */
4542
4673
  count(text: string): i32 {
4543
4674
  const len: i32 = toI32(text.length)
4544
4675
  let n: i32 = 0
@@ -4598,17 +4729,27 @@ export class Regex {
4598
4729
  * `String.prototype.replace`'s template: `$$` is `$`, `$&` the match,
4599
4730
  * `` $` `` and `$'` the text before and after it, `$1` to `$99` a group
4600
4731
  * and `$<name>` a named one; a group that took no part is "".
4732
+ *
4733
+ * @param text The text to search.
4734
+ * @param replacement The template each match is replaced by.
4735
+ * @returns The replaced text; `text` unchanged when nothing matches.
4601
4736
  */
4602
4737
  replace(text: string, replacement: string): string {
4603
4738
  return this.replaceMatches(text, replacement, false)
4604
4739
  }
4605
4740
 
4606
- /** `text` with every match replaced by `replacement`, as `replace` reads it. */
4741
+ /**
4742
+ * `text` with every match replaced by `replacement`, as `replace` reads it.
4743
+ *
4744
+ * @param text The text to search.
4745
+ * @param replacement The template each match is replaced by.
4746
+ * @returns The replaced text; `text` unchanged when nothing matches.
4747
+ */
4607
4748
  replaceAll(text: string, replacement: string): string {
4608
4749
  return this.replaceMatches(text, replacement, true)
4609
4750
  }
4610
4751
 
4611
- replaceMatches(text: string, replacement: string, all: boolean): string {
4752
+ private replaceMatches(text: string, replacement: string, all: boolean): string {
4612
4753
  const len: i32 = toI32(text.length)
4613
4754
  // Track every group only when the template can name one.
4614
4755
  let ns: i32 = 2
@@ -4650,7 +4791,7 @@ export class Regex {
4650
4791
  }
4651
4792
 
4652
4793
  /** Group `k` of the match in `mslots`, or "". */
4653
- slotText(text: string, ns: i32, k: i32): string {
4794
+ private slotText(text: string, ns: i32, k: i32): string {
4654
4795
  if (k < 0 || 2 * k + 1 >= ns || this.mslots[2 * k] < 0) {
4655
4796
  return ""
4656
4797
  }
@@ -4658,7 +4799,7 @@ export class Regex {
4658
4799
  }
4659
4800
 
4660
4801
  /** Append the replacement template, expanded for the match in `mslots`, to `parts`. */
4661
- expand(rep: string, text: string, ns: i32, parts: string[]): void {
4802
+ private expand(rep: string, text: string, ns: i32, parts: string[]): void {
4662
4803
  const n: i32 = toI32(rep.length)
4663
4804
  const groupsAfterZero: i32 = this.groups - 1
4664
4805
  let lit: i32 = 0
@@ -4714,6 +4855,9 @@ export class Regex {
4714
4855
  * part), an empty match where the last piece ended or at the end cuts
4715
4856
  * nothing, and "" splits to nothing when the pattern matches it, else to
4716
4857
  * `[""]`.
4858
+ *
4859
+ * @param text The text to cut.
4860
+ * @returns The pieces in order, each group's text after the piece before its match.
4717
4861
  */
4718
4862
  split(text: string): string[] {
4719
4863
  const len: i32 = toI32(text.length)
@@ -4755,6 +4899,10 @@ export class Regex {
4755
4899
  * Compile `pattern` with `flags`, a string of `i`, `m`, `s` and `u` in any
4756
4900
  * order; `u` changes nothing, as patterns are always read by code point.
4757
4901
  * The error names what is wrong and its byte in the pattern.
4902
+ *
4903
+ * @param pattern The pattern, in JavaScript's syntax without the slashes.
4904
+ * @param flags Any of `i` (ignore case), `m` (`^` and `$` match at line ends), `s` (`.` matches a newline) and `u`, each at most once.
4905
+ * @returns The compiled pattern, or the reason it does not compile, such as "regex: missing ) at byte 0" or `regex: invalid flags "x"`.
4758
4906
  */
4759
4907
  export const regexCompile = (pattern: string, flags: string): Result<Regex, string> => {
4760
4908
  let icase: boolean = false
@@ -4794,7 +4942,13 @@ export const regexCompile = (pattern: string, flags: string): Result<Regex, stri
4794
4942
  return Ok(re)
4795
4943
  }
4796
4944
 
4797
- /** `text` with every syntax character escaped, so that it compiles to a pattern matching exactly `text`. */
4945
+ /**
4946
+ * `text` with every syntax character escaped, so that it compiles to a
4947
+ * pattern matching exactly `text`.
4948
+ *
4949
+ * @param text The literal text to match.
4950
+ * @returns A pattern for `regexCompile`; `text` unchanged when it holds no syntax character.
4951
+ */
4798
4952
  export const regexEscape = (text: string): string => {
4799
4953
  const parts: string[] = []
4800
4954
  const n: i32 = toI32(text.length)