@bare-ts/lib 0.3.0 → 0.5.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/LICENSE +21 -201
- package/README.md +7 -8
- package/dist/codec/data.d.ts +1 -1
- package/dist/codec/data.js +10 -12
- package/dist/codec/float-array.d.ts +7 -12
- package/dist/codec/float-array.js +53 -45
- package/dist/codec/i16-array.d.ts +4 -7
- package/dist/codec/i16-array.js +35 -27
- package/dist/codec/i32-array.d.ts +4 -7
- package/dist/codec/i32-array.js +35 -27
- package/dist/codec/i64-array.d.ts +4 -7
- package/dist/codec/i64-array.js +35 -27
- package/dist/codec/i8-array.d.ts +3 -3
- package/dist/codec/i8-array.js +12 -14
- package/dist/codec/primitive.d.ts +3 -1
- package/dist/codec/primitive.js +227 -149
- package/dist/codec/string.d.ts +1 -1
- package/dist/codec/string.js +45 -31
- package/dist/codec/u16-array.d.ts +4 -7
- package/dist/codec/u16-array.js +35 -27
- package/dist/codec/u32-array.d.ts +4 -7
- package/dist/codec/u32-array.js +35 -27
- package/dist/codec/u64-array.d.ts +4 -7
- package/dist/codec/u64-array.js +35 -27
- package/dist/codec/u8-array.d.ts +9 -3
- package/dist/codec/u8-array.js +23 -18
- package/dist/codec/u8-clamped-array.d.ts +3 -3
- package/dist/codec/u8-clamped-array.js +13 -15
- package/dist/core/bare-error.d.ts +4 -1
- package/dist/core/bare-error.js +4 -8
- package/dist/core/byte-cursor.d.ts +22 -28
- package/dist/core/byte-cursor.js +33 -29
- package/dist/core/config.d.ts +4 -10
- package/dist/core/config.js +15 -20
- package/dist/env/dev.d.ts +1 -0
- package/dist/env/dev.development.d.ts +1 -0
- package/dist/env/dev.development.js +2 -0
- package/dist/env/dev.js +2 -0
- package/dist/env/dev.node.d.ts +1 -0
- package/dist/env/dev.node.js +2 -0
- package/dist/index.cjs +545 -324
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -2
- package/dist/index.js +17 -4
- package/dist/util/assert.d.ts +6 -8
- package/dist/util/assert.js +10 -13
- package/dist/util/constants.d.ts +4 -7
- package/dist/util/constants.js +11 -30
- package/dist/util/validator.d.ts +1 -1
- package/dist/util/validator.js +12 -24
- package/package.json +31 -25
- package/dist/codec/index.d.ts +0 -13
- package/dist/codec/index.js +0 -15
- package/dist/core/index.d.ts +0 -3
- package/dist/core/index.js +0 -5
- package/dist/util/util.d.ts +0 -1
- package/dist/util/util.js +0 -6
package/dist/codec/primitive.js
CHANGED
|
@@ -1,144 +1,174 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
"use strict";
|
|
2
|
+
import { BareError } from "../core/bare-error.js";
|
|
3
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
4
|
+
import { assert, DEV } from "../util/assert.js";
|
|
4
5
|
import {
|
|
5
6
|
INT_SAFE_MAX_BYTE_COUNT,
|
|
6
7
|
NON_CANONICAL_REPRESENTATION,
|
|
7
8
|
TOO_LARGE_NUMBER,
|
|
8
9
|
UINT_MAX_BYTE_COUNT,
|
|
9
|
-
|
|
10
|
+
UINT_SAFE32_MAX_BYTE_COUNT
|
|
10
11
|
} from "../util/constants.js";
|
|
11
12
|
import {
|
|
13
|
+
isI8,
|
|
12
14
|
isI16,
|
|
13
15
|
isI32,
|
|
14
16
|
isI64,
|
|
15
|
-
|
|
16
|
-
isSafeU64,
|
|
17
|
+
isU8,
|
|
17
18
|
isU16,
|
|
18
19
|
isU32,
|
|
19
20
|
isU64,
|
|
20
|
-
|
|
21
|
+
isU64Safe
|
|
21
22
|
} from "../util/validator.js";
|
|
22
|
-
function readBool(bc) {
|
|
23
|
+
export function readBool(bc) {
|
|
23
24
|
const val = readU8(bc);
|
|
24
25
|
if (val > 1) {
|
|
25
26
|
bc.offset--;
|
|
26
27
|
throw new BareError(bc.offset, "a bool must be equal to 0 or 1");
|
|
27
28
|
}
|
|
28
|
-
return val
|
|
29
|
+
return val > 0;
|
|
29
30
|
}
|
|
30
|
-
function writeBool(bc, x) {
|
|
31
|
+
export function writeBool(bc, x) {
|
|
31
32
|
writeU8(bc, x ? 1 : 0);
|
|
32
33
|
}
|
|
33
|
-
function readF32(bc) {
|
|
34
|
-
|
|
34
|
+
export function readF32(bc) {
|
|
35
|
+
check(bc, 4);
|
|
35
36
|
const result = bc.view.getFloat32(bc.offset, true);
|
|
36
37
|
bc.offset += 4;
|
|
37
38
|
return result;
|
|
38
39
|
}
|
|
39
|
-
function writeF32(bc, x) {
|
|
40
|
-
|
|
40
|
+
export function writeF32(bc, x) {
|
|
41
|
+
reserve(bc, 4);
|
|
41
42
|
bc.view.setFloat32(bc.offset, x, true);
|
|
42
|
-
|
|
43
|
+
if (DEV) {
|
|
44
|
+
assert(
|
|
45
|
+
Number.isNaN(x) || Math.abs(bc.view.getFloat32(bc.offset, true) - x) <= Number.EPSILON,
|
|
46
|
+
TOO_LARGE_NUMBER
|
|
47
|
+
);
|
|
48
|
+
}
|
|
43
49
|
bc.offset += 4;
|
|
44
50
|
}
|
|
45
|
-
function readF64(bc) {
|
|
46
|
-
|
|
51
|
+
export function readF64(bc) {
|
|
52
|
+
check(bc, 8);
|
|
47
53
|
const result = bc.view.getFloat64(bc.offset, true);
|
|
48
54
|
bc.offset += 8;
|
|
49
55
|
return result;
|
|
50
56
|
}
|
|
51
|
-
function writeF64(bc, x) {
|
|
52
|
-
|
|
57
|
+
export function writeF64(bc, x) {
|
|
58
|
+
reserve(bc, 8);
|
|
53
59
|
bc.view.setFloat64(bc.offset, x, true);
|
|
54
60
|
bc.offset += 8;
|
|
55
61
|
}
|
|
56
|
-
function readI8(bc) {
|
|
57
|
-
|
|
62
|
+
export function readI8(bc) {
|
|
63
|
+
check(bc, 1);
|
|
58
64
|
return bc.view.getInt8(bc.offset++);
|
|
59
65
|
}
|
|
60
|
-
function writeI8(bc, x) {
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
export function writeI8(bc, x) {
|
|
67
|
+
if (DEV) {
|
|
68
|
+
assert(isI8(x), TOO_LARGE_NUMBER);
|
|
69
|
+
}
|
|
70
|
+
reserve(bc, 1);
|
|
63
71
|
bc.view.setInt8(bc.offset++, x);
|
|
64
72
|
}
|
|
65
|
-
function readI16(bc) {
|
|
66
|
-
|
|
73
|
+
export function readI16(bc) {
|
|
74
|
+
check(bc, 2);
|
|
67
75
|
const result = bc.view.getInt16(bc.offset, true);
|
|
68
76
|
bc.offset += 2;
|
|
69
77
|
return result;
|
|
70
78
|
}
|
|
71
|
-
function writeI16(bc, x) {
|
|
72
|
-
|
|
73
|
-
|
|
79
|
+
export function writeI16(bc, x) {
|
|
80
|
+
if (DEV) {
|
|
81
|
+
assert(isI16(x), TOO_LARGE_NUMBER);
|
|
82
|
+
}
|
|
83
|
+
reserve(bc, 2);
|
|
74
84
|
bc.view.setInt16(bc.offset, x, true);
|
|
75
85
|
bc.offset += 2;
|
|
76
86
|
}
|
|
77
|
-
function readI32(bc) {
|
|
78
|
-
|
|
87
|
+
export function readI32(bc) {
|
|
88
|
+
check(bc, 4);
|
|
79
89
|
const result = bc.view.getInt32(bc.offset, true);
|
|
80
90
|
bc.offset += 4;
|
|
81
91
|
return result;
|
|
82
92
|
}
|
|
83
|
-
function writeI32(bc, x) {
|
|
84
|
-
|
|
85
|
-
|
|
93
|
+
export function writeI32(bc, x) {
|
|
94
|
+
if (DEV) {
|
|
95
|
+
assert(isI32(x), TOO_LARGE_NUMBER);
|
|
96
|
+
}
|
|
97
|
+
reserve(bc, 4);
|
|
86
98
|
bc.view.setInt32(bc.offset, x, true);
|
|
87
99
|
bc.offset += 4;
|
|
88
100
|
}
|
|
89
|
-
function readI64(bc) {
|
|
90
|
-
|
|
101
|
+
export function readI64(bc) {
|
|
102
|
+
check(bc, 8);
|
|
91
103
|
const result = bc.view.getBigInt64(bc.offset, true);
|
|
92
104
|
bc.offset += 8;
|
|
93
105
|
return result;
|
|
94
106
|
}
|
|
95
|
-
function writeI64(bc, x) {
|
|
96
|
-
|
|
97
|
-
|
|
107
|
+
export function writeI64(bc, x) {
|
|
108
|
+
if (DEV) {
|
|
109
|
+
assert(isI64(x), TOO_LARGE_NUMBER);
|
|
110
|
+
}
|
|
111
|
+
reserve(bc, 8);
|
|
98
112
|
bc.view.setBigInt64(bc.offset, x, true);
|
|
99
113
|
bc.offset += 8;
|
|
100
114
|
}
|
|
101
|
-
function readI64Safe(bc) {
|
|
102
|
-
const result = readU32(bc) + readI32(bc) *
|
|
115
|
+
export function readI64Safe(bc) {
|
|
116
|
+
const result = readU32(bc) + readI32(bc) * /* 2**32 */
|
|
117
|
+
4294967296;
|
|
103
118
|
if (!Number.isSafeInteger(result)) {
|
|
104
119
|
bc.offset -= 8;
|
|
105
120
|
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
106
121
|
}
|
|
107
122
|
return result;
|
|
108
123
|
}
|
|
109
|
-
function writeI64Safe(bc, x) {
|
|
110
|
-
|
|
111
|
-
|
|
124
|
+
export function writeI64Safe(bc, x) {
|
|
125
|
+
if (DEV) {
|
|
126
|
+
assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
|
|
127
|
+
}
|
|
128
|
+
let lowest32 = x >>> 0;
|
|
112
129
|
writeU32(bc, lowest32);
|
|
113
|
-
let highest32 = x /
|
|
130
|
+
let highest32 = x / /* 2**32 */
|
|
131
|
+
4294967296 | 0;
|
|
114
132
|
if (x < 0) {
|
|
115
|
-
highest32 = ~Math.abs(highest32)
|
|
133
|
+
highest32 = ~(Math.abs(highest32) & /* 2**21-1 */
|
|
134
|
+
2097151) >>> 0;
|
|
116
135
|
if (lowest32 === 0) {
|
|
117
|
-
highest32
|
|
136
|
+
if (highest32 === 2097151) {
|
|
137
|
+
lowest32 = 1;
|
|
138
|
+
} else {
|
|
139
|
+
highest32++;
|
|
140
|
+
}
|
|
118
141
|
}
|
|
119
142
|
}
|
|
120
143
|
writeU32(bc, highest32);
|
|
121
144
|
}
|
|
122
|
-
function readInt(bc) {
|
|
145
|
+
export function readInt(bc) {
|
|
123
146
|
const zigZag = readUint(bc);
|
|
124
147
|
return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
|
|
125
148
|
}
|
|
126
|
-
function writeInt(bc, x) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
149
|
+
export function writeInt(bc, x) {
|
|
150
|
+
const truncated = BigInt.asIntN(64, x);
|
|
151
|
+
if (DEV) {
|
|
152
|
+
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
153
|
+
}
|
|
154
|
+
const zigZag = truncated >> BigInt(63) ^ truncated << BigInt(1);
|
|
155
|
+
writeTruncatedUint(bc, zigZag);
|
|
130
156
|
}
|
|
131
|
-
function readIntSafe(bc) {
|
|
157
|
+
export function readIntSafe(bc) {
|
|
132
158
|
const firstByte = readU8(bc);
|
|
133
159
|
let result = (firstByte & 127) >> 1;
|
|
134
160
|
if (firstByte >= 128) {
|
|
135
|
-
let shiftMul =
|
|
161
|
+
let shiftMul = (
|
|
162
|
+
/* 2**6 */
|
|
163
|
+
64
|
|
164
|
+
);
|
|
136
165
|
let byteCount = 1;
|
|
137
166
|
let byte;
|
|
138
167
|
do {
|
|
139
168
|
byte = readU8(bc);
|
|
140
169
|
result += (byte & 127) * shiftMul;
|
|
141
|
-
shiftMul *=
|
|
170
|
+
shiftMul *= /* 2**7 */
|
|
171
|
+
128;
|
|
142
172
|
byteCount++;
|
|
143
173
|
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
144
174
|
if (byte === 0) {
|
|
@@ -156,80 +186,102 @@ function readIntSafe(bc) {
|
|
|
156
186
|
}
|
|
157
187
|
return result;
|
|
158
188
|
}
|
|
159
|
-
function writeIntSafe(bc, x) {
|
|
160
|
-
assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
|
|
189
|
+
export function writeIntSafe(bc, x) {
|
|
161
190
|
const sign = x < 0 ? 1 : 0;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
191
|
+
let zigZag = x < 0 ? -(x + 1) : x;
|
|
192
|
+
let first7Bits = (zigZag & 63) << 1 | sign;
|
|
193
|
+
zigZag = Math.floor(zigZag / /* 2**6 */
|
|
194
|
+
64);
|
|
195
|
+
if (zigZag > 0) {
|
|
196
|
+
if (!Number.isSafeInteger(x)) {
|
|
197
|
+
if (DEV) {
|
|
198
|
+
assert(false, TOO_LARGE_NUMBER);
|
|
199
|
+
}
|
|
200
|
+
const low = zigZag & 32767;
|
|
201
|
+
const high = (zigZag / 32768 >>> 0) * 32768;
|
|
202
|
+
if (first7Bits === 127 && low === 32767 && high === 4294967295) {
|
|
203
|
+
first7Bits &= ~2;
|
|
204
|
+
}
|
|
205
|
+
zigZag = high + low;
|
|
206
|
+
}
|
|
207
|
+
writeU8(bc, 128 | first7Bits);
|
|
208
|
+
writeUintSafe(bc, zigZag);
|
|
170
209
|
} else {
|
|
171
|
-
writeU8(bc,
|
|
210
|
+
writeU8(bc, first7Bits);
|
|
172
211
|
}
|
|
173
212
|
}
|
|
174
|
-
function readU8(bc) {
|
|
175
|
-
|
|
213
|
+
export function readU8(bc) {
|
|
214
|
+
check(bc, 1);
|
|
176
215
|
return bc.bytes[bc.offset++];
|
|
177
216
|
}
|
|
178
|
-
function writeU8(bc, x) {
|
|
179
|
-
|
|
180
|
-
|
|
217
|
+
export function writeU8(bc, x) {
|
|
218
|
+
if (DEV) {
|
|
219
|
+
assert(isU8(x), TOO_LARGE_NUMBER);
|
|
220
|
+
}
|
|
221
|
+
reserve(bc, 1);
|
|
181
222
|
bc.bytes[bc.offset++] = x;
|
|
182
223
|
}
|
|
183
|
-
function readU16(bc) {
|
|
184
|
-
|
|
224
|
+
export function readU16(bc) {
|
|
225
|
+
check(bc, 2);
|
|
185
226
|
const result = bc.view.getUint16(bc.offset, true);
|
|
186
227
|
bc.offset += 2;
|
|
187
228
|
return result;
|
|
188
229
|
}
|
|
189
|
-
function writeU16(bc, x) {
|
|
190
|
-
|
|
191
|
-
|
|
230
|
+
export function writeU16(bc, x) {
|
|
231
|
+
if (DEV) {
|
|
232
|
+
assert(isU16(x), TOO_LARGE_NUMBER);
|
|
233
|
+
}
|
|
234
|
+
reserve(bc, 2);
|
|
192
235
|
bc.view.setUint16(bc.offset, x, true);
|
|
193
236
|
bc.offset += 2;
|
|
194
237
|
}
|
|
195
|
-
function readU32(bc) {
|
|
196
|
-
|
|
238
|
+
export function readU32(bc) {
|
|
239
|
+
check(bc, 4);
|
|
197
240
|
const result = bc.view.getUint32(bc.offset, true);
|
|
198
241
|
bc.offset += 4;
|
|
199
242
|
return result;
|
|
200
243
|
}
|
|
201
|
-
function writeU32(bc, x) {
|
|
202
|
-
|
|
203
|
-
|
|
244
|
+
export function writeU32(bc, x) {
|
|
245
|
+
if (DEV) {
|
|
246
|
+
assert(isU32(x), TOO_LARGE_NUMBER);
|
|
247
|
+
}
|
|
248
|
+
reserve(bc, 4);
|
|
204
249
|
bc.view.setUint32(bc.offset, x, true);
|
|
205
250
|
bc.offset += 4;
|
|
206
251
|
}
|
|
207
|
-
function readU64(bc) {
|
|
208
|
-
|
|
252
|
+
export function readU64(bc) {
|
|
253
|
+
check(bc, 8);
|
|
209
254
|
const result = bc.view.getBigUint64(bc.offset, true);
|
|
210
255
|
bc.offset += 8;
|
|
211
256
|
return result;
|
|
212
257
|
}
|
|
213
|
-
function writeU64(bc, x) {
|
|
214
|
-
|
|
215
|
-
|
|
258
|
+
export function writeU64(bc, x) {
|
|
259
|
+
if (DEV) {
|
|
260
|
+
assert(isU64(x), TOO_LARGE_NUMBER);
|
|
261
|
+
}
|
|
262
|
+
reserve(bc, 8);
|
|
216
263
|
bc.view.setBigUint64(bc.offset, x, true);
|
|
217
264
|
bc.offset += 8;
|
|
218
265
|
}
|
|
219
|
-
function readU64Safe(bc) {
|
|
220
|
-
const result = readU32(bc) + readU32(bc) *
|
|
221
|
-
|
|
266
|
+
export function readU64Safe(bc) {
|
|
267
|
+
const result = readU32(bc) + readU32(bc) * /* 2**32 */
|
|
268
|
+
4294967296;
|
|
269
|
+
if (!isU64Safe(result)) {
|
|
222
270
|
bc.offset -= 8;
|
|
223
271
|
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
224
272
|
}
|
|
225
273
|
return result;
|
|
226
274
|
}
|
|
227
|
-
function writeU64Safe(bc, x) {
|
|
228
|
-
|
|
275
|
+
export function writeU64Safe(bc, x) {
|
|
276
|
+
if (DEV) {
|
|
277
|
+
assert(isU64Safe(x), TOO_LARGE_NUMBER);
|
|
278
|
+
}
|
|
229
279
|
writeU32(bc, x >>> 0);
|
|
230
|
-
writeU32(bc, x /
|
|
280
|
+
writeU32(bc, x / /* 2**32 */
|
|
281
|
+
4294967296 & /* 2**21-1 */
|
|
282
|
+
2097151);
|
|
231
283
|
}
|
|
232
|
-
function readUint(bc) {
|
|
284
|
+
export function readUint(bc) {
|
|
233
285
|
let low = readU8(bc);
|
|
234
286
|
if (low >= 128) {
|
|
235
287
|
low &= 127;
|
|
@@ -239,7 +291,8 @@ function readUint(bc) {
|
|
|
239
291
|
do {
|
|
240
292
|
byte = readU8(bc);
|
|
241
293
|
low += (byte & 127) * shiftMul;
|
|
242
|
-
shiftMul *=
|
|
294
|
+
shiftMul *= /* 2**7 */
|
|
295
|
+
128;
|
|
243
296
|
byteCount++;
|
|
244
297
|
} while (byte >= 128 && byteCount < 7);
|
|
245
298
|
let height = 0;
|
|
@@ -247,7 +300,8 @@ function readUint(bc) {
|
|
|
247
300
|
while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
|
|
248
301
|
byte = readU8(bc);
|
|
249
302
|
height += (byte & 127) * shiftMul;
|
|
250
|
-
shiftMul *=
|
|
303
|
+
shiftMul *= /* 2**7 */
|
|
304
|
+
128;
|
|
251
305
|
byteCount++;
|
|
252
306
|
}
|
|
253
307
|
if (byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
|
|
@@ -258,14 +312,21 @@ function readUint(bc) {
|
|
|
258
312
|
}
|
|
259
313
|
return BigInt(low);
|
|
260
314
|
}
|
|
261
|
-
function writeUint(bc, x) {
|
|
262
|
-
|
|
315
|
+
export function writeUint(bc, x) {
|
|
316
|
+
const truncated = BigInt.asUintN(64, x);
|
|
317
|
+
if (DEV) {
|
|
318
|
+
assert(truncated === x, TOO_LARGE_NUMBER);
|
|
319
|
+
}
|
|
320
|
+
writeTruncatedUint(bc, truncated);
|
|
321
|
+
}
|
|
322
|
+
function writeTruncatedUint(bc, x) {
|
|
263
323
|
let tmp = Number(BigInt.asUintN(7 * 7, x));
|
|
264
324
|
let rest = Number(x >> BigInt(7 * 7));
|
|
265
325
|
let byteCount = 0;
|
|
266
|
-
while (tmp >= 128 || rest
|
|
326
|
+
while (tmp >= 128 || rest > 0) {
|
|
267
327
|
writeU8(bc, 128 | tmp & 127);
|
|
268
|
-
tmp = Math.floor(tmp /
|
|
328
|
+
tmp = Math.floor(tmp / /* 2**7 */
|
|
329
|
+
128);
|
|
269
330
|
byteCount++;
|
|
270
331
|
if (byteCount === 7) {
|
|
271
332
|
tmp = rest;
|
|
@@ -274,72 +335,89 @@ function writeUint(bc, x) {
|
|
|
274
335
|
}
|
|
275
336
|
writeU8(bc, tmp);
|
|
276
337
|
}
|
|
277
|
-
function
|
|
338
|
+
export function readUintSafe32(bc) {
|
|
278
339
|
let result = readU8(bc);
|
|
279
340
|
if (result >= 128) {
|
|
280
341
|
result &= 127;
|
|
281
|
-
let
|
|
342
|
+
let shift = 7;
|
|
282
343
|
let byteCount = 1;
|
|
283
344
|
let byte;
|
|
284
345
|
do {
|
|
285
346
|
byte = readU8(bc);
|
|
286
|
-
result += (byte & 127)
|
|
287
|
-
|
|
347
|
+
result += (byte & 127) << shift >>> 0;
|
|
348
|
+
shift += 7;
|
|
288
349
|
byteCount++;
|
|
289
|
-
} while (byte >= 128 && byteCount <
|
|
350
|
+
} while (byte >= 128 && byteCount < UINT_SAFE32_MAX_BYTE_COUNT);
|
|
290
351
|
if (byte === 0) {
|
|
291
352
|
bc.offset -= byteCount - 1;
|
|
292
|
-
throw new BareError(
|
|
353
|
+
throw new BareError(
|
|
354
|
+
bc.offset - byteCount + 1,
|
|
355
|
+
NON_CANONICAL_REPRESENTATION
|
|
356
|
+
);
|
|
293
357
|
}
|
|
294
|
-
if (byteCount ===
|
|
358
|
+
if (byteCount === UINT_SAFE32_MAX_BYTE_COUNT && byte > 15) {
|
|
295
359
|
bc.offset -= byteCount - 1;
|
|
296
360
|
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
297
361
|
}
|
|
298
362
|
}
|
|
299
363
|
return result;
|
|
300
364
|
}
|
|
301
|
-
function
|
|
302
|
-
|
|
303
|
-
|
|
365
|
+
export function writeUintSafe32(bc, x) {
|
|
366
|
+
if (DEV) {
|
|
367
|
+
assert(isU32(x), TOO_LARGE_NUMBER);
|
|
368
|
+
}
|
|
369
|
+
let zigZag = x >>> 0;
|
|
370
|
+
while (zigZag >= 128) {
|
|
304
371
|
writeU8(bc, 128 | x & 127);
|
|
305
|
-
|
|
372
|
+
zigZag >>>= 7;
|
|
306
373
|
}
|
|
307
|
-
writeU8(bc,
|
|
308
|
-
}
|
|
309
|
-
export {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
374
|
+
writeU8(bc, zigZag);
|
|
375
|
+
}
|
|
376
|
+
export function readUintSafe(bc) {
|
|
377
|
+
let result = readU8(bc);
|
|
378
|
+
if (result >= 128) {
|
|
379
|
+
result &= 127;
|
|
380
|
+
let shiftMul = (
|
|
381
|
+
/* 2**7 */
|
|
382
|
+
128
|
|
383
|
+
);
|
|
384
|
+
let byteCount = 1;
|
|
385
|
+
let byte;
|
|
386
|
+
do {
|
|
387
|
+
byte = readU8(bc);
|
|
388
|
+
result += (byte & 127) * shiftMul;
|
|
389
|
+
shiftMul *= /* 2**7 */
|
|
390
|
+
128;
|
|
391
|
+
byteCount++;
|
|
392
|
+
} while (byte >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
|
|
393
|
+
if (byte === 0) {
|
|
394
|
+
bc.offset -= byteCount - 1;
|
|
395
|
+
throw new BareError(
|
|
396
|
+
bc.offset - byteCount + 1,
|
|
397
|
+
NON_CANONICAL_REPRESENTATION
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
if (byteCount === INT_SAFE_MAX_BYTE_COUNT && byte > 15) {
|
|
401
|
+
bc.offset -= byteCount - 1;
|
|
402
|
+
throw new BareError(bc.offset, TOO_LARGE_NUMBER);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
return result;
|
|
406
|
+
}
|
|
407
|
+
export function writeUintSafe(bc, x) {
|
|
408
|
+
if (DEV) {
|
|
409
|
+
assert(isU64Safe(x), TOO_LARGE_NUMBER);
|
|
410
|
+
}
|
|
411
|
+
let byteCount = 1;
|
|
412
|
+
let zigZag = x;
|
|
413
|
+
while (zigZag >= 128 && byteCount < INT_SAFE_MAX_BYTE_COUNT) {
|
|
414
|
+
writeU8(bc, 128 | zigZag & 127);
|
|
415
|
+
zigZag = Math.floor(zigZag / /* 2**7 */
|
|
416
|
+
128);
|
|
417
|
+
byteCount++;
|
|
418
|
+
}
|
|
419
|
+
if (byteCount === INT_SAFE_MAX_BYTE_COUNT) {
|
|
420
|
+
zigZag &= 15;
|
|
421
|
+
}
|
|
422
|
+
writeU8(bc, zigZag);
|
|
423
|
+
}
|
package/dist/codec/string.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ByteCursor } from "../core/byte-cursor.js";
|
|
2
2
|
export declare function readString(bc: ByteCursor): string;
|
|
3
3
|
export declare function writeString(bc: ByteCursor, x: string): void;
|
|
4
4
|
export declare function readFixedString(bc: ByteCursor, byteLen: number): string;
|
package/dist/codec/string.js
CHANGED
|
@@ -1,44 +1,54 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
import { BareError } from "../core/bare-error.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import { check, reserve } from "../core/byte-cursor.js";
|
|
4
|
+
import { assert, DEV } from "../util/assert.js";
|
|
5
|
+
import {
|
|
6
|
+
INVALID_UTF8_STRING,
|
|
7
|
+
TEXT_DECODER_THRESHOLD,
|
|
8
|
+
TEXT_ENCODER_THRESHOLD
|
|
9
|
+
} from "../util/constants.js";
|
|
10
|
+
import { isU32 } from "../util/validator.js";
|
|
11
|
+
import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
|
|
12
|
+
import { readUnsafeU8FixedArray, writeU8FixedArray } from "./u8-array.js";
|
|
13
|
+
export function readString(bc) {
|
|
14
|
+
return readFixedString(bc, readUintSafe32(bc));
|
|
8
15
|
}
|
|
9
|
-
function writeString(bc, x) {
|
|
10
|
-
if (x.length <
|
|
16
|
+
export function writeString(bc, x) {
|
|
17
|
+
if (x.length < TEXT_ENCODER_THRESHOLD) {
|
|
11
18
|
const byteLen = utf8ByteLength(x);
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
writeUintSafe32(bc, byteLen);
|
|
20
|
+
reserve(bc, byteLen);
|
|
14
21
|
writeUtf8Js(bc, x);
|
|
15
22
|
} else {
|
|
16
23
|
const strBytes = UTF8_ENCODER.encode(x);
|
|
17
|
-
|
|
24
|
+
writeUintSafe32(bc, strBytes.length);
|
|
18
25
|
writeU8FixedArray(bc, strBytes);
|
|
19
26
|
}
|
|
20
27
|
}
|
|
21
|
-
function readFixedString(bc, byteLen) {
|
|
22
|
-
if (
|
|
28
|
+
export function readFixedString(bc, byteLen) {
|
|
29
|
+
if (DEV) {
|
|
30
|
+
assert(isU32(byteLen));
|
|
31
|
+
}
|
|
32
|
+
if (byteLen < TEXT_DECODER_THRESHOLD) {
|
|
23
33
|
return readUtf8Js(bc, byteLen);
|
|
24
34
|
}
|
|
25
35
|
try {
|
|
26
|
-
return UTF8_DECODER.decode(bc
|
|
27
|
-
} catch (
|
|
36
|
+
return UTF8_DECODER.decode(readUnsafeU8FixedArray(bc, byteLen));
|
|
37
|
+
} catch (_cause) {
|
|
28
38
|
throw new BareError(bc.offset, INVALID_UTF8_STRING);
|
|
29
39
|
}
|
|
30
40
|
}
|
|
31
|
-
function writeFixedString(bc, x) {
|
|
32
|
-
if (x.length <
|
|
41
|
+
export function writeFixedString(bc, x) {
|
|
42
|
+
if (x.length < TEXT_ENCODER_THRESHOLD) {
|
|
33
43
|
const byteLen = utf8ByteLength(x);
|
|
34
|
-
|
|
44
|
+
reserve(bc, byteLen);
|
|
35
45
|
writeUtf8Js(bc, x);
|
|
36
46
|
} else {
|
|
37
47
|
writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
|
|
38
48
|
}
|
|
39
49
|
}
|
|
40
50
|
function readUtf8Js(bc, byteLen) {
|
|
41
|
-
|
|
51
|
+
check(bc, byteLen);
|
|
42
52
|
let result = "";
|
|
43
53
|
const bytes = bc.bytes;
|
|
44
54
|
let offset = bc.offset;
|
|
@@ -51,18 +61,29 @@ function readUtf8Js(bc, byteLen) {
|
|
|
51
61
|
if (offset < upperOffset && codePoint < 224) {
|
|
52
62
|
const byte2 = bytes[offset++];
|
|
53
63
|
codePoint = (byte1 & 31) << 6 | byte2 & 63;
|
|
54
|
-
malformed = codePoint >> 7 === 0 ||
|
|
64
|
+
malformed = codePoint >> 7 === 0 || // non-canonical char
|
|
65
|
+
byte1 >> 5 !== 6 || // invalid tag
|
|
66
|
+
byte2 >> 6 !== 2;
|
|
55
67
|
} else if (offset + 1 < upperOffset && codePoint < 240) {
|
|
56
68
|
const byte2 = bytes[offset++];
|
|
57
69
|
const byte3 = bytes[offset++];
|
|
58
70
|
codePoint = (byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63;
|
|
59
|
-
malformed = codePoint >> 11 === 0 ||
|
|
71
|
+
malformed = codePoint >> 11 === 0 || // non-canonical char or missing data
|
|
72
|
+
codePoint >> 11 === 27 || // surrogate char (0xD800 <= codePoint <= 0xDFFF)
|
|
73
|
+
byte1 >> 4 !== 14 || // invalid tag
|
|
74
|
+
byte2 >> 6 !== 2 || // invalid tag
|
|
75
|
+
byte3 >> 6 !== 2;
|
|
60
76
|
} else if (offset + 2 < upperOffset) {
|
|
61
77
|
const byte2 = bytes[offset++];
|
|
62
78
|
const byte3 = bytes[offset++];
|
|
63
79
|
const byte4 = bytes[offset++];
|
|
64
80
|
codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
|
|
65
|
-
malformed = codePoint >> 16 === 0 ||
|
|
81
|
+
malformed = codePoint >> 16 === 0 || // non-canonical char or missing data
|
|
82
|
+
codePoint > 1114111 || // too large code point
|
|
83
|
+
byte1 >> 3 !== 30 || // invalid tag
|
|
84
|
+
byte2 >> 6 !== 2 || // invalid tag
|
|
85
|
+
byte3 >> 6 !== 2 || // invalid tag
|
|
86
|
+
byte4 >> 6 !== 2;
|
|
66
87
|
}
|
|
67
88
|
if (malformed) {
|
|
68
89
|
throw new BareError(bc.offset, INVALID_UTF8_STRING);
|
|
@@ -115,12 +136,5 @@ function utf8ByteLength(s) {
|
|
|
115
136
|
}
|
|
116
137
|
return result;
|
|
117
138
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
export {
|
|
121
|
-
readFixedString,
|
|
122
|
-
readString,
|
|
123
|
-
writeFixedString,
|
|
124
|
-
writeString
|
|
125
|
-
};
|
|
126
|
-
//# sourceMappingURL=string.js.map
|
|
139
|
+
const UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
|
|
140
|
+
const UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
|