@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.
@@ -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 decodeBool(bc) {
19
- const val = decodeU8(bc);
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 encodeBool(bc, x) {
27
- encodeU8(bc, x ? 1 : 0);
26
+ function writeBool(bc, x) {
27
+ writeU8(bc, x ? 1 : 0);
28
28
  }
29
- function decodeF32(bc) {
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 encodeF32(bc, x) {
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 decodeF64(bc) {
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 encodeF64(bc, x) {
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 decodeI8(bc) {
60
+ function readI8(bc) {
61
61
  bc.check(1);
62
62
  return bc.view.getInt8(bc.offset++);
63
63
  }
64
- function encodeI8(bc, x) {
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 decodeI16(bc) {
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 encodeI16(bc, x) {
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 decodeI32(bc) {
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 encodeI32(bc, x) {
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 decodeI64(bc) {
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 encodeI64(bc, x) {
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 decodeI64Safe(bc) {
106
- const result = decodeU32(bc) + decodeI32(bc) * 4294967296;
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 encodeI64Safe(bc, x) {
113
+ function writeI64Safe(bc, x) {
114
114
  assert(Number.isSafeInteger(x), TOO_LARGE_NUMBER);
115
115
  const lowest32 = x >>> 0;
116
- encodeU32(bc, lowest32);
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
- encodeU32(bc, highest32);
124
+ writeU32(bc, highest32);
125
125
  }
126
- function decodeInt(bc) {
127
- const zigZag = decodeUint(bc);
126
+ function readInt(bc) {
127
+ const zigZag = readUint(bc);
128
128
  return zigZag >> BigInt(1) ^ -(zigZag & BigInt(1));
129
129
  }
130
- function encodeInt(bc, x) {
130
+ function writeInt(bc, x) {
131
131
  assert(isI64(x), TOO_LARGE_NUMBER);
132
132
  const zigZag = x >> BigInt(63) ^ x << BigInt(1);
133
- encodeUint(bc, zigZag);
133
+ writeUint(bc, zigZag);
134
134
  }
135
135
  var INT_SAFE_MAX_BYTE_COUNT = 8;
136
- function decodeIntSafe(bc) {
137
- const firstByte = decodeU8(bc);
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 = decodeU8(bc);
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 encodeIntSafe(bc, x) {
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
- encodeU8(bc, 128 | firstByte);
174
- encodeUintSafe(bc, x);
173
+ writeU8(bc, 128 | firstByte);
174
+ writeUintSafe(bc, x);
175
175
  } else {
176
- encodeU8(bc, firstByte);
176
+ writeU8(bc, firstByte);
177
177
  }
178
178
  }
179
- function decodeU8(bc) {
179
+ function readU8(bc) {
180
180
  bc.check(1);
181
- return bc.view.getUint8(bc.offset++);
181
+ return bc.bytes[bc.offset++];
182
182
  }
183
- function encodeU8(bc, x) {
183
+ function writeU8(bc, x) {
184
184
  assert(isU8(x), TOO_LARGE_NUMBER);
185
185
  bc.reserve(1);
186
- bc.view.setUint8(bc.offset++, x);
186
+ bc.bytes[bc.offset++] = x;
187
187
  }
188
- function decodeU16(bc) {
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 encodeU16(bc, x) {
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 decodeU32(bc) {
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 encodeU32(bc, x) {
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 decodeU64(bc) {
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 encodeU64(bc, x) {
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 decodeU64Safe(bc) {
225
- const result = decodeU32(bc) + decodeU32(bc) * 4294967296;
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 encodeU64Safe(bc, x) {
232
+ function writeU64Safe(bc, x) {
233
233
  assert(isSafeU64(x), TOO_LARGE_NUMBER);
234
- encodeU32(bc, x >>> 0);
235
- encodeU32(bc, x / 4294967296 >>> 0);
234
+ writeU32(bc, x >>> 0);
235
+ writeU32(bc, x / 4294967296 >>> 0);
236
236
  }
237
237
  var UINT_MAX_BYTE_COUNT = 10;
238
- function decodeUint(bc) {
239
- let byteCount = 0;
240
- let byte;
241
- let low = 0;
242
- let shiftMul = 1;
243
- do {
244
- byte = decodeU8(bc);
245
- low += (byte & 127) * shiftMul;
246
- shiftMul *= 128;
247
- byteCount++;
248
- } while (byte >= 128 && byteCount < 7);
249
- let height = 0;
250
- shiftMul = 1;
251
- while (byte >= 128 && byteCount < UINT_MAX_BYTE_COUNT) {
252
- byte = decodeU8(bc);
253
- height += (byte & 127) * shiftMul;
254
- shiftMul *= 128;
255
- byteCount++;
256
- }
257
- if (byteCount !== 1 && byte === 0 || byteCount === UINT_MAX_BYTE_COUNT && byte > 1) {
258
- bc.offset -= byteCount;
259
- throw new BareError(bc.offset, NON_CANONICAL_REPRESENTATION);
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) + (BigInt(height) << BigInt(7 * 7));
265
+ return BigInt(low);
262
266
  }
263
- function encodeUint(bc, x) {
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
- encodeU8(bc, 128 | tmp & 127);
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
- encodeU8(bc, tmp);
281
+ writeU8(bc, tmp);
278
282
  }
279
283
  var UINT_SAFE_MAX_BYTE_COUNT = 8;
280
- function decodeUintSafe(bc) {
281
- let result = 0;
282
- let shiftMul = 1;
283
- let byteCount = 0;
284
- let byte;
285
- do {
286
- byte = decodeU8(bc);
287
- result += (byte & 127) * shiftMul;
288
- shiftMul *= 128;
289
- byteCount++;
290
- } while (byte >= 128 && byteCount < UINT_SAFE_MAX_BYTE_COUNT);
291
- if (byteCount !== 1 && byte === 0) {
292
- bc.offset -= byteCount - 1;
293
- throw new BareError(bc.offset - byteCount + 1, NON_CANONICAL_REPRESENTATION);
294
- }
295
- if (byteCount === UINT_SAFE_MAX_BYTE_COUNT && byte > 15) {
296
- bc.offset -= byteCount - 1;
297
- throw new BareError(bc.offset, TOO_LARGE_NUMBER);
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 encodeUintSafe(bc, x) {
308
+ function writeUintSafe(bc, x) {
302
309
  assert(isSafeU64(x), TOO_LARGE_NUMBER);
303
310
  while (x >= 128) {
304
- encodeU8(bc, 128 | x & 127);
311
+ writeU8(bc, 128 | x & 127);
305
312
  x = Math.floor(x / 128);
306
313
  }
307
- encodeU8(bc, x);
314
+ writeU8(bc, x);
308
315
  }
309
- function decodeVoid(_dc) {
316
+ function readVoid(_dc) {
310
317
  return void 0;
311
318
  }
312
- function encodeVoid(_dc, _x) {
319
+ function writeVoid(_dc, _x) {
313
320
  }
314
321
  export {
315
- decodeBool,
316
- decodeF32,
317
- decodeF64,
318
- decodeI16,
319
- decodeI32,
320
- decodeI64,
321
- decodeI64Safe,
322
- decodeI8,
323
- decodeInt,
324
- decodeIntSafe,
325
- decodeU16,
326
- decodeU32,
327
- decodeU64,
328
- decodeU64Safe,
329
- decodeU8,
330
- decodeUint,
331
- decodeUintSafe,
332
- decodeVoid,
333
- encodeBool,
334
- encodeF32,
335
- encodeF64,
336
- encodeI16,
337
- encodeI32,
338
- encodeI64,
339
- encodeI64Safe,
340
- encodeI8,
341
- encodeInt,
342
- encodeIntSafe,
343
- encodeU16,
344
- encodeU32,
345
- encodeU64,
346
- encodeU64Safe,
347
- encodeU8,
348
- encodeUint,
349
- encodeUintSafe,
350
- encodeVoid
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
@@ -1,3 +1,5 @@
1
1
  import type { ByteCursor } from "../core/index.js";
2
- export declare function decodeString(bc: ByteCursor): string;
3
- export declare function encodeString(bc: ByteCursor, x: string): void;
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;
@@ -1,111 +1,113 @@
1
1
  // src/codec/string.ts
2
2
  import { BareError } from "../core/bare-error.js";
3
- import { decodeUintSafe, encodeUintSafe } from "./primitive.js";
4
- function decodeString(bc) {
5
- const strBytesLen = decodeUintSafe(bc);
6
- const strBytes = bc.read(strBytesLen);
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 strBytesLen < bc.config.textDecoderThreshold ? decodeUtf8Js(strBytes) : UTF8_DECODER.decode(strBytes);
26
+ return UTF8_DECODER.decode(bc.read(byteLen));
9
27
  } catch (cause) {
10
- throw new BareError(bc.offset - strBytesLen, "invalid UTF-8 string", {
11
- cause
12
- });
28
+ throw new BareError(bc.offset, INVALID_UTF8_STRING);
13
29
  }
14
30
  }
15
- function encodeString(bc, x) {
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
- encodeUtf8Js(bc, x);
35
+ writeUtf8Js(bc, x);
21
36
  } else {
22
- const strBytes = UTF8_ENCODER.encode(x);
23
- encodeUintSafe(bc, strBytes.length);
24
- bc.write(strBytes);
37
+ writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
25
38
  }
26
39
  }
27
- function decodeUtf8Js(bytes) {
28
- const bytesLen = bytes.length;
40
+ function readUtf8Js(bc, byteLen) {
41
+ bc.check(byteLen);
29
42
  let result = "";
30
- let i = 0;
31
- while (i < bytesLen) {
32
- let codePoint = bytes[i++] | 0;
33
- switch (Math.clz32(~codePoint << 24)) {
34
- case 0:
35
- break;
36
- case 2: {
37
- if (i < bytesLen) {
38
- codePoint = (codePoint & 31) << 6 | bytes[i] & 63;
39
- }
40
- if (codePoint >> 7 === 0 || bytes[i] >> 6 !== 2) {
41
- throw TypeError("Decoding failed");
42
- }
43
- i += 1;
44
- break;
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
- case 3: {
47
- if (i + 1 < bytesLen) {
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 encodeUtf8Js(bc, s) {
74
- const sLen = s.length;
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 < sLen) {
80
+ while (i < s.length) {
79
81
  const codePoint = s.codePointAt(i++);
80
82
  if (codePoint < 128) {
81
- view.setUint8(offset++, codePoint);
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
- view.setUint8(offset++, 240 | codePoint >> 18);
91
- view.setUint8(offset++, 128 | codePoint >> 12 & 63);
92
- view.setUint8(offset++, 128 | codePoint >> 6 & 63);
93
- view.setUint8(offset++, 128 | codePoint & 63);
94
- i++;
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
- const sLen = s.length;
101
- let result = sLen;
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 >= 128) {
106
+ if (codePoint > 127) {
105
107
  result++;
106
- if (codePoint >= 2048) {
108
+ if (codePoint > 2047) {
107
109
  result++;
108
- if (codePoint >= 65536) {
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
- decodeString,
120
- encodeString
121
+ readFixedString,
122
+ readString,
123
+ writeFixedString,
124
+ writeString
121
125
  };
122
126
  //# sourceMappingURL=string.js.map