@amritk/nish-aarch64-linux 0.44.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +18 -16
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
package/std/text.ts CHANGED
@@ -272,21 +272,25 @@ export const contains = (haystack: string, needle: string): boolean => toI32(hay
272
272
  * does not; the pieces' own `.length`s are still counted in each platform's
273
273
  * units, and NL8001 reads those where the program does.
274
274
  *
275
- * **An empty separator panics.** JavaScript splits a string on `""` into its
276
- * UTF-16 units, which natively would be its bytes, and the two answers differ
277
- * on any text that is not ASCII. A separator is almost always a literal the
278
- * caller chose, so an empty one is a mistake in the program, like an index out
279
- * 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.
280
282
  *
281
283
  * @param text The text to split.
282
- * @param sep The separator, compared byte for byte; it must not be empty.
283
- * Occurrences are found left to right and do not overlap.
284
- * @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.
285
289
  */
286
290
  export const split = (text: string, sep: string): string[] => {
287
291
  const sepLength: i32 = toI32(sep.length)
288
292
  if (sepLength === 0) {
289
- panic("split: an empty separator")
293
+ return splitCodePoints(text, -1)
290
294
  }
291
295
  const length: i32 = toI32(text.length)
292
296
  const parts: string[] = []
@@ -308,7 +312,9 @@ export const split = (text: string, sep: string): string[] => {
308
312
  if (start >= 0 && start < length && at >= 0 && at < length) {
309
313
  parts.push(text.substring(start, at))
310
314
  }
311
- 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))
312
318
  }
313
319
  }
314
320
 
@@ -325,26 +331,26 @@ export const split = (text: string, sep: string): string[] => {
325
331
  * readings agree on every `i32`. Refusing a negative limit would have been a
326
332
  * panic where JavaScript answers.
327
333
  *
328
- * **An empty separator panics**, whatever the limit, for the reason `split`
329
- * gives; JavaScript would answer `[]` for a limit of 0, and its UTF-16 units
330
- * otherwise.
334
+ * **An empty separator splits `text` into its code points**, as `split`'s
335
+ * does, and keeps the first `limit` of them.
331
336
  *
332
337
  * @param text The text to split.
333
- * @param sep The separator, compared byte for byte; it must not be empty.
334
- * 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.
335
341
  * @param limit The most pieces kept; a negative one keeps them all.
336
342
  * @returns A fresh array of at most `limit` pieces, in order.
337
343
  */
338
344
  export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
345
+ if (limit === 0) {
346
+ return []
347
+ }
339
348
  const sepLength: i32 = toI32(sep.length)
340
349
  if (sepLength === 0) {
341
- panic("splitLimit: an empty separator")
350
+ return splitCodePoints(text, limit)
342
351
  }
343
352
  const length: i32 = toI32(text.length)
344
353
  const parts: string[] = []
345
- if (limit === 0) {
346
- return parts
347
- }
348
354
  let start: i32 = 0
349
355
  while (true) {
350
356
  // Searching from `start` rather than cutting `text` down after each match
@@ -366,7 +372,9 @@ export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
366
372
  if (toI32(parts.length) === limit) {
367
373
  return parts
368
374
  }
369
- 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))
370
378
  }
371
379
  }
372
380
 
@@ -395,17 +403,23 @@ const ASCII_MINUS: i32 = 45
395
403
  * the bit the halving dropped, 0 or 1.
396
404
  *
397
405
  * It is written with `>>` and `&` rather than `/` and `%` because under Node
398
- * an `i32` is a JavaScript number, and `/` there would give a fraction. The
399
- * 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.
400
409
  */
