nish 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.
- package/README.md +11 -5
- package/docs/AI.md +287 -129
- package/package.json +9 -9
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- 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
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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,
|
|
158
|
+
lines.push(text.substring(start, base))
|
|
90
159
|
}
|
|
91
|
-
start =
|
|
160
|
+
start = base + 1
|
|
92
161
|
}
|
|
93
|
-
|
|
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
|
|
207
|
-
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
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;
|
|
214
|
-
* Occurrences are found left to right and do not
|
|
215
|
-
*
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
260
|
-
*
|
|
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;
|
|
265
|
-
* Occurrences are found left to right and do not
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
330
|
-
*
|
|
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:
|
|
410
|
+
const fixedDigitsHalve = (digits: u8[], count: i32): i32 => {
|
|
333
411
|
let carry: i32 = 0
|
|
334
|
-
let i: i32 = count
|
|
335
|
-
while (i
|
|
336
|
-
|
|
337
|
-
digits
|
|
338
|
-
|
|
339
|
-
|
|
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:
|
|
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
|
|
356
|
-
|
|
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
|
|
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:
|
|
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^
|
|
418
|
-
// integer, with its last `
|
|
419
|
-
// `10^
|
|
420
|
-
// to take out, since every step stays an integer below 2^53.
|
|
421
|
-
const
|
|
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 <
|
|
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(
|
|
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) <=
|
|
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 `
|
|
457
|
-
// above the number. The point goes before the last `
|
|
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 >
|
|
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
|
|
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
|
-
|
|
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
|
|
547
|
-
const
|
|
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
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
*
|
|
567
|
-
*
|
|
568
|
-
*
|
|
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
|
|
572
|
-
* ASCII
|
|
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
|
|
581
|
-
|
|
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
|
|
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
|
-
*
|
|
606
|
-
*
|
|
607
|
-
*
|
|
608
|
-
*
|
|
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
|
|
730
|
-
*
|
|
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,
|
|
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
|
-
|
|
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 +
|
|
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,
|
|
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,
|
|
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
|
-
*
|
|
808
|
-
*
|
|
809
|
-
*
|
|
810
|
-
*
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
829
|
-
while (left >= fillLength) {
|
|
964
|
+
while (left >= step) {
|
|
830
965
|
parts.push(fill)
|
|
831
|
-
left -=
|
|
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,
|
|
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
|
-
*
|
|
854
|
-
*
|
|
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
|
|
859
|
-
*
|
|
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
|
|
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
|
|
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
|
|
1029
|
+
text + textPadding(text, width, fill)
|