@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.
Files changed (57) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +7 -8
  3. package/dist/codec/data.d.ts +1 -1
  4. package/dist/codec/data.js +10 -12
  5. package/dist/codec/float-array.d.ts +7 -12
  6. package/dist/codec/float-array.js +53 -45
  7. package/dist/codec/i16-array.d.ts +4 -7
  8. package/dist/codec/i16-array.js +35 -27
  9. package/dist/codec/i32-array.d.ts +4 -7
  10. package/dist/codec/i32-array.js +35 -27
  11. package/dist/codec/i64-array.d.ts +4 -7
  12. package/dist/codec/i64-array.js +35 -27
  13. package/dist/codec/i8-array.d.ts +3 -3
  14. package/dist/codec/i8-array.js +12 -14
  15. package/dist/codec/primitive.d.ts +3 -1
  16. package/dist/codec/primitive.js +227 -149
  17. package/dist/codec/string.d.ts +1 -1
  18. package/dist/codec/string.js +45 -31
  19. package/dist/codec/u16-array.d.ts +4 -7
  20. package/dist/codec/u16-array.js +35 -27
  21. package/dist/codec/u32-array.d.ts +4 -7
  22. package/dist/codec/u32-array.js +35 -27
  23. package/dist/codec/u64-array.d.ts +4 -7
  24. package/dist/codec/u64-array.js +35 -27
  25. package/dist/codec/u8-array.d.ts +9 -3
  26. package/dist/codec/u8-array.js +23 -18
  27. package/dist/codec/u8-clamped-array.d.ts +3 -3
  28. package/dist/codec/u8-clamped-array.js +13 -15
  29. package/dist/core/bare-error.d.ts +4 -1
  30. package/dist/core/bare-error.js +4 -8
  31. package/dist/core/byte-cursor.d.ts +22 -28
  32. package/dist/core/byte-cursor.js +33 -29
  33. package/dist/core/config.d.ts +4 -10
  34. package/dist/core/config.js +15 -20
  35. package/dist/env/dev.d.ts +1 -0
  36. package/dist/env/dev.development.d.ts +1 -0
  37. package/dist/env/dev.development.js +2 -0
  38. package/dist/env/dev.js +2 -0
  39. package/dist/env/dev.node.d.ts +1 -0
  40. package/dist/env/dev.node.js +2 -0
  41. package/dist/index.cjs +545 -324
  42. package/dist/index.d.cts +16 -0
  43. package/dist/index.d.ts +16 -2
  44. package/dist/index.js +17 -4
  45. package/dist/util/assert.d.ts +6 -8
  46. package/dist/util/assert.js +10 -13
  47. package/dist/util/constants.d.ts +4 -7
  48. package/dist/util/constants.js +11 -30
  49. package/dist/util/validator.d.ts +1 -1
  50. package/dist/util/validator.js +12 -24
  51. package/package.json +31 -25
  52. package/dist/codec/index.d.ts +0 -13
  53. package/dist/codec/index.js +0 -15
  54. package/dist/core/index.d.ts +0 -3
  55. package/dist/core/index.js +0 -5
  56. package/dist/util/util.d.ts +0 -1
  57. package/dist/util/util.js +0 -6
@@ -1,144 +1,174 @@
1
- // src/codec/primitive.ts
2
- import { assert } from "../util/assert.js";
3
- import { BareError } from "../core/index.js";
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
- UINT_SAFE_MAX_BYTE_COUNT
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
- isI8,
16
- isSafeU64,
17
+ isU8,
17
18
  isU16,
18
19
  isU32,
19
20
  isU64,
20
- isU8
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 !== 0;
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
- bc.check(4);
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
- bc.reserve(4);
40
+ export function writeF32(bc, x) {
41
+ reserve(bc, 4);
41
42
  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
+ 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
- bc.check(8);
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
- bc.reserve(8);
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
- bc.check(1);
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
- assert(isI8(x), TOO_LARGE_NUMBER);
62
- bc.reserve(1);
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
- bc.check(2);
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
- assert(isI16(x), TOO_LARGE_NUMBER);
73
- bc.reserve(2);
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
- bc.check(4);
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
- assert(isI32(x), TOO_LARGE_NUMBER);
85
- bc.reserve(4);
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
- bc.check(8);
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
- assert(isI64(x), TOO_LARGE_NUMBER);
97
- bc.reserve(8);
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) * 4294967296;
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
- assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
111
- const lowest32 = x >>> 0;
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 / 4294967296 | 0;
130
+ let highest32 = x / /* 2**32 */
131
+ 4294967296 | 0;
114
132
  if (x < 0) {
115
- highest32 = ~Math.abs(highest32) >>> 0;
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
- assert(isI64(x), TOO_LARGE_NUMBER);
128
- const zigZag = x >> BigInt(63) ^ x << BigInt(1);
129
- writeUint(bc, zigZag);
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 = 64;
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 *= 128;
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
- 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);
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, firstByte);
210
+ writeU8(bc, first7Bits);
172
211
  }