401
- const fixedDigitsHalve = (digits: i32[], count: i32): i32 => {
410
+ const fixedDigitsHalve = (digits: u8[], count: i32): i32 => {
402
411
  let carry: i32 = 0
403
- let i: i32 = count - 1
404
- while (i >= 0 && i < toI32(digits.length)) {
405
- const value: i32 = carry * 10 + digits[i]
406
- digits[i] = value >> 1
407
- carry = value & 1
408
- 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
409
423
  }
410
424
  return carry
411
425
  }
@@ -416,18 +430,22 @@ const fixedDigitsHalve = (digits: i32[], count: i32): i32 => {
416
430
  * array grows by a digit when the answer carries out of the top. The name is
417
431
  * long for the reason `isTextBlankByte`'s is.
418
432
  */
419
- const fixedDigitsAdd = (digits: i32[], doubling: boolean, amount: i32): void => {
420
- let carry: i32 = amount
433
+ const fixedDigitsAdd = (digits: u8[], doubling: boolean, amount: i32): void => {
434
+ let carry: i32 = amount & 1
421
435
  let i: i32 = 0
422
436
  const length: i32 = toI32(digits.length)
423
437
  while (i < length) {
424
- const value: i32 = (doubling ? digits[i] * 2 : digits[i]) + carry
425
- 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)
426
444
  carry = value >= 10 ? 1 : 0
427
445
  i += 1
428
446
  }
429
447
  if (carry > 0) {
430
- digits.push(carry)
448
+ digits.push(toU8(carry))
431
449
  }
432
450
  }
433
451
 
@@ -452,7 +470,7 @@ const fixedDigitsAdd = (digits: i32[], doubling: boolean, amount: i32): void =>
452
470
  *
453
471
  * **`digits` outside 0 to 100 panics**, where JavaScript throws a
454
472
  * `RangeError`: the count is almost always a literal the caller chose, so a
455
- * 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.
456
474
  *
457
475
  * @param x The number to write.
458
476
  * @param digits How many digits to write after the point, from 0 to 100; with
@@ -471,8 +489,10 @@ export const toFixed = (x: f64, digits: i32): string => {
471
489
  const negative: boolean = x < 0
472
490
  // `x` is `m * 2^exponent` for an integer `m` below 2^53: scaling it into
473
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.
474
494
  let m: f64 = negative ? -x : x
475
- let exponent: i32 = 0
495
+ let exponent: f64 = 0
476
496
  if (m > 0) {
477
497
  while (m < TWO_TO_52) {
478
498
  m *= 2
@@ -483,19 +503,20 @@ export const toFixed = (x: f64, digits: i32): string => {
483
503
  exponent += 1
484
504
  }
485
505
  }
486
- // The number written is `m * 10^digits * 2^exponent`, rounded to an
487
- // integer, with its last `digits` digits after the point. Multiplying by
488
- // `10^digits` is that many zeros under the units; `m`'s own digits are exact
489
- // to take out, since every step stays an integer below 2^53.
490
- 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[] = []
491
512
  let i: i32 = 0
492
- while (i < digits) {
493
- decimal.push(0)
513
+ while (i < places) {
514
+ decimal.push(toU8(0))
494
515
  i += 1
495
516
  }
496
517
  while (m > 0) {
497
518
  const last: f64 = m % 10
498
- decimal.push(toI32(last))
519
+ decimal.push(toU8(last))
499
520
  m = (m - last) / 10
500
521
  }
501
522
  while (exponent > 0) {
@@ -510,7 +531,7 @@ export const toFixed = (x: f64, digits: i32): string => {
510
531
  let dropped: i32 = 0
511
532
  while (exponent < 0 && count > 0) {
512
533
  dropped = fixedDigitsHalve(decimal, count)
513
- while (count > 0 && count <= toI32(decimal.length) && decimal[count - 1] === 0) {
534
+ while (count > 0 && count <= toI32(decimal.length) && decimal[count - 1] === toU8(0)) {
514
535
  count -= 1
515
536
  }
516
537
  exponent += 1
@@ -518,15 +539,15 @@ export const toFixed = (x: f64, digits: i32): string => {
518
539
  if (exponent === 0 && dropped === 1) {
519
540
  fixedDigitsAdd(decimal, false, 1)
520
541
  }
521
- while (toI32(decimal.length) <= digits) {
522
- decimal.push(0)
542
+ while (toI32(decimal.length) <= places) {
543
+ decimal.push(toU8(0))
523
544
  }
524
545
  // The text is the digits from the highest that is not zero down, but never
525
- // from below the units, which is index `digits`; the halving leaves zeros
526
- // 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
527
548
  // minus in front of any negative `x`.
528
549
  let at: i32 = toI32(decimal.length) - 1
529
- 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)) {
530
551
  at -= 1
531
552
  }
532
553
  const bytes: u8[] = []
@@ -535,8 +556,8 @@ export const toFixed = (x: f64, digits: i32): string => {
535
556
  }
536
557
  while (at >= 0 && at < toI32(decimal.length)) {
537
558
  // Read before the pushes: a push may move an array, so it ends the proof.
538
- const digit: i32 = decimal[at]
539
- if (at === digits - 1) {
559
+ const digit: i32 = toI32(decimal[at])
560
+ if (at + 1 === places) {
540
561
  bytes.push(toU8(ASCII_DOT))
541
562
  }
542
563
  bytes.push(toU8(ASCII_ZERO + digit))
@@ -579,7 +600,9 @@ export const replaceAll = (text: string, needle: string, replacement: string): s
579
600
  return parts.join(replacement)
580
601
  }
581
602
  parts.push(rest.substring(0, at))
582
- 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))
583
606
  }
584
607
  }
585
608
 
@@ -612,49 +635,41 @@ export const firstDifference = (left: string[], right: string[]): i32 => {
612
635
  return leftLength === rightLength ? -1 : i
613
636
  }
614
637
 
615
- /** The most bytes `indexOfAny` takes in its set: what one 16-byte vector of the runtime kernel compares. */
616
- const INDEX_OF_ANY_MAX_SET: i32 = 16
617
-
618
- /** The first byte that is not ASCII; `indexOfAny`'s set must stay below it. */
619
- 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
620
640
 
621
641
  /**
622
642
  * The first index at or after `from` whose byte is one of `bytes`, or -1.
623
643
  *
624
- * `bytes` is a set of 1 to 16 ASCII bytes, and anything else panics: an empty
625
- * set matches nothing, more than 16 is more than one vector of the runtime
626
- * kernel compares, and an ASCII set is what keeps the answer the same under Node,
627
- * where `charCodeAt` reads a UTF-16 unit rather than a byte. An ASCII byte
628
- * never occurs inside a multi-byte UTF-8 sequence, so a non-ASCII `text`
629
- * passes through and only its ASCII bytes can match — as in `splitLines`, the
630
- * index is a byte offset natively. `from` is clamped to `[0, text.length]`,
631
- * as `substring` clamps, so a negative `from` searches from the start and one
632
- * past the end answers -1.
633
- *
634
- * Natively the search is the runtime's `nish_str_index_of_any`, which reads
635
- * 16 or 32 bytes at a time (docs/LANGUAGE.md, "Byte search"). It is
636
- * **variable-time**: how long it takes says where the first match is, so it is
637
- * 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.
638
660
  *
639
661
  * @param text The text searched.
640
- * @param bytes The set of bytes looked for, written as a string of 1 to 16
641
- * 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.
642
664
  * @param from Where the search starts, as a byte offset, clamped into
643
665
  * `[0, text.length]`.
644
666
  * @returns The byte offset of the first match at or after `from`, or -1 when
645
- * there is none.
667
+ * there is none, always so for an empty set.
646
668
  */
647
669
  export const indexOfAny = (text: string, bytes: string, from: i32): i32 => {
648
670
  const count: i32 = toI32(bytes.length)
649
- if (count < 1 || count > INDEX_OF_ANY_MAX_SET) {
650
- panic("indexOfAny: the set must hold 1 to 16 bytes")
651
- }
652
- let j: i32 = 0
653
- while (j < count) {
654
- if (toI32(bytes.charCodeAt(j)) >= NON_ASCII) {
655
- panic("indexOfAny: every byte of the set must be ASCII")
656
- }
657
- j += 1
671
+ if (count === 0) {
672
+ return -1
658
673
  }
659
674
  const length: i32 = toI32(text.length)
660
675
  let start: i32 = from
@@ -664,20 +679,30 @@ export const indexOfAny = (text: string, bytes: string, from: i32): i32 => {
664
679
  if (start > length) {
665
680
  start = length
666
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
+ }
667
686
  return indexOfAnyFrom(text, bytes, start)
668
687
  }
669
688
 
670
689
  /**
671
- * `indexOfAny`'s walk, once its arguments are checked: the first index at or
672
- * 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.
673
691
  *
674
- * This loop is what runs under Node. Natively the compiler recognises the one
675
- * call `indexOfAny` makes to it and calls the runtime kernel instead
676
- * (`src/kernels.ts`), so the loop is the kernel's meaning written in the
677
- * 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
678
696
  * `isTextBlankByte`'s is.
679
697
  */
680
- 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 => {
681
706
  const length: i32 = toI32(text.length)
682
707
  const count: i32 = toI32(bytes.length)
683
708
  let i: i32 = from
@@ -783,6 +808,47 @@ export const codePointLength = (text: string): i32 => {
783
808
  return toI32(count)
784
809
  }
785
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
+
786
852
  /** The first and last lower-case ASCII letters, `a` and `z`. */
787
853
  const ASCII_LOWER_A: i32 = 97
788
854
  const ASCII_LOWER_Z: i32 = 122
@@ -795,22 +861,26 @@ const ASCII_UPPER_Z: i32 = 90
795
861
  const ASCII_CASE_DISTANCE: i32 = 32
796
862
 
797
863
  /**
798
- * `text` with every unit from `low` to `high` moved by `delta`, and every other
799
- * 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.
800
868
  *
801
869
  * Natively the units are bytes, so the answer is built as bytes, one array and
802
870
  * one `String.fromBytes`. Under Node a unit can be wider than a byte, so the
803
871
  * unchanged runs are cut out of `text` whole and only the changed letters are
804
872
  * made one at a time. The name is long for the reason `isTextBlankByte`'s is.
805
873
  */
806
- const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): string => {
874
+ const shiftTextAsciiRange = (text: string, low: i32, high: i32, toUpper: boolean): string => {
807
875
  const length: i32 = toI32(text.length)
808
876
  let i: i32 = 0
809
877
  if (textUnitsAreBytes()) {
810
878
  const bytes: u8[] = []
811
879
  while (i < length) {
812
880
  const code: i32 = toI32(text.charCodeAt(i))
813
- 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))
814
884
  i += 1
815
885
  }
816
886
  return String.fromBytes(bytes, 0, length)
@@ -825,7 +895,7 @@ const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): str
825
895
  if (start >= 0 && start < length) {
826
896
  parts.push(text.substring(start, i))
827
897
  }
828
- parts.push(String.fromCharCode(code + delta))
898
+ parts.push(String.fromCharCode(toUpper ? code - ASCII_CASE_DISTANCE : code + ASCII_CASE_DISTANCE))
829
899
  start = i + 1
830
900
  }
831
901
  i += 1
@@ -852,7 +922,7 @@ const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): str
852
922
  * @returns A fresh string as long as `text`.
853
923
  */
854
924
  export const asciiUpper = (text: string): string =>
855
- shiftTextAsciiRange(text, ASCII_LOWER_A, ASCII_LOWER_Z, -ASCII_CASE_DISTANCE)
925
+ shiftTextAsciiRange(text, ASCII_LOWER_A, ASCII_LOWER_Z, true)
856
926
 
857
927
  /**
858
928
  * `text` with its ASCII letters `A` to `Z` made `a` to `z`, and nothing else
@@ -866,40 +936,50 @@ export const asciiUpper = (text: string): string =>
866
936
  * @returns A fresh string as long as `text`.
867
937
  */
868
938
  export const asciiLower = (text: string): string =>
869
- shiftTextAsciiRange(text, ASCII_UPPER_A, ASCII_UPPER_Z, ASCII_CASE_DISTANCE)
939
+ shiftTextAsciiRange(text, ASCII_UPPER_A, ASCII_UPPER_Z, false)
870
940
 
871
941
  /**
872
942
  * The padding `padStart` and `padEnd` put beside `text`: `fill` repeated and
873
943
  * cut so that `text` and the padding together are `width` units long, or `""`
874
944
  * when `text` is that long already or `fill` is empty.
875
945
  *
876
- * `fill` must be ASCII, and `caller` names the function in the panic when it
877
- * is not. A multi-byte fill cut at a byte width could end in half a UTF-8
878
- * sequence natively, which is text that is not well-formed, and padding is
879
- * done with spaces and zeros. The fill is checked before the width, so a bad
880
- * 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
881
950
  * reason `isTextBlankByte`'s is.
882
951
  */
883
- 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)
884
954
  const fillLength: i32 = toI32(fill.length)
885
- let j: i32 = 0
886
- while (j < fillLength) {
887
- if (toI32(fill.charCodeAt(j)) >= NON_ASCII) {
888
- panic(`${caller}: every byte of the fill must be ASCII`)
889
- }
890
- j += 1
891
- }
892
- const needed: i32 = width - toI32(text.length)
893
- if (needed <= 0 || fillLength === 0) {
955
+ if (width <= textLength || fillLength === 0) {
894
956
  return ""
895
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)
896
963
  const parts: string[] = []
897
- let left: i32 = needed
898
- while (left >= fillLength) {
964
+ while (left >= step) {
899
965
  parts.push(fill)
900
- 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
901
981
  }
902
- parts.push(fill.substring(0, left))
982
+ parts.push(fill.substring(0, cut))
903
983
  return parts.join("")
904
984
  }
905
985
 
@@ -919,18 +999,19 @@ const textPadding = (text: string, width: i32, fill: string, caller: string): st
919
999
  * a string rather than a number. Pad ASCII text, or count the width with
920
1000
  * `codePointLength` yourself, when the answer must be the same on both.
921
1001
  *
922
- * **A fill that is not ASCII panics**, for the reason `indexOfAny`'s set must
923
- * 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.
924
1005
  *
925
1006
  * @param text The text to pad.
926
1007
  * @param width The length the answer is padded to, in `text.length`'s units.
927
- * @param fill The ASCII text repeated as the padding; `" "` and `"0"` are
928
- * the usual ones.
1008
+ * @param fill The text repeated as the padding; `" "` and `"0"` are the
1009
+ * usual ones.
929
1010
  * @returns `text` with the padding in front; `text` unchanged when no
930
1011
  * padding is needed.
931
1012
  */
932
1013
  export const padStart = (text: string, width: i32, fill: string): string =>
933
- textPadding(text, width, fill, "padStart") + text
1014
+ textPadding(text, width, fill) + text
934
1015
 
935
1016
  /**
936
1017
  * `text` with `fill` repeated after it until it is `width` units long:
@@ -940,9 +1021,9 @@ export const padStart = (text: string, width: i32, fill: string): string =>
940
1021
  *
941
1022
  * @param text The text to pad.
942
1023
  * @param width The length the answer is padded to, in `text.length`'s units.
943
- * @param fill The ASCII text repeated as the padding.
1024
+ * @param fill The text repeated as the padding, cut as `padStart` cuts it.
944
1025
  * @returns `text` with the padding after it; `text` unchanged when no
945
1026
  * padding is needed.
946
1027
  */
947
1028
  export const padEnd = (text: string, width: i32, fill: string): string =>
948
- text + textPadding(text, width, fill, "padEnd")
1029
+ text + textPadding(text, width, fill)
package/std/threads.ts CHANGED
@@ -782,6 +782,14 @@ export class AtomicI64 {
782
782
  }
783
783
  }
784
784
 
785
+ /**
786
+ * The panic of an atomic array made with a negative length: the message
787
+ * `new Array` of one prints, so the two refuse it alike.
788
+ */
789
+ const negativeAtomicLength = (): void => {
790
+ panic("array length out of range")
791
+ }
792
+
785
793
  /**
786
794
  * `length` `i32` counters in one contiguous buffer, each of which a scope's
787
795
  * tasks and a parallel body may update together (docs/LANGUAGE.md,
@@ -814,7 +822,13 @@ export class AtomicI32Array {
814
822
  */
815
823
  constructor(length: i32) {
816
824
  this.length = length
817
- this.values = new Array<i32>(length)
825
+ if (length >= 0) {
826
+ this.values = new Array<i32>(length)
827
+ } else {
828
+ negativeAtomicLength()
829
+ // Never reached: `negativeAtomicLength` stops the program.
830
+ this.values = []
831
+ }
818
832
  this.values.fill(0)
819
833
  }
820
834
 
@@ -964,7 +978,13 @@ export class AtomicI64Array {
964
978
  */
965
979
  constructor(length: i32) {
966
980
  this.length = length
967
- this.values = new Array<i64>(length)
981
+ if (length >= 0) {
982
+ this.values = new Array<i64>(length)
983
+ } else {
984
+ negativeAtomicLength()
985
+ // Never reached: `negativeAtomicLength` stops the program.
986
+ this.values = []
987
+ }
968
988
  this.values.fill(0)
969
989
  }
970
990