@bare-ts/lib 0.5.0 → 0.6.1

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 (62) hide show
  1. package/README.md +3 -3
  2. package/dist/codec/data.d.ts +1 -1
  3. package/dist/codec/data.js +10 -14
  4. package/dist/codec/f32-array.d.ts +5 -0
  5. package/dist/codec/f32-array.js +51 -0
  6. package/dist/codec/f64-array.d.ts +5 -0
  7. package/dist/codec/f64-array.js +51 -0
  8. package/dist/codec/{primitive.d.ts → fixed-primitive.d.ts} +1 -11
  9. package/dist/codec/fixed-primitive.js +197 -0
  10. package/dist/codec/i16-array.d.ts +1 -1
  11. package/dist/codec/i16-array.js +31 -32
  12. package/dist/codec/i32-array.d.ts +1 -1
  13. package/dist/codec/i32-array.js +31 -32
  14. package/dist/codec/i64-array.d.ts +1 -1
  15. package/dist/codec/i64-array.js +31 -32
  16. package/dist/codec/i8-array.d.ts +1 -1
  17. package/dist/codec/i8-array.js +11 -10
  18. package/dist/codec/int.d.ts +5 -0
  19. package/dist/codec/int.js +78 -0
  20. package/dist/codec/string.d.ts +1 -1
  21. package/dist/codec/string.js +143 -114
  22. package/dist/codec/u16-array.d.ts +1 -1
  23. package/dist/codec/u16-array.js +31 -32
  24. package/dist/codec/u32-array.d.ts +1 -1
  25. package/dist/codec/u32-array.js +31 -32
  26. package/dist/codec/u64-array.d.ts +1 -1
  27. package/dist/codec/u64-array.js +31 -32
  28. package/dist/codec/u8-array.d.ts +1 -1
  29. package/dist/codec/u8-array.js +25 -19
  30. package/dist/codec/u8-clamped-array.d.ts +1 -1
  31. package/dist/codec/u8-clamped-array.js +11 -10
  32. package/dist/codec/uint.d.ts +8 -0
  33. package/dist/codec/uint.js +138 -0
  34. package/dist/core/bare-error.js +12 -8
  35. package/dist/core/byte-cursor.d.ts +1 -1
  36. package/dist/core/byte-cursor.js +102 -30
  37. package/dist/core/config.js +12 -17
  38. package/dist/index.cjs +147 -90
  39. package/dist/index.d.cts +21 -16
  40. package/dist/index.d.ts +21 -16
  41. package/dist/index.js +9 -3
  42. package/dist/util/assert.d.ts +10 -1
  43. package/dist/util/assert.js +24 -13
  44. package/dist/util/constants.js +6 -2
  45. package/dist/util/validator.d.ts +27 -0
  46. package/dist/util/validator.js +38 -10
  47. package/imports/dev.d.ts +4 -0
  48. package/imports/dev.development.d.ts +4 -0
  49. package/imports/dev.development.js +4 -0
  50. package/imports/dev.js +4 -0
  51. package/imports/dev.node.d.ts +4 -0
  52. package/imports/dev.node.js +4 -0
  53. package/package.json +20 -16
  54. package/dist/codec/float-array.d.ts +0 -9
  55. package/dist/codec/float-array.js +0 -93
  56. package/dist/codec/primitive.js +0 -423
  57. package/dist/env/dev.d.ts +0 -1
  58. package/dist/env/dev.development.d.ts +0 -1
  59. package/dist/env/dev.development.js +0 -2
  60. package/dist/env/dev.js +0 -2
  61. package/dist/env/dev.node.d.ts +0 -1
  62. package/dist/env/dev.node.js +0 -2
@@ -1,4 +1,4 @@
1
- import type { ByteCursor } from "../core/byte-cursor.js";
1
+ import type { ByteCursor } from "../core/byte-cursor.ts";
2
2
  export declare function readI8Array(bc: ByteCursor): Int8Array<ArrayBuffer>;
3
3
  export declare function writeI8Array(bc: ByteCursor, x: Int8Array): void;
4
4
  export declare function readI8FixedArray(bc: ByteCursor, len: number): Int8Array<ArrayBuffer>;
@@ -1,22 +1,23 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
2
3
  import { assert, DEV } from "../util/assert.js";
3
4
  import { isU32 } from "../util/validator.js";
4
5
  import { readFixedData } from "./data.js";
