functionalscript 0.9.2 → 0.10.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/bnf/data/test.f.d.ts +2 -0
- package/bnf/data/test.f.js +39 -2
- package/bnf/module.f.d.ts +3 -2
- package/bnf/module.f.js +5 -4
- package/cas/module.f.d.ts +5 -0
- package/cas/module.f.js +5 -0
- package/ci/module.f.js +45 -35
- package/ci/module.js +3 -3
- package/crypto/hmac/test.f.js +1 -1
- package/crypto/sign/module.f.d.ts +5 -0
- package/crypto/sign/test.f.js +1 -1
- package/dev/index/module.js +2 -2
- package/dev/module.f.d.ts +5 -0
- package/dev/module.f.js +5 -0
- package/dev/tf/all.test.js +2 -2
- package/dev/tf/module.f.js +7 -2
- package/dev/version/module.f.js +5 -0
- package/djs/ast/module.f.d.ts +5 -0
- package/djs/module.f.d.ts +5 -0
- package/djs/parser/module.f.d.ts +5 -0
- package/djs/parser/module.f.js +5 -0
- package/djs/parser-new/module.f.js +6 -2
- package/djs/parser-new/test.f.js +2 -1
- package/djs/serializer/module.f.d.ts +5 -0
- package/djs/transpiler/module.f.d.ts +5 -0
- package/djs/transpiler/module.f.js +5 -0
- package/fjs/module.f.d.ts +5 -0
- package/fjs/module.f.js +5 -0
- package/fjs/module.js +2 -2
- package/fsc/module.f.js +5 -0
- package/fsm/module.f.d.ts +5 -0
- package/fsm/module.f.js +5 -0
- package/io/module.d.ts +3 -3
- package/io/module.f.d.ts +1 -1
- package/io/module.f.js +8 -1
- package/io/module.js +8 -7
- package/io/virtual/module.f.d.ts +5 -0
- package/json/parser/module.f.d.ts +5 -0
- package/json/parser/module.f.js +5 -0
- package/json/serializer/module.f.d.ts +5 -0
- package/json/serializer/module.f.js +5 -0
- package/json/tokenizer/module.f.js +5 -0
- package/package.json +1 -1
- package/path/module.f.d.ts +5 -0
- package/text/utf8/module.f.d.ts +1 -1
- package/types/asn.1/module.f.d.ts +62 -0
- package/types/asn.1/module.f.js +276 -0
- package/types/asn.1/test.f.d.ts +44 -0
- package/types/asn.1/test.f.js +312 -0
- package/types/base128/module.f.d.ts +20 -0
- package/types/base128/module.f.js +43 -0
- package/types/base128/test.f.d.ts +2 -0
- package/types/base128/test.f.js +26 -0
- package/types/bigfloat/module.f.js +5 -0
- package/types/bit_vec/module.f.d.ts +14 -3
- package/types/bit_vec/module.f.js +21 -2
- package/types/bit_vec/test.f.d.ts +1 -0
- package/types/bit_vec/test.f.js +28 -6
- package/types/btree/find/module.f.d.ts +5 -0
- package/types/btree/module.f.d.ts +5 -0
- package/types/btree/module.f.js +5 -0
- package/types/btree/remove/module.f.d.ts +5 -0
- package/types/btree/set/module.f.d.ts +5 -0
- package/types/btree/types/module.f.d.ts +5 -0
- package/types/byte_set/module.f.js +5 -0
- package/types/cbase32/module.f.d.ts +5 -0
- package/types/cbase32/module.f.js +5 -0
- package/types/effect/mock/module.f.d.ts +5 -0
- package/types/effect/module.f.d.ts +5 -0
- package/types/effect/node/module.f.d.ts +5 -1
- package/types/effect/node/module.f.js +1 -0
- package/types/effect/node/test.f.d.ts +1 -0
- package/types/effect/node/test.f.js +19 -1
- package/types/effect/node/virtual/module.f.d.ts +3 -0
- package/types/effect/node/virtual/module.f.js +10 -0
- package/types/function/compare/module.f.d.ts +5 -0
- package/types/function/operator/module.f.d.ts +5 -0
- package/types/list/module.f.js +5 -0
- package/types/map/module.f.js +5 -0
- package/types/nibble_set/module.f.d.ts +5 -0
- package/types/nominal/module.f.d.ts +5 -0
- package/types/nullable/module.f.d.ts +5 -0
- package/types/ordered_map/module.f.d.ts +5 -0
- package/types/range/module.f.d.ts +5 -0
- package/types/sorted_list/module.f.d.ts +5 -0
- package/website/module.f.js +5 -0
- package/website/module.js +3 -3
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { empty, length, listToVec, msb, uint, unpack, vec, vec8 } from "../bit_vec/module.f.js";
|
|
2
|
+
import { asBase } from "../nominal/module.f.js";
|
|
3
|
+
import { decodeRaw, decodeInteger, encodeRaw, encodeInteger, integer, encode, decode, constructedSequence, octetString, boolean, constructedSet } from "./module.f.js";
|
|
4
|
+
const { concat, popFront: pop } = msb;
|
|
5
|
+
const cat = listToVec(msb);
|
|
6
|
+
const pop8 = pop(8n);
|
|
7
|
+
const check = (tag, v, rest) => {
|
|
8
|
+
const s = encodeRaw([tag, v]);
|
|
9
|
+
const [[t0, v0], r] = decodeRaw(concat(s)(rest));
|
|
10
|
+
if (t0 !== tag) {
|
|
11
|
+
throw `t0: ${t0}`;
|
|
12
|
+
}
|
|
13
|
+
if (v0 !== v) {
|
|
14
|
+
throw `v0: ${asBase(v0)}`;
|
|
15
|
+
}
|
|
16
|
+
if (r !== rest) {
|
|
17
|
+
throw `r: ${asBase(r)}`;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const integerValueCheck = (i, v) => {
|
|
21
|
+
const v0 = encodeInteger(i);
|
|
22
|
+
if (v !== v0) {
|
|
23
|
+
throw `encode: ${asBase(v)}, ${asBase(v0)}`;
|
|
24
|
+
}
|
|
25
|
+
const i0 = decodeInteger(v);
|
|
26
|
+
if (i !== i0) {
|
|
27
|
+
throw [i, i0];
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const ch0 = (r, v, rest) => {
|
|
31
|
+
const [r0, rest0] = decode(concat(v)(rest));
|
|
32
|
+
if (rest0 !== rest) {
|
|
33
|
+
throw `rest: ${asBase(rest0)}`;
|
|
34
|
+
}
|
|
35
|
+
const v0 = encode(r);
|
|
36
|
+
const v1 = encode(r0);
|
|
37
|
+
if (v !== v0) {
|
|
38
|
+
throw `encode: ${asBase(v)}, ${asBase(v0)}`;
|
|
39
|
+
}
|
|
40
|
+
if (v !== v1) {
|
|
41
|
+
throw `encode: ${asBase(v)}, ${asBase(v1)}`;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const ch = (r, v) => {
|
|
45
|
+
ch0(r, v, empty);
|
|
46
|
+
ch0(r, v, vec8(0x23n));
|
|
47
|
+
ch0(r, v, vec(16n)(0x2345n));
|
|
48
|
+
};
|
|
49
|
+
export default {
|
|
50
|
+
encodeSmall: () => {
|
|
51
|
+
const v = vec8(0x13n);
|
|
52
|
+
const x = encodeRaw([integer, v]);
|
|
53
|
+
const lx = length(x);
|
|
54
|
+
if (lx !== 24n) {
|
|
55
|
+
throw lx;
|
|
56
|
+
}
|
|
57
|
+
const [tag, x1] = pop8(x);
|
|
58
|
+
if (tag !== BigInt(integer)) {
|
|
59
|
+
throw tag;
|
|
60
|
+
}
|
|
61
|
+
const [len, x2] = pop8(x1);
|
|
62
|
+
if (len !== 1n) {
|
|
63
|
+
throw len;
|
|
64
|
+
}
|
|
65
|
+
const { length: intLen, uint } = unpack(x2);
|
|
66
|
+
if (intLen !== 8n) {
|
|
67
|
+
throw intLen;
|
|
68
|
+
}
|
|
69
|
+
if (uint !== 0x13n) {
|
|
70
|
+
throw uint;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
encode7F: () => {
|
|
74
|
+
const valueLen = 0x7fn << 3n;
|
|
75
|
+
const value = 0x1234n;
|
|
76
|
+
const v = vec(valueLen)(value);
|
|
77
|
+
const x = encodeRaw([integer, v]);
|
|
78
|
+
const lx = length(x);
|
|
79
|
+
if (lx !== 0x81n << 3n) {
|
|
80
|
+
throw lx;
|
|
81
|
+
}
|
|
82
|
+
const [tag, x1] = pop8(x);
|
|
83
|
+
if (tag !== BigInt(integer)) {
|
|
84
|
+
throw tag;
|
|
85
|
+
}
|
|
86
|
+
const [len, x2] = pop8(x1);
|
|
87
|
+
if (len !== 0x7fn) {
|
|
88
|
+
throw len;
|
|
89
|
+
}
|
|
90
|
+
const { length: intLen, uint } = unpack(x2);
|
|
91
|
+
if (intLen !== valueLen) {
|
|
92
|
+
throw intLen;
|
|
93
|
+
}
|
|
94
|
+
if (uint !== value) {
|
|
95
|
+
throw uint;
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
encode80: () => {
|
|
99
|
+
const valueLen = 0x80n << 3n;
|
|
100
|
+
const value = 0x123456n;
|
|
101
|
+
const v = vec(valueLen)(value);
|
|
102
|
+
const x = encodeRaw([integer, v]);
|
|
103
|
+
const lx = length(x);
|
|
104
|
+
if (lx !== 0x83n << 3n) {
|
|
105
|
+
throw lx;
|
|
106
|
+
}
|
|
107
|
+
const [tag, x1] = pop8(x);
|
|
108
|
+
if (tag !== BigInt(integer)) {
|
|
109
|
+
throw tag;
|
|
110
|
+
}
|
|
111
|
+
const [lenLen, x2] = pop8(x1);
|
|
112
|
+
if (lenLen !== 0x81n) {
|
|
113
|
+
throw lenLen;
|
|
114
|
+
}
|
|
115
|
+
const [len, x3] = pop8(x2);
|
|
116
|
+
if (len !== 0x80n) {
|
|
117
|
+
throw len;
|
|
118
|
+
}
|
|
119
|
+
const { length: intLen, uint } = unpack(x3);
|
|
120
|
+
if (intLen !== valueLen) {
|
|
121
|
+
throw intLen;
|
|
122
|
+
}
|
|
123
|
+
if (uint !== value) {
|
|
124
|
+
throw uint;
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
encodeFF: () => {
|
|
128
|
+
const valueLen = 0xffn << 3n;
|
|
129
|
+
const value = 0x123456n;
|
|
130
|
+
const v = vec(valueLen)(value);
|
|
131
|
+
const x = encodeRaw([integer, v]);
|
|
132
|
+
const lx = length(x);
|
|
133
|
+
if (lx !== valueLen + (3n << 3n)) {
|
|
134
|
+
throw `lx: ${lx}`;
|
|
135
|
+
}
|
|
136
|
+
const [tag, x1] = pop8(x);
|
|
137
|
+
if (tag !== BigInt(integer)) {
|
|
138
|
+
throw tag;
|
|
139
|
+
}
|
|
140
|
+
const [lenLen, x2] = pop8(x1);
|
|
141
|
+
if (lenLen !== 0x81n) {
|
|
142
|
+
throw lenLen;
|
|
143
|
+
}
|
|
144
|
+
const [len, x3] = pop8(x2);
|
|
145
|
+
if (len !== 0xffn) {
|
|
146
|
+
throw len;
|
|
147
|
+
}
|
|
148
|
+
const { length: intLen, uint } = unpack(x3);
|
|
149
|
+
if (intLen !== valueLen) {
|
|
150
|
+
throw intLen;
|
|
151
|
+
}
|
|
152
|
+
if (uint !== value) {
|
|
153
|
+
throw uint;
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
encode103: () => {
|
|
157
|
+
const valueLen = 0x103n << 3n;
|
|
158
|
+
const value = 0x123456n;
|
|
159
|
+
const v = vec(valueLen)(value);
|
|
160
|
+
const x = encodeRaw([integer, v]);
|
|
161
|
+
const lx = length(x);
|
|
162
|
+
if (lx !== valueLen + (4n << 3n)) {
|
|
163
|
+
throw `lx: ${lx}`;
|
|
164
|
+
}
|
|
165
|
+
const [tag, x1] = pop8(x);
|
|
166
|
+
if (tag !== BigInt(integer)) {
|
|
167
|
+
throw tag;
|
|
168
|
+
}
|
|
169
|
+
const [lenLen, x2] = pop8(x1);
|
|
170
|
+
if (lenLen !== 0x82n) {
|
|
171
|
+
throw lenLen;
|
|
172
|
+
}
|
|
173
|
+
const [len, x3] = pop(16n)(x2);
|
|
174
|
+
if (len !== 0x103n) {
|
|
175
|
+
throw len;
|
|
176
|
+
}
|
|
177
|
+
const { length: intLen, uint } = unpack(x3);
|
|
178
|
+
if (intLen !== valueLen) {
|
|
179
|
+
throw intLen;
|
|
180
|
+
}
|
|
181
|
+
if (uint !== value) {
|
|
182
|
+
throw uint;
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
ed: [
|
|
186
|
+
() => {
|
|
187
|
+
const tag = integer;
|
|
188
|
+
const v = vec(0x10n)(0x8234n);
|
|
189
|
+
if (asBase(v) !== 0x8234n) {
|
|
190
|
+
throw asBase(v).toString(16);
|
|
191
|
+
}
|
|
192
|
+
check(integer, v, empty);
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
x80: () => check(integer, vec(0x80n)(0x8234n), empty),
|
|
196
|
+
x100: () => check(integer, vec(0x100n)(0x8234n), vec8(0x23n)),
|
|
197
|
+
x1000: () => check(integer, vec(0x1000n)(0x8234n), vec8(0x23n)),
|
|
198
|
+
x10000: () => check(integer, vec(0x10000n)(0x8234n), vec8(0x23n)),
|
|
199
|
+
x20000: () => check(integer, vec(0x20000n)(0x8234n), vec8(0x23n)),
|
|
200
|
+
x40000: () => check(integer, vec(0x40000n)(0x8234n), empty),
|
|
201
|
+
x80000: () => check(integer, vec(0x80000n)(0x8234n), empty),
|
|
202
|
+
xC0000: () => check(integer, vec(0xc0000n)(0x8234n), empty),
|
|
203
|
+
xE0000: () => check(integer, vec(0xe0000n)(0x8234n), empty),
|
|
204
|
+
xF0000: () => check(integer, vec(0xf0000n)(0x8234n), empty),
|
|
205
|
+
xFFF00: () => check(integer, vec(0xfff00n)(0x8234n), empty),
|
|
206
|
+
xFFF80: () => check(integer, vec(0xfff80n)(0x8234n), empty),
|
|
207
|
+
xFFFC0: () => check(integer, vec(0xfffc0n)(0x8234n), empty),
|
|
208
|
+
xFFFD0: () => check(integer, vec(0xfffd0n)(0x8234n), empty),
|
|
209
|
+
//// fail on Bun because it has a smaller limit for BigInt
|
|
210
|
+
//xFFFD8: () => check(integer, vec(0xF_FFD8n)(0x8234n), empty),
|
|
211
|
+
//xFFFE0: () => check(integer, vec(0xF_FFE0n)(0x8234n), empty),
|
|
212
|
+
//e100000: () => check(integer, vec(0x10_0000n)(0x8234n), empty),
|
|
213
|
+
//x100000: () => check(integer, vec(0x10_0000n)(0x8234n), vec8(0x23n)),
|
|
214
|
+
//x1000000: () => check(integer, vec(0x100_0000n)(0x8234n), vec8(0x23n)),
|
|
215
|
+
//x10000000: () => check(integer, vec(0x1000_0000n)(0x8234n), vec8(0x23n)),
|
|
216
|
+
//x20000000: () => check(integer, vec(0x2000_0000n)(0x8234n), vec8(0x23n)),
|
|
217
|
+
//x30000000: () => check(integer, vec(0x3000_0000n)(0x8234n), vec8(0x23n)),
|
|
218
|
+
//x38000000: () => check(integer, vec(0x3800_0000n)(0x8234n), vec8(0x23n)),
|
|
219
|
+
//x3C000000: () => check(integer, vec(0x3800_0000n)(0x8234n), vec8(0x23n)),
|
|
220
|
+
//x3E000000: () => check(integer, vec(0x3E00_0000n)(0x8234n), vec8(0x23n)),
|
|
221
|
+
//x3F000000: () => check(integer, vec(0x3F00_0000n)(0x8234n), vec8(0x23n)),
|
|
222
|
+
//x3F800000: () => check(integer, vec(0x3F80_0000n)(0x8234n), vec8(0x23n)),
|
|
223
|
+
//x3FFFFFC0: () => check(integer, vec(0x3FFF_FFC0n)(0x8234n), vec8(0x23n)),
|
|
224
|
+
//e3FFFFFC8: () => check(integer, vec(0x3FFF_FFC8n)(0x8234n), empty),
|
|
225
|
+
//// fail on Node
|
|
226
|
+
//x3FFFFFC8: () => check(integer, vec(0x3FFF_FFC1n)(0x8234n), vec8(0x23n)),
|
|
227
|
+
//x40000000: () => check(integer, vec(0x4000_0000n)(0x8234n), vec8(0x23n)),
|
|
228
|
+
// check(integer, vec(0x1_0000_0000n)(0x8234n), vec8(0x23n))
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
integerValue: {
|
|
232
|
+
zero: () => integerValueCheck(0n, vec8(0n)),
|
|
233
|
+
one: () => integerValueCheck(1n, vec8(1n)),
|
|
234
|
+
minusOne: () => integerValueCheck(-1n, vec8(0xffn)),
|
|
235
|
+
x7F: () => integerValueCheck(0x7fn, vec8(0x7fn)),
|
|
236
|
+
x80: () => integerValueCheck(0x80n, vec(16n)(0x80n)),
|
|
237
|
+
xFF: () => integerValueCheck(0xffn, vec(16n)(0xffn)),
|
|
238
|
+
nx7F: () => integerValueCheck(-0x7fn, vec8(0x81n)),
|
|
239
|
+
nx80: () => integerValueCheck(-0x80n, vec8(0x80n)),
|
|
240
|
+
nx81: () => integerValueCheck(-0x81n, vec(16n)(0xff7fn)),
|
|
241
|
+
nx7FFF: () => integerValueCheck(-0x7fffn, vec(16n)(0x8001n)),
|
|
242
|
+
nx8000: () => integerValueCheck(-0x8000n, vec(16n)(0x8000n)),
|
|
243
|
+
nx8001: () => integerValueCheck(-0x8001n, vec(24n)(0xff7fffn)),
|
|
244
|
+
},
|
|
245
|
+
encodeDecode: {
|
|
246
|
+
integer: () => {
|
|
247
|
+
ch([integer, 0n], cat([vec8(BigInt(integer)), vec8(1n), vec8(0n)]));
|
|
248
|
+
ch([integer, 1n], cat([vec8(BigInt(integer)), vec8(1n), vec8(1n)]));
|
|
249
|
+
},
|
|
250
|
+
sequence: () => {
|
|
251
|
+
ch([constructedSequence, []], cat([vec8(BigInt(constructedSequence)), vec8(0n)]));
|
|
252
|
+
ch([constructedSequence, [[integer, 0n]]], cat([vec8(BigInt(constructedSequence)), vec8(3n), encode([integer, 0n])]));
|
|
253
|
+
ch([constructedSequence, [[integer, 1n], [integer, 2n]]], cat([
|
|
254
|
+
vec8(BigInt(constructedSequence)),
|
|
255
|
+
vec8(6n),
|
|
256
|
+
encode([integer, 1n]),
|
|
257
|
+
encode([integer, 2n])
|
|
258
|
+
]));
|
|
259
|
+
ch([constructedSequence, [[octetString, vec8(0x23n)], [boolean, true], [boolean, false]]], cat([
|
|
260
|
+
vec8(BigInt(constructedSequence)),
|
|
261
|
+
vec8(9n),
|
|
262
|
+
encode([octetString, vec8(0x23n)]),
|
|
263
|
+
encode([boolean, true]),
|
|
264
|
+
encode([boolean, false]),
|
|
265
|
+
]));
|
|
266
|
+
},
|
|
267
|
+
set: () => {
|
|
268
|
+
ch([constructedSet, [[integer, 2n], [integer, 1n]]], cat([
|
|
269
|
+
vec8(BigInt(constructedSet)),
|
|
270
|
+
vec8(6n),
|
|
271
|
+
encode([integer, 1n]),
|
|
272
|
+
encode([integer, 2n])
|
|
273
|
+
]));
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
raw: [
|
|
277
|
+
() => {
|
|
278
|
+
const e = encodeRaw([0x00n, vec8(0x23n)]);
|
|
279
|
+
if (e !== cat([vec8(0x00n), vec8(1n), vec8(0x23n)])) {
|
|
280
|
+
throw `encode: ${length(e)}: ${uint(e).toString(2)}`;
|
|
281
|
+
}
|
|
282
|
+
const [[tag, value], rest] = decodeRaw(e);
|
|
283
|
+
if (rest !== empty) {
|
|
284
|
+
throw `rest: ${asBase(rest)}`;
|
|
285
|
+
}
|
|
286
|
+
if (tag !== 0x00n) {
|
|
287
|
+
throw `tag: ${tag}`;
|
|
288
|
+
}
|
|
289
|
+
if (value !== vec8(0x23n)) {
|
|
290
|
+
throw `value: ${asBase(value)}`;
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
() => {
|
|
294
|
+
const e = encodeRaw([0x1f20n, vec(16n)(0x1234n)]);
|
|
295
|
+
if (e !== cat([vec8(0x1fn), vec8(0x20n), vec8(2n), vec(16n)(0x1234n)])) {
|
|
296
|
+
const l = length(e);
|
|
297
|
+
const u = uint(e);
|
|
298
|
+
throw `encode: ${l}: ${u.toString(16)}`;
|
|
299
|
+
}
|
|
300
|
+
const [[tag, value], rest] = decodeRaw(e);
|
|
301
|
+
if (rest !== empty) {
|
|
302
|
+
throw `rest: ${asBase(rest)}`;
|
|
303
|
+
}
|
|
304
|
+
if (tag !== 0x1f20n) {
|
|
305
|
+
throw `tag: ${tag}`;
|
|
306
|
+
}
|
|
307
|
+
if (value !== vec(16n)(0x1234n)) {
|
|
308
|
+
throw `value: ${asBase(value)}`;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
]
|
|
312
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base-128 encoding and decoding utilities over bit vectors.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Vec } from '../bit_vec/module.f.ts';
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a bigint into an MSB Base128 vector.
|
|
9
|
+
*
|
|
10
|
+
* @param uint The bigint to encode.
|
|
11
|
+
* @returns The encoded MSB Base128 vector.
|
|
12
|
+
*/
|
|
13
|
+
export declare const encode: (uint: bigint) => Vec;
|
|
14
|
+
/**
|
|
15
|
+
* Decodes an MSB Base128 vector into a bigint.
|
|
16
|
+
*
|
|
17
|
+
* @param v The MSB Base128 vector to decode.
|
|
18
|
+
* @returns A tuple containing the decoded bigint and the remaining vector.
|
|
19
|
+
*/
|
|
20
|
+
export declare const decode: (v: Vec) => readonly [bigint, Vec];
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base-128 encoding and decoding utilities over bit vectors.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { vec8, msb, empty } from "../bit_vec/module.f.js";
|
|
7
|
+
const { concat, popFront } = msb;
|
|
8
|
+
const pop8 = popFront(8n);
|
|
9
|
+
/**
|
|
10
|
+
* Encodes a bigint into an MSB Base128 vector.
|
|
11
|
+
*
|
|
12
|
+
* @param uint The bigint to encode.
|
|
13
|
+
* @returns The encoded MSB Base128 vector.
|
|
14
|
+
*/
|
|
15
|
+
export const encode = (uint) => {
|
|
16
|
+
let result = empty;
|
|
17
|
+
while (true) {
|
|
18
|
+
const item = uint & 0x7fn;
|
|
19
|
+
const flag = result === empty ? 0n : 0x80n;
|
|
20
|
+
result = concat(vec8(flag | item))(result);
|
|
21
|
+
uint >>= 7n;
|
|
22
|
+
if (uint === 0n) {
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Decodes an MSB Base128 vector into a bigint.
|
|
29
|
+
*
|
|
30
|
+
* @param v The MSB Base128 vector to decode.
|
|
31
|
+
* @returns A tuple containing the decoded bigint and the remaining vector.
|
|
32
|
+
*/
|
|
33
|
+
export const decode = (v) => {
|
|
34
|
+
let result = 0n;
|
|
35
|
+
while (true) {
|
|
36
|
+
const [byte, rest] = pop8(v);
|
|
37
|
+
result = (result << 7n) | (byte & 0x7fn);
|
|
38
|
+
if (byte < 0x80n) {
|
|
39
|
+
return [result, rest];
|
|
40
|
+
}
|
|
41
|
+
v = rest;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { empty, vec, vec8 } from "../bit_vec/module.f.js";
|
|
2
|
+
import { asBase } from "../nominal/module.f.js";
|
|
3
|
+
import { encode, decode } from "./module.f.js";
|
|
4
|
+
const test = (a, b) => {
|
|
5
|
+
const encoded = encode(a);
|
|
6
|
+
if (encoded !== b) {
|
|
7
|
+
throw `encoded: ${asBase(encoded).toString(16)}, expected: ${asBase(b).toString(16)}`;
|
|
8
|
+
}
|
|
9
|
+
const [decoded, rest] = decode(b);
|
|
10
|
+
if (decoded !== a) {
|
|
11
|
+
throw `decoded: ${decoded.toString(16)}, expected: ${a.toString(16)}`;
|
|
12
|
+
}
|
|
13
|
+
if (rest !== empty) {
|
|
14
|
+
throw `rest: ${asBase(rest).toString(16)}, expected: ${asBase(empty).toString(16)}`;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
export default () => {
|
|
18
|
+
test(0n, vec8(0n));
|
|
19
|
+
test(1n, vec8(1n));
|
|
20
|
+
test(0x7fn, vec8(0x7fn));
|
|
21
|
+
test(0x80n, vec(16n)(0x8100n));
|
|
22
|
+
test(0x81n, vec(16n)(0x8101n));
|
|
23
|
+
test(0x82n, vec(16n)(0x8102n));
|
|
24
|
+
test(0x3fffn, vec(16n)(0xff7fn));
|
|
25
|
+
test(0x4000n, vec(24n)(0x818000n));
|
|
26
|
+
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { Fold, Reduce as OpReduce } from
|
|
2
|
-
import { type List, type Thunk } from
|
|
3
|
-
import { type Nominal } from
|
|
1
|
+
import type { Fold, Reduce as OpReduce } from '../function/operator/module.f.ts';
|
|
2
|
+
import { type List, type Thunk } from '../list/module.f.ts';
|
|
3
|
+
import { type Nominal } from '../nominal/module.f.ts';
|
|
4
|
+
import { type Sign } from '../function/compare/module.f.ts';
|
|
4
5
|
/**
|
|
5
6
|
* A vector of bits represented as a signed `bigint`.
|
|
6
7
|
*/
|
|
@@ -186,3 +187,13 @@ export declare const listToVec: ({ concat }: BitOrder) => (list: List<Vec>) => V
|
|
|
186
187
|
*/
|
|
187
188
|
export declare const repeat: Fold<bigint, Vec>;
|
|
188
189
|
export declare const isVec: <T>(v: Vec | T) => v is Vec;
|
|
190
|
+
/**
|
|
191
|
+
* Lexically compares two vectors based on their unsigned integer values,
|
|
192
|
+
* normalizing them to the same length. If the values are equal,
|
|
193
|
+
* the shorter vector is considered smaller.
|
|
194
|
+
*
|
|
195
|
+
* a < b => -1
|
|
196
|
+
* a > b => 1
|
|
197
|
+
* a === b => 0
|
|
198
|
+
*/
|
|
199
|
+
export declare const msbCmp: (av: Vec) => (bv: Vec) => Sign;
|
|
@@ -21,11 +21,12 @@
|
|
|
21
21
|
*
|
|
22
22
|
* @module
|
|
23
23
|
*/
|
|
24
|
-
import {
|
|
24
|
+
import { bitLength, mask, max, min, xor } from "../bigint/module.f.js";
|
|
25
25
|
import { flip } from "../function/module.f.js";
|
|
26
26
|
import { fold } from "../list/module.f.js";
|
|
27
27
|
import { asBase, asNominal } from "../nominal/module.f.js";
|
|
28
28
|
import { repeat as mRepeat } from "../monoid/module.f.js";
|
|
29
|
+
import { cmp } from "../function/compare/module.f.js";
|
|
29
30
|
/**
|
|
30
31
|
* An empty vector of bits.
|
|
31
32
|
*/
|
|
@@ -55,7 +56,7 @@ export const vec = (len) => {
|
|
|
55
56
|
const lastBit = 1n << last;
|
|
56
57
|
return ui => {
|
|
57
58
|
// normalize `u`
|
|
58
|
-
const u = m &
|
|
59
|
+
const u = m & ui;
|
|
59
60
|
//
|
|
60
61
|
const sign = u >> last;
|
|
61
62
|
const x = sign !== 0n ? u : -(u ^ lastBit);
|
|
@@ -203,3 +204,21 @@ export const listToVec = ({ concat }) => fold(flip(concat))(empty);
|
|
|
203
204
|
*/
|
|
204
205
|
export const repeat = mRepeat({ identity: empty, operation: lsb.concat });
|
|
205
206
|
export const isVec = (v) => typeof v === 'bigint';
|
|
207
|
+
/**
|
|
208
|
+
* Lexically compares two vectors based on their unsigned integer values,
|
|
209
|
+
* normalizing them to the same length. If the values are equal,
|
|
210
|
+
* the shorter vector is considered smaller.
|
|
211
|
+
*
|
|
212
|
+
* a < b => -1
|
|
213
|
+
* a > b => 1
|
|
214
|
+
* a === b => 0
|
|
215
|
+
*/
|
|
216
|
+
export const msbCmp = (av) => (bv) => {
|
|
217
|
+
const au = unpack(av);
|
|
218
|
+
const bu = unpack(bv);
|
|
219
|
+
const al = au.length;
|
|
220
|
+
const bl = bu.length;
|
|
221
|
+
const { a, b } = msbNorm(au)(bu)(min(al)(bl));
|
|
222
|
+
const result = cmp(a)(b);
|
|
223
|
+
return result !== 0 ? result : cmp(al)(bl);
|
|
224
|
+
};
|
package/types/bit_vec/test.f.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mask } from "../bigint/module.f.js";
|
|
2
2
|
import { asBase, asNominal } from "../nominal/module.f.js";
|
|
3
|
-
import { length, empty, uint, vec, lsb, msb, repeat, vec8 } from "./module.f.js";
|
|
3
|
+
import { length, empty, uint, vec, lsb, msb, repeat, vec8, msbCmp } from "./module.f.js";
|
|
4
4
|
const unsafeVec = (a) => asNominal(a);
|
|
5
5
|
// 0x8 = 0b1000 = 0 + 8
|
|
6
6
|
// 0x9 = 0b1001 = 1 + 8
|
|
@@ -347,16 +347,16 @@ export default {
|
|
|
347
347
|
const v = vec(len)(ui);
|
|
348
348
|
const x = asBase(v);
|
|
349
349
|
if (x !== raw) {
|
|
350
|
-
throw x
|
|
350
|
+
throw `x: ${x}, raw: ${raw}`;
|
|
351
351
|
}
|
|
352
352
|
const len2 = length(v);
|
|
353
353
|
if (len2 !== len) {
|
|
354
354
|
throw len2;
|
|
355
355
|
}
|
|
356
356
|
const u = uint(v);
|
|
357
|
-
const mui = mask(len) &
|
|
357
|
+
const mui = mask(len) & ui;
|
|
358
358
|
if (u !== mui) {
|
|
359
|
-
throw u;
|
|
359
|
+
throw [u, mui];
|
|
360
360
|
}
|
|
361
361
|
};
|
|
362
362
|
// 0n
|
|
@@ -373,7 +373,15 @@ export default {
|
|
|
373
373
|
() => c(2n)(1n)(-3n),
|
|
374
374
|
() => c(2n)(2n)(2n),
|
|
375
375
|
() => c(2n)(3n)(3n),
|
|
376
|
-
|
|
376
|
+
// -1 is 0b.....1
|
|
377
|
+
// -2 is 0b....10
|
|
378
|
+
// -3 is 0b...101
|
|
379
|
+
// -4 is 0b...100
|
|
380
|
+
// -5 is 0b..1011
|
|
381
|
+
// -6 is 0b..1010
|
|
382
|
+
// -7 is 0b..1011, cut 0b11, vec: -0b11
|
|
383
|
+
// -8 is 0b..1100 = ~(8-1) = ~7
|
|
384
|
+
() => c(2n)(-7n)(-3n), //< overflow.
|
|
377
385
|
];
|
|
378
386
|
},
|
|
379
387
|
concat2: () => {
|
|
@@ -420,5 +428,19 @@ export default {
|
|
|
420
428
|
if (repeat(7n)(vec(5n)(0x13n)) !== vec(35n)(21059194483n)) {
|
|
421
429
|
throw 'repeat failed';
|
|
422
430
|
}
|
|
431
|
+
},
|
|
432
|
+
msbCmp: () => {
|
|
433
|
+
const c = (a) => (b) => (r) => {
|
|
434
|
+
const result = msbCmp(a)(b);
|
|
435
|
+
if (result !== r) {
|
|
436
|
+
throw `result: ${result}, expected: ${r}`;
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
c(vec(4n)(0x5n))(vec(4n)(0x5n))(0); // 0b0101 == 0b0101
|
|
440
|
+
c(vec(4n)(0x5n))(vec(4n)(0x6n))(-1); // 0b0101 < 0b0110
|
|
441
|
+
c(vec(4n)(0x6n))(vec(4n)(0x5n))(1); // 0b0110 > 0b0101
|
|
442
|
+
c(vec(4n)(0x5n))(vec(5n)(0x5n))(1); // 0b0101_ < 0b00101
|
|
443
|
+
c(vec(5n)(0x5n))(vec(4n)(0x5n))(-1); // 0b00101 < 0b0101_
|
|
444
|
+
c(vec(4n)(0x5n))(vec(5n)(0xan))(-1); // 0b0101_ < 0b01010
|
|
423
445
|
}
|
|
424
446
|
};
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lookup operations for persistent B-tree structures.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
import type { Leaf1, Leaf2, Branch3, Branch5, TNode } from '../types/module.f.ts';
|
|
2
7
|
import type { List } from '../../list/module.f.ts';
|
|
3
8
|
import { type Compare } from '../../function/compare/module.f.ts';
|
package/types/btree/module.f.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removal operations for persistent B-tree structures.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
import type { TNode, Tree } from '../types/module.f.ts';
|
|
2
7
|
import type { Compare } from '../../function/compare/module.f.ts';
|
|
3
8
|
export declare const nodeRemove: <T>(c: Compare<T>) => (node: TNode<T>) => Tree<T>;
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Insertion and update operations for persistent B-tree structures.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
import type { TNode, Tree } from '../types/module.f.ts';
|
|
2
7
|
import type { Compare } from '../../function/compare/module.f.ts';
|
|
3
8
|
export declare const set: <T>(c: Compare<T>) => (f: (value: T | null) => T) => (tree: Tree<T>) => TNode<T>;
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mock effect runtimes for testing effectful programs.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
1
6
|
import type { Effect, Operations } from "../module.f.ts";
|
|
2
7
|
export type MemOperationMap<O extends Operations, S> = {
|
|
3
8
|
readonly [K in keyof O]: (state: S, payload: O[K][0]) => readonly [S, O[K][1]];
|