@amritk/nish-x86_64-linux 0.37.0 → 0.38.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 +32 -10
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/nish.d.ts +344 -48
- package/runtime/nish.h +29 -7
- package/runtime/runtime-async.c +228 -45
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-region.c +85 -7
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +3 -3
- package/runtime/runtime.c +50 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +86 -28
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +2 -0
- package/std/array.ts +266 -0
- package/std/host/text.ts +86 -0
- 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 +12 -0
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +18 -4
- package/std/net/http1-server.ts +13 -3
- package/std/net/postgres-async.ts +1005 -0
- package/std/net/postgres.ts +7 -4
- package/std/net/serve.ts +763 -0
- package/std/text.ts +231 -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,62 @@ 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
|
+
|
|
184
246
|
/**
|
|
185
247
|
* `text` with every occurrence of `needle` replaced by `replacement`.
|
|
186
248
|
*
|
|
@@ -418,3 +480,167 @@ export const codePointLength = (text: string): i32 => {
|
|
|
418
480
|
}
|
|
419
481
|
return toI32(count)
|
|
420
482
|
}
|
|
483
|
+
|
|
484
|
+
/** The first and last lower-case ASCII letters, `a` and `z`. */
|
|
485
|
+
const ASCII_LOWER_A: i32 = 97
|
|
486
|
+
const ASCII_LOWER_Z: i32 = 122
|
|
487
|
+
|
|
488
|
+
/** The first and last upper-case ASCII letters, `A` and `Z`. */
|
|
489
|
+
const ASCII_UPPER_A: i32 = 65
|
|
490
|
+
const ASCII_UPPER_Z: i32 = 90
|
|
491
|
+
|
|
492
|
+
/** How far apart an ASCII letter's two cases are: `a` is `A` plus this. */
|
|
493
|
+
const ASCII_CASE_DISTANCE: i32 = 32
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* `text` with every unit from `low` to `high` moved by `delta`, and every other
|
|
497
|
+
* unit as it was: `asciiUpper` and `asciiLower`'s one walk.
|
|
498
|
+
*
|
|
499
|
+
* Natively the units are bytes, so the answer is built as bytes, one array and
|
|
500
|
+
* one `String.fromBytes`. Under Node a unit can be wider than a byte, so the
|
|
501
|
+
* unchanged runs are cut out of `text` whole and only the changed letters are
|
|
502
|
+
* made one at a time. The name is long for the reason `isTextBlankByte`'s is.
|
|
503
|
+
*/
|
|
504
|
+
const shiftTextAsciiRange = (text: string, low: i32, high: i32, delta: i32): string => {
|
|
505
|
+
const length: i32 = toI32(text.length)
|
|
506
|
+
let i: i32 = 0
|
|
507
|
+
if (textUnitsAreBytes()) {
|
|
508
|
+
const bytes: u8[] = []
|
|
509
|
+
while (i < length) {
|
|
510
|
+
const code: i32 = toI32(text.charCodeAt(i))
|
|
511
|
+
bytes.push(toU8(code >= low && code <= high ? code + delta : code))
|
|
512
|
+
i += 1
|
|
513
|
+
}
|
|
514
|
+
return String.fromBytes(bytes, 0, length)
|
|
515
|
+
}
|
|
516
|
+
const parts: string[] = []
|
|
517
|
+
let start: i32 = 0
|
|
518
|
+
while (i < length) {
|
|
519
|
+
const code: i32 = toI32(text.charCodeAt(i))
|
|
520
|
+
if (code >= low && code <= high) {
|
|
521
|
+
// `start` never passes `i`, which is below `length`, so this always
|
|
522
|
+
// holds; it is what proves `start` within `text`, as in `splitLines`.
|
|
523
|
+
if (start >= 0 && start < length) {
|
|
524
|
+
parts.push(text.substring(start, i))
|
|
525
|
+
}
|
|
526
|
+
parts.push(String.fromCharCode(code + delta))
|
|
527
|
+
start = i + 1
|
|
528
|
+
}
|
|
529
|
+
i += 1
|
|
530
|
+
}
|
|
531
|
+
if (start < length) {
|
|
532
|
+
parts.push(text.substring(start, length))
|
|
533
|
+
}
|
|
534
|
+
return parts.join("")
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* `text` with its ASCII letters `a` to `z` made `A` to `Z`, and nothing else
|
|
539
|
+
* changed: every other byte, a UTF-8 sequence's included, passes through.
|
|
540
|
+
*
|
|
541
|
+
* This is not JavaScript's `toUpperCase`, which knows Unicode's case table:
|
|
542
|
+
* that gives `"SS"` for `"ß"` and `"É"` for `"é"`, where this gives `"ß"` and
|
|
543
|
+
* `"é"`. That table is more than this runtime has room for, and the text this
|
|
544
|
+
* is for, identifiers, keywords and protocol tokens, is ASCII. On ASCII text
|
|
545
|
+
* the two agree, and this answers the same natively and under Node for any
|
|
546
|
+
* text, since a byte or unit from 97 to 122 is always the letter itself, so
|
|
547
|
+
* NL8001 (`--warn-portability`) has nothing to warn about, and does not.
|
|
548
|
+
*
|
|
549
|
+
* @param text The text whose ASCII letters are upper-cased.
|
|
550
|
+
* @returns A fresh string as long as `text`.
|
|
551
|
+
*/
|
|
552
|
+
export const asciiUpper = (text: string): string =>
|
|
553
|
+
shiftTextAsciiRange(text, ASCII_LOWER_A, ASCII_LOWER_Z, -ASCII_CASE_DISTANCE)
|
|
554
|
+
|
|
555
|
+
/**
|
|
556
|
+
* `text` with its ASCII letters `A` to `Z` made `a` to `z`, and nothing else
|
|
557
|
+
* changed: every other byte, a UTF-8 sequence's included, passes through.
|
|
558
|
+
*
|
|
559
|
+
* This is not JavaScript's `toLowerCase`, for the reason `asciiUpper` gives:
|
|
560
|
+
* `"É"` stays `"É"` here. On ASCII text the two agree, and this answers the
|
|
561
|
+
* same natively and under Node for any text.
|
|
562
|
+
*
|
|
563
|
+
* @param text The text whose ASCII letters are lower-cased.
|
|
564
|
+
* @returns A fresh string as long as `text`.
|
|
565
|
+
*/
|
|
566
|
+
export const asciiLower = (text: string): string =>
|
|
567
|
+
shiftTextAsciiRange(text, ASCII_UPPER_A, ASCII_UPPER_Z, ASCII_CASE_DISTANCE)
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* The padding `padStart` and `padEnd` put beside `text`: `fill` repeated and
|
|
571
|
+
* cut so that `text` and the padding together are `width` units long, or `""`
|
|
572
|
+
* when `text` is that long already or `fill` is empty.
|
|
573
|
+
*
|
|
574
|
+
* `fill` must be ASCII, and `caller` names the function in the panic when it
|
|
575
|
+
* is not. A multi-byte fill cut at a byte width could end in half a UTF-8
|
|
576
|
+
* sequence natively, which is text that is not well-formed, and padding is
|
|
577
|
+
* done with spaces and zeros. The fill is checked before the width, so a bad
|
|
578
|
+
* fill panics whether or not any padding was needed. The name is long for the
|
|
579
|
+
* reason `isTextBlankByte`'s is.
|
|
580
|
+
*/
|
|
581
|
+
const textPadding = (text: string, width: i32, fill: string, caller: string): string => {
|
|
582
|
+
const fillLength: i32 = toI32(fill.length)
|
|
583
|
+
let j: i32 = 0
|
|
584
|
+
while (j < fillLength) {
|
|
585
|
+
if (toI32(fill.charCodeAt(j)) >= NON_ASCII) {
|
|
586
|
+
panic(`${caller}: every byte of the fill must be ASCII`)
|
|
587
|
+
}
|
|
588
|
+
j += 1
|
|
589
|
+
}
|
|
590
|
+
const needed: i32 = width - toI32(text.length)
|
|
591
|
+
if (needed <= 0 || fillLength === 0) {
|
|
592
|
+
return ""
|
|
593
|
+
}
|
|
594
|
+
const parts: string[] = []
|
|
595
|
+
let left: i32 = needed
|
|
596
|
+
while (left >= fillLength) {
|
|
597
|
+
parts.push(fill)
|
|
598
|
+
left -= fillLength
|
|
599
|
+
}
|
|
600
|
+
parts.push(fill.substring(0, left))
|
|
601
|
+
return parts.join("")
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* `text` with `fill` repeated in front of it until it is `width` units long:
|
|
606
|
+
* JavaScript's `text.padStart(width, fill)` for ASCII text. A `width` no
|
|
607
|
+
* longer than `text`, or an empty `fill`, gives `text` unchanged, and a fill of
|
|
608
|
+
* several characters repeats and is cut at the width, so
|
|
609
|
+
* `padStart("7", 4, "ab")` is `"aba7"`.
|
|
610
|
+
*
|
|
611
|
+
* **`width` counts the units `text.length` counts**: bytes natively and UTF-16
|
|
612
|
+
* units under Node. So on text that is not ASCII the two answers differ:
|
|
613
|
+
* `padStart("é", 3, " ")` is `" é"` natively, where `é` is two bytes, and
|
|
614
|
+
* `" é"` under Node, where it is one unit. NL8001 (`--warn-portability`)
|
|
615
|
+
* does not warn about it: the warning reads offsets in the program it is given,
|
|
616
|
+
* the library's own `text.length` is not walked, and what reaches the caller is
|
|
617
|
+
* a string rather than a number. Pad ASCII text, or count the width with
|
|
618
|
+
* `codePointLength` yourself, when the answer must be the same on both.
|
|
619
|
+
*
|
|
620
|
+
* **A fill that is not ASCII panics**, for the reason `indexOfAny`'s set must
|
|
621
|
+
* be ASCII: natively it would be cut in the middle of a UTF-8 sequence.
|
|
622
|
+
*
|
|
623
|
+
* @param text The text to pad.
|
|
624
|
+
* @param width The length the answer is padded to, in `text.length`'s units.
|
|
625
|
+
* @param fill The ASCII text repeated as the padding; `" "` and `"0"` are
|
|
626
|
+
* the usual ones.
|
|
627
|
+
* @returns `text` with the padding in front; `text` unchanged when no
|
|
628
|
+
* padding is needed.
|
|
629
|
+
*/
|
|
630
|
+
export const padStart = (text: string, width: i32, fill: string): string =>
|
|
631
|
+
textPadding(text, width, fill, "padStart") + text
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* `text` with `fill` repeated after it until it is `width` units long:
|
|
635
|
+
* JavaScript's `text.padEnd(width, fill)` for ASCII text, with `padStart`'s
|
|
636
|
+
* rules. `padEnd("7", 4, "ab")` is `"7aba"`, and `padEnd("é", 3, " ")` is
|
|
637
|
+
* `"é "` natively and `"é "` under Node.
|
|
638
|
+
*
|
|
639
|
+
* @param text The text to pad.
|
|
640
|
+
* @param width The length the answer is padded to, in `text.length`'s units.
|
|
641
|
+
* @param fill The ASCII text repeated as the padding.
|
|
642
|
+
* @returns `text` with the padding after it; `text` unchanged when no
|
|
643
|
+
* padding is needed.
|
|
644
|
+
*/
|
|
645
|
+
export const padEnd = (text: string, width: i32, fill: string): string =>
|
|
646
|
+
text + textPadding(text, width, fill, "padEnd")
|