5
- import { readUintSafe, writeUintSafe32 } from "./primitive.js";
6
6
  import { writeU8FixedArray } from "./u8-array.js";
7
+ import { readUintSafe, writeUintSafe32 } from "./uint.js";
7
8
  export function readI8Array(bc) {
8
- return readI8FixedArray(bc, readUintSafe(bc));
9
+ return readI8FixedArray(bc, readUintSafe(bc));
9
10
  }
10
11
  export function writeI8Array(bc, x) {
11
- writeUintSafe32(bc, x.length);
12
- writeI8FixedArray(bc, x);
12
+ writeUintSafe32(bc, x.length);
13
+ writeI8FixedArray(bc, x);
13
14
  }
14
15
  export function readI8FixedArray(bc, len) {
15
- if (DEV) {
16
- assert(isU32(len));
17
- }
18
- return new Int8Array(readFixedData(bc, len));
16
+ if (DEV) {
17
+ assert(isU32(len));
18
+ }
19
+ return new Int8Array(readFixedData(bc, len));
19
20
  }
20
21
  export function writeI8FixedArray(bc, x) {
21
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
22
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
22
23
  }
@@ -0,0 +1,5 @@
1
+ import type { ByteCursor } from "../core/byte-cursor.ts";
2
+ export declare function readInt(bc: ByteCursor): bigint;
3
+ export declare function writeInt(bc: ByteCursor, x: bigint): void;
4
+ export declare function readIntSafe(bc: ByteCursor): number;
5
+ export declare function writeIntSafe(bc: ByteCursor, x: number): void;
@@ -0,0 +1,78 @@
1
+ import { BareError } from "../core/bare-error.js";
2
+ import { assert, DEV } from "../util/assert.js";
3
+ import { INT_SAFE_MAX_BYTE_COUNT, TOO_LARGE_NUMBER } from "../util/constants.js";
4
+ import { readU8, writeU8 } from "./fixed-primitive.js";
5
+ import { readUint, writeUintSafe, writeUintTruncated } from "./uint.js";
6
+ export function readInt(bc) {
7
+ const zigZag = readUint(bc);
8
+ return (zigZag >> BigInt(1)) ^ -(zigZag & BigInt(1));
9
+ }
10
+ export function writeInt(bc, x) {
11
+ // truncate to mimic DataView#setBigInt64
12
+ // this is useful when assertions are skipped
13
+ const truncated = BigInt.asIntN(64, x);
14
+ if (DEV) {
15
+ assert(truncated === x, TOO_LARGE_NUMBER);
16
+ }
17
+ const zigZag = (truncated >> BigInt(63)) ^ (truncated << BigInt(1));
18
+ writeUintTruncated(bc, zigZag);
19
+ }
20
+ export function readIntSafe(bc) {
21
+ const firstByte = readU8(bc);
22
+ let result = (firstByte & 0x7f) >> 1;
23
+ if (firstByte >= 0x80) {
24
+ let shiftMul = /* 2**6 */ 0x40;
25
+ let byteCount = 1;
26
+ let byte;
27
+ do {
28
+ byte = readU8(bc);
29
+ result += (byte & 0x7f) * shiftMul;
30
+ shiftMul *= /* 2**7 */ 0x80;
31
+ byteCount++;
32
+ } while (byte >= 0x80 && byteCount < INT_SAFE_MAX_BYTE_COUNT);
33
+ if (byte === 0) {
34
+ bc.offset -= byteCount - 1;
35
+ throw new BareError(bc.offset, "must be canonical");
36
+ }
37
+ if (byteCount === INT_SAFE_MAX_BYTE_COUNT &&
38
+ (byte > 0x1f || firstByte === 0xff)) {
39
+ // First byte must not be equal to 0xff in order to exclude -2**53
40
+ // Number.MIN_SAFE_INTEGER equals to -(2**53 - 1)
41
+ bc.offset -= byteCount - 1;
42
+ throw new BareError(bc.offset, TOO_LARGE_NUMBER);
43
+ }
44
+ }
45
+ const isNeg = (firstByte & 1) === 1;
46
+ if (isNeg) {
47
+ result = -result - 1;
48
+ }
49
+ return result;
50
+ }
51
+ export function writeIntSafe(bc, x) {
52
+ const sign = x < 0 ? 1 : 0;
53
+ let zigZag = x < 0 ? -(x + 1) : x;
54
+ let first7Bits = ((zigZag & 0x3f) << 1) | sign;
55
+ zigZag = Math.floor(zigZag / /* 2**6 */ 0x40);
56
+ if (zigZag > 0) {
57
+ if (!Number.isSafeInteger(x)) {
58
+ if (DEV) {
59
+ assert(false, TOO_LARGE_NUMBER);
60
+ }
61
+ // keep only the remaining 53 - 6 = 47 bits
62
+ // this is useful when assertions are skipped
63
+ const low = zigZag & 0x7fff;
64
+ const high = ((zigZag / 0x8000) >>> 0) * 0x8000;
65
+ if (first7Bits === 0x7f && low === 0x7fff && high === 4294967295) {
66
+ // maps -2**53 to Number.MIN_SAFE_INTEGER
67
+ // this is useful when assertions are skipped
68
+ first7Bits &= ~0b10;
69
+ }
70
+ zigZag = high + low;
71
+ }
72
+ writeU8(bc, 0x80 | first7Bits);
73
+ writeUintSafe(bc, zigZag);
74
+ }
75
+ else {
76
+ writeU8(bc, first7Bits);
77
+ }
78
+ }
@@ -1,4 +1,4 @@
1
- import { type ByteCursor } from "../core/byte-cursor.js";
1
+ import { type ByteCursor } from "../core/byte-cursor.ts";
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,140 +1,169 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
2
3
  import { BareError } from "../core/bare-error.js";
