@amritk/nish-x86_64-linux 0.42.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 (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  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 +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
package/std/text.ts CHANGED
@@ -44,6 +44,8 @@
44
44
  * immutable, so its length cannot change underneath the local.
45
45
  */
46
46
 
47
+ import { bitmask, eq, loadU8x16, splatU8x16 } from "nish:simd"
48
+
47
49
  const NEWLINE: i32 = 10
48
50
  const CARRIAGE_RETURN: i32 = 13
49
51
  const SPACE: i32 = 32
@@ -71,6 +73,13 @@ const isTextBlankByte = (code: i32): boolean =>
71
73
  * function guess at the file's provenance, and a caller that needs it gone can
72
74
  * `trimEnd` each line.
73
75
  *
76
+ * The scan runs sixteen positions at a time with `nish:simd`. The text is
77
+ * copied, 4096 positions at a time, as one flag byte per position into a
78
+ * buffer the lane-wise compare reads sixteen flags at a time: a string has no
79
+ * lane-wise load of its own. A flag rather than the character, so that a UTF-16
80
+ * unit under Node whose low byte is 10 is not taken for a newline. Text under
81
+ * sixteen positions is scanned one position at a time, with no buffer.
82
+ *
74
83
  * @param text The text to split; an empty one has no lines.
75
84
  * @returns A fresh array of the lines, in order.
76
85
  */
@@ -78,21 +87,81 @@ export const splitLines = (text: string): string[] => {
78
87
  const length: i32 = toI32(text.length)
79
88
  const lines: string[] = []
80
89
  let start: i32 = 0
81
- let i: i32 = 0
82
- while (i < length) {
83
- if (toI32(text.charCodeAt(i)) === NEWLINE) {
84
- // `start` is never negative and never passes `i`, which is below
85
- // `length`, so this test always holds. It is written because it is what
86
- // proves `start` within `text`: the clamp on that bound is dead once it
87
- // is proven, and the emitter drops it.
90
+ let base: i32 = 0
91
+ // Text too short for one lane-wise compare takes no buffer and goes straight
92
+ // to the scalar loop below.
93
+ if (length >= 16) {
94
+ // 4096 positions per chunk, spelled as a literal at each use: the provers
95
+ // read a literal's value, not a constant's.
96
+ const flags: u8[] = new Array<u8>(4096)
97
+ const one = splatU8x16(1)
98
+ // `base` is where the chunk starts. Its bound leaves room for `base + 4095`
99
+ // below `2 ** 31`; the scalar loop after this one takes any text past it.
100
+ while (base >= 0 && base < length && base <= 2147479551) {
101
+ let n: i32 = 0
102
+ let end: i32 = base
103
+ while (end >= 0 && end < length && n >= 0 && n < toI32(flags.length)) {
104
+ flags[n] = toU8(toI32(text.charCodeAt(end)) === NEWLINE ? 1 : 0)
105
+ end += 1
106
+ n += 1
107
+ }
108
+ let i: i32 = 0
109
+ while (i >= 0 && i <= 4080 && i <= n - 16) {
110
+ // Each set bit is one newline in these sixteen flags, lowest first. The
111
+ // length test always holds; it is what proves the load's lanes in range,
112
+ // so the load is a proven index site and `--deny-panics` callers build.
113
+ // The masks keep `bits`, the leading-zero count and `lane` in ranges the
114
+ // overflow prover can see.
115
+ let bits: i32 = 0
116
+ if (i <= toI32(flags.length) - 16) {
117
+ bits = bitmask(eq(loadU8x16(flags, i), one)) & 0xffff
118
+ }
119
+ while (bits !== 0) {
120
+ const zeros: i32 = toI32(Math.clz32(bits & (0 - bits))) & 31
121
+ const lane: i32 = (31 - zeros) & 15
122
+ const at: i32 = base + i + lane
123
+ // `start` is never negative and never passes `at`, which is below
124
+ // `length`, so this always holds: it is what proves both bounds
125
+ // within `text` and `at + 1` free of overflow.
126
+ if (start >= 0 && start < length && at >= 0 && at < length) {
127
+ lines.push(text.substring(start, at))
128
+ start = at + 1
129
+ }
130
+ bits = bits & (bits - 1) & 0xffff
131
+ }
132
+ i += 16
133
+ }
134
+ // The chunk's last fewer-than-sixteen flags, one at a time; the tests
135
+ // always hold, as above.
136
+ while (i >= 0 && i < 4096 && i < n && i < toI32(flags.length)) {
137
+ if (flags[i] === toU8(1)) {
138
+ const at: i32 = base + i
139
+ if (start >= 0 && start < length && at >= 0 && at < length) {
140
+ lines.push(text.substring(start, at))
141
+ start = at + 1
142
+ }
143
+ }
144
+ i += 1
145
+ }
146
+ if (n <= 0) {
147
+ break
148
+ }
149
+ base = end
150
+ }
151
+ }
152
+ // Text under sixteen positions, and positions past the bound above, one at a
153
+ // time.
154
+ while (base >= 0 && base < length) {
155
+ if (toI32(text.charCodeAt(base)) === NEWLINE) {
156
+ // Always holds, as above; it proves `start` within `text`.
88
157
  if (start >= 0 && start < length) {
89
- lines.push(text.substring(start, i))
158
+ lines.push(text.substring(start, base))
90
159
  }
91
- start = i + 1
160
+ start = base + 1
92
161
  }
93
- i += 1
162
+ base += 1
94
163
  }
95
- if (start < length) {
164
+ if (start >= 0 && start < length) {
96
165
  lines.push(text.substring(start, length))
97
166
  }
98
167
  return lines
@@ -203,21 +272,25 @@ export const contains = (haystack: string, needle: string): boolean => toI32(hay
203
272
  * does not; the pieces' own `.length`s are still counted in each platform's
204
273
  * units, and NL8001 reads those where the program does.
205
274
  *
206
- * **An empty separator panics.** JavaScript splits a string on `""` into its
207
- * UTF-16 units, which natively would be its bytes, and the two answers differ
208
- * on any text that is not ASCII. A separator is almost always a literal the
209
- * caller chose, so an empty one is a mistake in the program, like an index out
210
- * of range, and not a condition a caller could recover from.
275
+ * **An empty separator splits `text` into its code points**, one piece each,
276
+ * and `split("", "")` is `[]`, as it is in JavaScript. JavaScript splits on
277
+ * `""` into UTF-16 units instead, which natively would be bytes, and a byte of
278
+ * a multi-byte character is not text; a code point is a whole character on
279
+ * both hosts. So the answer is JavaScript's for any text without characters
280
+ * past U+FFFF, and for those, an emoji say, JavaScript's two halves of a
281
+ * surrogate pair are one piece here, natively and under Node alike.
211
282
  *
212
283
  * @param text The text to split.
213
- * @param sep The separator, compared byte for byte; it must not be empty.
214
- * Occurrences are found left to right and do not overlap.
215
- * @returns A fresh array of the pieces, in order; never empty.
284
+ * @param sep The separator, compared byte for byte; an empty one splits
285
+ * between code points. Occurrences are found left to right and do not
286
+ * overlap.
287
+ * @returns A fresh array of the pieces, in order; empty only when both `text`
288
+ * and `sep` are.
216
289
  */
217
290
  export const split = (text: string, sep: string): string[] => {
218
291
  const sepLength: i32 = toI32(sep.length)
219
292
  if (sepLength === 0) {
220
- panic("split: an empty separator")
293
+ return splitCodePoints(text, -1)
221
294
  }
222
295
  const length: i32 = toI32(text.length)
223
296
  const parts: string[] = []
@@ -239,7 +312,9 @@ export const split = (text: string, sep: string): string[] => {
239
312
  if (start >= 0 && start < length && at >= 0 && at < length) {
240
313
  parts.push(text.substring(start, at))
241
314
  }
242
- start = at + sepLength
315
+ // A hit is a whole separator that ends by `length`, so the sum is at
316
+ // most `length`: it neither wraps a `u32` nor leaves an `i32`.
317
+ start = toI32(toU32(at) + toU32(sepLength))
243
318
  }
244
319
  }
245
320
 
@@ -256,26 +331,26 @@ export const split = (text: string, sep: string): string[] => {
256
331
  * readings agree on every `i32`. Refusing a negative limit would have been a
257
332
  * panic where JavaScript answers.
258
333
  *
259
- * **An empty separator panics**, whatever the limit, for the reason `split`
260
- * gives; JavaScript would answer `[]` for a limit of 0, and its UTF-16 units
261
- * otherwise.
334
+ * **An empty separator splits `text` into its code points**, as `split`'s
335
+ * does, and keeps the first `limit` of them.
262
336
  *
263
337
  * @param text The text to split.
264
- * @param sep The separator, compared byte for byte; it must not be empty.
265
- * Occurrences are found left to right and do not overlap.
338
+ * @param sep The separator, compared byte for byte; an empty one splits
339
+ * between code points. Occurrences are found left to right and do not
340
+ * overlap.
266
341
  * @param limit The most pieces kept; a negative one keeps them all.
267
342
  * @returns A fresh array of at most `limit` pieces, in order.
268
343
  */
269
344
  export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
345
+ if (limit === 0) {
346
+ return []
347
+ }
270
348
  const sepLength: i32 = toI32(sep.length)
271
349
  if (sepLength === 0) {
272
- panic("splitLimit: an empty separator")
350
+ return splitCodePoints(text, limit)
273
351
  }
274
352
  const length: i32 = toI32(text.length)
275
353
  const parts: string[] = []
276
- if (limit === 0) {
277
- return parts
278
- }
279
354
  let start: i32 = 0
280
355
  while (true) {
281
356
  // Searching from `start` rather than cutting `text` down after each match
@@ -297,7 +372,9 @@ export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
297
372
  if (toI32(parts.length) === limit) {
298
373
  return parts
299
374
  }
300
- start = at + sepLength
375
+ // A hit is a whole separator that ends by `length`, so the sum is at
376
+ // most `length`: it neither wraps a `u32` nor leaves an `i32`.
377
+ start = toI32(toU32(at) + toU32(sepLength))
301
378
  }
302
379
  }
303
380
 
@@ -326,17 +403,23 @@ const ASCII_MINUS: i32 = 45
326
403
  * the bit the halving dropped, 0 or 1.
327
404
  *
328
405
  * It is written with `>>` and `&` rather than `/` and `%` because under Node
329
- * an `i32` is a JavaScript number, and `/` there would give a fraction. The
330
- * name is long for the reason `isTextBlankByte`'s is.
406
+ * an `i32` is a JavaScript number, and `/` there would give a fraction. A
407
+ * digit is a `u8`, so `carry * 10 + digit` is known to stay small. The name is
408
+ * long for the reason `isTextBlankByte`'s is.
331
409
  */
332
- const fixedDigitsHalve = (digits: i32[], count: i32): i32 => {
410
+ const fixedDigitsHalve = (digits: u8[], count: i32): i32 => {
333
411
  let carry: i32 = 0
334
- let i: i32 = count - 1
335
- while (i >= 0 && i < toI32(digits.length)) {
336
- const value: i32 = carry * 10 + digits[i]
337
- digits[i] = value >> 1
338
- carry = value & 1
339
- i -= 1
412
+ let i: i32 = count
413
+ while (i > 0 && i <= toI32(digits.length)) {
414
+ // `i` is above 0, so the digit below it is at `i - 1`; the test is what
415
+ // proves that index within `digits`, and it always holds.
416
+ const at: i32 = i - 1
417
+ if (at >= 0 && at < toI32(digits.length)) {
418
+ const value: i32 = (carry & 1) * 10 + toI32(digits[at])
419
+ digits[at] = toU8(value >> 1)
420
+ carry = value & 1
421
+ }
422
+ i = at
340
423
  }
341
424
  return carry
342
425
  }
@@ -347,18 +430,22 @@ const fixedDigitsHalve = (digits: i32[], count: i32): i32 => {
347
430
  * array grows by a digit when the answer carries out of the top. The name is
348
431
  * long for the reason `isTextBlankByte`'s is.
349
432
  */
350
- const fixedDigitsAdd = (digits: i32[], doubling: boolean, amount: i32): void => {
351
- let carry: i32 = amount
433
+ const fixedDigitsAdd = (digits: u8[], doubling: boolean, amount: i32): void => {
434
+ let carry: i32 = amount & 1
352
435
  let i: i32 = 0
353
436
  const length: i32 = toI32(digits.length)
354
437
  while (i < length) {
355
- const value: i32 = (doubling ? digits[i] * 2 : digits[i]) + carry
356
- digits[i] = value >= 10 ? value - 10 : value
438
+ const digit: i32 = toI32(digits[i])
439
+ let value: i32 = digit + (carry & 1)
440
+ if (doubling) {
441
+ value += digit
442
+ }
443
+ digits[i] = toU8(value >= 10 ? value - 10 : value)
357
444
  carry = value >= 10 ? 1 : 0
358
445
  i += 1
359
446
  }
360
447
  if (carry > 0) {
361
- digits.push(carry)
448
+ digits.push(toU8(carry))
362
449
  }
363
450
  }
364
451
 
@@ -383,7 +470,7 @@ const fixedDigitsAdd = (digits: i32[], doubling: boolean, amount: i32): void =>
383
470
  *
384
471
  * **`digits` outside 0 to 100 panics**, where JavaScript throws a
385
472
  * `RangeError`: the count is almost always a literal the caller chose, so a
386
- * bad one is a mistake in the program, as `split`'s empty separator is.
473
+ * bad one is a mistake in the program, as an out-of-range index is.
387
474
  *
388
475
  * @param x The number to write.
389
476
  * @param digits How many digits to write after the point, from 0 to 100; with
@@ -402,8 +489,10 @@ export const toFixed = (x: f64, digits: i32): string => {
402
489
  const negative: boolean = x < 0
403
490
  // `x` is `m * 2^exponent` for an integer `m` below 2^53: scaling it into
404
491
  // [2^52, 2^53) by twos is exact, a subnormal included, and says `exponent`.
492
+ // The exponent is an `f64` because the count of steps is only known to be
493
+ // small from the format of `x`, which no bound states; it is exact there.
405
494
  let m: f64 = negative ? -x : x
406
- let exponent: i32 = 0
495
+ let exponent: f64 = 0
407
496
  if (m > 0) {
408
497
  while (m < TWO_TO_52) {
409
498
  m *= 2
@@ -414,19 +503,20 @@ export const toFixed = (x: f64, digits: i32): string => {
414
503
  exponent += 1
415
504
  }
416
505
  }
417
- // The number written is `m * 10^digits * 2^exponent`, rounded to an
418
- // integer, with its last `digits` digits after the point. Multiplying by
419
- // `10^digits` is that many zeros under the units; `m`'s own digits are exact
420
- // to take out, since every step stays an integer below 2^53.
421
- const decimal: i32[] = []
506
+ // The number written is `m * 10^places * 2^exponent`, rounded to an
507
+ // integer, with its last `places` digits after the point. Multiplying by
508
+ // `10^places` is that many zeros under the units; `m`'s own digits are
509
+ // exact to take out, since every step stays an integer below 2^53.
510
+ const places: i32 = digits
511
+ const decimal: u8[] = []
422
512
  let i: i32 = 0
423
- while (i < digits) {
424
- decimal.push(0)
513
+ while (i < places) {
514
+ decimal.push(toU8(0))
425
515
  i += 1
426
516
  }
427
517
  while (m > 0) {
428
518
  const last: f64 = m % 10
429
- decimal.push(toI32(last))
519
+ decimal.push(toU8(last))
430
520
  m = (m - last) / 10
431
521
  }
432
522
  while (exponent > 0) {
@@ -441,7 +531,7 @@ export const toFixed = (x: f64, digits: i32): string => {
441
531
  let dropped: i32 = 0
442
532
  while (exponent < 0 && count > 0) {
443
533
  dropped = fixedDigitsHalve(decimal, count)
444
- while (count > 0 && count <= toI32(decimal.length) && decimal[count - 1] === 0) {
534
+ while (count > 0 && count <= toI32(decimal.length) && decimal[count - 1] === toU8(0)) {
445
535
  count -= 1
446
536
  }
447
537
  exponent += 1
@@ -449,15 +539,15 @@ export const toFixed = (x: f64, digits: i32): string => {
449
539
  if (exponent === 0 && dropped === 1) {
450
540
  fixedDigitsAdd(decimal, false, 1)
451
541
  }
452
- while (toI32(decimal.length) <= digits) {
453
- decimal.push(0)
542
+ while (toI32(decimal.length) <= places) {
543
+ decimal.push(toU8(0))
454
544
  }
455
545
  // The text is the digits from the highest that is not zero down, but never
456
- // from below the units, which is index `digits`; the halving leaves zeros
457
- // above the number. The point goes before the last `digits` of them, and a
546
+ // from below the units, which is index `places`; the halving leaves zeros
547
+ // above the number. The point goes before the last `places` of them, and a
458
548
  // minus in front of any negative `x`.
459
549
  let at: i32 = toI32(decimal.length) - 1
460
- while (at > digits && at >= 0 && at < toI32(decimal.length) && decimal[at] === 0) {
550
+ while (at > places && at >= 0 && at < toI32(decimal.length) && decimal[at] === toU8(0)) {
461
551
  at -= 1
462
552
  }
463
553
  const bytes: u8[] = []
@@ -466,8 +556,8 @@ export const toFixed = (x: f64, digits: i32): string => {
466
556
  }
467
557
  while (at >= 0 && at < toI32(decimal.length)) {
468
558
  // Read before the pushes: a push may move an array, so it ends the proof.
469
- const digit: i32 = decimal[at]
470
- if (at === digits - 1) {
559
+ const digit: i32 = toI32(decimal[at])
560
+ if (at + 1 === places) {
471
561
  bytes.push(toU8(ASCII_DOT))
472
562
  }
473
563
  bytes.push(toU8(ASCII_ZERO + digit))
@@ -510,7 +600,9 @@ export const replaceAll = (text: string, needle: string, replacement: string): s
510
600
  return parts.join(replacement)
511
601
  }
512
602
  parts.push(rest.substring(0, at))
513
- rest = rest.substring(at + needleLength, toI32(rest.length))
603
+ // A hit is a whole `needle` inside `rest`, so the sum is at most
604
+ // `rest.length`: it neither wraps a `u32` nor leaves an `i32`.
605
+ rest = rest.substring(toI32(toU32(at) + toU32(needleLength)), toI32(rest.length))
514
606
  }
515
607
  }
516
608
 
@@ -543,49 +635,41 @@ export const firstDifference = (left: string[], right: string[]): i32 => {
543
635
  return leftLength === rightLength ? -1 : i
544
636
  }
545
637
 
546
- /** The most bytes `indexOfAny` takes in its set: what one 16-byte vector of the runtime kernel compares. */
547
- const INDEX_OF_ANY_MAX_SET: i32 = 16
548
-
549
- /** The first byte that is not ASCII; `indexOfAny`'s set must stay below it. */
550
- const NON_ASCII: i32 = 128
638
+ /** The most bytes the runtime kernel takes in `indexOfAny`'s set: what one 16-byte vector compares. */
639
+ const INDEX_OF_ANY_KERNEL_SET: i32 = 16
551
640
 
552
641
  /**
553
642
  * The first index at or after `from` whose byte is one of `bytes`, or -1.
554
643
  *
555
- * `bytes` is a set of 1 to 16 ASCII bytes, and anything else panics: an empty
556
- * set matches nothing, more than 16 is more than one vector of the runtime
557
- * kernel compares, and an ASCII set is what keeps the answer the same under Node,
558
- * where `charCodeAt` reads a UTF-16 unit rather than a byte. An ASCII byte
559
- * never occurs inside a multi-byte UTF-8 sequence, so a non-ASCII `text`
560
- * passes through and only its ASCII bytes can match — as in `splitLines`, the
561
- * index is a byte offset natively. `from` is clamped to `[0, text.length]`,
562
- * as `substring` clamps, so a negative `from` searches from the start and one
563
- * past the end answers -1.
564
- *
565
- * Natively the search is the runtime's `nish_str_index_of_any`, which reads
566
- * 16 or 32 bytes at a time (docs/LANGUAGE.md, "Byte search"). It is
567
- * **variable-time**: how long it takes says where the first match is, so it is
568
- * never for secret data.
644
+ * The set is read unit by unit, as `text` is. An ASCII byte never occurs
645
+ * inside a multi-byte UTF-8 sequence, so with an ASCII set a non-ASCII `text`
646
+ * passes through, only its ASCII bytes can match, and the answer is the same
647
+ * natively and under Node but for the index itself, a byte offset natively,
648
+ * as in `splitLines`. A non-ASCII character in the set is not one member:
649
+ * natively each of its bytes is, so `"é"` also finds the first byte of `"è"`,
650
+ * while under Node its UTF-16 unit is; keep the set ASCII for one answer on
651
+ * both. An empty set matches nothing. `from` is clamped to
652
+ * `[0, text.length]`, as `substring` clamps, so a negative `from` searches
653
+ * from the start and one past the end answers -1.
654
+ *
655
+ * Natively a set of 1 to 16 bytes is searched by the runtime's
656
+ * `nish_str_index_of_any`, which reads 16 or 32 bytes at a time
657
+ * (docs/LANGUAGE.md, "Byte search"); a larger set is a plain loop that tests
658
+ * each byte against each member. It is **variable-time**: how long it takes
659
+ * says where the first match is, so it is never for secret data.
569
660
  *
570
661
  * @param text The text searched.
571
- * @param bytes The set of bytes looked for, written as a string of 1 to 16
572
- * ASCII characters; their order does not matter.
662
+ * @param bytes The set of bytes looked for, written as a string; their order
663
+ * does not matter, and ASCII ones give the same answer on every host.
573
664
  * @param from Where the search starts, as a byte offset, clamped into
574
665
  * `[0, text.length]`.
575
666
  * @returns The byte offset of the first match at or after `from`, or -1 when
576
- * there is none.
667
+ * there is none, always so for an empty set.
577
668
  */
578
669
  export const indexOfAny = (text: string, bytes: string, from: i32): i32 => {
579
670
  const count: i32 = toI32(bytes.length)
580
- if (count < 1 || count > INDEX_OF_ANY_MAX_SET) {
581
- panic("indexOfAny: the set must hold 1 to 16 bytes")
582
- }
583
- let j: i32 = 0
584
- while (j < count) {
585
- if (toI32(bytes.charCodeAt(j)) >= NON_ASCII) {
586
- panic("indexOfAny: every byte of the set must be ASCII")
587
- }
588
- j += 1
671
+ if (count === 0) {
672
+ return -1
589
673
  }
590
674
  const length: i32 = toI32(text.length)
591
675
  let start: i32 = from
@@ -595,20 +679,30 @@ export const indexOfAny = (text: string, bytes: string, from: i32): i32 => {
595
679
  if (start > length) {
596
680
  start = length
597
681
  }
682
+ // The kernel's contract is a set of 1 to 16 bytes, one vector of them.
683
+ if (count > INDEX_OF_ANY_KERNEL_SET) {
684
+ return indexOfAnyScan(text, bytes, start)
685
+ }
598
686
  return indexOfAnyFrom(text, bytes, start)
599
687
  }
600
688
 
601
689
  /**
602
- * `indexOfAny`'s walk, once its arguments are checked: the first index at or
603
- * after `from` whose byte is in `bytes`, or -1.
690
+ * `indexOfAny`'s walk for a set of 1 to 16 bytes, once `from` is clamped.
604
691
  *
605
- * This loop is what runs under Node. Natively the compiler recognises the one
606
- * call `indexOfAny` makes to it and calls the runtime kernel instead
607
- * (`src/kernels.ts`), so the loop is the kernel's meaning written in the
608
- * language rather than its implementation. The name is long for the reason
692
+ * Natively the compiler recognises the one call `indexOfAny` makes to it and
693
+ * calls the runtime kernel instead (`src/kernels.ts`), so what it calls is the
694
+ * kernel's meaning written in the language rather than its implementation;
695
+ * under Node that walk runs. The name is long for the reason
609
696
  * `isTextBlankByte`'s is.
610
697
  */
611
- const indexOfAnyFrom = (text: string, bytes: string, from: i32): i32 => {
698
+ const indexOfAnyFrom = (text: string, bytes: string, from: i32): i32 => indexOfAnyScan(text, bytes, from)
699
+
700
+ /**
701
+ * The first index at or after `from`, which is never negative, whose unit is
702
+ * one of `bytes`'s, or -1: `indexOfAny`'s search written as a loop. The name
703
+ * is long for the reason `isTextBlankByte`'s is.
704
+ */
705
+ const indexOfAnyScan = (text: string, bytes: string, from: i32): i32 => {
612
706
  const length: i32 = toI32(text.length)
613
707
  const count: i32 = toI32(bytes.length)
614
708
  let i: i32 = from
@@ -714,6 +808,47 @@ export const codePointLength = (text: string): i32 => {
714
808
  return toI32(count)
715
809
  }
716
810
 
811
+ /**
812
+ * `text`'s code points as one-code-point strings, the first `limit` of them,
813
+ * or all of them for a negative `limit`: `split` and `splitLimit` on an empty
814
+ * separator. A piece starts at each unit `codePointLength` counts, so the two
815
+ * agree on how many pieces there are, natively and under Node. The name is
816
+ * long for the reason `isTextBlankByte`'s is.
817
+ */
818
+ const splitCodePoints = (text: string, limit: i32): string[] => {
819
+ const length: i32 = toI32(text.length)
820
+ const bytes: boolean = textUnitsAreBytes()
821
+ const parts: string[] = []
822
+ let start: i32 = 0
823
+ let afterHigh: boolean = false
824
+ let i: i32 = 0
825
+ while (i < length) {
826
+ const unit: i32 = toI32(text.charCodeAt(i))
827
+ const starts: boolean = bytes
828
+ ? (unit & UTF8_TOP_BITS) !== UTF8_CONTINUATION
829
+ : !(afterHigh && unit >= LOW_SURROGATE && unit < SURROGATE_END)
830
+ afterHigh = unit >= HIGH_SURROGATE && unit < LOW_SURROGATE
831
+ // The unit at 0 starts the first piece, which has nothing before it to
832
+ // take; every later start closes the piece before it.
833
+ if (starts && i > 0) {
834
+ // `start` is an earlier `i`, so this always holds; it is what proves
835
+ // `start` within `text`, as in `splitLines`.
836
+ if (start >= 0 && start < length) {
837
+ parts.push(text.substring(start, i))
838
+ }
839
+ if (toI32(parts.length) === limit) {
840
+ return parts
841
+ }
842
+ start = i
843
+ }
844
+ i += 1
845
+ }
846
+ if (length > 0) {
847
+ parts.push(text.substring(start, length))
848
+ }
849
+ return parts
850
+ }
851
+
717
852
  /** The first and last lower-case ASCII letters, `a` and `z`. */
718
853
  const ASCII_LOWER_A: i32 = 97
719
854
  const ASCII_LOWER_Z: i32 = 122
@@ -726,22 +861,26 @@ const ASCII_UPPER_Z: i32 = 90
726
861
  const ASCII_CASE_DISTANCE: i32 = 32
727
862
 
728
863
  /**
729
- * `text` with every unit from `low` to `high` moved by `delta`, and every other
730
- * unit as it was: `asciiUpper` and `asciiLower`'s one walk.
864
+ * `text` with every unit from `low` to `high` moved up or down the ASCII table
865
+ * by the distance between the cases, down when `toUpper` is set, and every
866
+ * other unit as it was: `asciiUpper` and `asciiLower`'s one walk. The callers'
867
+ * ranges are letters, so a moved unit is a letter of the other case.
731
868
  *
732
869
  * Natively the units are bytes, so the answer is built as bytes, one array and
733
870
  * one `String.fromBytes`. Under Node a unit can be wider than a byte, so the
734
871
  * unchanged runs are cut out of `text` whole and only the changed letters are
735
872
  * made one at a time. The name is long for the reason `isTextBlankByte`'s is.
736
873
  */
737
- const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): string => {
874
+ const shiftTextAsciiRange = (text: string, low: i32, high: i32, toUpper: boolean): string => {
738
875
  const length: i32 = toI32(text.length)
739
876
  let i: i32 = 0
740
877
  if (textUnitsAreBytes()) {
741
878
  const bytes: u8[] = []
742
879
  while (i < length) {
743
880
  const code: i32 = toI32(text.charCodeAt(i))
744
- bytes.push(toU8(code >= low && code <= high ? code + delta : code))
881
+ // A unit is a byte here, so either move stays well inside an `i32`.
882
+ const moved: i32 = toUpper ? code - ASCII_CASE_DISTANCE : code + ASCII_CASE_DISTANCE
883
+ bytes.push(toU8(code >= low && code <= high ? moved : code))
745
884
  i += 1
746
885
  }
747
886
  return String.fromBytes(bytes, 0, length)
@@ -756,7 +895,7 @@ const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): str
756
895
  if (start >= 0 && start < length) {
757
896
  parts.push(text.substring(start, i))
758
897
  }
759
- parts.push(String.fromCharCode(code + delta))
898
+ parts.push(String.fromCharCode(toUpper ? code - ASCII_CASE_DISTANCE : code + ASCII_CASE_DISTANCE))
760
899
  start = i + 1
761
900
  }
762
901
  i += 1
@@ -783,7 +922,7 @@ const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): str
783
922
  * @returns A fresh string as long as `text`.
784
923
  */
785
924
  export const asciiUpper = (text: string): string =>
786
- shiftTextAsciiRange(text, ASCII_LOWER_A, ASCII_LOWER_Z, -ASCII_CASE_DISTANCE)
925
+ shiftTextAsciiRange(text, ASCII_LOWER_A, ASCII_LOWER_Z, true)
787
926
 
788
927
  /**
789
928
  * `text` with its ASCII letters `A` to `Z` made `a` to `z`, and nothing else
@@ -797,40 +936,50 @@ export const asciiUpper = (text: string): string =>
797
936
  * @returns A fresh string as long as `text`.
798
937
  */
799
938
  export const asciiLower = (text: string): string =>
800
- shiftTextAsciiRange(text, ASCII_UPPER_A, ASCII_UPPER_Z, ASCII_CASE_DISTANCE)
939
+ shiftTextAsciiRange(text, ASCII_UPPER_A, ASCII_UPPER_Z, false)
801
940
 
802
941
  /**
803
942
  * The padding `padStart` and `padEnd` put beside `text`: `fill` repeated and
804
943
  * cut so that `text` and the padding together are `width` units long, or `""`
805
944
  * when `text` is that long already or `fill` is empty.
806
945
  *
807
- * `fill` must be ASCII, and `caller` names the function in the panic when it
808
- * is not. A multi-byte fill cut at a byte width could end in half a UTF-8
809
- * sequence natively, which is text that is not well-formed, and padding is
810
- * done with spaces and zeros. The fill is checked before the width, so a bad
811
- * fill panics whether or not any padding was needed. The name is long for the
946
+ * The last copy of `fill` is cut only where a code point starts, so a
947
+ * non-ASCII fill never leaves half a UTF-8 sequence natively or half a
948
+ * surrogate pair under Node; the padding is then a unit or more short of the
949
+ * width rather than text that is not well-formed. The name is long for the
812
950
  * reason `isTextBlankByte`'s is.
813
951
  */
814
- const textPadding = (text: string, width: i32, fill: string, caller: string): string => {
952
+ const textPadding = (text: string, width: i32, fill: string): string => {
953
+ const textLength: i32 = toI32(text.length)
815
954
  const fillLength: i32 = toI32(fill.length)
816
- let j: i32 = 0
817
- while (j < fillLength) {
818
- if (toI32(fill.charCodeAt(j)) >= NON_ASCII) {
819
- panic(`${caller}: every byte of the fill must be ASCII`)
820
- }
821
- j += 1
822
- }
823
- const needed: i32 = width - toI32(text.length)
824
- if (needed <= 0 || fillLength === 0) {
955
+ if (width <= textLength || fillLength === 0) {
825
956
  return ""
826
957
  }
958
+ // `width` is above `textLength`, which is never negative, so the gap
959
+ // neither wraps as a `u32` nor fits less well than `width` did. `left` stays
960
+ // a `u32` because it only counts down while it is at least `fillLength`.
961
+ let left: u32 = toU32(width) - toU32(textLength)
962
+ const step: u32 = toU32(fillLength)
827
963
  const parts: string[] = []
828
- let left: i32 = needed
829
- while (left >= fillLength) {
964
+ while (left >= step) {
830
965
  parts.push(fill)
831
- left -= fillLength
966
+ left -= step
967
+ }
968
+ // `left` is below `fillLength` here, so it is an index into `fill`.
969
+ let cut: i32 = toI32(left)
970
+ const bytes: boolean = textUnitsAreBytes()
971
+ while (cut > 0 && cut < fillLength) {
972
+ const unit: i32 = toI32(fill.charCodeAt(cut))
973
+ const before: i32 = toI32(fill.charCodeAt(cut - 1))
974
+ const inside: boolean = bytes
975
+ ? (unit & UTF8_TOP_BITS) === UTF8_CONTINUATION
976
+ : unit >= LOW_SURROGATE && unit < SURROGATE_END && before >= HIGH_SURROGATE && before < LOW_SURROGATE
977
+ if (!inside) {
978
+ break
979
+ }
980
+ cut -= 1
832
981
  }
833
- parts.push(fill.substring(0, left))
982
+ parts.push(fill.substring(0, cut))
834
983
  return parts.join("")
835
984
  }
836
985
 
@@ -850,18 +999,19 @@ const textPadding = (text: string, width: i32, fill: string, caller: string): st
850
999
  * a string rather than a number. Pad ASCII text, or count the width with
851
1000
  * `codePointLength` yourself, when the answer must be the same on both.
852
1001
  *
853
- * **A fill that is not ASCII panics**, for the reason `indexOfAny`'s set must
854
- * be ASCII: natively it would be cut in the middle of a UTF-8 sequence.
1002
+ * A fill that is not ASCII is cut only where a code point starts, so its
1003
+ * last copy may fall a unit or more short: `padStart("7", 2, "é")` is `"7"`
1004
+ * natively, where `é` is two bytes and one does not fit, and `"é7"` under Node.
855
1005
  *
856
1006
  * @param text The text to pad.
857
1007
  * @param width The length the answer is padded to, in `text.length`'s units.
858
- * @param fill The ASCII text repeated as the padding; `" "` and `"0"` are
859
- * the usual ones.
1008
+ * @param fill The text repeated as the padding; `" "` and `"0"` are the
1009
+ * usual ones.
860
1010
  * @returns `text` with the padding in front; `text` unchanged when no
861
1011
  * padding is needed.
862
1012
  */
863
1013
  export const padStart = (text: string, width: i32, fill: string): string =>
864
- textPadding(text, width, fill, "padStart") + text
1014
+ textPadding(text, width, fill) + text
865
1015
 
866
1016
  /**
867
1017
  * `text` with `fill` repeated after it until it is `width` units long:
@@ -871,9 +1021,9 @@ export const padStart = (text: string, width: i32, fill: string): string =>
871
1021
  *
872
1022
  * @param text The text to pad.
873
1023
  * @param width The length the answer is padded to, in `text.length`'s units.
874
- * @param fill The ASCII text repeated as the padding.
1024
+ * @param fill The text repeated as the padding, cut as `padStart` cuts it.
875
1025
  * @returns `text` with the padding after it; `text` unchanged when no
876
1026
  * padding is needed.
877
1027
  */
878
1028
  export const padEnd = (text: string, width: i32, fill: string): string =>
879
- text + textPadding(text, width, fill, "padEnd")
1029
+ text + textPadding(text, width, fill)