@amritk/nish-aarch64-linux 0.37.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
package/std/text.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `std/text` — the string operations a program has to write itself.
|
|
3
3
|
*
|
|
4
|
-
* import { replaceAll, splitLines, splitWhitespace, trim } from "nish/text";
|
|
4
|
+
* import { asciiUpper, padStart, replaceAll, split, splitLines, splitWhitespace, trim } from "nish/text";
|
|
5
5
|
*
|
|
6
6
|
* export const main = (): i32 => {
|
|
7
7
|
* const lines: string[] = splitLines("a b\n c \n"); // ["a b", " c "]
|
|
8
8
|
* const words: string[] = splitWhitespace(lines[0]); // ["a", "b"]
|
|
9
9
|
* const c: string = trim(lines[1]); // "c"
|
|
10
10
|
* const dashed: string = replaceAll("a b", " ", "-"); // "a-b"
|
|
11
|
+
* const fields: string[] = split("a,,b", ","); // ["a", "", "b"]
|
|
12
|
+
* const loud: string = asciiUpper("get_id"); // "GET_ID"
|
|
13
|
+
* const code: string = padStart("7", 3, "0"); // "007"
|
|
11
14
|
* return toI32(words.length);
|
|
12
15
|
* };
|
|
13
16
|
*
|
|
@@ -21,10 +24,13 @@
|
|
|
21
24
|
* **So a program that reads a file and wants its lines writes the loop.** This
|
|
22
25
|
* module is that loop, written once. Every offset here is a **byte** offset,
|
|
23
26
|
* like `s.length` itself, and every function but one is ASCII-only where it
|
|
24
|
-
* inspects characters at all — a UTF-8 string passes through `splitLines
|
|
25
|
-
* `
|
|
26
|
-
* multi-byte sequence. The one is
|
|
27
|
-
* rather than bytes and answers the
|
|
27
|
+
* inspects characters at all — a UTF-8 string passes through `splitLines`,
|
|
28
|
+
* `splitWhitespace`, `asciiUpper` and `asciiLower` untouched, because an ASCII
|
|
29
|
+
* byte cannot appear inside a multi-byte sequence. The one is
|
|
30
|
+
* `codePointLength`, which counts code points rather than bytes and answers the
|
|
31
|
+
* same natively and under Node; it says how. `padStart` and `padEnd` count a
|
|
32
|
+
* width in `s.length`'s units, so they are the two whose answers differ under
|
|
33
|
+
* Node on text that is not ASCII; each says by how much.
|
|
28
34
|
*
|
|
29
35
|
* **Every width this module declares is spelled** (`i32`, never `number`) *and*
|
|
30
36
|
* every value a builtin hands it is converted at the read: `s.length`,
|
|
@@ -181,6 +187,295 @@ export const trim = (text: string): string => trimEnd(trimStart(text))
|
|
|
181
187
|
*/
|
|
182
188
|
export const contains = (haystack: string, needle: string): boolean => toI32(haystack.indexOf(needle)) >= 0
|
|
183
189
|
|
|
190
|
+
/**
|
|
191
|
+
* The pieces of `text` between the occurrences of `sep`: JavaScript's
|
|
192
|
+
* `text.split(sep)` for a separator that is not empty. A separator at the
|
|
193
|
+
* start, at the end or next to another gives an empty piece, so
|
|
194
|
+
* `split(",a,,b,", ",")` is `["", "a", "", "b", ""]`; `split("", ",")` is
|
|
195
|
+
* `[""]`; and a separator that never occurs gives `[text]`. Compare
|
|
196
|
+
* `splitWhitespace`, which drops the empty pieces.
|
|
197
|
+
*
|
|
198
|
+
* The pieces are the same natively and under Node for any text and any
|
|
199
|
+
* separator, multi-byte ones included: a match is a whole UTF-8 sequence
|
|
200
|
+
* natively and a whole run of UTF-16 units under Node, and a well-formed
|
|
201
|
+
* string holds a code point's encoding only where that code point is. So
|
|
202
|
+
* there is nothing for NL8001 (`--warn-portability`) to warn about, and it
|
|
203
|
+
* does not; the pieces' own `.length`s are still counted in each platform's
|
|
204
|
+
* units, and NL8001 reads those where the program does.
|
|
205
|
+
*
|
|
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.
|
|
211
|
+
*
|
|
212
|
+
* @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.
|
|
216
|
+
*/
|
|
217
|
+
export const split = (text: string, sep: string): string[] => {
|
|
218
|
+
const sepLength: i32 = toI32(sep.length)
|
|
219
|
+
if (sepLength === 0) {
|
|
220
|
+
panic("split: an empty separator")
|
|
221
|
+
}
|
|
222
|
+
const length: i32 = toI32(text.length)
|
|
223
|
+
const parts: string[] = []
|
|
224
|
+
let start: i32 = 0
|
|
225
|
+
while (true) {
|
|
226
|
+
// Searching from `start` rather than cutting `text` down after each match
|
|
227
|
+
// copies nothing until the piece is taken.
|
|
228
|
+
const at: i32 = toI32(text.indexOf(sep, start))
|
|
229
|
+
// `start` is 0 or one past a match, so it is never negative and never
|
|
230
|
+
// past `length`, and a hit starts at or after `start` and a whole
|
|
231
|
+
// separator before `length`. The tests around the `substring`s therefore
|
|
232
|
+
// always hold, but for the empty tail after a final separator; they are
|
|
233
|
+
// written because they are what proves both bounds within `text`, as in
|
|
234
|
+
// `splitLines`.
|
|
235
|
+
if (at < 0) {
|
|
236
|
+
parts.push(start >= 0 && start < length ? text.substring(start, length) : "")
|
|
237
|
+
return parts
|
|
238
|
+
}
|
|
239
|
+
if (start >= 0 && start < length && at >= 0 && at < length) {
|
|
240
|
+
parts.push(text.substring(start, at))
|
|
241
|
+
}
|
|
242
|
+
start = at + sepLength
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* The first `limit` pieces of `text` between the occurrences of `sep`:
|
|
248
|
+
* JavaScript's `text.split(sep, limit)`. The pieces past the limit are
|
|
249
|
+
* dropped, not joined into the last one, so `splitLimit("a,b,c", ",", 2)` is `["a", "b"]`, and a
|
|
250
|
+
* limit of 0 gives `[]`.
|
|
251
|
+
*
|
|
252
|
+
* **A negative limit keeps every piece**, as it does under Node: JavaScript
|
|
253
|
+
* reads the limit with `ToUint32`, so `-1` is 4294967295, and any negative
|
|
254
|
+
* `i32` is at least 2147483648 there, more pieces than a string can split
|
|
255
|
+
* into. So `splitLimit(text, sep, -1)` is `split(text, sep)`, and the two
|
|
256
|
+
* readings agree on every `i32`. Refusing a negative limit would have been a
|
|
257
|
+
* panic where JavaScript answers.
|
|
258
|
+
*
|
|
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.
|
|
262
|
+
*
|
|
263
|
+
* @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.
|
|
266
|
+
* @param limit The most pieces kept; a negative one keeps them all.
|
|
267
|
+
* @returns A fresh array of at most `limit` pieces, in order.
|
|
268
|
+
*/
|
|
269
|
+
export const splitLimit = (text: string, sep: string, limit: i32): string[] => {
|
|
270
|
+
const sepLength: i32 = toI32(sep.length)
|
|
271
|
+
if (sepLength === 0) {
|
|
272
|
+
panic("splitLimit: an empty separator")
|
|
273
|
+
}
|
|
274
|
+
const length: i32 = toI32(text.length)
|
|
275
|
+
const parts: string[] = []
|
|
276
|
+
if (limit === 0) {
|
|
277
|
+
return parts
|
|
278
|
+
}
|
|
279
|
+
let start: i32 = 0
|
|
280
|
+
while (true) {
|
|
281
|
+
// Searching from `start` rather than cutting `text` down after each match
|
|
282
|
+
// copies nothing until the piece is taken.
|
|
283
|
+
const at: i32 = toI32(text.indexOf(sep, start))
|
|
284
|
+
// `start` is 0 or one past a match, so it is never negative and never
|
|
285
|
+
// past `length`, and a hit starts at or after `start` and a whole
|
|
286
|
+
// separator before `length`. The tests around the `substring`s therefore
|
|
287
|
+
// always hold, but for the empty tail after a final separator; they are
|
|
288
|
+
// written because they are what proves both bounds within `text`, as in
|
|
289
|
+
// `splitLines`.
|
|
290
|
+
if (at < 0) {
|
|
291
|
+
parts.push(start >= 0 && start < length ? text.substring(start, length) : "")
|
|
292
|
+
return parts
|
|
293
|
+
}
|
|
294
|
+
if (start >= 0 && start < length && at >= 0 && at < length) {
|
|
295
|
+
parts.push(text.substring(start, at))
|
|
296
|
+
}
|
|
297
|
+
if (toI32(parts.length) === limit) {
|
|
298
|
+
return parts
|
|
299
|
+
}
|
|
300
|
+
start = at + sepLength
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The most digits `toFixed` takes after the point; JavaScript throws a `RangeError` past it. */
|
|
305
|
+
const TO_FIXED_MAX_DIGITS: i32 = 100
|
|
306
|
+
|
|
307
|
+
/** Where `toFixed` stops writing fixed notation and answers `String(x)`, as JavaScript does. */
|
|
308
|
+
const TO_FIXED_LIMIT: f64 = 1e21
|
|
309
|
+
|
|
310
|
+
/** 2^52 and 2^53: an `f64` from the first up to the second is an integer, and its whole significand. */
|
|
311
|
+
const TWO_TO_52: f64 = 4503599627370496
|
|
312
|
+
const TWO_TO_53: f64 = 9007199254740992
|
|
313
|
+
|
|
314
|
+
/** The byte `0`; a decimal digit `d` is written as this plus `d`. */
|
|
315
|
+
const ASCII_ZERO: i32 = 48
|
|
316
|
+
|
|
317
|
+
/** The byte `.`. */
|
|
318
|
+
const ASCII_DOT: i32 = 46
|
|
319
|
+
|
|
320
|
+
/** The byte `-`. */
|
|
321
|
+
const ASCII_MINUS: i32 = 45
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* `digits`, a decimal number with its units digit first, halved in place,
|
|
325
|
+
* reading only its lowest `count` digits (the rest are zero). The answer is
|
|
326
|
+
* the bit the halving dropped, 0 or 1.
|
|
327
|
+
*
|
|
328
|
+
* 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.
|
|
331
|
+
*/
|
|
332
|
+
const fixedDigitsHalve = (digits: i32[], count: i32): i32 => {
|
|
333
|
+
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
|
|
340
|
+
}
|
|
341
|
+
return carry
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* `digits`, a decimal number with its units digit first, doubled in place when
|
|
346
|
+
* `doubling` is set, and then with `amount`, 0 or 1, added at the units. The
|
|
347
|
+
* array grows by a digit when the answer carries out of the top. The name is
|
|
348
|
+
* long for the reason `isTextBlankByte`'s is.
|
|
349
|
+
*/
|
|
350
|
+
const fixedDigitsAdd = (digits: i32[], doubling: boolean, amount: i32): void => {
|
|
351
|
+
let carry: i32 = amount
|
|
352
|
+
let i: i32 = 0
|
|
353
|
+
const length: i32 = toI32(digits.length)
|
|
354
|
+
while (i < length) {
|
|
355
|
+
const value: i32 = (doubling ? digits[i] * 2 : digits[i]) + carry
|
|
356
|
+
digits[i] = value >= 10 ? value - 10 : value
|
|
357
|
+
carry = value >= 10 ? 1 : 0
|
|
358
|
+
i += 1
|
|
359
|
+
}
|
|
360
|
+
if (carry > 0) {
|
|
361
|
+
digits.push(carry)
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* `x` written with exactly `digits` digits after the point: JavaScript's
|
|
367
|
+
* `x.toFixed(digits)`, and the same text natively and under Node.
|
|
368
|
+
*
|
|
369
|
+
* The answer is the decimal closest to the **exact binary value** of `x`, and
|
|
370
|
+
* a tie between two takes the larger, as ECMA-262 says. That is not rounding
|
|
371
|
+
* the shortest printed decimal: `1.005` is stored as a little less than
|
|
372
|
+
* 1.005, so `toFixed(1.005, 2)` is `"1.00"`, while `2.5` is exact and a tie, so
|
|
373
|
+
* `toFixed(2.5, 0)` is `"3"` and `toFixed(-2.5, 0)` is `"-3"`. The digits are
|
|
374
|
+
* worked out in full, one decimal digit per array element, so the value is
|
|
375
|
+
* never rounded on the way; that is why this is written here and not with a
|
|
376
|
+
* multiplication by a power of ten, which rounds twice.
|
|
377
|
+
*
|
|
378
|
+
* The edges are JavaScript's. `NaN` gives `"NaN"`. `x` at or beyond 1e21 in
|
|
379
|
+
* size gives `String(x)`, the text a template hole gives, so
|
|
380
|
+
* `toFixed(1e21, 2)` is `"1e+21"`, and the infinities give `"Infinity"` and
|
|
381
|
+
* `"-Infinity"`. `-0` loses its sign, `"0.00"`, but a negative value that
|
|
382
|
+
* rounds to zero keeps it: `toFixed(-0.0001, 2)` is `"-0.00"`.
|
|
383
|
+
*
|
|
384
|
+
* **`digits` outside 0 to 100 panics**, where JavaScript throws a
|
|
385
|
+
* `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.
|
|
387
|
+
*
|
|
388
|
+
* @param x The number to write.
|
|
389
|
+
* @param digits How many digits to write after the point, from 0 to 100; with
|
|
390
|
+
* 0 there is no point.
|
|
391
|
+
* @returns `x` in fixed notation, or `String(x)` when `x` is NaN or at least
|
|
392
|
+
* 1e21 in size.
|
|
393
|
+
*/
|
|
394
|
+
export const toFixed = (x: f64, digits: i32): string => {
|
|
395
|
+
if (digits < 0 || digits > TO_FIXED_MAX_DIGITS) {
|
|
396
|
+
panic("toFixed: digits must be 0 to 100")
|
|
397
|
+
}
|
|
398
|
+
// biome-ignore lint/suspicious/noSelfCompare: NaN is the only value that is not itself; Nish has no Number.isNaN
|
|
399
|
+
if (x !== x || x >= TO_FIXED_LIMIT || x <= -TO_FIXED_LIMIT) {
|
|
400
|
+
return `${x}`
|
|
401
|
+
}
|
|
402
|
+
const negative: boolean = x < 0
|
|
403
|
+
// `x` is `m * 2^exponent` for an integer `m` below 2^53: scaling it into
|
|
404
|
+
// [2^52, 2^53) by twos is exact, a subnormal included, and says `exponent`.
|
|
405
|
+
let m: f64 = negative ? -x : x
|
|
406
|
+
let exponent: i32 = 0
|
|
407
|
+
if (m > 0) {
|
|
408
|
+
while (m < TWO_TO_52) {
|
|
409
|
+
m *= 2
|
|
410
|
+
exponent -= 1
|
|
411
|
+
}
|
|
412
|
+
while (m >= TWO_TO_53) {
|
|
413
|
+
m /= 2
|
|
414
|
+
exponent += 1
|
|
415
|
+
}
|
|
416
|
+
}
|
|
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[] = []
|
|
422
|
+
let i: i32 = 0
|
|
423
|
+
while (i < digits) {
|
|
424
|
+
decimal.push(0)
|
|
425
|
+
i += 1
|
|
426
|
+
}
|
|
427
|
+
while (m > 0) {
|
|
428
|
+
const last: f64 = m % 10
|
|
429
|
+
decimal.push(toI32(last))
|
|
430
|
+
m = (m - last) / 10
|
|
431
|
+
}
|
|
432
|
+
while (exponent > 0) {
|
|
433
|
+
fixedDigitsAdd(decimal, true, 0)
|
|
434
|
+
exponent -= 1
|
|
435
|
+
}
|
|
436
|
+
// A negative exponent halves the number that many times. The integer part
|
|
437
|
+
// is what is left; it rounds up exactly when the last bit dropped is 1,
|
|
438
|
+
// which is a remainder of at least a half, a tie included. Once the number
|
|
439
|
+
// is zero every bit still to drop is zero too, so the walk stops there.
|
|
440
|
+
let count: i32 = toI32(decimal.length)
|
|
441
|
+
let dropped: i32 = 0
|
|
442
|
+
while (exponent < 0 && count > 0) {
|
|
443
|
+
dropped = fixedDigitsHalve(decimal, count)
|
|
444
|
+
while (count > 0 && count <= toI32(decimal.length) && decimal[count - 1] === 0) {
|
|
445
|
+
count -= 1
|
|
446
|
+
}
|
|
447
|
+
exponent += 1
|
|
448
|
+
}
|
|
449
|
+
if (exponent === 0 && dropped === 1) {
|
|
450
|
+
fixedDigitsAdd(decimal, false, 1)
|
|
451
|
+
}
|
|
452
|
+
while (toI32(decimal.length) <= digits) {
|
|
453
|
+
decimal.push(0)
|
|
454
|
+
}
|
|
455
|
+
// 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
|
|
458
|
+
// minus in front of any negative `x`.
|
|
459
|
+
let at: i32 = toI32(decimal.length) - 1
|
|
460
|
+
while (at > digits && at >= 0 && at < toI32(decimal.length) && decimal[at] === 0) {
|
|
461
|
+
at -= 1
|
|
462
|
+
}
|
|
463
|
+
const bytes: u8[] = []
|
|
464
|
+
if (negative) {
|
|
465
|
+
bytes.push(toU8(ASCII_MINUS))
|
|
466
|
+
}
|
|
467
|
+
while (at >= 0 && at < toI32(decimal.length)) {
|
|
468
|
+
// 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) {
|
|
471
|
+
bytes.push(toU8(ASCII_DOT))
|
|
472
|
+
}
|
|
473
|
+
bytes.push(toU8(ASCII_ZERO + digit))
|
|
474
|
+
at -= 1
|
|
475
|
+
}
|
|
476
|
+
return String.fromBytes(bytes, 0, toI32(bytes.length))
|
|
477
|
+
}
|
|
478
|
+
|
|
184
479
|
/**
|
|
185
480
|
* `text` with every occurrence of `needle` replaced by `replacement`.
|
|
186
481
|
*
|
|
@@ -418,3 +713,167 @@ export const codePointLength = (text: string): i32 => {
|
|
|
418
713
|
}
|
|
419
714
|
return toI32(count)
|
|
420
715
|
}
|
|
716
|
+
|
|
717
|
+
/** The first and last lower-case ASCII letters, `a` and `z`. */
|
|
718
|
+
const ASCII_LOWER_A: i32 = 97
|
|
719
|
+
const ASCII_LOWER_Z: i32 = 122
|
|
720
|
+
|
|
721
|
+
/** The first and last upper-case ASCII letters, `A` and `Z`. */
|
|
722
|
+
const ASCII_UPPER_A: i32 = 65
|
|
723
|
+
const ASCII_UPPER_Z: i32 = 90
|
|
724
|
+
|
|
725
|
+
/** How far apart an ASCII letter's two cases are: `a` is `A` plus this. */
|
|
726
|
+
const ASCII_CASE_DISTANCE: i32 = 32
|
|
727
|
+
|
|
728
|
+
/**
|
|
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.
|
|
731
|
+
*
|
|
732
|
+
* Natively the units are bytes, so the answer is built as bytes, one array and
|
|
733
|
+
* one `String.fromBytes`. Under Node a unit can be wider than a byte, so the
|
|
734
|
+
* unchanged runs are cut out of `text` whole and only the changed letters are
|
|
735
|
+
* made one at a time. The name is long for the reason `isTextBlankByte`'s is.
|
|
736
|
+
*/
|
|
737
|
+
const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): string => {
|
|
738
|
+
const length: i32 = toI32(text.length)
|
|
739
|
+
let i: i32 = 0
|
|
740
|
+
if (textUnitsAreBytes()) {
|
|
741
|
+
const bytes: u8[] = []
|
|
742
|
+
while (i < length) {
|
|
743
|
+
const code: i32 = toI32(text.charCodeAt(i))
|
|
744
|
+
bytes.push(toU8(code >= low && code <= high ? code + delta : code))
|
|
745
|
+
i += 1
|
|
746
|
+
}
|
|
747
|
+
return String.fromBytes(bytes, 0, length)
|
|
748
|
+
}
|
|
749
|
+
const parts: string[] = []
|
|
750
|
+
let start: i32 = 0
|
|
751
|
+
while (i < length) {
|
|
752
|
+
const code: i32 = toI32(text.charCodeAt(i))
|
|
753
|
+
if (code >= low && code <= high) {
|
|
754
|
+
// `start` never passes `i`, which is below `length`, so this always
|
|
755
|
+
// holds; it is what proves `start` within `text`, as in `splitLines`.
|
|
756
|
+
if (start >= 0 && start < length) {
|
|
757
|
+
parts.push(text.substring(start, i))
|
|
758
|
+
}
|
|
759
|
+
parts.push(String.fromCharCode(code + delta))
|
|
760
|
+
start = i + 1
|
|
761
|
+
}
|
|
762
|
+
i += 1
|
|
763
|
+
}
|
|
764
|
+
if (start < length) {
|
|
765
|
+
parts.push(text.substring(start, length))
|
|
766
|
+
}
|
|
767
|
+
return parts.join("")
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* `text` with its ASCII letters `a` to `z` made `A` to `Z`, and nothing else
|
|
772
|
+
* changed: every other byte, a UTF-8 sequence's included, passes through.
|
|
773
|
+
*
|
|
774
|
+
* This is not JavaScript's `toUpperCase`, which knows Unicode's case table:
|
|
775
|
+
* that gives `"SS"` for `"ß"` and `"É"` for `"é"`, where this gives `"ß"` and
|
|
776
|
+
* `"é"`. That table is more than this runtime has room for, and the text this
|
|
777
|
+
* is for, identifiers, keywords and protocol tokens, is ASCII. On ASCII text
|
|
778
|
+
* the two agree, and this answers the same natively and under Node for any
|
|
779
|
+
* text, since a byte or unit from 97 to 122 is always the letter itself, so
|
|
780
|
+
* NL8001 (`--warn-portability`) has nothing to warn about, and does not.
|
|
781
|
+
*
|
|
782
|
+
* @param text The text whose ASCII letters are upper-cased.
|
|
783
|
+
* @returns A fresh string as long as `text`.
|
|
784
|
+
*/
|
|
785
|
+
export const asciiUpper = (text: string): string =>
|
|
786
|
+
shiftTextAsciiRange(text, ASCII_LOWER_A, ASCII_LOWER_Z, -ASCII_CASE_DISTANCE)
|
|
787
|
+
|
|
788
|
+
/**
|
|
789
|
+
* `text` with its ASCII letters `A` to `Z` made `a` to `z`, and nothing else
|
|
790
|
+
* changed: every other byte, a UTF-8 sequence's included, passes through.
|
|
791
|
+
*
|
|
792
|
+
* This is not JavaScript's `toLowerCase`, for the reason `asciiUpper` gives:
|
|
793
|
+
* `"É"` stays `"É"` here. On ASCII text the two agree, and this answers the
|
|
794
|
+
* same natively and under Node for any text.
|
|
795
|
+
*
|
|
796
|
+
* @param text The text whose ASCII letters are lower-cased.
|
|
797
|
+
* @returns A fresh string as long as `text`.
|
|
798
|
+
*/
|
|
799
|
+
export const asciiLower = (text: string): string =>
|
|
800
|
+
shiftTextAsciiRange(text, ASCII_UPPER_A, ASCII_UPPER_Z, ASCII_CASE_DISTANCE)
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* The padding `padStart` and `padEnd` put beside `text`: `fill` repeated and
|
|
804
|
+
* cut so that `text` and the padding together are `width` units long, or `""`
|
|
805
|
+
* when `text` is that long already or `fill` is empty.
|
|
806
|
+
*
|
|
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
|
|
812
|
+
* reason `isTextBlankByte`'s is.
|
|
813
|
+
*/
|
|
814
|
+
const textPadding = (text: string, width: i32, fill: string, caller: string): string => {
|
|
815
|
+
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) {
|
|
825
|
+
return ""
|
|
826
|
+
}
|
|
827
|
+
const parts: string[] = []
|
|
828
|
+
let left: i32 = needed
|
|
829
|
+
while (left >= fillLength) {
|
|
830
|
+
parts.push(fill)
|
|
831
|
+
left -= fillLength
|
|
832
|
+
}
|
|
833
|
+
parts.push(fill.substring(0, left))
|
|
834
|
+
return parts.join("")
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* `text` with `fill` repeated in front of it until it is `width` units long:
|
|
839
|
+
* JavaScript's `text.padStart(width, fill)` for ASCII text. A `width` no
|
|
840
|
+
* longer than `text`, or an empty `fill`, gives `text` unchanged, and a fill of
|
|
841
|
+
* several characters repeats and is cut at the width, so
|
|
842
|
+
* `padStart("7", 4, "ab")` is `"aba7"`.
|
|
843
|
+
*
|
|
844
|
+
* **`width` counts the units `text.length` counts**: bytes natively and UTF-16
|
|
845
|
+
* units under Node. So on text that is not ASCII the two answers differ:
|
|
846
|
+
* `padStart("é", 3, " ")` is `" é"` natively, where `é` is two bytes, and
|
|
847
|
+
* `" é"` under Node, where it is one unit. NL8001 (`--warn-portability`)
|
|
848
|
+
* does not warn about it: the warning reads offsets in the program it is given,
|
|
849
|
+
* the library's own `text.length` is not walked, and what reaches the caller is
|
|
850
|
+
* a string rather than a number. Pad ASCII text, or count the width with
|
|
851
|
+
* `codePointLength` yourself, when the answer must be the same on both.
|
|
852
|
+
*
|
|
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.
|
|
855
|
+
*
|
|
856
|
+
* @param text The text to pad.
|
|
857
|
+
* @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.
|
|
860
|
+
* @returns `text` with the padding in front; `text` unchanged when no
|
|
861
|
+
* padding is needed.
|
|
862
|
+
*/
|
|
863
|
+
export const padStart = (text: string, width: i32, fill: string): string =>
|
|
864
|
+
textPadding(text, width, fill, "padStart") + text
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* `text` with `fill` repeated after it until it is `width` units long:
|
|
868
|
+
* JavaScript's `text.padEnd(width, fill)` for ASCII text, with `padStart`'s
|
|
869
|
+
* rules. `padEnd("7", 4, "ab")` is `"7aba"`, and `padEnd("é", 3, " ")` is
|
|
870
|
+
* `"é "` natively and `"é "` under Node.
|
|
871
|
+
*
|
|
872
|
+
* @param text The text to pad.
|
|
873
|
+
* @param width The length the answer is padded to, in `text.length`'s units.
|
|
874
|
+
* @param fill The ASCII text repeated as the padding.
|
|
875
|
+
* @returns `text` with the padding after it; `text` unchanged when no
|
|
876
|
+
* padding is needed.
|
|
877
|
+
*/
|
|
878
|
+
export const padEnd = (text: string, width: i32, fill: string): string =>
|
|
879
|
+
text + textPadding(text, width, fill, "padEnd")
|