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/base64.ts
CHANGED
|
@@ -2,7 +2,17 @@
|
|
|
2
2
|
* `nish/base64` — standard base64 (RFC 4648 §4) with `=` padding, the encoding
|
|
3
3
|
* kitty graphics frames are sent in.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* import { base64Encode, base64EncodeInto, base64EncodedLength } from "nish/base64";
|
|
6
|
+
*
|
|
7
|
+
* export const main = (): i32 => {
|
|
8
|
+
* const hi: u8[] = [72, 105]; // "Hi"
|
|
9
|
+
* const text: string = base64Encode(hi); // "SGk="
|
|
10
|
+
* const out: u8[] = new Array<u8>(base64EncodedLength(2) + 1);
|
|
11
|
+
* const end: i32 = base64EncodeInto(hi, 0, 2, out, 1); // out[1..5) is "SGk=", end is 5
|
|
12
|
+
* return end;
|
|
13
|
+
* };
|
|
14
|
+
*
|
|
15
|
+
* **The alphabet is `A-Z a-z 0-9 + /`.** Every three bytes become four characters,
|
|
6
16
|
* and a final group of one or two bytes becomes two or three characters filled
|
|
7
17
|
* out to four with `=` (RFC 4648 §4, the padding rules of §3.2), so the text is
|
|
8
18
|
* always a multiple of four long.
|
|
@@ -13,15 +23,15 @@
|
|
|
13
23
|
* can be encoded chunk by chunk straight into its output buffer.
|
|
14
24
|
* - `base64Encode(bytes)` is the whole array as a string.
|
|
15
25
|
*
|
|
16
|
-
* This module only encodes
|
|
26
|
+
* **This module only encodes**: what it is for writes base64 and never reads it
|
|
17
27
|
* back. It is not the strict decoder of `nish/crypto/base64url`, which is a
|
|
18
28
|
* different alphabet without padding and is written not to branch on the data;
|
|
19
29
|
* what goes through here is pixels, not secrets, so a sextet becomes its
|
|
20
30
|
* character by indexing the alphabet. The output is ASCII, so the string is
|
|
21
31
|
* the same bytes natively and under Node.
|
|
22
32
|
*
|
|
23
|
-
* Private helpers
|
|
24
|
-
* (`docs/wp26-stdlib.md` §3e)
|
|
33
|
+
* **Private helpers carry the module's name** because they share the
|
|
34
|
+
* importing program's flat symbol namespace (`docs/wp26-stdlib.md` §3e).
|
|
25
35
|
*/
|
|
26
36
|
|
|
27
37
|
/** The 64 characters of RFC 4648 §4's Table 1, in value order. */
|
|
@@ -34,6 +44,10 @@ const BASE64_PAD: i32 = 61
|
|
|
34
44
|
* The characters `n` bytes encode to: four for every three, a short final
|
|
35
45
|
* group counted as a whole one. Panics when `n` is negative or the length
|
|
36
46
|
* would pass 2^31 - 1, which `n` above 1,610,612,733 does.
|
|
47
|
+
*
|
|
48
|
+
* @param n How many bytes are to be encoded; zero answers zero.
|
|
49
|
+
* @returns The number of base64 characters, padding included, that `n` bytes
|
|
50
|
+
* encode to: always a multiple of four.
|
|
37
51
|
*/
|
|
38
52
|
export const base64EncodedLength = (n: i32): i32 => {
|
|
39
53
|
if (n < 0) {
|
|
@@ -55,6 +69,15 @@ const base64CharOf = (v: i32): u8 => toU8(BASE64_ALPHABET.charCodeAt(v))
|
|
|
55
69
|
* `at + base64EncodedLength(len)`. Nothing is allocated. Panics when the source
|
|
56
70
|
* range is not inside `src` or the encoding does not fit in `dst` from `at`;
|
|
57
71
|
* both are checked before anything is written.
|
|
72
|
+
*
|
|
73
|
+
* @param src The bytes to encode from; only `src[off..off+len)` is read.
|
|
74
|
+
* @param off Where in `src` the bytes to encode start.
|
|
75
|
+
* @param len How many bytes of `src` to encode; zero writes nothing and
|
|
76
|
+
* answers `at`.
|
|
77
|
+
* @param dst The buffer the characters are written into, one ASCII byte per
|
|
78
|
+
* character; bytes outside the written range are left as they were.
|
|
79
|
+
* @param at Where in `dst` the first character goes.
|
|
80
|
+
* @returns The index in `dst` just past the last character written.
|
|
58
81
|
*/
|
|
59
82
|
export const base64EncodeInto = (src: u8[], off: i32, len: i32, dst: u8[], at: i32): i32 => {
|
|
60
83
|
// Refused before `toI32`, which saturates a longer length under
|
|
@@ -104,7 +127,13 @@ export const base64EncodeInto = (src: u8[], off: i32, len: i32, dst: u8[], at: i
|
|
|
104
127
|
return o
|
|
105
128
|
}
|
|
106
129
|
|
|
107
|
-
/**
|
|
130
|
+
/**
|
|
131
|
+
* `bytes` as padded base64. Panics past 1,610,612,733 bytes, whose encoding
|
|
132
|
+
* would not fit a string.
|
|
133
|
+
*
|
|
134
|
+
* @param bytes The bytes to encode, all of them; an empty array answers `""`.
|
|
135
|
+
* @returns A fresh string holding the padded encoding.
|
|
136
|
+
*/
|
|
108
137
|
export const base64Encode = (bytes: u8[]): string => {
|
|
109
138
|
if (bytes.length > 2147483647) {
|
|
110
139
|
panic("base64Encode: more than 2^31 - 1 bytes")
|
package/std/bytes.ts
CHANGED
|
@@ -8,18 +8,21 @@
|
|
|
8
8
|
*
|
|
9
9
|
* import { getU32BE, setU16LE } from "nish/bytes";
|
|
10
10
|
*
|
|
11
|
-
* const
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* export const main = (): i32 => {
|
|
12
|
+
* const header: u8[] = [0, 0, 1, 0, 0, 0];
|
|
13
|
+
* const length = getU32BE(header, 0); // 256
|
|
14
|
+
* setU16LE(header, 4, toU16(513)); // header[4] = 1, header[5] = 2
|
|
15
|
+
* return toI32(length);
|
|
16
|
+
* };
|
|
14
17
|
*
|
|
15
|
-
* Every function takes the array and the offset of its first byte; the `LE`
|
|
18
|
+
* **Every function takes the array and the offset of its first byte; the `LE`
|
|
16
19
|
* name puts the least significant byte there, the `BE` name the most
|
|
17
|
-
* significant
|
|
20
|
+
* significant.** The widths are `u16`, `u32` and `u64`, the signed `i32` and
|
|
18
21
|
* `i64`, which are the same bytes read as two's complement, and `f64`, whose
|
|
19
22
|
* bytes are its IEEE 754 bits (`f64ToBits`). There is no `f32`: the language
|
|
20
23
|
* has no bit cast for one.
|
|
21
24
|
*
|
|
22
|
-
* Each access is an ordinary index
|
|
25
|
+
* **Each access is an ordinary index**, so an offset whose bytes do not all lie in
|
|
23
26
|
* the array panics `index`, naming the first byte that does not. A guard in
|
|
24
27
|
* the caller does not yet remove these checks: in a loop guarded by
|
|
25
28
|
* `off + 3 < b.length`, the checker proves an index it can see but not one
|
|
@@ -31,8 +34,8 @@
|
|
|
31
34
|
* eight bytes unchecked, which lowers to one load and a byte swap. An offset
|
|
32
35
|
* outside the range still goes byte by byte, so it panics as the others do.
|
|
33
36
|
*
|
|
34
|
-
* The in-range answers are byte for byte those of the copies `std/` keeps for
|
|
35
|
-
* itself
|
|
37
|
+
* **The in-range answers are byte for byte those of the copies `std/` keeps for
|
|
38
|
+
* itself**: `sha256LoadWord` (`std/crypto/sha256.ts`) for `getU32BE`,
|
|
36
39
|
* `chacha20Word` and `poly1305Le64` (`std/crypto/chacha20poly1305.ts`) for
|
|
37
40
|
* `getU32LE` and `getU64LE`, `loadWord` and `storeWord` (`std/crypto/sha512.ts`)
|
|
38
41
|
* for `getU64BE` and `setU64BE`, `http2FramePut32` (`std/net/http2-frame.ts`)
|
|
@@ -41,8 +44,8 @@
|
|
|
41
44
|
* TLS word helpers read zero past the end and drop bytes written there, which
|
|
42
45
|
* their callers rely on, where this module panics.
|
|
43
46
|
*
|
|
44
|
-
* Under Node (`docs/RUN_UNDER_NODE.md`) the `u16` and `u32` functions are
|
|
45
|
-
* expected to answer as they do natively
|
|
47
|
+
* **Under Node (`docs/RUN_UNDER_NODE.md`) the `u16` and `u32` functions are
|
|
48
|
+
* expected to answer as they do natively**, because the top byte of a `u32` is
|
|
46
49
|
* multiplied in rather than shifted and each byte written is masked before
|
|
47
50
|
* its `toU8`: there `<<` turns bit 31 into a sign and the prelude's `toU8`
|
|
48
51
|
* and `toU32` saturate, and natively the multiply and the mask fold into the
|
|
@@ -52,40 +55,81 @@
|
|
|
52
55
|
* 64-bit functions and `f64` meet its BigInt `u64` and `i64`, which `toU8`
|
|
53
56
|
* refuses.
|
|
54
57
|
*
|
|
55
|
-
* The module also has the runs a parser walks and the copy it makes of
|
|
58
|
+
* **The module also has the runs a parser walks and the copy it makes of
|
|
59
|
+
* them.**
|
|
56
60
|
* `skipBlank`, `skipDigits`, `skipJsonText` and `skipJsonPlain` each answer
|
|
57
61
|
* the first index in a range whose byte is not in their class — JSON's blanks,
|
|
58
62
|
* the ASCII digits, the bytes a JSON string holds as themselves, the bytes a
|
|
59
63
|
* JSON writer copies without an escape — and `copyBytes` copies a
|
|
60
64
|
* window of one array into another, or into itself, as one move:
|
|
61
65
|
*
|
|
62
|
-
*
|
|
63
|
-
* const start = skipBlank(text, 0, 5); // 2
|
|
64
|
-
* const end = skipDigits(text, start, 5); // 4
|
|
66
|
+
* import { skipBlank, skipDigits } from "nish/bytes";
|
|
65
67
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
+
* export const main = (): i32 => {
|
|
69
|
+
* const text: u8[] = [32, 32, 52, 50, 44]; // " 42,"
|
|
70
|
+
* const start = skipBlank(text, 0, 5); // 2
|
|
71
|
+
* const end = skipDigits(text, start, 5); // 4
|
|
72
|
+
* return end - start;
|
|
73
|
+
* };
|
|
74
|
+
*
|
|
75
|
+
* **The four runs clamp their range and never panic; `copyBytes` panics on a
|
|
76
|
+
* range outside its array.** Natively each is a runtime kernel
|
|
68
77
|
* (docs/LANGUAGE.md, "Byte runs"), and the runs are variable-time: they are
|
|
69
78
|
* for text, never for secret data.
|
|
70
79
|
*/
|
|
71
80
|
|
|
72
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* The `u16` whose low byte is `b[off]` and high byte `b[off + 1]`.
|
|
83
|
+
*
|
|
84
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
85
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
86
|
+
* `b[off + 1]`, and a byte of those outside `b` panics `index`.
|
|
87
|
+
* @returns The number read.
|
|
88
|
+
*/
|
|
73
89
|
export const getU16LE = (b: u8[], off: i32): u16 => toU16(b[off]) | (toU16(b[off + 1]) << 8)
|
|
74
90
|
|
|
75
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* The `u16` whose high byte is `b[off]` and low byte `b[off + 1]`.
|
|
93
|
+
*
|
|
94
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
95
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
96
|
+
* `b[off + 1]`, and a byte of those outside `b` panics `index`.
|
|
97
|
+
* @returns The number read.
|
|
98
|
+
*/
|
|
76
99
|
export const getU16BE = (b: u8[], off: i32): u16 => (toU16(b[off]) << 8) | toU16(b[off + 1])
|
|
77
100
|
|
|
78
|
-
/**
|
|
101
|
+
/**
|
|
102
|
+
* The `u32` whose bytes, least significant first, are `b[off]` to `b[off + 3]`.
|
|
103
|
+
*
|
|
104
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
105
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
106
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
107
|
+
* @returns The number read.
|
|
108
|
+
*/
|
|
79
109
|
export const getU32LE = (b: u8[], off: i32): u32 =>
|
|
80
110
|
(toU32(b[off]) | (toU32(b[off + 1]) << 8) | (toU32(b[off + 2]) << 16)) +
|
|
81
111
|
toU32(b[off + 3]) * toU32(0x1000000)
|
|
82
112
|
|
|
83
|
-
/**
|
|
113
|
+
/**
|
|
114
|
+
* The `u32` whose bytes, most significant first, are `b[off]` to `b[off + 3]`.
|
|
115
|
+
*
|
|
116
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
117
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
118
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
119
|
+
* @returns The number read.
|
|
120
|
+
*/
|
|
84
121
|
export const getU32BE = (b: u8[], off: i32): u32 =>
|
|
85
122
|
toU32(b[off]) * toU32(0x1000000) +
|
|
86
123
|
((toU32(b[off + 1]) << 16) | (toU32(b[off + 2]) << 8) | toU32(b[off + 3]))
|
|
87
124
|
|
|
88
|
-
/**
|
|
125
|
+
/**
|
|
126
|
+
* The `u64` whose bytes, least significant first, are `b[off]` to `b[off + 7]`.
|
|
127
|
+
*
|
|
128
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
129
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
130
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
131
|
+
* @returns The number read.
|
|
132
|
+
*/
|
|
89
133
|
export const getU64LE = (b: u8[], off: i32): u64 => {
|
|
90
134
|
let v = toU64(0)
|
|
91
135
|
for (let i: i32 = 0; i < 8; i++) {
|
|
@@ -108,6 +152,11 @@ const getU64BEByByte = (b: u8[], off: i32): u64 => {
|
|
|
108
152
|
* The fast path's ors are grouped in pairs, which LLVM folds into one load and
|
|
109
153
|
* a `bswap`; a flat chain of eight `or`s stayed byte by byte. The byte loop is
|
|
110
154
|
* its own function so its last load is not merged with the fast path's.
|
|
155
|
+
*
|
|
156
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
157
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
158
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
159
|
+
* @returns The number read.
|
|
111
160
|
*/
|
|
112
161
|
export const getU64BE = (b: u8[], off: i32): u64 => {
|
|
113
162
|
if (off >= 0 && off < 2147483640 && off + 8 <= toI32(b.length)) {
|
|
@@ -123,37 +172,100 @@ export const getU64BE = (b: u8[], off: i32): u64 => {
|
|
|
123
172
|
return getU64BEByByte(b, off)
|
|
124
173
|
}
|
|
125
174
|
|
|
126
|
-
/**
|
|
175
|
+
/**
|
|
176
|
+
* `getU32LE`'s bits as a two's-complement `i32`.
|
|
177
|
+
*
|
|
178
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
179
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
180
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
181
|
+
* @returns The number read, from -2^31 to 2^31 - 1.
|
|
182
|
+
*/
|
|
127
183
|
export const getI32LE = (b: u8[], off: i32): i32 => toI32(getU32LE(b, off))
|
|
128
184
|
|
|
129
|
-
/**
|
|
185
|
+
/**
|
|
186
|
+
* `getU32BE`'s bits as a two's-complement `i32`.
|
|
187
|
+
*
|
|
188
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
189
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
190
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
191
|
+
* @returns The number read, from -2^31 to 2^31 - 1.
|
|
192
|
+
*/
|
|
130
193
|
export const getI32BE = (b: u8[], off: i32): i32 => toI32(getU32BE(b, off))
|
|
131
194
|
|
|
132
|
-
/**
|
|
195
|
+
/**
|
|
196
|
+
* `getU64LE`'s bits as a two's-complement `i64`.
|
|
197
|
+
*
|
|
198
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
199
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
200
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
201
|
+
* @returns The number read, from -2^63 to 2^63 - 1.
|
|
202
|
+
*/
|
|
133
203
|
export const getI64LE = (b: u8[], off: i32): i64 => toI64(getU64LE(b, off))
|
|
134
204
|
|
|
135
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* `getU64BE`'s bits as a two's-complement `i64`.
|
|
207
|
+
*
|
|
208
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
209
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
210
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
211
|
+
* @returns The number read, from -2^63 to 2^63 - 1.
|
|
212
|
+
*/
|
|
136
213
|
export const getI64BE = (b: u8[], off: i32): i64 => toI64(getU64BE(b, off))
|
|
137
214
|
|
|
138
|
-
/**
|
|
215
|
+
/**
|
|
216
|
+
* The `f64` whose IEEE 754 bits are `getU64LE`'s.
|
|
217
|
+
*
|
|
218
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
219
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
220
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
221
|
+
* @returns The double whose bits the eight bytes are.
|
|
222
|
+
*/
|
|
139
223
|
export const getF64LE = (b: u8[], off: i32): f64 => bitsToF64(toI64(getU64LE(b, off)))
|
|
140
224
|
|
|
141
|
-
/**
|
|
225
|
+
/**
|
|
226
|
+
* The `f64` whose IEEE 754 bits are `getU64BE`'s.
|
|
227
|
+
*
|
|
228
|
+
* @param b The bytes the number is read from; they are not changed.
|
|
229
|
+
* @param off Where in `b` its first byte is; it covers `b[off]` to
|
|
230
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
231
|
+
* @returns The double whose bits the eight bytes are.
|
|
232
|
+
*/
|
|
142
233
|
export const getF64BE = (b: u8[], off: i32): f64 => bitsToF64(toI64(getU64BE(b, off)))
|
|
143
234
|
|
|
144
|
-
/**
|
|
235
|
+
/**
|
|
236
|
+
* Writes `v` into `b[off]` and `b[off + 1]`, low byte first.
|
|
237
|
+
*
|
|
238
|
+
* @param b The bytes written into; only the two at `off` change.
|
|
239
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
240
|
+
* `b[off + 1]`, and a byte of those outside `b` panics `index`.
|
|
241
|
+
* @param v The number to store.
|
|
242
|
+
*/
|
|
145
243
|
export const setU16LE = (b: u8[], off: i32, v: u16): void => {
|
|
146
244
|
b[off] = toU8(v & 255)
|
|
147
245
|
b[off + 1] = toU8((v >> 8) & 255)
|
|
148
246
|
}
|
|
149
247
|
|
|
150
|
-
/**
|
|
248
|
+
/**
|
|
249
|
+
* Writes `v` into `b[off]` and `b[off + 1]`, high byte first.
|
|
250
|
+
*
|
|
251
|
+
* @param b The bytes written into; only the two at `off` change.
|
|
252
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
253
|
+
* `b[off + 1]`, and a byte of those outside `b` panics `index`.
|
|
254
|
+
* @param v The number to store.
|
|
255
|
+
*/
|
|
151
256
|
export const setU16BE = (b: u8[], off: i32, v: u16): void => {
|
|
152
257
|
b[off] = toU8((v >> 8) & 255)
|
|
153
258
|
b[off + 1] = toU8(v & 255)
|
|
154
259
|
}
|
|
155
260
|
|
|
156
|
-
/**
|
|
261
|
+
/**
|
|
262
|
+
* Writes `v` into `b[off]` to `b[off + 3]`, least significant byte first.
|
|
263
|
+
*
|
|
264
|
+
* @param b The bytes written into; only the four at `off` change.
|
|
265
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
266
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
267
|
+
* @param v The number to store.
|
|
268
|
+
*/
|
|
157
269
|
export const setU32LE = (b: u8[], off: i32, v: u32): void => {
|
|
158
270
|
b[off] = toU8(v & 255)
|
|
159
271
|
b[off + 1] = toU8((v >> 8) & 255)
|
|
@@ -161,7 +273,14 @@ export const setU32LE = (b: u8[], off: i32, v: u32): void => {
|
|
|
161
273
|
b[off + 3] = toU8((v >> 24) & 255)
|
|
162
274
|
}
|
|
163
275
|
|
|
164
|
-
/**
|
|
276
|
+
/**
|
|
277
|
+
* Writes `v` into `b[off]` to `b[off + 3]`, most significant byte first.
|
|
278
|
+
*
|
|
279
|
+
* @param b The bytes written into; only the four at `off` change.
|
|
280
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
281
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
282
|
+
* @param v The number to store.
|
|
283
|
+
*/
|
|
165
284
|
export const setU32BE = (b: u8[], off: i32, v: u32): void => {
|
|
166
285
|
b[off] = toU8((v >> 24) & 255)
|
|
167
286
|
b[off + 1] = toU8((v >> 16) & 255)
|
|
@@ -169,36 +288,96 @@ export const setU32BE = (b: u8[], off: i32, v: u32): void => {
|
|
|
169
288
|
b[off + 3] = toU8(v & 255)
|
|
170
289
|
}
|
|
171
290
|
|
|
172
|
-
/**
|
|
291
|
+
/**
|
|
292
|
+
* Writes `v` into `b[off]` to `b[off + 7]`, least significant byte first.
|
|
293
|
+
*
|
|
294
|
+
* @param b The bytes written into; only the eight at `off` change.
|
|
295
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
296
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
297
|
+
* @param v The number to store.
|
|
298
|
+
*/
|
|
173
299
|
export const setU64LE = (b: u8[], off: i32, v: u64): void => {
|
|
174
300
|
for (let i: i32 = 0; i < 8; i++) {
|
|
175
301
|
b[off + i] = toU8((v >> toU64(i * 8)) & toU64(255))
|
|
176
302
|
}
|
|
177
303
|
}
|
|
178
304
|
|
|
179
|
-
/**
|
|
305
|
+
/**
|
|
306
|
+
* Writes `v` into `b[off]` to `b[off + 7]`, most significant byte first.
|
|
307
|
+
*
|
|
308
|
+
* @param b The bytes written into; only the eight at `off` change.
|
|
309
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
310
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
311
|
+
* @param v The number to store.
|
|
312
|
+
*/
|
|
180
313
|
export const setU64BE = (b: u8[], off: i32, v: u64): void => {
|
|
181
314
|
for (let i: i32 = 0; i < 8; i++) {
|
|
182
315
|
b[off + i] = toU8((v >> toU64((7 - i) * 8)) & toU64(255))
|
|
183
316
|
}
|
|
184
317
|
}
|
|
185
318
|
|
|
186
|
-
/**
|
|
319
|
+
/**
|
|
320
|
+
* Writes `v`'s two's-complement bits as `setU32LE` does.
|
|
321
|
+
*
|
|
322
|
+
* @param b The bytes written into; only the four at `off` change.
|
|
323
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
324
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
325
|
+
* @param v The number to store; a negative one is stored as its two's
|
|
326
|
+
* complement.
|
|
327
|
+
*/
|
|
187
328
|
export const setI32LE = (b: u8[], off: i32, v: i32): void => setU32LE(b, off, toU32(v))
|
|
188
329
|
|
|
189
|
-
/**
|
|
330
|
+
/**
|
|
331
|
+
* Writes `v`'s two's-complement bits as `setU32BE` does.
|
|
332
|
+
*
|
|
333
|
+
* @param b The bytes written into; only the four at `off` change.
|
|
334
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
335
|
+
* `b[off + 3]`, and a byte of those outside `b` panics `index`.
|
|
336
|
+
* @param v The number to store; a negative one is stored as its two's
|
|
337
|
+
* complement.
|
|
338
|
+
*/
|
|
190
339
|
export const setI32BE = (b: u8[], off: i32, v: i32): void => setU32BE(b, off, toU32(v))
|
|
191
340
|
|
|
192
|
-
/**
|
|
341
|
+
/**
|
|
342
|
+
* Writes `v`'s two's-complement bits as `setU64LE` does.
|
|
343
|
+
*
|
|
344
|
+
* @param b The bytes written into; only the eight at `off` change.
|
|
345
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
346
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
347
|
+
* @param v The number to store; a negative one is stored as its two's
|
|
348
|
+
* complement.
|
|
349
|
+
*/
|
|
193
350
|
export const setI64LE = (b: u8[], off: i32, v: i64): void => setU64LE(b, off, toU64(v))
|
|
194
351
|
|
|
195
|
-
/**
|
|
352
|
+
/**
|
|
353
|
+
* Writes `v`'s two's-complement bits as `setU64BE` does.
|
|
354
|
+
*
|
|
355
|
+
* @param b The bytes written into; only the eight at `off` change.
|
|
356
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
357
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
358
|
+
* @param v The number to store; a negative one is stored as its two's
|
|
359
|
+
* complement.
|
|
360
|
+
*/
|
|
196
361
|
export const setI64BE = (b: u8[], off: i32, v: i64): void => setU64BE(b, off, toU64(v))
|
|
197
362
|
|
|
198
|
-
/**
|
|
363
|
+
/**
|
|
364
|
+
* Writes `v`'s IEEE 754 bits as `setU64LE` does.
|
|
365
|
+
*
|
|
366
|
+
* @param b The bytes written into; only the eight at `off` change.
|
|
367
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
368
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
369
|
+
* @param v The double to store, as its IEEE 754 bits.
|
|
370
|
+
*/
|
|
199
371
|
export const setF64LE = (b: u8[], off: i32, v: f64): void => setU64LE(b, off, toU64(f64ToBits(v)))
|
|
200
372
|
|
|
201
|
-
/**
|
|
373
|
+
/**
|
|
374
|
+
* Writes `v`'s IEEE 754 bits as `setU64BE` does.
|
|
375
|
+
*
|
|
376
|
+
* @param b The bytes written into; only the eight at `off` change.
|
|
377
|
+
* @param off Where in `b` the first byte goes; it covers `b[off]` to
|
|
378
|
+
* `b[off + 7]`, and a byte of those outside `b` panics `index`.
|
|
379
|
+
* @param v The double to store, as its IEEE 754 bits.
|
|
380
|
+
*/
|
|
202
381
|
export const setF64BE = (b: u8[], off: i32, v: f64): void => setU64BE(b, off, toU64(f64ToBits(v)))
|
|
203
382
|
|
|
204
383
|
// ---- Byte-class runs and the byte copy (WP38 S6a) ----------------------------------------
|
|
@@ -245,13 +424,30 @@ const bytesRunStart = (from: i32, end: i32): i32 => {
|
|
|
245
424
|
* or 32 bytes at a time (docs/LANGUAGE.md, "Byte runs"). It is
|
|
246
425
|
* **variable-time**: how long it takes says how long the run is, so it is
|
|
247
426
|
* never for secret data.
|
|
427
|
+
*
|
|
428
|
+
* @param b The bytes scanned; they are not changed.
|
|
429
|
+
* @param from Where the scan starts, clamped into `[0, to]`.
|
|
430
|
+
* @param to Where the scan stops, exclusive, clamped into `[0, b.length]`.
|
|
431
|
+
* @returns The index of the first byte in the clamped range that is not a
|
|
432
|
+
* blank, or
|
|
433
|
+
* the clamped `to` when there is none.
|
|
248
434
|
*/
|
|
249
435
|
export const skipBlank = (b: u8[], from: i32, to: i32): i32 => {
|
|
250
436
|
const end: i32 = bytesRunEnd(b, to)
|
|
251
437
|
return skipBlankWalk(b, bytesRunStart(from, end), end)
|
|
252
438
|
}
|
|
253
439
|
|
|
254
|
-
/**
|
|
440
|
+
/**
|
|
441
|
+
* `skipBlank` for the ASCII digits `0` to `9`: the same clamps, the same
|
|
442
|
+
* kernel family, `nish_bytes_skip_digits`.
|
|
443
|
+
*
|
|
444
|
+
* @param b The bytes scanned; they are not changed.
|
|
445
|
+
* @param from Where the scan starts, clamped into `[0, to]`.
|
|
446
|
+
* @param to Where the scan stops, exclusive, clamped into `[0, b.length]`.
|
|
447
|
+
* @returns The index of the first byte in the clamped range that is not a
|
|
448
|
+
* digit, or
|
|
449
|
+
* the clamped `to` when there is none.
|
|
450
|
+
*/
|
|
255
451
|
export const skipDigits = (b: u8[], from: i32, to: i32): i32 => {
|
|
256
452
|
const end: i32 = bytesRunEnd(b, to)
|
|
257
453
|
return skipDigitsWalk(b, bytesRunStart(from, end), end)
|
|
@@ -268,6 +464,13 @@ export const skipDigits = (b: u8[], from: i32, to: i32): i32 => {
|
|
|
268
464
|
* the scan is `nish_bytes_skip_json_text`: blocks of ASCII 16 or 32 bytes at a
|
|
269
465
|
* time, each multi-byte sequence a byte at a time. Variable-time, as
|
|
270
466
|
* `skipBlank` is.
|
|
467
|
+
*
|
|
468
|
+
* @param b The bytes scanned; they are not changed.
|
|
469
|
+
* @param from Where the scan starts, clamped into `[0, to]`.
|
|
470
|
+
* @param to Where the scan stops, exclusive, clamped into `[0, b.length]`.
|
|
471
|
+
* @returns The index of the first byte in the clamped range that is not in
|
|
472
|
+
* the run, or
|
|
473
|
+
* the clamped `to` when there is none.
|
|
271
474
|
*/
|
|
272
475
|
export const skipJsonText = (b: u8[], from: i32, to: i32): i32 => {
|
|
273
476
|
const end: i32 = bytesRunEnd(b, to)
|
|
@@ -283,6 +486,13 @@ export const skipJsonText = (b: u8[], from: i32, to: i32): i32 => {
|
|
|
283
486
|
* the bytes of a string through as they are. Natively the scan is
|
|
284
487
|
* `nish_bytes_skip_json_plain`, 16 bytes at a time. Variable-time, as
|
|
285
488
|
* `skipBlank` is.
|
|
489
|
+
*
|
|
490
|
+
* @param b The bytes scanned; they are not changed.
|
|
491
|
+
* @param from Where the scan starts, clamped into `[0, to]`.
|
|
492
|
+
* @param to Where the scan stops, exclusive, clamped into `[0, b.length]`.
|
|
493
|
+
* @returns The index of the first byte in the clamped range that a JSON
|
|
494
|
+
* writer must escape, or
|
|
495
|
+
* the clamped `to` when there is none.
|
|
286
496
|
*/
|
|
287
497
|
export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
|
|
288
498
|
const end: i32 = bytesRunEnd(b, to)
|
|
@@ -300,8 +510,14 @@ export const skipJsonPlain = (b: u8[], from: i32, to: i32): i32 => {
|
|
|
300
510
|
* `0 <= from <= from + count <= src.length` and the same for `at` in `dst` —
|
|
301
511
|
* panics `copyBytes` before any byte is written. Natively the copy is one
|
|
302
512
|
* `llvm.memmove`.
|
|
513
|
+
*
|
|
514
|
+
* @param dst The array written into; only `dst[at..at+count)` changes.
|
|
515
|
+
* @param at Where in `dst` the first copied byte goes.
|
|
516
|
+
* @param src The array copied from, which may be `dst` itself.
|
|
517
|
+
* @param from Where in `src` the copied bytes start.
|
|
518
|
+
* @param count How many bytes to copy; zero copies nothing.
|
|
303
519
|
*/
|
|
304
|
-
export const copyBytes = (dst: u8[], at: i32, src: u8[], from: i32, count: i32): void => {
|
|
520
|
+
export const copyBytes = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
|
|
305
521
|
if (count < 0 || at < 0 || from < 0 || at > toI32(dst.length) - count || from > toI32(src.length) - count) {
|
|
306
522
|
panic("copyBytes: the range lies outside an array")
|
|
307
523
|
}
|
|
@@ -425,7 +641,7 @@ const skipJsonPlainWalk = (b: u8[], from: i32, to: i32): i32 => {
|
|
|
425
641
|
}
|
|
426
642
|
|
|
427
643
|
/** `copyBytes`'s walk, once its ranges are checked: forward, or backward when `dst` is `src` and the bytes move up. */
|
|
428
|
-
const copyBytesWalk = (dst: u8[], at: i32, src: u8[], from: i32, count: i32): void => {
|
|
644
|
+
const copyBytesWalk = (dst: u8[], at: i32, src: readonly u8[], from: i32, count: i32): void => {
|
|
429
645
|
if (at <= from) {
|
|
430
646
|
let k: i32 = 0
|
|
431
647
|
while (k < count) {
|