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