173
212
  }
174
- function readU8(bc) {
175
- bc.check(1);
213
+ export function readU8(bc) {
214
+ check(bc, 1);
176
215
  return bc.bytes[bc.offset++];
177
216
  }
178
- function writeU8(bc, x) {
179
- assert(isU8(x), TOO_LARGE_NUMBER);
180
- bc.reserve(1);
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
- bc.check(2);
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
- assert(isU16(x), TOO_LARGE_NUMBER);
191
- bc.reserve(2);
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
- bc.check(4);
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
- assert(isU32(x), TOO_LARGE_NUMBER);
203
- bc.reserve(4);
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
- bc.check(8);
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
- assert(isU64(x), TOO_LARGE_NUMBER);
215
- bc.reserve(8);
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) * 4294967296;
221
- if (!isSafeU64(result)) {
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
- assert(isSafeU64(x), TOO_LARGE_NUMBER);
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 / 4294967296 >>> 0);
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 *= 128;
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 *= 128;
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
- assert(isU64(x), TOO_LARGE_NUMBER);
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 !== 0) {
326
+ while (tmp >= 128 || rest > 0) {
267
327
  writeU8(bc, 128 | tmp & 127);
268
- tmp = Math.floor(tmp / 128);
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 readUintSafe(bc) {
338
+ export function readUintSafe32(bc) {
278
339
  let result = readU8(bc);
279
340
  if (result >= 128) {
280
341
  result &= 127;
281
- let shiftMul = 128;
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) * shiftMul;
287
- shiftMul *= 128;
347
+ result += (byte & 127) << shift >>> 0;
348
+ shift += 7;
288
349
  byteCount++;
289
- } while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
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(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
353
+ throw new BareError(
354
+ bc.offset - byteCount + 1,
355
+ NON_CANONICAL_REPRESENTATION
356
+ );
293
357
  }
294
- if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
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 writeUintSafe(bc, x) {
302
- assert(isSafeU64(x), TOO_LARGE_NUMBER);
303
- while (x >= 128) {
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
- x = Math.floor(x / 128);
372
+ zigZag >>>= 7;
306
373
  }
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
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
+ }
@@ -1,4 +1,4 @@
1
- import type { ByteCursor } from "../core/index.js";
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;
@@ -1,44 +1,54 @@
1
- // src/codec/string.ts
1
+ "use strict";
2
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));
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 < bc.config.textEncoderThreshold) {
16
+ export function writeString(bc, x) {
17
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
11
18
  const byteLen = utf8ByteLength(x);
12
- writeUintSafe(bc, byteLen);
13
- bc.reserve(byteLen);
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
- writeUintSafe(bc, strBytes.length);
24
+ writeUintSafe32(bc, strBytes.length);
18
25
  writeU8FixedArray(bc, strBytes);
19
26
  }
20
27
  }
21
- function readFixedString(bc, byteLen) {
22
- if (byteLen < bc.config.textDecoderThreshold) {
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.read(byteLen));
27
- } catch (cause) {
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 < bc.config.textEncoderThreshold) {
41
+ export function writeFixedString(bc, x) {
42
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
33
43
  const byteLen = utf8ByteLength(x);
34
- bc.reserve(byteLen);
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
- bc.check(byteLen);
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 || byte1 >> 5 !== 6 || byte2 >> 6 !== 2;
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 || codePoint >> 11 === 27 || byte1 >> 4 !== 14 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2;
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 || codePoint > 1114111 || byte1 >> 3 !== 30 || byte2 >> 6 !== 2 || byte3 >> 6 !== 2 || byte4 >> 6 !== 2;
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
- 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
139
+ const UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
140
+ const UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();