nish 0.34.1 → 0.36.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/README.md +7 -6
- package/docs/AI.md +86 -16
- package/docs/INSTALL.md +22 -13
- package/llms.txt +5 -2
- package/package.json +15 -5
- package/runtime/async.d.mts +49 -0
- package/runtime/async.mjs +27 -0
- package/runtime/nish.d.ts +4681 -427
- package/runtime/nish.h +6 -2
- package/runtime/nish.mjs +4 -0
- package/runtime/runtime-net.c +22 -0
- package/runtime/runtime-shm.c +93 -2
- package/runtime/vite.d.ts +30 -0
- package/runtime/vite.mjs +150 -0
- package/scripts/build.sh +108 -13
- package/std/README.md +131 -131
- package/std/base64.ts +34 -5
- package/std/bytes.ts +258 -42
- package/std/cli-args.ts +296 -70
- package/std/cli-flags.ts +184 -19
- package/std/cli.ts +475 -54
- package/std/collections.ts +121 -23
- package/std/compress/checksum.ts +44 -14
- package/std/compress/deflate.ts +224 -111
- package/std/compress/gzip.ts +217 -53
- package/std/compress/zlib.ts +63 -7
- package/std/crypto/aes.ts +80 -9
- package/std/crypto/base64url.ts +22 -0
- package/std/crypto/bcrypt-batch.ts +88 -21
- package/std/crypto/bcrypt.ts +60 -0
- package/std/crypto/chacha20poly1305.ts +53 -0
- package/std/crypto/ct.ts +32 -13
- package/std/crypto/ed25519.ts +47 -0
- package/std/crypto/hkdf.ts +198 -13
- package/std/crypto/hmac.ts +235 -31
- package/std/crypto/jwt.ts +192 -63
- package/std/crypto/p256.ts +159 -16
- package/std/crypto/p384.ts +28 -3
- package/std/crypto/pki-roots.ts +32 -2
- package/std/crypto/pki.ts +81 -15
- package/std/crypto/rsa.ts +33 -3
- package/std/crypto/sha1.ts +3 -0
- package/std/crypto/sha256.ts +24 -1
- package/std/crypto/sha512.ts +99 -14
- package/std/crypto/x25519.ts +152 -14
- package/std/crypto/x509-random.ts +10 -0
- package/std/crypto/x509.ts +67 -7
- package/std/json-equal.ts +19 -0
- package/std/json-format.ts +112 -13
- package/std/json-number.ts +64 -14
- package/std/json-pointer.ts +23 -1
- package/std/json-tokenizer.ts +100 -29
- package/std/json-typed.ts +713 -66
- package/std/json-value.ts +400 -124
- package/std/json.ts +138 -10
- package/std/map.ts +25 -7
- package/std/math.ts +36 -4
- package/std/net/buffer-pool.ts +42 -11
- package/std/net/dns-config.ts +109 -23
- package/std/net/dns-message.ts +62 -7
- package/std/net/dns-resolver.ts +247 -53
- package/std/net/dns.ts +203 -35
- package/std/net/fetch.ts +151 -61
- package/std/net/handler.ts +278 -80
- package/std/net/hpack.ts +262 -52
- package/std/net/http-compress.ts +125 -15
- package/std/net/http-fields.ts +85 -9
- package/std/net/http1-client-codec.ts +100 -19
- package/std/net/http1-codec.ts +297 -77
- package/std/net/http1-server.ts +430 -94
- package/std/net/http2-frame.ts +272 -35
- package/std/net/http2-tls.ts +135 -34
- package/std/net/http2.ts +247 -106
- package/std/net/http3-frame.ts +154 -17
- package/std/net/http3-server.ts +275 -101
- package/std/net/http3.ts +307 -115
- package/std/net/outbound.ts +37 -3
- package/std/net/postgres-carrier.ts +144 -8
- package/std/net/postgres.ts +801 -112
- package/std/net/prefork.ts +164 -31
- package/std/net/qpack.ts +105 -18
- package/std/net/quic-conn-ack.ts +59 -12
- package/std/net/quic-conn-cid.ts +124 -27
- package/std/net/quic-conn-params.ts +70 -10
- package/std/net/quic-datagram.ts +43 -3
- package/std/net/quic-frame.ts +286 -12
- package/std/net/quic-listener.ts +101 -32
- package/std/net/quic-packet.ts +302 -21
- package/std/net/quic-recovery.ts +264 -52
- package/std/net/quic-stream.ts +353 -91
- package/std/net/quic.ts +1124 -201
- package/std/net/router.ts +87 -40
- package/std/net/tls/client.ts +166 -51
- package/std/net/tls/codec.ts +413 -56
- package/std/net/tls/record-client.ts +110 -55
- package/std/net/tls/record-server.ts +159 -59
- package/std/net/tls/record.ts +109 -12
- package/std/net/tls/schedule.ts +235 -26
- package/std/net/tls/ticket.ts +159 -40
- package/std/net/tls/verify.ts +49 -3
- package/std/net/tls-tcp.ts +250 -45
- package/std/net/tls.ts +221 -59
- package/std/net/upstream.ts +81 -45
- package/std/net/url.ts +97 -13
- package/std/net/websocket.ts +122 -12
- package/std/net/webtransport.ts +220 -89
- package/std/pair.ts +10 -2
- package/std/random.ts +34 -9
- package/std/regex.ts +352 -198
- package/std/region.ts +70 -15
- package/std/secret.ts +52 -11
- package/std/sort.ts +17 -2
- package/std/sqlite.ts +256 -35
- package/std/sync/ring.ts +430 -40
- package/std/sync/shm.ts +222 -15
- package/std/term-io.ts +137 -64
- package/std/term-pixels.ts +144 -19
- package/std/term-probe.ts +66 -25
- package/std/term-rate.ts +128 -57
- package/std/term.ts +332 -121
- package/std/testing.ts +130 -32
- package/std/text.ts +73 -7
- package/std/threads.ts +229 -41
- package/std/yaml-json.ts +94 -82
- package/std/yaml-lines.ts +47 -15
- package/std/yaml-write.ts +24 -16
- package/std/yaml.ts +764 -384
package/std/testing.ts
CHANGED
|
@@ -1,14 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `std/testing` — a test runner for Nish programs, written in Nish.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
* corpus, assembles it, links it and diffs the result against a golden, so it
|
|
6
|
-
* has to be able to spawn a compiler and read a directory. This module answers
|
|
7
|
-
* the other half of the question — a *program* that checks its own behaviour
|
|
8
|
-
* and reports it — and it answers it in the language, with no Node anywhere.
|
|
4
|
+
* import { Suite } from "nish/testing";
|
|
9
5
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
6
|
+
* export const main = (): i32 => {
|
|
7
|
+
* const t = new Suite("arrays");
|
|
8
|
+
* const a: i32[] = [1, 2, 3];
|
|
9
|
+
* if (!t.eqI32("length", toI32(a.length), 3)) {
|
|
10
|
+
* return t.done(); // a[2] below would panic, not fail
|
|
11
|
+
* }
|
|
12
|
+
* t.eqI32("last", a[2], 3);
|
|
13
|
+
* return t.done(); // 0 when nothing failed, 1 otherwise
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
* **It is the other half of `tests/run.js`.** The repository's own suite is
|
|
17
|
+
* `tests/run.js`, a Node program: it compiles the corpus, assembles it, links
|
|
18
|
+
* it and diffs the result against a golden, so it has to be able to spawn a
|
|
19
|
+
* compiler and read a directory. This module answers the other half of the
|
|
20
|
+
* question — a *program* that checks its own behaviour and reports it — and it
|
|
21
|
+
* answers it in the language, with no Node anywhere.
|
|
22
|
+
*
|
|
23
|
+
* **Five language rules shape the API**, and each of them removes a shape that
|
|
24
|
+
* a JavaScript test framework would have reached for first:
|
|
12
25
|
*
|
|
13
26
|
* 1. **A function is not a value** ([LANGUAGE.md](../docs/LANGUAGE.md) §
|
|
14
27
|
* Functions), so there is no `test("name", () => { ... })`. A suite is an
|
|
@@ -33,14 +46,15 @@
|
|
|
33
46
|
* 5. **There are no optional or default parameters**, so every assertion
|
|
34
47
|
* takes its name and its detail in full.
|
|
35
48
|
*
|
|
36
|
-
* The output is deliberately the shape `tests/run.js` prints —
|
|
37
|
-
* `FAIL <name>` with an indented detail line,
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* from one that proved
|
|
49
|
+
* **The output is deliberately the shape `tests/run.js` prints** —
|
|
50
|
+
* `PASS <name>`, `FAIL <name>` with an indented detail line,
|
|
51
|
+
* `SKIP <name> (<reason>)`, and a summary that ends
|
|
52
|
+
* `N passed, M failed, K skipped` — so a Nish test program reads like the rest
|
|
53
|
+
* of the suite and a skip is counted rather than merely mentioned. A green run
|
|
54
|
+
* that skipped half its checks should look different from one that proved
|
|
55
|
+
* everything.
|
|
42
56
|
*
|
|
43
|
-
* The widths are spelled explicitly (`i32`, `i64`, `f64`) rather than as
|
|
57
|
+
* **The widths are spelled explicitly** (`i32`, `i64`, `f64`) rather than as
|
|
44
58
|
* `number`, so the module means the same thing under `--number-mode f64` as it
|
|
45
59
|
* does by default, exactly as `examples/arrays.ts` does — and every length this
|
|
46
60
|
* module reads from a builtin is converted with `toI32`, because `.length`
|
|
@@ -49,18 +63,6 @@
|
|
|
49
63
|
* comparisons against a length below would lower to `fcmp` on an `f64` in that
|
|
50
64
|
* mode and to `icmp` on an `i32` here, which is the same module compiling to
|
|
51
65
|
* two different programs.
|
|
52
|
-
*
|
|
53
|
-
* import { Suite } from "../std/testing";
|
|
54
|
-
*
|
|
55
|
-
* export const main = (): number => {
|
|
56
|
-
* const t = new Suite("arrays");
|
|
57
|
-
* const a: i32[] = [1, 2, 3];
|
|
58
|
-
* if (!t.eqI32("length", a.length, 3)) {
|
|
59
|
-
* return t.done(); // a[2] below would panic, not fail
|
|
60
|
-
* }
|
|
61
|
-
* t.eqI32("last", a[2], 3);
|
|
62
|
-
* return t.done(); // 0 when nothing failed, 1 otherwise
|
|
63
|
-
* };
|
|
64
66
|
*/
|
|
65
67
|
|
|
66
68
|
/**
|
|
@@ -132,26 +134,38 @@ const testingFirstDifferentLine = (actual: string[], expected: string[]): i32 =>
|
|
|
132
134
|
*/
|
|
133
135
|
export class Suite {
|
|
134
136
|
/** Names the summary line, so several suites in one program stay apart. */
|
|
135
|
-
name: string
|
|
137
|
+
private name: string
|
|
136
138
|
// These three have initializers because the constructor does not assign them,
|
|
137
139
|
// and every field holds a value once it returns. `name` does not, and an
|
|
138
140
|
// initializer there would only be a store the constructor overwrites.
|
|
139
|
-
passed: i32 = 0
|
|
140
|
-
failed: i32 = 0
|
|
141
|
-
skipped: i32 = 0
|
|
141
|
+
private passed: i32 = 0
|
|
142
|
+
private failed: i32 = 0
|
|
143
|
+
private skipped: i32 = 0
|
|
142
144
|
/**
|
|
143
145
|
* The names of the checks that failed, recapped by `done`. The detail of
|
|
144
146
|
* each was printed when it happened; this is the list a reader wants after
|
|
145
147
|
* a hundred lines of output have scrolled the failures off the screen.
|
|
146
148
|
*/
|
|
147
|
-
failures: string[]
|
|
149
|
+
private failures: string[]
|
|
148
150
|
|
|
151
|
+
/**
|
|
152
|
+
* An empty suite: nothing passed, failed or skipped yet.
|
|
153
|
+
*
|
|
154
|
+
* @param name what the summary line `done` prints starts with, so several
|
|
155
|
+
* suites in one program stay apart.
|
|
156
|
+
*/
|
|
149
157
|
constructor(name: string) {
|
|
150
158
|
this.name = name
|
|
151
159
|
this.failures = []
|
|
152
160
|
}
|
|
153
161
|
|
|
154
|
-
/**
|
|
162
|
+
/**
|
|
163
|
+
* Records a pass. Public because a program with a check of its own shape
|
|
164
|
+
* still wants the tally.
|
|
165
|
+
*
|
|
166
|
+
* @param name the check's name, printed after `PASS`.
|
|
167
|
+
* @returns `true`, so a check of its own shape can answer what this answers.
|
|
168
|
+
*/
|
|
155
169
|
pass(name: string): boolean {
|
|
156
170
|
this.passed += 1
|
|
157
171
|
console.log(`PASS ${name}`)
|
|
@@ -162,6 +176,10 @@ export class Suite {
|
|
|
162
176
|
* Records a failure, with `detail` on its own indented line when there is
|
|
163
177
|
* one. An empty `detail` prints no second line, which is what a check whose
|
|
164
178
|
* name already says everything wants.
|
|
179
|
+
*
|
|
180
|
+
* @param name the check's name, printed after `FAIL` and recapped by `done`.
|
|
181
|
+
* @param detail what went wrong, printed on the next line; empty for none.
|
|
182
|
+
* @returns `false`, so a check of its own shape can answer what this answers.
|
|
165
183
|
*/
|
|
166
184
|
fail(name: string, detail: string): boolean {
|
|
167
185
|
this.failed += 1
|
|
@@ -177,12 +195,23 @@ export class Suite {
|
|
|
177
195
|
* Records a check that did not run, and why. It answers `void` rather than a
|
|
178
196
|
* `boolean` because there is no outcome to branch on: the caller has already
|
|
179
197
|
* decided to skip, the way `tests/run.js` decides when `llvm-as` is missing.
|
|
198
|
+
*
|
|
199
|
+
* @param name the check's name, printed after `SKIP`.
|
|
200
|
+
* @param reason why it did not run, printed in parentheses after the name.
|
|
180
201
|
*/
|
|
181
202
|
skip(name: string, reason: string): void {
|
|
182
203
|
this.skipped += 1
|
|
183
204
|
console.log(`SKIP ${name} (${reason})`)
|
|
184
205
|
}
|
|
185
206
|
|
|
207
|
+
/**
|
|
208
|
+
* Passes when `condition` is true.
|
|
209
|
+
*
|
|
210
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
211
|
+
* @param condition what the check asserts.
|
|
212
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
213
|
+
* that depends on it.
|
|
214
|
+
*/
|
|
186
215
|
ok(name: string, condition: boolean): boolean {
|
|
187
216
|
if (condition) {
|
|
188
217
|
return this.pass(name)
|
|
@@ -190,6 +219,15 @@ export class Suite {
|
|
|
190
219
|
return this.fail(name, "expected true, got false")
|
|
191
220
|
}
|
|
192
221
|
|
|
222
|
+
/**
|
|
223
|
+
* Passes when `actual` equals `expected`; a failure prints both.
|
|
224
|
+
*
|
|
225
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
226
|
+
* @param actual the value the program computed.
|
|
227
|
+
* @param expected the value it should be.
|
|
228
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
229
|
+
* that depends on it.
|
|
230
|
+
*/
|
|
193
231
|
eqBool(name: string, actual: boolean, expected: boolean): boolean {
|
|
194
232
|
if (actual === expected) {
|
|
195
233
|
return this.pass(name)
|
|
@@ -197,6 +235,15 @@ export class Suite {
|
|
|
197
235
|
return this.fail(name, `expected ${expected}, got ${actual}`)
|
|
198
236
|
}
|
|
199
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Passes when `actual` equals `expected`; a failure prints both.
|
|
240
|
+
*
|
|
241
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
242
|
+
* @param actual the value the program computed.
|
|
243
|
+
* @param expected the value it should be.
|
|
244
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
245
|
+
* that depends on it.
|
|
246
|
+
*/
|
|
200
247
|
eqI32(name: string, actual: i32, expected: i32): boolean {
|
|
201
248
|
if (actual === expected) {
|
|
202
249
|
return this.pass(name)
|
|
@@ -204,6 +251,15 @@ export class Suite {
|
|
|
204
251
|
return this.fail(name, `expected ${expected}, got ${actual}`)
|
|
205
252
|
}
|
|
206
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Passes when `actual` equals `expected`; a failure prints both.
|
|
256
|
+
*
|
|
257
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
258
|
+
* @param actual the value the program computed.
|
|
259
|
+
* @param expected the value it should be.
|
|
260
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
261
|
+
* that depends on it.
|
|
262
|
+
*/
|
|
207
263
|
eqI64(name: string, actual: i64, expected: i64): boolean {
|
|
208
264
|
if (actual === expected) {
|
|
209
265
|
return this.pass(name)
|
|
@@ -215,6 +271,12 @@ export class Suite {
|
|
|
215
271
|
* Exact `f64` equality, which is `fcmp oeq`: `NaN` is equal to nothing, so a
|
|
216
272
|
* check of a computation that can produce one fails rather than passing by
|
|
217
273
|
* accident. Use `nearF64` for anything that went through arithmetic.
|
|
274
|
+
*
|
|
275
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
276
|
+
* @param actual the value the program computed.
|
|
277
|
+
* @param expected the value it should be.
|
|
278
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
279
|
+
* that depends on it.
|
|
218
280
|
*/
|
|
219
281
|
eqF64(name: string, actual: f64, expected: f64): boolean {
|
|
220
282
|
if (actual === expected) {
|
|
@@ -227,6 +289,15 @@ export class Suite {
|
|
|
227
289
|
* `f64` equality within `tolerance`, the absolute difference. The tolerance
|
|
228
290
|
* is a parameter rather than a constant here because the right epsilon
|
|
229
291
|
* belongs to the computation, not to the harness.
|
|
292
|
+
*
|
|
293
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
294
|
+
* @param actual the value the program computed.
|
|
295
|
+
* @param expected the value it should be.
|
|
296
|
+
* @param tolerance the largest difference that still passes, inclusive. A
|
|
297
|
+
* `NaN` on either side fails whatever it is, and so do two equal
|
|
298
|
+
* infinities, since their difference is `NaN`.
|
|
299
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
300
|
+
* that depends on it.
|
|
230
301
|
*/
|
|
231
302
|
nearF64(name: string, actual: f64, expected: f64, tolerance: f64): boolean {
|
|
232
303
|
let delta: f64 = actual - expected
|
|
@@ -243,6 +314,12 @@ export class Suite {
|
|
|
243
314
|
* String equality by content (`nish_str_eq`), which is what `===` on two
|
|
244
315
|
* strings already means. The values are quoted in the message so that a
|
|
245
316
|
* trailing space or an empty string is visible in the diff.
|
|
317
|
+
*
|
|
318
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
319
|
+
* @param actual the value the program computed.
|
|
320
|
+
* @param expected the value it should be.
|
|
321
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
322
|
+
* that depends on it.
|
|
246
323
|
*/
|
|
247
324
|
eqStr(name: string, actual: string, expected: string): boolean {
|
|
248
325
|
if (actual === expected) {
|
|
@@ -261,6 +338,12 @@ export class Suite {
|
|
|
261
338
|
* The detail quotes the needle in full and the haystack only to
|
|
262
339
|
* `TESTING_EXCERPT` bytes: the needle is what a reader compares, and the
|
|
263
340
|
* haystack is what they have already got.
|
|
341
|
+
*
|
|
342
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
343
|
+
* @param haystack the text searched, such as captured output.
|
|
344
|
+
* @param needle the text it must contain; an empty one is found in anything.
|
|
345
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
346
|
+
* that depends on it.
|
|
264
347
|
*/
|
|
265
348
|
contains(name: string, haystack: string, needle: string): boolean {
|
|
266
349
|
if (toI32(haystack.indexOf(needle)) >= 0) {
|
|
@@ -279,6 +362,13 @@ export class Suite {
|
|
|
279
362
|
* nothing is met by anything. The detail names the first fragment that is
|
|
280
363
|
* missing and how many are, because a detail that quoted all of them would bury
|
|
281
364
|
* the one a reader fixes first.
|
|
365
|
+
*
|
|
366
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
367
|
+
* @param haystack the text searched, such as captured output.
|
|
368
|
+
* @param needles the fragments it must contain, in any order and each
|
|
369
|
+
* anywhere; empty ones are skipped.
|
|
370
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
371
|
+
* that depends on it.
|
|
282
372
|
*/
|
|
283
373
|
containsAll(name: string, haystack: string, needles: string[]): boolean {
|
|
284
374
|
let looked: i32 = 0
|
|
@@ -317,6 +407,12 @@ export class Suite {
|
|
|
317
407
|
* by hand. `<end of input>` is the side that ran out, which is what a missing
|
|
318
408
|
* or an extra line at the end looks like from here, and the line number is the
|
|
319
409
|
* one an editor takes.
|
|
410
|
+
*
|
|
411
|
+
* @param name the check's name, printed after `PASS` or `FAIL`.
|
|
412
|
+
* @param actual the lines the program produced.
|
|
413
|
+
* @param expected the lines it should have produced.
|
|
414
|
+
* @returns whether the check passed, so the caller can stop before a read
|
|
415
|
+
* that depends on it.
|
|
320
416
|
*/
|
|
321
417
|
eqLines(name: string, actual: string[], expected: string[]): boolean {
|
|
322
418
|
const at: i32 = testingFirstDifferentLine(actual, expected)
|
|
@@ -333,6 +429,8 @@ export class Suite {
|
|
|
333
429
|
* nothing failed, 1 otherwise. A skip is not a failure, so it does not move
|
|
334
430
|
* the code — it moves the count on the summary line, which is the whole
|
|
335
431
|
* reason the count is printed.
|
|
432
|
+
*
|
|
433
|
+
* @returns 0 when no check failed and 1 otherwise, for `main` to return.
|
|
336
434
|
*/
|
|
337
435
|
done(): i32 {
|
|
338
436
|
if (toI32(this.failures.length) > 0) {
|
package/std/text.ts
CHANGED
|
@@ -1,14 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `std/text` — the string operations a program has to write itself.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* import { replaceAll, splitLines, splitWhitespace, trim } from "nish/text";
|
|
5
|
+
*
|
|
6
|
+
* export const main = (): i32 => {
|
|
7
|
+
* const lines: string[] = splitLines("a b\n c \n"); // ["a b", " c "]
|
|
8
|
+
* const words: string[] = splitWhitespace(lines[0]); // ["a", "b"]
|
|
9
|
+
* const c: string = trim(lines[1]); // "c"
|
|
10
|
+
* const dashed: string = replaceAll("a b", " ", "-"); // "a-b"
|
|
11
|
+
* return toI32(words.length);
|
|
12
|
+
* };
|
|
13
|
+
*
|
|
14
|
+
* **The language gives a string `length`, `charCodeAt`, `substring`, `indexOf`,
|
|
15
|
+
* `startsWith`, `endsWith` and `===` by content, and an array `join`.** It does
|
|
6
16
|
* **not** give `split`, `trim`, `toLowerCase` or a regular expression (that is
|
|
7
17
|
* `std/regex`), and that is a decision rather than a gap: each allocates, and
|
|
8
18
|
* several need a locale or a character table this runtime has no room for
|
|
9
19
|
* (`docs/wp7-runtime.md` keeps the runtime under a measured byte budget).
|
|
10
20
|
*
|
|
11
|
-
* So a program that reads a file and wants its lines writes the loop
|
|
21
|
+
* **So a program that reads a file and wants its lines writes the loop.** This
|
|
12
22
|
* module is that loop, written once. Every offset here is a **byte** offset,
|
|
13
23
|
* like `s.length` itself, and every function but one is ASCII-only where it
|
|
14
24
|
* inspects characters at all — a UTF-8 string passes through `splitLines` and
|
|
@@ -16,7 +26,7 @@
|
|
|
16
26
|
* multi-byte sequence. The one is `codePointLength`, which counts code points
|
|
17
27
|
* rather than bytes and answers the same natively and under Node; it says how.
|
|
18
28
|
*
|
|
19
|
-
* Every width this module declares is spelled (`i32`, never `number`) *and*
|
|
29
|
+
* **Every width this module declares is spelled** (`i32`, never `number`) *and*
|
|
20
30
|
* every value a builtin hands it is converted at the read: `s.length`,
|
|
21
31
|
* `a.length`, `s.charCodeAt(i)` and `s.indexOf(t)` all answer `number`
|
|
22
32
|
* (LANGUAGE.md, *Arrays and strings as receivers*), and `number` is `f64` under
|
|
@@ -54,6 +64,9 @@ const isTextBlankByte = (code: i32): boolean =>
|
|
|
54
64
|
* the end. A `\r\n` line ending keeps its `\r`: stripping it would make this
|
|
55
65
|
* function guess at the file's provenance, and a caller that needs it gone can
|
|
56
66
|
* `trimEnd` each line.
|
|
67
|
+
*
|
|
68
|
+
* @param text The text to split; an empty one has no lines.
|
|
69
|
+
* @returns A fresh array of the lines, in order.
|
|
57
70
|
*/
|
|
58
71
|
export const splitLines = (text: string): string[] => {
|
|
59
72
|
const length: i32 = toI32(text.length)
|
|
@@ -84,6 +97,11 @@ export const splitLines = (text: string): string[] => {
|
|
|
84
97
|
* Leading, trailing and repeated blanks produce no empty elements, which is what
|
|
85
98
|
* splitting a line of command-line flags wants — and it is why this is not
|
|
86
99
|
* `split(text, " ")`, which would.
|
|
100
|
+
*
|
|
101
|
+
* @param text The text to split; the blanks are space, tab, newline and
|
|
102
|
+
* carriage return.
|
|
103
|
+
* @returns A fresh array of the non-blank runs, in order; empty when `text` is
|
|
104
|
+
* all blank.
|
|
87
105
|
*/
|
|
88
106
|
export const splitWhitespace = (text: string): string[] => {
|
|
89
107
|
const length: i32 = toI32(text.length)
|
|
@@ -109,7 +127,14 @@ export const splitWhitespace = (text: string): string[] => {
|
|
|
109
127
|
return parts
|
|
110
128
|
}
|
|
111
129
|
|
|
112
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* `text` without its leading blank bytes.
|
|
132
|
+
*
|
|
133
|
+
* @param text The text to trim; the blanks are space, tab, newline and
|
|
134
|
+
* carriage return.
|
|
135
|
+
* @returns `text` from its first non-blank byte on, or `""` when it is all
|
|
136
|
+
* blank.
|
|
137
|
+
*/
|
|
113
138
|
export const trimStart = (text: string): string => {
|
|
114
139
|
const length: i32 = toI32(text.length)
|
|
115
140
|
let i: i32 = 0
|
|
@@ -119,7 +144,14 @@ export const trimStart = (text: string): string => {
|
|
|
119
144
|
return text.substring(i, length)
|
|
120
145
|
}
|
|
121
146
|
|
|
122
|
-
/**
|
|
147
|
+
/**
|
|
148
|
+
* `text` without its trailing blank bytes.
|
|
149
|
+
*
|
|
150
|
+
* @param text The text to trim; the blanks are space, tab, newline and
|
|
151
|
+
* carriage return.
|
|
152
|
+
* @returns `text` up to and including its last non-blank byte, or `""` when it
|
|
153
|
+
* is all blank.
|
|
154
|
+
*/
|
|
123
155
|
export const trimEnd = (text: string): string => {
|
|
124
156
|
let end: i32 = toI32(text.length)
|
|
125
157
|
while (end > 0 && isTextBlankByte(toI32(text.charCodeAt(end - 1)))) {
|
|
@@ -128,13 +160,24 @@ export const trimEnd = (text: string): string => {
|
|
|
128
160
|
return text.substring(0, end)
|
|
129
161
|
}
|
|
130
162
|
|
|
131
|
-
/**
|
|
163
|
+
/**
|
|
164
|
+
* `text` without blank bytes at either end.
|
|
165
|
+
*
|
|
166
|
+
* @param text The text to trim; the blanks are space, tab, newline and
|
|
167
|
+
* carriage return.
|
|
168
|
+
* @returns `text` from its first non-blank byte to its last, or `""` when it is
|
|
169
|
+
* all blank.
|
|
170
|
+
*/
|
|
132
171
|
export const trim = (text: string): string => trimEnd(trimStart(text))
|
|
133
172
|
|
|
134
173
|
/**
|
|
135
174
|
* Whether `needle` occurs anywhere in `haystack`. One `indexOf`, named, because
|
|
136
175
|
* `indexOf(...) >= 0` at a call site reads as arithmetic where the question is a
|
|
137
176
|
* yes or a no. `contains(s, "")` is `true`, as `indexOf("")` is `0`.
|
|
177
|
+
*
|
|
178
|
+
* @param haystack The text searched.
|
|
179
|
+
* @param needle The text looked for, compared byte for byte.
|
|
180
|
+
* @returns `true` when `needle` occurs in `haystack`.
|
|
138
181
|
*/
|
|
139
182
|
export const contains = (haystack: string, needle: string): boolean => toI32(haystack.indexOf(needle)) >= 0
|
|
140
183
|
|
|
@@ -148,6 +191,12 @@ export const contains = (haystack: string, needle: string): boolean => toI32(hay
|
|
|
148
191
|
*
|
|
149
192
|
* An empty `needle` answers `text` unchanged, because every other answer is a
|
|
150
193
|
* choice about how many empty matches a string contains.
|
|
194
|
+
*
|
|
195
|
+
* @param text The text whose occurrences are replaced.
|
|
196
|
+
* @param needle The text looked for. Occurrences are found left to right and
|
|
197
|
+
* do not overlap: after a match the search goes on past its end.
|
|
198
|
+
* @param replacement The text each occurrence becomes; it is not searched.
|
|
199
|
+
* @returns The text with every occurrence replaced.
|
|
151
200
|
*/
|
|
152
201
|
export const replaceAll = (text: string, needle: string, replacement: string): string => {
|
|
153
202
|
const needleLength: i32 = toI32(needle.length)
|
|
@@ -176,6 +225,12 @@ export const replaceAll = (text: string, needle: string, replacement: string): s
|
|
|
176
225
|
* they are equal. A caller comparing generated text against a golden wants the
|
|
177
226
|
* first differing line and not a boolean, because printing two whole files is
|
|
178
227
|
* not a diagnostic.
|
|
228
|
+
*
|
|
229
|
+
* @param left One side's lines.
|
|
230
|
+
* @param right The other side's lines, compared to `left`'s with `!==` by
|
|
231
|
+
* content.
|
|
232
|
+
* @returns The index of the first line that differs, the shorter length when
|
|
233
|
+
* one side is a prefix of the other, or `-1` when they are equal.
|
|
179
234
|
*/
|
|
180
235
|
export const firstDifference = (left: string[], right: string[]): i32 => {
|
|
181
236
|
const leftLength: i32 = toI32(left.length)
|
|
@@ -216,6 +271,14 @@ const NON_ASCII: i32 = 128
|
|
|
216
271
|
* 16 or 32 bytes at a time (docs/LANGUAGE.md, "Byte search"). It is
|
|
217
272
|
* **variable-time**: how long it takes says where the first match is, so it is
|
|
218
273
|
* never for secret data.
|
|
274
|
+
*
|
|
275
|
+
* @param text The text searched.
|
|
276
|
+
* @param bytes The set of bytes looked for, written as a string of 1 to 16
|
|
277
|
+
* ASCII characters; their order does not matter.
|
|
278
|
+
* @param from Where the search starts, as a byte offset, clamped into
|
|
279
|
+
* `[0, text.length]`.
|
|
280
|
+
* @returns The byte offset of the first match at or after `from`, or -1 when
|
|
281
|
+
* there is none.
|
|
219
282
|
*/
|
|
220
283
|
export const indexOfAny = (text: string, bytes: string, from: i32): i32 => {
|
|
221
284
|
const count: i32 = toI32(bytes.length)
|
|
@@ -320,6 +383,9 @@ const textUnitsAreBytes = (): boolean => toI32("\u00e9".length) === 2
|
|
|
320
383
|
* stray continuation byte counts nothing, or the two halves of a pair joined
|
|
321
384
|
* at run time, which are one string under Node and two lone surrogates
|
|
322
385
|
* natively.
|
|
386
|
+
*
|
|
387
|
+
* @param text The text whose code points are counted.
|
|
388
|
+
* @returns How many code points `text` holds, counted as above.
|
|
323
389
|
*/
|
|
324
390
|
export const codePointLength = (text: string): i32 => {
|
|
325
391
|
const length: i32 = toI32(text.length)
|