@bare-ts/lib 0.1.0 → 0.3.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 +12 -2
- package/dist/codec/data.d.ts +4 -4
- package/dist/codec/data.js +26 -0
- package/dist/codec/float-array.d.ts +12 -12
- package/dist/codec/float-array.js +85 -0
- package/dist/codec/i16-array.d.ts +6 -6
- package/dist/codec/i16-array.js +43 -0
- package/dist/codec/i32-array.d.ts +6 -6
- package/dist/codec/i32-array.js +43 -0
- package/dist/codec/i64-array.d.ts +6 -6
- package/dist/codec/i64-array.js +43 -0
- package/dist/codec/i8-array.d.ts +4 -4
- package/dist/codec/i8-array.js +24 -0
- package/dist/codec/index.js +15 -0
- package/dist/codec/primitive.d.ts +34 -36
- package/dist/codec/primitive.js +345 -0
- package/dist/codec/string.d.ts +4 -2
- package/dist/codec/string.js +126 -0
- package/dist/codec/u16-array.d.ts +6 -6
- package/dist/codec/u16-array.js +43 -0
- package/dist/codec/u32-array.d.ts +6 -6
- package/dist/codec/u32-array.js +43 -0
- package/dist/codec/u64-array.d.ts +6 -6
- package/dist/codec/u64-array.js +43 -0
- package/dist/codec/u8-array.d.ts +4 -4
- package/dist/codec/u8-array.js +27 -0
- package/dist/codec/u8-clamped-array.d.ts +4 -4
- package/dist/codec/u8-clamped-array.js +24 -0
- package/dist/core/bare-error.js +14 -0
- package/dist/core/byte-cursor.d.ts +26 -8
- package/dist/core/byte-cursor.js +42 -0
- package/dist/core/config.d.ts +6 -1
- package/dist/core/config.js +27 -0
- package/dist/core/index.js +5 -0
- package/dist/index.cjs +591 -636
- package/dist/index.js +3 -987
- package/dist/util/assert.d.ts +6 -1
- package/dist/util/assert.js +1 -4
- package/dist/util/constants.d.ts +13 -0
- package/dist/util/constants.js +30 -0
- package/dist/util/util.js +6 -0
- package/dist/util/validator.js +40 -0
- package/package.json +16 -14
- package/CHANGELOG.md +0 -12
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
// src/codec/primitive.ts
|
|
2
|
+
import { assert } from "../util/assert.js";
|
|
3
|
+
import { BareError } from "../core/index.js";
|
|
4
|
+
import {
|
|
5
|
+
INT_SAFE_MAX_BYTE_COUNT,
|
|
6
|
+
NON_CANONICAL_REPRESENTATION,
|
|
7
|
+
TOO_LARGE_NUMBER,
|
|
8
|
+
UINT_MAX_BYTE_COUNT,
|
|
9
|
+
UINT_SAFE_MAX_BYTE_COUNT
|
|
10
|
+
} from "../util/constants.js";
|
|
11
|
+
import {
|
|
12
|
+
isI16,
|
|
13
|
+
isI32,
|
|
14
|
+
isI64,
|
|
15
|
+
isI8,
|
|
16
|
+
isSafeU64,
|
|
17
|
+
isU16,
|
|
18
|
+
isU32,
|
|
19
|
+
isU64,
|
|
20
|
+
isU8
|
|
21
|
+
} from "../util/validator.js";
|
|
22
|
+
function readBool(bc) {
|
|
23
|
+
const val = readU8(bc);
|
|
24
|
+
if (val > 1) {
|
|
25
|
+
bc.offset--;
|
|
26
|
+
throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
|
|
27
|
+
}
|
|
28
|
+
return val !== 0;
|
|
29
|
+
}
|
|
30
|
+
function writeBool(bc, x) {
|
|
31
|
+
writeU8(bc, x ? 1 : 0);
|
|
32
|
+
}
|
|
33
|
+
function readF32(bc) {
|
|
34
|
+
bc.check(4);
|
|
35
|
+
const result = bc.view.getFloat32(bc.offset, true);
|
|
36
|
+
bc.offset += 4;
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function writeF32(bc, x) {
|
|
40
|
+
bc.reserve(4);
|
|
41
|
+
bc.view.setFloat32(bc.offset, x, true);
|
|
42
|
+
assert(Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON, TOO_LARGE_NUMBER);
|
|
43
|
+
bc.offset += 4;
|
|
44
|
+
}
|
|
45
|
+
function readF64(bc) {
|
|
46
|
+
bc.check(8);
|
|
47
|
+
const result = bc.view.getFloat64(bc.offset, true);
|
|
48
|
+
bc.offset += 8;
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
function writeF64(bc, x) {
|
|
52
|
+
bc.reserve(8);
|
|
53
|
+
bc.view.setFloat64(bc.offset, x, true);
|
|
54
|
+
bc.offset += 8;
|
|
55
|
+
}
|
|
56
|
+
function readI8(bc) {
|
|
57
|
+
bc.check(1);
|
|
58
|
+
return bc.view.getInt8(bc.offset++);
|
|
59
|
+
}
|
|
60
|
+
function writeI8(bc, x) {
|
|
61
|
+
assert(isI8(x), TOO_LARGE_NUMBER);
|
|
62
|
+
bc.reserve(1);
|
|
63
|
+
bc.view.setInt8(bc.offset++, x);
|
|
64
|
+
}
|
|
65
|
+
function readI16(bc) {
|
|
66
|
+
bc.check(2);
|
|
67
|
+
const result = bc.view.getInt16(bc.offset, true);
|
|
68
|
+
bc.offset += 2;
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
function writeI16(bc, x) {
|
|
72
|
+
assert(isI16(x), TOO_LARGE_NUMBER);
|
|
73
|
+
bc.reserve(2);
|
|
74
|
+
bc.view.setInt16(bc.offset, x, true);
|
|
75
|
+
bc.offset += 2;
|
|
76
|
+
}
|
|
77
|
+
function readI32(bc) {
|
|
78
|
+
bc.check(4);
|
|
79
|
+
const result = bc.view.getInt32(bc.offset, true);
|
|
80
|
+
bc.offset += 4;
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
function writeI32(bc, x) {
|
|
84
|
+
assert(isI32(x), TOO_LARGE_NUMBER);
|
|
85
|
+
bc.reserve(4);
|
|
86
|
+
bc.view.setInt32(bc.offset, x, true);
|
|
87
|
+
bc.offset += 4;
|
|
88
|
+
}
|
|
89
|
+
function readI64(bc) {
|
|
90
|
+
bc.check(8);
|
|
91
|
+
const result = bc.view.getBigInt64(bc.offset, true);
|
|
92
|
+
bc.offset += 8;
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
function writeI64(bc, x) {
|
|
96
|
+
assert(isI64(x), TOO_LARGE_NUMBER);
|
|
97
|
+
bc.reserve(8);
|
|
98
|
+
bc.view.setBigInt64(bc.offset, x, true);
|
|
99
|
+
bc.offset += 8;
|
|
100
|
+
}
|
|
101
|
+
function readI64Safe(bc) {
|
|
102
|
+
const result = readU32(bc) + readI32(bc) * 4294967296;
|
|
103
|
+
if (!Number.isSafeInteger(result)) {
|
|
104
|
+
bc.offset -= 8;
|
|
105
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
function writeI64Safe(bc, x) {
|
|
110
|
+
assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
|
|
111
|
+
const lowest32 = x >>> 0;
|
|
112
|
+
writeU32(bc, lowest32);
|
|
113
|
+
let highest32 = x / 4294967296 | 0;
|
|
114
|
+
if (x < 0) {
|
|
115
|
+
highest32 = ~Math.abs(highest32) >>> 0;
|
|
116
|
+
if (lowest32 === 0) {
|
|
117
|
+
highest32++;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
writeU32(bc, highest32);
|
|
121
|
+
}
|
|
122
|
+
function readInt(bc) {
|
|
123
|
+
const zigZag = readUint(bc);
|
|
124
|
+
return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
|
|
125
|
+
}
|
|
126
|
+
function writeInt(bc, x) {
|
|
127
|
+
assert(isI64(x), TOO_LARGE_NUMBER);
|
|
128
|
+
const zigZag = x >> BigInt(63) ^ x << BigInt(1);
|
|
129
|
+
writeUint(bc, zigZag);
|
|
130
|
+
}
|
|
131
|
+
function readIntSafe(bc) {
|
|
132
|
+
const firstByte = readU8(bc);
|
|
133
|
+
let result = (firstByte & 127) >> 1;
|
|
134
|
+
if (firstByte >= 128) {
|
|
135
|
+
let shiftMul = 64;
|
|
136
|
+
let byteCount = 1;
|
|
137
|
+
let byte;
|
|
138
|
+
do {
|
|
139
|
+
byte = readU8(bc);
|
|
140
|
+
result += (byte & 127) * shiftMul;
|
|
141
|
+
shiftMul *= 128;
|
|
142
|
+
byteCount++;
|
|
143
|
+
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
144
|
+
if (byte === 0) {
|
|
145
|
+
bc.offset -= byteCount - 1;
|
|
146
|
+
throw new BareError(bc.offset, "must be canonical");
|
|
147
|
+
}
|
|
148
|
+
if (byteCount === INT_SAFE_MAX_BYTE_COUNT && (byte > 31 || firstByte === 255)) {
|
|
149
|
+
bc.offset -= byteCount - 1;
|
|
150
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const isNeg = (firstByte & 1) === 1;
|
|
154
|
+
if (isNeg) {
|
|
155
|
+
result = -result - 1;
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
function writeIntSafe(bc, x) {
|
|
160
|
+
assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
|
|
161
|
+
const sign = x < 0 ? 1 : 0;
|
|
162
|
+
if (x < 0) {
|
|
163
|
+
x = -(x + 1);
|
|
164
|
+
}
|
|
165
|
+
const firstByte = (x & 63) << 1 | sign;
|
|
166
|
+
x = Math.floor(x / 64);
|
|
167
|
+
if (x > 0) {
|
|
168
|
+
writeU8(bc, 128 | firstByte);
|
|
169
|
+
writeUintSafe(bc, x);
|
|
170
|
+
} else {
|
|
171
|
+
writeU8(bc, firstByte);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
function readU8(bc) {
|
|
175
|
+
bc.check(1);
|
|
176
|
+
return bc.bytes[bc.offset++];
|
|
177
|
+
}
|
|
178
|
+
function writeU8(bc, x) {
|
|
179
|
+
assert(isU8(x), TOO_LARGE_NUMBER);
|
|
180
|
+
bc.reserve(1);
|
|
181
|
+
bc.bytes[bc.offset++] = x;
|
|
182
|
+
}
|
|
183
|
+
function readU16(bc) {
|
|
184
|
+
bc.check(2);
|
|
185
|
+
const result = bc.view.getUint16(bc.offset, true);
|
|
186
|
+
bc.offset += 2;
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
function writeU16(bc, x) {
|
|
190
|
+
assert(isU16(x), TOO_LARGE_NUMBER);
|
|
191
|
+
bc.reserve(2);
|
|
192
|
+
bc.view.setUint16(bc.offset, x, true);
|
|
193
|
+
bc.offset += 2;
|
|
194
|
+
}
|
|
195
|
+
function readU32(bc) {
|
|
196
|
+
bc.check(4);
|
|
197
|
+
const result = bc.view.getUint32(bc.offset, true);
|
|
198
|
+
bc.offset += 4;
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
function writeU32(bc, x) {
|
|
202
|
+
assert(isU32(x), TOO_LARGE_NUMBER);
|
|
203
|
+
bc.reserve(4);
|
|
204
|
+
bc.view.setUint32(bc.offset, x, true);
|
|
205
|
+
bc.offset += 4;
|
|
206
|
+
}
|
|
207
|
+
function readU64(bc) {
|
|
208
|
+
bc.check(8);
|
|
209
|
+
const result = bc.view.getBigUint64(bc.offset, true);
|
|
210
|
+
bc.offset += 8;
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
function writeU64(bc, x) {
|
|
214
|
+
assert(isU64(x), TOO_LARGE_NUMBER);
|
|
215
|
+
bc.reserve(8);
|
|
216
|
+
bc.view.setBigUint64(bc.offset, x, true);
|
|
217
|
+
bc.offset += 8;
|
|
218
|
+
}
|
|
219
|
+
function readU64Safe(bc) {
|
|
220
|
+
const result = readU32(bc) + readU32(bc) * 4294967296;
|
|
221
|
+
if (!isSafeU64(result)) {
|
|
222
|
+
bc.offset -= 8;
|
|
223
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
function writeU64Safe(bc, x) {
|
|
228
|
+
assert(isSafeU64(x), TOO_LARGE_NUMBER);
|
|
229
|
+
writeU32(bc, x >>> 0);
|
|
230
|
+
writeU32(bc, x / 4294967296 >>> 0);
|
|
231
|
+
}
|
|
232
|
+
function readUint(bc) {
|
|
233
|
+
let low = readU8(bc);
|
|
234
|
+
if (low >= 128) {
|
|
235
|
+
low &= 127;
|
|
236
|
+
let shiftMul = 128;
|
|
237
|
+
let byteCount = 1;
|
|
238
|
+
let byte;
|
|
239
|
+
do {
|
|
240
|
+
byte = readU8(bc);
|
|
241
|
+
low += (byte & 127) * shiftMul;
|
|
242
|
+
shiftMul *= 128;
|
|
243
|
+
byteCount++;
|
|
244
|
+
} while (byte >= 128 && byteCount < 7);
|
|
245
|
+
let height = 0;
|
|
246
|
+
shiftMul = 1;
|
|
247
|
+
while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
|
|
248
|
+
byte = readU8(bc);
|
|
249
|
+
height += (byte & 127) * shiftMul;
|
|
250
|
+
shiftMul *= 128;
|
|
251
|
+
byteCount++;
|
|
252
|
+
}
|
|
253
|
+
if (byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
|
|
254
|
+
bc.offset -= byteCount;
|
|
255
|
+
throw new BareError(bc.offset, NON_CANONICAL_REPRESENTATION);
|
|
256
|
+
}
|
|
257
|
+
return BigInt(low) + (BigInt(height) << BigInt(7 * 7));
|
|
258
|
+
}
|
|
259
|
+
return BigInt(low);
|
|
260
|
+
}
|
|
261
|
+
function writeUint(bc, x) {
|
|
262
|
+
assert(isU64(x), TOO_LARGE_NUMBER);
|
|
263
|
+
let tmp = Number(BigInt.asUintN(7 * 7, x));
|
|
264
|
+
let rest = Number(x >> BigInt(7 * 7));
|
|
265
|
+
let byteCount = 0;
|
|
266
|
+
while (tmp >= 128 || rest !== 0) {
|
|
267
|
+
writeU8(bc, 128 | tmp & 127);
|
|
268
|
+
tmp = Math.floor(tmp / 128);
|
|
269
|
+
byteCount++;
|
|
270
|
+
if (byteCount === 7) {
|
|
271
|
+
tmp = rest;
|
|
272
|
+
rest = 0;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
writeU8(bc, tmp);
|
|
276
|
+
}
|
|
277
|
+
function readUintSafe(bc) {
|
|
278
|
+
let result = readU8(bc);
|
|
279
|
+
if (result >= 128) {
|
|
280
|
+
result &= 127;
|
|
281
|
+
let shiftMul = 128;
|
|
282
|
+
let byteCount = 1;
|
|
283
|
+
let byte;
|
|
284
|
+
do {
|
|
285
|
+
byte = readU8(bc);
|
|
286
|
+
result += (byte & 127) * shiftMul;
|
|
287
|
+
shiftMul *= 128;
|
|
288
|
+
byteCount++;
|
|
289
|
+
} while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
|
|
290
|
+
if (byte === 0) {
|
|
291
|
+
bc.offset -= byteCount - 1;
|
|
292
|
+
throw new BareError(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
|
|
293
|
+
}
|
|
294
|
+
if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
|
|
295
|
+
bc.offset -= byteCount - 1;
|
|
296
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return result;
|
|
300
|
+
}
|
|
301
|
+
function writeUintSafe(bc, x) {
|
|
302
|
+
assert(isSafeU64(x), TOO_LARGE_NUMBER);
|
|
303
|
+
while (x >= 128) {
|
|
304
|
+
writeU8(bc, 128 | x & 127);
|
|
305
|
+
x = Math.floor(x / 128);
|
|
306
|
+
}
|
|
307
|
+
writeU8(bc, x);
|
|
308
|
+
}
|
|
309
|
+
export {
|
|
310
|
+
readBool,
|
|
311
|
+
readF32,
|
|
312
|
+
readF64,
|
|
313
|
+
readI16,
|
|
314
|
+
readI32,
|
|
315
|
+
readI64,
|
|
316
|
+
readI64Safe,
|
|
317
|
+
readI8,
|
|
318
|
+
readInt,
|
|
319
|
+
readIntSafe,
|
|
320
|
+
readU16,
|
|
321
|
+
readU32,
|
|
322
|
+
readU64,
|
|
323
|
+
readU64Safe,
|
|
324
|
+
readU8,
|
|
325
|
+
readUint,
|
|
326
|
+
readUintSafe,
|
|
327
|
+
writeBool,
|
|
328
|
+
writeF32,
|
|
329
|
+
writeF64,
|
|
330
|
+
writeI16,
|
|
331
|
+
writeI32,
|
|
332
|
+
writeI64,
|
|
333
|
+
writeI64Safe,
|
|
334
|
+
writeI8,
|
|
335
|
+
writeInt,
|
|
336
|
+
writeIntSafe,
|
|
337
|
+
writeU16,
|
|
338
|
+
writeU32,
|
|
339
|
+
writeU64,
|
|
340
|
+
writeU64Safe,
|
|
341
|
+
writeU8,
|
|
342
|
+
writeUint,
|
|
343
|
+
writeUintSafe
|
|
344
|
+
};
|
|
345
|
+
//# sourceMappingURL=primitive.js.map
|
package/dist/codec/string.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
2
|
+
export declare function readString(bc: ByteCursor): string;
|
|
3
|
+
export declare function writeString(bc: ByteCursor, x: string): void;
|
|
4
|
+
export declare function readFixedString(bc: ByteCursor, byteLen: number): string;
|
|
5
|
+
export declare function writeFixedString(bc: ByteCursor, x: string): void;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// src/codec/string.ts
|
|
2
|
+
import { BareError } from "../core/bare-error.js";
|
|
3
|
+
import { INVALID_UTF8_STRING } from "../util/constants.js";
|
|
4
|
+
import { readUintSafe, writeUintSafe } from "./primitive.js";
|
|
5
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
6
|
+
function readString(bc) {
|
|
7
|
+
return readFixedString(bc, readUintSafe(bc));
|
|
8
|
+
}
|
|
9
|
+
function writeString(bc, x) {
|
|
10
|
+
if (x.length < bc.config.textEncoderThreshold) {
|
|
11
|
+
const byteLen = utf8ByteLength(x);
|
|
12
|
+
writeUintSafe(bc, byteLen);
|
|
13
|
+
bc.reserve(byteLen);
|
|
14
|
+
writeUtf8Js(bc, x);
|
|
15
|
+
} else {
|
|
16
|
+
const strBytes = UTF8_ENCODER.encode(x);
|
|
17
|
+
writeUintSafe(bc, strBytes.length);
|
|
18
|
+
writeU8FixedArray(bc, strBytes);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function readFixedString(bc, byteLen) {
|
|
22
|
+
if (byteLen < bc.config.textDecoderThreshold) {
|
|
23
|
+
return readUtf8Js(bc, byteLen);
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return UTF8_DECODER.decode(bc.read(byteLen));
|
|
27
|
+
} catch (cause) {
|
|
28
|
+
throw new BareError(bc.offset, INVALID_UTF8_STRING);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function writeFixedString(bc, x) {
|
|
32
|
+
if (x.length < bc.config.textEncoderThreshold) {
|
|
33
|
+
const byteLen = utf8ByteLength(x);
|
|
34
|
+
bc.reserve(byteLen);
|
|
35
|
+
writeUtf8Js(bc, x);
|
|
36
|
+
} else {
|
|
37
|
+
writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function readUtf8Js(bc, byteLen) {
|
|
41
|
+
bc.check(byteLen);
|
|
42
|
+
let result = "";
|
|
43
|
+
const bytes = bc.bytes;
|
|
44
|
+
let offset = bc.offset;
|
|
45
|
+
const upperOffset = offset + byteLen;
|
|
46
|
+
while (offset < upperOffset) {
|
|
47
|
+
let codePoint = bytes[offset++];
|
|
48
|
+
if (codePoint > 127) {
|
|
49
|
+
let malformed = true;
|
|
50
|
+
const byte1 = codePoint;
|
|
51
|
+
if (offset < upperOffset && codePoint < 224) {
|
|
52
|
+
const byte2 = bytes[offset++];
|
|
53
|
+
codePoint = (byte1 & 31) << 6 | byte2 & 63;
|
|
54
|
+
malformed = codePoint >> 7 === 0 || byte1 >> 5 !== 6 || byte2 >> 6 !== 2;
|
|
55
|
+
} else if (offset + 1 < upperOffset && codePoint < 240) {
|
|
56
|
+
const byte2 = bytes[offset++];
|
|
57
|
+
const byte3 = bytes[offset++];
|
|
58
|
+
codePoint = (byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63;
|
|
59
|
+
malformed = codePoint >> 11 === 0 || codePoint >> 11 === 27 || byte1 >> 4 !== 14 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2;
|
|
60
|
+
} else if (offset + 2 < upperOffset) {
|
|
61
|
+
const byte2 = bytes[offset++];
|
|
62
|
+
const byte3 = bytes[offset++];
|
|
63
|
+
const byte4 = bytes[offset++];
|
|
64
|
+
codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
|
|
65
|
+
malformed = codePoint >> 16 === 0 || codePoint > 1114111 || byte1 >> 3 !== 30 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2 || byte4 >> 6 !== 2;
|
|
66
|
+
}
|
|
67
|
+
if (malformed) {
|
|
68
|
+
throw new BareError(bc.offset, INVALID_UTF8_STRING);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
result += String.fromCodePoint(codePoint);
|
|
72
|
+
}
|
|
73
|
+
bc.offset = offset;
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
function writeUtf8Js(bc, s) {
|
|
77
|
+
const bytes = bc.bytes;
|
|
78
|
+
let offset = bc.offset;
|
|
79
|
+
let i = 0;
|
|
80
|
+
while (i < s.length) {
|
|
81
|
+
const codePoint = s.codePointAt(i++);
|
|
82
|
+
if (codePoint < 128) {
|
|
83
|
+
bytes[offset++] = codePoint;
|
|
84
|
+
} else {
|
|
85
|
+
if (codePoint < 2048) {
|
|
86
|
+
bytes[offset++] = 192 | codePoint >> 6;
|
|
87
|
+
} else {
|
|
88
|
+
if (codePoint < 65536) {
|
|
89
|
+
bytes[offset++] = 224 | codePoint >> 12;
|
|
90
|
+
} else {
|
|
91
|
+
bytes[offset++] = 240 | codePoint >> 18;
|
|
92
|
+
bytes[offset++] = 128 | codePoint >> 12 & 63;
|
|
93
|
+
i++;
|
|
94
|
+
}
|
|
95
|
+
bytes[offset++] = 128 | codePoint >> 6 & 63;
|
|
96
|
+
}
|
|
97
|
+
bytes[offset++] = 128 | codePoint & 63;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
bc.offset = offset;
|
|
101
|
+
}
|
|
102
|
+
function utf8ByteLength(s) {
|
|
103
|
+
let result = s.length;
|
|
104
|
+
for (let i = 0; i < s.length; i++) {
|
|
105
|
+
const codePoint = s.codePointAt(i);
|
|
106
|
+
if (codePoint > 127) {
|
|
107
|
+
result++;
|
|
108
|
+
if (codePoint > 2047) {
|
|
109
|
+
result++;
|
|
110
|
+
if (codePoint > 65535) {
|
|
111
|
+
i++;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
var UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
|
|
119
|
+
var UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
|
|
120
|
+
export {
|
|
121
|
+
readFixedString,
|
|
122
|
+
readString,
|
|
123
|
+
writeFixedString,
|
|
124
|
+
writeString
|
|
125
|
+
};
|
|
126
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare function
|
|
4
|
-
declare function
|
|
5
|
-
export declare const
|
|
6
|
-
export declare function
|
|
7
|
-
declare function
|
|
2
|
+
export declare const readU16FixedArray: typeof readU16FixedArrayLE;
|
|
3
|
+
export declare function readU16Array(bc: ByteCursor): Uint16Array;
|
|
4
|
+
declare function readU16FixedArrayLE(bc: ByteCursor, len: number): Uint16Array;
|
|
5
|
+
export declare const writeU16FixedArray: typeof writeU16FixedArrayLE;
|
|
6
|
+
export declare function writeU16Array(bc: ByteCursor, x: Uint16Array): void;
|
|
7
|
+
declare function writeU16FixedArrayLE(bc: ByteCursor, x: Uint16Array): void;
|
|
8
8
|
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/codec/u16-array.ts
|
|
2
|
+
import { U16_BYTE_COUNT } from "../util/constants.js";
|
|
3
|
+
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
|
|
4
|
+
import { readFixedData } from "./data.js";
|
|
5
|
+
import { readU16, readUintSafe, writeU16, writeUintSafe } from "./primitive.js";
|
|
6
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
7
|
+
var readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLE : readU16FixedArrayBE;
|
|
8
|
+
function readU16Array(bc) {
|
|
9
|
+
return readU16FixedArray(bc, readUintSafe(bc));
|
|
10
|
+
}
|
|
11
|
+
function readU16FixedArrayLE(bc, len) {
|
|
12
|
+
const byteCount = len * U16_BYTE_COUNT;
|
|
13
|
+
return new Uint16Array(readFixedData(bc, byteCount));
|
|
14
|
+
}
|
|
15
|
+
function readU16FixedArrayBE(bc, len) {
|
|
16
|
+
bc.check(len * U16_BYTE_COUNT);
|
|
17
|
+
const result = new Uint16Array(len);
|
|
18
|
+
for (let i = 0; i < len; i++)
|
|
19
|
+
result[i] = readU16(bc);
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
var writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLE : writeU16FixedArrayBE;
|
|
23
|
+
function writeU16Array(bc, x) {
|
|
24
|
+
writeUintSafe(bc, x.length);
|
|
25
|
+
if (x.length !== 0) {
|
|
26
|
+
writeU16FixedArray(bc, x);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function writeU16FixedArrayLE(bc, x) {
|
|
30
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
31
|
+
}
|
|
32
|
+
function writeU16FixedArrayBE(bc, x) {
|
|
33
|
+
bc.reserve(x.length * U16_BYTE_COUNT);
|
|
34
|
+
for (let i = 0; i < x.length; i++)
|
|
35
|
+
writeU16(bc, x[i]);
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
readU16Array,
|
|
39
|
+
readU16FixedArray,
|
|
40
|
+
writeU16Array,
|
|
41
|
+
writeU16FixedArray
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=u16-array.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare function
|
|
4
|
-
declare function
|
|
5
|
-
export declare const
|
|
6
|
-
export declare function
|
|
7
|
-
declare function
|
|
2
|
+
export declare const readU32FixedArray: typeof readU32FixedArrayLE;
|
|
3
|
+
export declare function readU32Array(bc: ByteCursor): Uint32Array;
|
|
4
|
+
declare function readU32FixedArrayLE(bc: ByteCursor, len: number): Uint32Array;
|
|
5
|
+
export declare const writeU32FixedArray: typeof writeU32FixedArrayLE;
|
|
6
|
+
export declare function writeU32Array(bc: ByteCursor, x: Uint32Array): void;
|
|
7
|
+
declare function writeU32FixedArrayLE(bc: ByteCursor, x: Uint32Array): void;
|
|
8
8
|
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/codec/u32-array.ts
|
|
2
|
+
import { U32_BYTE_COUNT } from "../util/constants.js";
|
|
3
|
+
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
|
|
4
|
+
import { readFixedData } from "./data.js";
|
|
5
|
+
import { readU32, readUintSafe, writeU32, writeUintSafe } from "./primitive.js";
|
|
6
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
7
|
+
var readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLE : readU32FixedArrayBE;
|
|
8
|
+
function readU32Array(bc) {
|
|
9
|
+
return readU32FixedArray(bc, readUintSafe(bc));
|
|
10
|
+
}
|
|
11
|
+
function readU32FixedArrayLE(bc, len) {
|
|
12
|
+
const byteCount = len * U32_BYTE_COUNT;
|
|
13
|
+
return new Uint32Array(readFixedData(bc, byteCount));
|
|
14
|
+
}
|
|
15
|
+
function readU32FixedArrayBE(bc, len) {
|
|
16
|
+
bc.check(len * U32_BYTE_COUNT);
|
|
17
|
+
const result = new Uint32Array(len);
|
|
18
|
+
for (let i = 0; i < len; i++)
|
|
19
|
+
result[i] = readU32(bc);
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
var writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLE : writeU32FixedArrayBE;
|
|
23
|
+
function writeU32Array(bc, x) {
|
|
24
|
+
writeUintSafe(bc, x.length);
|
|
25
|
+
if (x.length !== 0) {
|
|
26
|
+
writeU32FixedArray(bc, x);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function writeU32FixedArrayLE(bc, x) {
|
|
30
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
31
|
+
}
|
|
32
|
+
function writeU32FixedArrayBE(bc, x) {
|
|
33
|
+
bc.reserve(x.length * U32_BYTE_COUNT);
|
|
34
|
+
for (let i = 0; i < x.length; i++)
|
|
35
|
+
writeU32(bc, x[i]);
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
readU32Array,
|
|
39
|
+
readU32FixedArray,
|
|
40
|
+
writeU32Array,
|
|
41
|
+
writeU32FixedArray
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=u32-array.js.map
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare const
|
|
3
|
-
export declare function
|
|
4
|
-
declare function
|
|
5
|
-
export declare const
|
|
6
|
-
export declare function
|
|
7
|
-
declare function
|
|
2
|
+
export declare const readU64FixedArray: typeof readU64FixedArrayLE;
|
|
3
|
+
export declare function readU64Array(bc: ByteCursor): BigUint64Array;
|
|
4
|
+
declare function readU64FixedArrayLE(bc: ByteCursor, len: number): BigUint64Array;
|
|
5
|
+
export declare const writeU64FixedArray: typeof writeU64FixedArrayLE;
|
|
6
|
+
export declare function writeU64Array(bc: ByteCursor, x: BigUint64Array): void;
|
|
7
|
+
declare function writeU64FixedArrayLE(bc: ByteCursor, x: BigUint64Array): void;
|
|
8
8
|
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/codec/u64-array.ts
|
|
2
|
+
import { U64_BYTE_COUNT } from "../util/constants.js";
|
|
3
|
+
import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/util.js";
|
|
4
|
+
import { readFixedData } from "./data.js";
|
|
5
|
+
import { readU64, readUintSafe, writeU64, writeUintSafe } from "./primitive.js";
|
|
6
|
+
import { writeU8FixedArray } from "./u8-array.js";
|
|
7
|
+
var readU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU64FixedArrayLE : readU64FixedArrayBE;
|
|
8
|
+
function readU64Array(bc) {
|
|
9
|
+
return readU64FixedArray(bc, readUintSafe(bc));
|
|
10
|
+
}
|
|
11
|
+
function readU64FixedArrayLE(bc, len) {
|
|
12
|
+
const byteCount = len * U64_BYTE_COUNT;
|
|
13
|
+
return new BigUint64Array(readFixedData(bc, byteCount));
|
|
14
|
+
}
|
|
15
|
+
function readU64FixedArrayBE(bc, len) {
|
|
16
|
+
bc.check(len * U64_BYTE_COUNT);
|
|
17
|
+
const result = new BigUint64Array(len);
|
|
18
|
+
for (let i = 0; i < len; i++)
|
|
19
|
+
result[i] = readU64(bc);
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
var writeU64FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU64FixedArrayLE : writeU64FixedArrayBE;
|
|
23
|
+
function writeU64Array(bc, x) {
|
|
24
|
+
writeUintSafe(bc, x.length);
|
|
25
|
+
if (x.length !== 0) {
|
|
26
|
+
writeU64FixedArray(bc, x);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function writeU64FixedArrayLE(bc, x) {
|
|
30
|
+
writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
|
|
31
|
+
}
|
|
32
|
+
function writeU64FixedArrayBE(bc, x) {
|
|
33
|
+
bc.reserve(x.length * U64_BYTE_COUNT);
|
|
34
|
+
for (let i = 0; i < x.length; i++)
|
|
35
|
+
writeU64(bc, x[i]);
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
readU64Array,
|
|
39
|
+
readU64FixedArray,
|
|
40
|
+
writeU64Array,
|
|
41
|
+
writeU64FixedArray
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=u64-array.js.map
|
package/dist/codec/u8-array.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ByteCursor } from "../core/index.js";
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
2
|
+
export declare function readU8Array(bc: ByteCursor): Uint8Array;
|
|
3
|
+
export declare function writeU8Array(bc: ByteCursor, x: Uint8Array): void;
|
|
4
|
+
export declare function readU8FixedArray(bc: ByteCursor, len: number): Uint8Array;
|
|
5
|
+
export declare function writeU8FixedArray(bc: ByteCursor, x: Uint8Array): void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/codec/u8-array.ts
|
|
2
|
+
import { readUintSafe, writeUintSafe } from "./primitive.js";
|
|
3
|
+
function readU8Array(bc) {
|
|
4
|
+
return readU8FixedArray(bc, readUintSafe(bc));
|
|
5
|
+
}
|
|
6
|
+
function writeU8Array(bc, x) {
|
|
7
|
+
writeUintSafe(bc, x.length);
|
|
8
|
+
writeU8FixedArray(bc, x);
|
|
9
|
+
}
|
|
10
|
+
function readU8FixedArray(bc, len) {
|
|
11
|
+
return bc.read(len).slice();
|
|
12
|
+
}
|
|
13
|
+
function writeU8FixedArray(bc, x) {
|
|
14
|
+
const len = x.length;
|
|
15
|
+
if (len !== 0) {
|
|
16
|
+
bc.reserve(len);
|
|
17
|
+
bc.bytes.set(x, bc.offset);
|
|
18
|
+
bc.offset += len;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
readU8Array,
|
|
23
|
+
readU8FixedArray,
|
|
24
|
+
writeU8Array,
|
|
25
|
+
writeU8FixedArray
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=u8-array.js.map
|