3
4
  import { check, reserve } from "../core/byte-cursor.js";
4
5
  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";
6
+ import { INVALID_UTF8_STRING, TEXT_DECODER_THRESHOLD, TEXT_ENCODER_THRESHOLD, } from "../util/constants.js";
10
7
  import { isU32 } from "../util/validator.js";
11
- import { readUintSafe32, writeUintSafe32 } from "./primitive.js";
12
8
  import { readUnsafeU8FixedArray, writeU8FixedArray } from "./u8-array.js";
9
+ import { readUintSafe32, writeUintSafe32 } from "./uint.js";
13
10
  export function readString(bc) {
14
- return readFixedString(bc, readUintSafe32(bc));
11
+ return readFixedString(bc, readUintSafe32(bc));
15
12
  }
16
13
  export function writeString(bc, x) {
17
- if (x.length < TEXT_ENCODER_THRESHOLD) {
18
- const byteLen = utf8ByteLength(x);
19
- writeUintSafe32(bc, byteLen);
20
- reserve(bc, byteLen);
21
- writeUtf8Js(bc, x);
22
- } else {
23
- const strBytes = UTF8_ENCODER.encode(x);
24
- writeUintSafe32(bc, strBytes.length);
25
- writeU8FixedArray(bc, strBytes);
26
- }
14
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
15
+ const byteLen = utf8ByteLength(x);
16
+ writeUintSafe32(bc, byteLen);
17
+ reserve(bc, byteLen);
18
+ writeUtf8Js(bc, x);
19
+ }
20
+ else {
21
+ const strBytes = UTF8_ENCODER.encode(x);
22
+ writeUintSafe32(bc, strBytes.length);
23
+ writeU8FixedArray(bc, strBytes);
24
+ }
27
25
  }
28
26
  export function readFixedString(bc, byteLen) {
29
- if (DEV) {
30
- assert(isU32(byteLen));
31
- }
32
- if (byteLen < TEXT_DECODER_THRESHOLD) {
33
- return readUtf8Js(bc, byteLen);
34
- }
35
- try {
36
- return UTF8_DECODER.decode(readUnsafeU8FixedArray(bc, byteLen));
37
- } catch (_cause) {
38
- throw new BareError(bc.offset, INVALID_UTF8_STRING);
39
- }
27
+ if (DEV) {
28
+ assert(isU32(byteLen));
29
+ }
30
+ if (byteLen < TEXT_DECODER_THRESHOLD) {
31
+ return readUtf8Js(bc, byteLen);
32
+ }
33
+ try {
34
+ return UTF8_DECODER.decode(readUnsafeU8FixedArray(bc, byteLen));
35
+ }
36
+ catch (_cause) {
37
+ throw new BareError(bc.offset, INVALID_UTF8_STRING);
38
+ }
40
39
  }
41
40
  export function writeFixedString(bc, x) {
42
- if (x.length < TEXT_ENCODER_THRESHOLD) {
43
- const byteLen = utf8ByteLength(x);
44
- reserve(bc, byteLen);
45
- writeUtf8Js(bc, x);
46
- } else {
47
- writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
48
- }
41
+ if (x.length < TEXT_ENCODER_THRESHOLD) {
42
+ const byteLen = utf8ByteLength(x);
43
+ reserve(bc, byteLen);
44
+ writeUtf8Js(bc, x);
45
+ }
46
+ else {
47
+ writeU8FixedArray(bc, UTF8_ENCODER.encode(x));
48
+ }
49
49
  }
50
50
  function readUtf8Js(bc, byteLen) {
51
- check(bc, byteLen);
52
- let result = "";
53
- const bytes = bc.bytes;
54
- let offset = bc.offset;
55
- const upperOffset = offset + byteLen;
56
- while (offset < upperOffset) {
57
- let codePoint = bytes[offset++];
58
- if (codePoint > 127) {
59
- let malformed = true;
60
- const byte1 = codePoint;
61
- if (offset < upperOffset && codePoint < 224) {
62
- const byte2 = bytes[offset++];
63
- codePoint = (byte1 & 31) << 6 | byte2 & 63;
64
- malformed = codePoint >> 7 === 0 || // non-canonical char
65
- byte1 >> 5 !== 6 || // invalid tag
66
- byte2 >> 6 !== 2;
67
- } else if (offset + 1 < upperOffset && codePoint < 240) {
68
- const byte2 = bytes[offset++];
69
- const byte3 = bytes[offset++];
70
- codePoint = (byte1 & 15) << 12 | (byte2 & 63) << 6 | byte3 & 63;
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;
76
- } else if (offset + 2 < upperOffset) {
77
- const byte2 = bytes[offset++];
78
- const byte3 = bytes[offset++];
79
- const byte4 = bytes[offset++];
80
- codePoint = (byte1 & 7) << 18 | (byte2 & 63) << 12 | (byte3 & 63) << 6 | byte4 & 63;
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;
87
- }
88
- if (malformed) {
89
- throw new BareError(bc.offset, INVALID_UTF8_STRING);
90
- }
51
+ // `check` asserts that `byteLen` is a `u32`
52
+ check(bc, byteLen);
53
+ let result = "";
54
+ const bytes = bc.bytes;
55
+ let offset = bc.offset;
56
+ const upperOffset = offset + byteLen;
57
+ while (offset < upperOffset) {
58
+ let codePoint = bytes[offset++];
59
+ if (codePoint > 0x7f) {
60
+ let malformed = true;
61
+ const byte1 = codePoint;
62
+ if (offset < upperOffset && codePoint < 0xe0) {
63
+ // 110x_xxxx 10xx_xxxx
64
+ const byte2 = bytes[offset++];
65
+ codePoint = ((byte1 & 0x1f) << 6) | (byte2 & 0x3f);
66
+ malformed =
67
+ codePoint >> 7 === 0 || // non-canonical char
68
+ byte1 >> 5 !== 0b110 || // invalid tag
69
+ byte2 >> 6 !== 0b10; // invalid tag
70
+ }
71
+ else if (offset + 1 < upperOffset && codePoint < 0xf0) {
72
+ // 1110_xxxx 10xx_xxxx 10xx_xxxx
73
+ const byte2 = bytes[offset++];
74
+ const byte3 = bytes[offset++];
75
+ codePoint =
76
+ ((byte1 & 0xf) << 12) |
77
+ ((byte2 & 0x3f) << 6) |
78
+ (byte3 & 0x3f);
79
+ malformed =
80
+ codePoint >> 11 === 0 || // non-canonical char or missing data
81
+ codePoint >> 11 === 0x1b || // surrogate char (0xD800 <= codePoint <= 0xDFFF)
82
+ byte1 >> 4 !== 0b1110 || // invalid tag
83
+ byte2 >> 6 !== 0b10 || // invalid tag
84
+ byte3 >> 6 !== 0b10; // invalid tag
85
+ }
86
+ else if (offset + 2 < upperOffset) {
87
+ // 1110_xxxx 10xx_xxxx 10xx_xxxx 10xx_xxxx
88
+ const byte2 = bytes[offset++];
89
+ const byte3 = bytes[offset++];
90
+ const byte4 = bytes[offset++];
91
+ codePoint =
92
+ ((byte1 & 0x7) << 18) |
93
+ ((byte2 & 0x3f) << 12) |
94
+ ((byte3 & 0x3f) << 6) |
95
+ (byte4 & 0x3f);
96
+ malformed =
97
+ codePoint >> 16 === 0 || // non-canonical char or missing data
98
+ codePoint > 0x10ffff || // too large code point
99
+ byte1 >> 3 !== 0b11110 || // invalid tag
100
+ byte2 >> 6 !== 0b10 || // invalid tag
101
+ byte3 >> 6 !== 0b10 || // invalid tag
102
+ byte4 >> 6 !== 0b10; // invalid tag
103
+ }
104
+ if (malformed) {
105
+ throw new BareError(bc.offset, INVALID_UTF8_STRING);
106
+ }
107
+ }
108
+ result += String.fromCodePoint(codePoint);
91
109
  }
92
- result += String.fromCodePoint(codePoint);
93
- }
94
- bc.offset = offset;
95
- return result;
110
+ bc.offset = offset;
111
+ return result;
96
112
  }
97
113
  function writeUtf8Js(bc, s) {
98
- const bytes = bc.bytes;
99
- let offset = bc.offset;
100
- let i = 0;
101
- while (i < s.length) {
102
- const codePoint = s.codePointAt(i++);
103
- if (codePoint < 128) {
104
- bytes[offset++] = codePoint;
105
- } else {
106
- if (codePoint < 2048) {
107
- bytes[offset++] = 192 | codePoint >> 6;
108
- } else {
109
- if (codePoint < 65536) {
110
- bytes[offset++] = 224 | codePoint >> 12;
111
- } else {
112
- bytes[offset++] = 240 | codePoint >> 18;
113
- bytes[offset++] = 128 | codePoint >> 12 & 63;
114
- i++;
114
+ const bytes = bc.bytes;
115
+ let offset = bc.offset;
116
+ let i = 0;
117
+ while (i < s.length) {
118
+ const codePoint = s.codePointAt(i++); // i is a valid index
119
+ if (codePoint < 0x80) {
120
+ bytes[offset++] = codePoint;
121
+ }
122
+ else {
123
+ if (codePoint < 0x800) {
124
+ bytes[offset++] = 0xc0 | (codePoint >> 6);
125
+ }
126
+ else {
127
+ if (codePoint < 65536) {
128
+ bytes[offset++] = 0xe0 | (codePoint >> 12);
129
+ }
130
+ else {
131
+ bytes[offset++] = 0xf0 | (codePoint >> 18);
132
+ bytes[offset++] = 0x80 | ((codePoint >> 12) & 0x3f);
133
+ i++; // surrogate pair encoded as two ucs2 chars
134
+ }
135
+ bytes[offset++] = 0x80 | ((codePoint >> 6) & 0x3f);
136
+ }
137
+ bytes[offset++] = 0x80 | (codePoint & 0x3f);
115
138
  }
116
- bytes[offset++] = 128 | codePoint >> 6 & 63;
117
- }
118
- bytes[offset++] = 128 | codePoint & 63;
119
139
  }
120
- }
121
- bc.offset = offset;
140
+ bc.offset = offset;
122
141
  }
123
142
  function utf8ByteLength(s) {
124
- let result = s.length;
125
- for (let i = 0; i < s.length; i++) {
126
- const codePoint = s.codePointAt(i);
127
- if (codePoint > 127) {
128
- result++;
129
- if (codePoint > 2047) {
130
- result++;
131
- if (codePoint > 65535) {
132
- i++;
143
+ let result = s.length;
144
+ for (let i = 0; i < s.length; i++) {
145
+ const codePoint = s.codePointAt(i); // i is a valid index
146
+ if (codePoint > 0x7f) {
147
+ result++;
148
+ if (codePoint > 0x7ff) {
149
+ result++;
150
+ if (codePoint > 65535) {
151
+ i++; // surrogate pair encoded as two ucs2 chars
152
+ }
153
+ }
133
154
  }
134
- }
135
155
  }
136
- }
137
- return result;
156
+ return result;
138
157
  }
158
+ /**
159
+ * UTF-8 decoding and encoding using API that is supported in Node >= 12 and
160
+ * modern browsers:
161
+ * https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/write
162
+ * https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/read
163
+ *
164
+ * If you're running in an environment where it's not available,
165
+ * please use a polyfill, such as:
166
+ * https://github.com/anonyco/FastestSmallestTextEncoderDecoder
167
+ */
139
168
  const UTF8_DECODER = /* @__PURE__ */ new TextDecoder("utf-8", { fatal: true });
140
169
  const UTF8_ENCODER = /* @__PURE__ */ new TextEncoder();
@@ -1,4 +1,4 @@
1
- import { type ByteCursor } from "../core/byte-cursor.js";
1
+ import { type ByteCursor } from "../core/byte-cursor.ts";
2
2
  export declare const readU16FixedArray: (bc: ByteCursor, len: number) => Uint16Array<ArrayBuffer>;
3
3
  export declare function readU16Array(bc: ByteCursor): Uint16Array<ArrayBuffer>;
4
4
  export declare const writeU16FixedArray: (bc: ByteCursor, x: Uint16Array) => void;
@@ -1,51 +1,50 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
2
3
  import { check, reserve } from "../core/byte-cursor.js";
3
4
  import { assert, DEV } from "../util/assert.js";
4
5
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
6
  import { isU32 } from "../util/validator.js";
6
7
  import { readFixedData } from "./data.js";
7
- import {
8
- readU16,
9
- readUintSafe32,
10
- writeU16,
11
- writeUintSafe32
12
- } from "./primitive.js";
8
+ import { readU16, writeU16 } from "./fixed-primitive.js";
13
9
  import { writeU8FixedArray } from "./u8-array.js";
14
- export const readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU16FixedArrayLe : readU16FixedArrayBe;
10
+ import { readUintSafe32, writeUintSafe32 } from "./uint.js";
11
+ export const readU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM
12
+ ? readU16FixedArrayLe
13
+ : readU16FixedArrayBe;
15
14
  export function readU16Array(bc) {
16
- return readU16FixedArray(bc, readUintSafe32(bc));
15
+ return readU16FixedArray(bc, readUintSafe32(bc));
17
16
  }
18
17
  function readU16FixedArrayLe(bc, len) {
19
- if (DEV) {
20
- assert(isU32(len));
21
- }
22
- const byteCount = len * 2;
23
- return new Uint16Array(readFixedData(bc, byteCount));
18
+ if (DEV) {
19
+ assert(isU32(len));
20
+ }
21
+ const byteCount = len * 2;
22
+ return new Uint16Array(readFixedData(bc, byteCount));
24
23
  }
25
24
  function readU16FixedArrayBe(bc, len) {
26
- if (DEV) {
27
- assert(isU32(len));
28
- }
29
- check(bc, len * 2);
30
- const result = new Uint16Array(len);
31
- for (let i = 0; i < len; i++) {
32
- result[i] = readU16(bc);
33
- }
34
- return result;
25
+ if (DEV) {
26
+ assert(isU32(len));
27
+ }
28
+ check(bc, len * 2);
29
+ const result = new Uint16Array(len);
30
+ for (let i = 0; i < len; i++) {
31
+ result[i] = readU16(bc);
32
+ }
33
+ return result;
35
34
  }
36
35
  export const writeU16FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU16FixedArrayLe : writeU16FixedArrayBe;
37
36
  export function writeU16Array(bc, x) {
38
- writeUintSafe32(bc, x.length);
39
- if (x.length > 0) {
40
- writeU16FixedArray(bc, x);
41
- }
37
+ writeUintSafe32(bc, x.length);
38
+ if (x.length > 0) {
39
+ writeU16FixedArray(bc, x);
40
+ }
42
41
  }
43
42
  function writeU16FixedArrayLe(bc, x) {
44
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
43
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
45
44
  }
46
45
  function writeU16FixedArrayBe(bc, x) {
47
- reserve(bc, x.length * 2);
48
- for (let i = 0; i < x.length; i++) {
49
- writeU16(bc, x[i]);
50
- }
46
+ reserve(bc, x.length * 2);
47
+ for (let i = 0; i < x.length; i++) {
48
+ writeU16(bc, x[i]);
49
+ }
51
50
  }
@@ -1,4 +1,4 @@
1
- import { type ByteCursor } from "../core/byte-cursor.js";
1
+ import { type ByteCursor } from "../core/byte-cursor.ts";
2
2
  export declare const readU32FixedArray: (bc: ByteCursor, len: number) => Uint32Array<ArrayBuffer>;
3
3
  export declare function readU32Array(bc: ByteCursor): Uint32Array<ArrayBuffer>;
4
4
  export declare const writeU32FixedArray: (bc: ByteCursor, x: Uint32Array) => void;
@@ -1,51 +1,50 @@
1
- "use strict";
1
+ //! Copyright (c) 2022 Victorien Elvinger
2
+ //! Licensed under the MIT License (https://mit-license.org/)
2
3
  import { check, reserve } from "../core/byte-cursor.js";
3
4
  import { assert, DEV } from "../util/assert.js";
4
5
  import { IS_LITTLE_ENDIAN_PLATFORM } from "../util/constants.js";
5
6
  import { isU32 } from "../util/validator.js";
6
7
  import { readFixedData } from "./data.js";
7
- import {
8
- readU32,
9
- readUintSafe32,
10
- writeU32,
11
- writeUintSafe32
12
- } from "./primitive.js";
8
+ import { readU32, writeU32 } from "./fixed-primitive.js";
13
9
  import { writeU8FixedArray } from "./u8-array.js";
14
- export const readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? readU32FixedArrayLe : readU32FixedArrayBe;
10
+ import { readUintSafe32, writeUintSafe32 } from "./uint.js";
11
+ export const readU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM
12
+ ? readU32FixedArrayLe
13
+ : readU32FixedArrayBe;
15
14
  export function readU32Array(bc) {
16
- return readU32FixedArray(bc, readUintSafe32(bc));
15
+ return readU32FixedArray(bc, readUintSafe32(bc));
17
16
  }
18
17
  function readU32FixedArrayLe(bc, len) {
19
- if (DEV) {
20
- assert(isU32(len));
21
- }
22
- const byteCount = len * 4;
23
- return new Uint32Array(readFixedData(bc, byteCount));
18
+ if (DEV) {
19
+ assert(isU32(len));
20
+ }
21
+ const byteCount = len * 4;
22
+ return new Uint32Array(readFixedData(bc, byteCount));
24
23
  }
25
24
  function readU32FixedArrayBe(bc, len) {
26
- if (DEV) {
27
- assert(isU32(len));
28
- }
29
- check(bc, len * 4);
30
- const result = new Uint32Array(len);
31
- for (let i = 0; i < len; i++) {
32
- result[i] = readU32(bc);
33
- }
34
- return result;
25
+ if (DEV) {
26
+ assert(isU32(len));
27
+ }
28
+ check(bc, len * 4);
29
+ const result = new Uint32Array(len);
30
+ for (let i = 0; i < len; i++) {
31
+ result[i] = readU32(bc);
32
+ }
33
+ return result;
35
34
  }
36
35
  export const writeU32FixedArray = IS_LITTLE_ENDIAN_PLATFORM ? writeU32FixedArrayLe : writeU32FixedArrayBe;
37
36
  export function writeU32Array(bc, x) {
38
- writeUintSafe32(bc, x.length);
39
- if (x.length > 0) {
40
- writeU32FixedArray(bc, x);
41
- }
37
+ writeUintSafe32(bc, x.length);
38
+ if (x.length > 0) {
39
+ writeU32FixedArray(bc, x);
40
+ }
42
41
  }
43
42
  function writeU32FixedArrayLe(bc, x) {
44
- writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
43
+ writeU8FixedArray(bc, new Uint8Array(x.buffer, x.byteOffset, x.byteLength));
45
44
  }
46
45
  function writeU32FixedArrayBe(bc, x) {
47
- reserve(bc, x.length * 4);
48
- for (let i = 0; i < x.length; i++) {
49
- writeU32(bc, x[i]);
50
- }
46
+ reserve(bc, x.length * 4);
47
+ for (let i = 0; i < x.length; i++) {
48
+ writeU32(bc, x[i]);
49
+ }
51
50
  }
@@ -1,4 +1,4 @@
1
- import { type ByteCursor } from "../core/byte-cursor.js";
1
+ import { type ByteCursor } from "../core/byte-cursor.ts";
2
2
  export declare const readU64FixedArray: (bc: ByteCursor, len: number) => BigUint64Array<ArrayBuffer>;
3
3
  export declare function readU64Array(bc: ByteCursor): BigUint64Array<ArrayBuffer>;
4
4
  export declare const writeU64FixedArray: (bc: ByteCursor, x: BigUint64Array) => void;