@atcute/uint8array 1.0.4 → 1.0.6

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.
@@ -1,13 +1,24 @@
1
1
  export declare const alloc: (size: number) => Uint8Array<ArrayBuffer>;
2
2
  export declare const allocUnsafe: (size: number) => Uint8Array<ArrayBuffer>;
3
- export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
4
- export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
5
- export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
6
- export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array<ArrayBuffer>;
3
+ export declare const compare: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => number;
4
+ export declare const equals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
5
+ export declare const timingSafeEquals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
6
+ export declare const concat: (arrays: Uint8Array<ArrayBufferLike>[], size?: number | undefined) => Uint8Array<ArrayBuffer>;
7
7
  export declare const encodeUtf8: (str: string) => Uint8Array<ArrayBuffer>;
8
- export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
8
+ export declare const encodeUtf8Into: (to: Uint8Array<ArrayBufferLike>, str: string, offset?: number | undefined, length?: number | undefined) => number;
9
9
  /**
10
10
  * decodes a UTF-8 string from a given buffer
11
+ * @param from source buffer
12
+ * @param offset byte offset to start reading from
13
+ * @param length number of bytes to read
14
+ * @returns decoded string
11
15
  */
12
- export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
13
- export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array<ArrayBuffer>>;
16
+ export declare const decodeUtf8From: (from: Uint8Array<ArrayBufferLike>, offset?: number, length?: number) => string;
17
+ /**
18
+ * calculates the UTF-8 byte length of a string
19
+ * @param str string to measure
20
+ * @returns byte length when encoded as UTF-8
21
+ */
22
+ export declare const getUtf8Length: (str: string) => number;
23
+ export declare const toSha256: (buffer: Uint8Array<ArrayBufferLike>) => Promise<Uint8Array<ArrayBuffer>>;
24
+ //# sourceMappingURL=index.bun.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.bun.d.ts","sourceRoot":"","sources":["../lib/index.bun.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,KAAK,2CAEjB,CAAC;AAEF,eAAO,MAAM,WAAW,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,UAAU,CAAC,WAAW,CAAgB,CAAC;AAEnF,eAAO,MAAM,OAAO,4EAEnB,CAAC;AAEF,eAAO,MAAM,MAAM,6EAElB,CAAC;AAEF,eAAO,MAAM,gBAAgB,6EAE5B,CAAC;AAEF,eAAO,MAAM,MAAM,+FAGlB,CAAC;AAEF,eAAO,MAAM,UAAU,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,UAAU,CAAC,WAAW,CAAwC,CAAC;AAEzG,eAAO,MAAM,cAAc,oHAc1B,CAAC;AAyEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,iFAU1B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,yBAEzB,CAAC;AAEF,eAAO,MAAM,QAAQ,2EAEpB,CAAC"}
package/dist/index.bun.js CHANGED
@@ -1,10 +1,11 @@
1
1
  import { allocUnsafe as _allocUnsafe, concatArrayBuffers as _concat } from 'bun';
2
2
  import { Buffer as NodeBuffer } from 'node:buffer';
3
3
  import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
4
+ const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
4
5
  const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
5
6
  const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
7
+ const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
6
8
  const textEncoder = new TextEncoder();
7
- const textDecoder = new TextDecoder();
8
9
  const toUint8Array = (buffer) => {
9
10
  return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
10
11
  };
@@ -40,47 +41,123 @@ export const encodeUtf8Into = (to, str, offset, length) => {
40
41
  const result = textEncoder.encodeInto(str, buffer);
41
42
  return result.written;
42
43
  };
43
- const fromCharCode = String.fromCharCode;
44
- /**
45
- * decodes a UTF-8 string from a given buffer
46
- */
47
- export const decodeUtf8From = (from, offset, length) => {
48
- let buffer;
49
- if (offset === undefined) {
50
- buffer = from;
51
- }
52
- else if (length === undefined) {
53
- buffer = from.subarray(offset);
44
+ const _fromCharCode = String.fromCharCode;
45
+ // fully unrolled short string decoder, inspired by cbor-x
46
+ // returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
47
+ const _shortString = (from, p, length) => {
48
+ if (length < 4) {
49
+ if (length < 2) {
50
+ if (length === 0)
51
+ return '';
52
+ const a = from[p];
53
+ if (a & 0x80)
54
+ return null;
55
+ return _fromCharCode(a);
56
+ }
57
+ const a = from[p];
58
+ const b = from[p + 1];
59
+ if ((a | b) & 0x80)
60
+ return null;
61
+ if (length === 2)
62
+ return _fromCharCode(a, b);
63
+ const c = from[p + 2];
64
+ if (c & 0x80)
65
+ return null;
66
+ return _fromCharCode(a, b, c);
54
67
  }
55
- else {
56
- buffer = from.subarray(offset, offset + length);
68
+ const a = from[p];
69
+ const b = from[p + 1];
70
+ const c = from[p + 2];
71
+ const d = from[p + 3];
72
+ if ((a | b | c | d) & 0x80)
73
+ return null;
74
+ if (length < 8) {
75
+ if (length === 4)
76
+ return _fromCharCode(a, b, c, d);
77
+ const e = from[p + 4];
78
+ if (e & 0x80)
79
+ return null;
80
+ if (length === 5)
81
+ return _fromCharCode(a, b, c, d, e);
82
+ const f = from[p + 5];
83
+ if (f & 0x80)
84
+ return null;
85
+ if (length === 6)
86
+ return _fromCharCode(a, b, c, d, e, f);
87
+ const g = from[p + 6];
88
+ if (g & 0x80)
89
+ return null;
90
+ return _fromCharCode(a, b, c, d, e, f, g);
57
91
  }
58
- const end = buffer.length;
59
- if (end > 24) {
60
- return textDecoder.decode(buffer);
92
+ const e = from[p + 4];
93
+ const f = from[p + 5];
94
+ const g = from[p + 6];
95
+ const h = from[p + 7];
96
+ if ((e | f | g | h) & 0x80)
97
+ return null;
98
+ if (length < 12) {
99
+ if (length === 8)
100
+ return _fromCharCode(a, b, c, d, e, f, g, h);
101
+ const i = from[p + 8];
102
+ if (i & 0x80)
103
+ return null;
104
+ if (length === 9)
105
+ return _fromCharCode(a, b, c, d, e, f, g, h, i);
106
+ const j = from[p + 9];
107
+ if (j & 0x80)
108
+ return null;
109
+ if (length === 10)
110
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
111
+ const k = from[p + 10];
112
+ if (k & 0x80)
113
+ return null;
114
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
61
115
  }
62
- {
63
- let str = '';
64
- let idx = 0;
65
- for (; idx + 3 < end; idx += 4) {
66
- const a = buffer[idx];
67
- const b = buffer[idx + 1];
68
- const c = buffer[idx + 2];
69
- const d = buffer[idx + 3];
70
- if ((a | b | c | d) & 0x80) {
71
- return str + textDecoder.decode(buffer.subarray(idx));
72
- }
73
- str += fromCharCode(a, b, c, d);
74
- }
75
- for (; idx < end; idx++) {
76
- const x = buffer[idx];
77
- if (x & 0x80) {
78
- return str + textDecoder.decode(buffer.subarray(idx));
79
- }
80
- str += fromCharCode(x);
81
- }
82
- return str;
116
+ const i = from[p + 8];
117
+ const j = from[p + 9];
118
+ const k = from[p + 10];
119
+ const l = from[p + 11];
120
+ if ((i | j | k | l) & 0x80)
121
+ return null;
122
+ if (length === 12)
123
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
124
+ const m = from[p + 12];
125
+ if (m & 0x80)
126
+ return null;
127
+ if (length === 13)
128
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
129
+ const n = from[p + 13];
130
+ if (n & 0x80)
131
+ return null;
132
+ if (length === 14)
133
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
134
+ const o = from[p + 14];
135
+ if (o & 0x80)
136
+ return null;
137
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
138
+ };
139
+ /**
140
+ * decodes a UTF-8 string from a given buffer
141
+ * @param from source buffer
142
+ * @param offset byte offset to start reading from
143
+ * @param length number of bytes to read
144
+ * @returns decoded string
145
+ */
146
+ export const decodeUtf8From = (from, offset = 0, length = from.length) => {
147
+ if (length <= 15) {
148
+ const result = _shortString(from, offset, length);
149
+ if (result !== null)
150
+ return result;
83
151
  }
152
+ return _utf8Slice.call(from, offset, offset + length);
153
+ };
154
+ /**
155
+ * calculates the UTF-8 byte length of a string
156
+ * @param str string to measure
157
+ * @returns byte length when encoded as UTF-8
158
+ */
159
+ export const getUtf8Length = (str) => {
160
+ return _byteLength(str, 'utf8');
84
161
  };
85
162
  export const toSha256 = async (buffer) => {
86
163
  return toUint8Array(_hash('sha256', buffer, 'buffer'));
@@ -1 +1 @@
1
- {"version":3,"file":"index.bun.js","sourceRoot":"","sources":["../lib/index.bun.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,IAAI,YAAY,EAAE,kBAAkB,IAAI,OAAO,EAAE,MAAM,KAAK,CAAC;AAEjF,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,IAAI,IAAI,KAAK,EAAE,eAAe,IAAI,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEjF,MAAM,QAAQ,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC;AAC5D,MAAM,OAAO,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC;AAE1D,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AACtC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC,MAAM,YAAY,GAAG,CAAC,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AE,CAAC,EAAE,CAAC,CAAC,CAAC;AAAA,CAClE,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC7B,IAAgB,EAChB,MAAM,GAAW,CAAC,EAClB,MAAM,GAAW,IAAI,CAAC,MAAM,EACnB,EAAE,CAAC;IACZ,IAAI,MAAM,IAAI,EAAE,EAAE,CAAC;QAClB,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAClD,IAAI,MAAM,KAAK,IAAI;YAAE,OAAO,MAAM,CAAC;IACpC,CAAC;IACD,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AAAA,CACtD,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC;IACrD,OAAO,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AAAA,CAChC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAoC,EAAE,CAAC;IACvF,OAAO,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAA4B,CAAC;AAAA,CAClF,CAAC"}
package/dist/index.d.ts CHANGED
@@ -10,19 +10,19 @@ export declare const allocUnsafe: (size: number) => Uint8Array<ArrayBuffer>;
10
10
  /**
11
11
  * compares two Uint8Array buffers
12
12
  */
13
- export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
13
+ export declare const compare: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => number;
14
14
  /**
15
15
  * checks if the two Uint8Array buffers are equal
16
16
  */
17
- export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
17
+ export declare const equals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
18
18
  /**
19
19
  * checks if the two Uint8Array buffers are equal, timing-safe version
20
20
  */
21
- export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
21
+ export declare const timingSafeEquals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
22
22
  /**
23
23
  * concatenates multiple Uint8Array buffers into one
24
24
  */
25
- export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array<ArrayBuffer>;
25
+ export declare const concat: (arrays: Uint8Array<ArrayBufferLike>[], size?: number | undefined) => Uint8Array<ArrayBuffer>;
26
26
  /**
27
27
  * encodes a UTF-8 string
28
28
  */
@@ -30,12 +30,23 @@ export declare const encodeUtf8: (str: string) => Uint8Array<ArrayBuffer>;
30
30
  /**
31
31
  * encodes a UTF-8 string into a given buffer
32
32
  */
33
- export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
33
+ export declare const encodeUtf8Into: (to: Uint8Array<ArrayBufferLike>, str: string, offset?: number | undefined, length?: number | undefined) => number;
34
34
  /**
35
35
  * decodes a UTF-8 string from a given buffer
36
+ * @param from source buffer
37
+ * @param offset byte offset to start reading from
38
+ * @param length number of bytes to read
39
+ * @returns decoded string
36
40
  */
37
- export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
41
+ export declare const decodeUtf8From: (from: Uint8Array<ArrayBufferLike>, offset?: number, length?: number) => string;
42
+ /**
43
+ * calculates the UTF-8 byte length of a string
44
+ * @param str string to measure
45
+ * @returns byte length when encoded as UTF-8
46
+ */
47
+ export declare const getUtf8Length: (str: string) => number;
38
48
  /**
39
49
  * get a SHA-256 digest of this buffer
40
50
  */
41
51
  export declare const toSha256: (buffer: Uint8Array<ArrayBuffer>) => Promise<Uint8Array<ArrayBuffer>>;
52
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,KAAK,2CAEjB,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,WAAW,2CAAQ,CAAC;AAEjC;;GAEG;AACH,eAAO,MAAM,OAAO,4EAyBnB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,MAAM,6EAelB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,gBAAgB,6EAU5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,MAAM,+FAuBlB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,UAAU,0CAEtB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,oHAc1B,CAAC;AAyEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,iFAU1B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,yBAyCzB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,uEAEpB,CAAC"}
package/dist/index.js CHANGED
@@ -110,47 +110,158 @@ export const encodeUtf8Into = (to, str, offset, length) => {
110
110
  const result = textEncoder.encodeInto(str, buffer);
111
111
  return result.written;
112
112
  };
113
- const fromCharCode = String.fromCharCode;
113
+ const _fromCharCode = String.fromCharCode;
114
+ // fully unrolled short string decoder, inspired by cbor-x
115
+ // returns null if non-ASCII byte encountered, signaling fallback to TextDecoder
116
+ const _shortString = (from, p, length) => {
117
+ if (length < 4) {
118
+ if (length < 2) {
119
+ if (length === 0)
120
+ return '';
121
+ const a = from[p];
122
+ if (a & 0x80)
123
+ return null;
124
+ return _fromCharCode(a);
125
+ }
126
+ const a = from[p];
127
+ const b = from[p + 1];
128
+ if ((a | b) & 0x80)
129
+ return null;
130
+ if (length === 2)
131
+ return _fromCharCode(a, b);
132
+ const c = from[p + 2];
133
+ if (c & 0x80)
134
+ return null;
135
+ return _fromCharCode(a, b, c);
136
+ }
137
+ const a = from[p];
138
+ const b = from[p + 1];
139
+ const c = from[p + 2];
140
+ const d = from[p + 3];
141
+ if ((a | b | c | d) & 0x80)
142
+ return null;
143
+ if (length < 8) {
144
+ if (length === 4)
145
+ return _fromCharCode(a, b, c, d);
146
+ const e = from[p + 4];
147
+ if (e & 0x80)
148
+ return null;
149
+ if (length === 5)
150
+ return _fromCharCode(a, b, c, d, e);
151
+ const f = from[p + 5];
152
+ if (f & 0x80)
153
+ return null;
154
+ if (length === 6)
155
+ return _fromCharCode(a, b, c, d, e, f);
156
+ const g = from[p + 6];
157
+ if (g & 0x80)
158
+ return null;
159
+ return _fromCharCode(a, b, c, d, e, f, g);
160
+ }
161
+ const e = from[p + 4];
162
+ const f = from[p + 5];
163
+ const g = from[p + 6];
164
+ const h = from[p + 7];
165
+ if ((e | f | g | h) & 0x80)
166
+ return null;
167
+ if (length < 12) {
168
+ if (length === 8)
169
+ return _fromCharCode(a, b, c, d, e, f, g, h);
170
+ const i = from[p + 8];
171
+ if (i & 0x80)
172
+ return null;
173
+ if (length === 9)
174
+ return _fromCharCode(a, b, c, d, e, f, g, h, i);
175
+ const j = from[p + 9];
176
+ if (j & 0x80)
177
+ return null;
178
+ if (length === 10)
179
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
180
+ const k = from[p + 10];
181
+ if (k & 0x80)
182
+ return null;
183
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
184
+ }
185
+ const i = from[p + 8];
186
+ const j = from[p + 9];
187
+ const k = from[p + 10];
188
+ const l = from[p + 11];
189
+ if ((i | j | k | l) & 0x80)
190
+ return null;
191
+ if (length === 12)
192
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
193
+ const m = from[p + 12];
194
+ if (m & 0x80)
195
+ return null;
196
+ if (length === 13)
197
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
198
+ const n = from[p + 13];
199
+ if (n & 0x80)
200
+ return null;
201
+ if (length === 14)
202
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
203
+ const o = from[p + 14];
204
+ if (o & 0x80)
205
+ return null;
206
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
207
+ };
114
208
  /**
115
209
  * decodes a UTF-8 string from a given buffer
210
+ * @param from source buffer
211
+ * @param offset byte offset to start reading from
212
+ * @param length number of bytes to read
213
+ * @returns decoded string
116
214
  */
117
- export const decodeUtf8From = (from, offset, length) => {
118
- let buffer;
119
- if (offset === undefined) {
120
- buffer = from;
215
+ export const decodeUtf8From = (from, offset = 0, length = from.length) => {
216
+ if (length <= 15) {
217
+ const result = _shortString(from, offset, length);
218
+ if (result !== null)
219
+ return result;
121
220
  }
122
- else if (length === undefined) {
123
- buffer = from.subarray(offset);
221
+ return textDecoder.decode(from.subarray(offset, offset + length));
222
+ };
223
+ /**
224
+ * calculates the UTF-8 byte length of a string
225
+ * @param str string to measure
226
+ * @returns byte length when encoded as UTF-8
227
+ */
228
+ export const getUtf8Length = (str) => {
229
+ const len = str.length;
230
+ let u16pos = 0;
231
+ let u8pos = 0;
232
+ // ASCII fast-path: batch process 4 chars at a time
233
+ while (u16pos + 3 < len) {
234
+ const a = str.charCodeAt(u16pos);
235
+ const b = str.charCodeAt(u16pos + 1);
236
+ const c = str.charCodeAt(u16pos + 2);
237
+ const d = str.charCodeAt(u16pos + 3);
238
+ if ((a | b | c | d) >= 0x80) {
239
+ break;
240
+ }
241
+ u16pos += 4;
242
+ u8pos += 4;
124
243
  }
125
- else {
126
- buffer = from.subarray(offset, offset + length);
127
- }
128
- const end = buffer.length;
129
- if (end > 24) {
130
- return textDecoder.decode(buffer);
131
- }
132
- {
133
- let str = '';
134
- let idx = 0;
135
- for (; idx + 3 < end; idx += 4) {
136
- const a = buffer[idx];
137
- const b = buffer[idx + 1];
138
- const c = buffer[idx + 2];
139
- const d = buffer[idx + 3];
140
- if ((a | b | c | d) & 0x80) {
141
- return str + textDecoder.decode(buffer.subarray(idx));
142
- }
143
- str += fromCharCode(a, b, c, d);
244
+ // handle remaining chars
245
+ while (u16pos < len) {
246
+ const code = str.charCodeAt(u16pos);
247
+ if (code < 0x80) {
248
+ u16pos += 1;
249
+ u8pos += 1;
144
250
  }
145
- for (; idx < end; idx++) {
146
- const x = buffer[idx];
147
- if (x & 0x80) {
148
- return str + textDecoder.decode(buffer.subarray(idx));
149
- }
150
- str += fromCharCode(x);
251
+ else if (code < 0x800) {
252
+ u16pos += 1;
253
+ u8pos += 2;
254
+ }
255
+ else if (code < 0xd800 || code > 0xdbff) {
256
+ u16pos += 1;
257
+ u8pos += 3;
258
+ }
259
+ else {
260
+ u16pos += 2;
261
+ u8pos += 4;
151
262
  }
152
- return str;
153
263
  }
264
+ return u8pos;
154
265
  };
155
266
  /**
156
267
  * get a SHA-256 digest of this buffer
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AACtC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC;AAEtC,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;AAE7B;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,IAAY,EAA2B,EAAE;IAC9D,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,CAAC;AAEjC;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAa,EAAE,CAAa,EAAU,EAAE;IAC/D,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IACtB,MAAM,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC;IAEtB,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC;IACV,CAAC;IACD,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC,CAAC;IACX,CAAC;IAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE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,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC;YAC7B,MAAM;QACP,CAAC;QAED,MAAM,IAAI,CAAC,CAAC;QACZ,KAAK,IAAI,CAAC,CAAC;IACZ,CAAC;IAED,yBAAyB;IACzB,OAAO,MAAM,GAAG,GAAG,EAAE,CAAC;QACrB,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;QAEpC,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;YACjB,MAAM,IAAI,CAAC,CAAC;YACZ,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;aAAM,IAAI,IAAI,GAAG,KAAK,EAAE,CAAC;YACzB,MAAM,IAAI,CAAC,CAAC;YACZ,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;aAAM,IAAI,IAAI,GAAG,MAAM,IAAI,IAAI,GAAG,MAAM,EAAE,CAAC;YAC3C,MAAM,IAAI,CAAC,CAAC;YACZ,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;aAAM,CAAC;YACP,MAAM,IAAI,CAAC,CAAC;YACZ,KAAK,IAAI,CAAC,CAAC;QACZ,CAAC;IACF,CAAC;IAED,OAAO,KAAK,CAAC;AAAA,CACb,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAA+B,EAAoC,EAAE,CAAC;IACpG,OAAO,IAAI,UAAU,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;AAAA,CAC9D,CAAC"}
@@ -1,10 +1,24 @@
1
1
  export declare const alloc: (size: number) => Uint8Array<ArrayBuffer>;
2
2
  export declare const allocUnsafe: (size: number) => Uint8Array<ArrayBuffer>;
3
- export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
4
- export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
5
- export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
6
- export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array<ArrayBuffer>;
3
+ export declare const compare: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => number;
4
+ export declare const equals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
5
+ export declare const timingSafeEquals: (a: Uint8Array<ArrayBufferLike>, b: Uint8Array<ArrayBufferLike>) => boolean;
6
+ export declare const concat: (arrays: Uint8Array<ArrayBufferLike>[], size?: number | undefined) => Uint8Array<ArrayBuffer>;
7
7
  export declare const encodeUtf8: (str: string) => Uint8Array<ArrayBuffer>;
8
- export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
9
- export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
10
- export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array<ArrayBuffer>>;
8
+ export declare const encodeUtf8Into: (to: Uint8Array<ArrayBufferLike>, str: string, offset?: number | undefined, length?: number | undefined) => number;
9
+ /**
10
+ * decodes a UTF-8 string from a given buffer
11
+ * @param from source buffer
12
+ * @param offset byte offset to start reading from
13
+ * @param length number of bytes to read
14
+ * @returns decoded string
15
+ */
16
+ export declare const decodeUtf8From: (from: Uint8Array<ArrayBufferLike>, offset?: number, length?: number) => string;
17
+ /**
18
+ * calculates the UTF-8 byte length of a string
19
+ * @param str string to measure
20
+ * @returns byte length when encoded as UTF-8
21
+ */
22
+ export declare const getUtf8Length: (str: string) => number;
23
+ export declare const toSha256: (buffer: Uint8Array<ArrayBufferLike>) => Promise<Uint8Array<ArrayBuffer>>;
24
+ //# sourceMappingURL=index.node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.node.d.ts","sourceRoot":"","sources":["../lib/index.node.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,KAAK,2CAEjB,CAAC;AAEF,eAAO,MAAM,WAAW,2CAEvB,CAAC;AAEF,eAAO,MAAM,OAAO,4EAEnB,CAAC;AAEF,eAAO,MAAM,MAAM,6EAElB,CAAC;AAEF,eAAO,MAAM,gBAAgB,6EAE5B,CAAC;AAEF,eAAO,MAAM,MAAM,+FAElB,CAAC;AAEF,eAAO,MAAM,UAAU,0CAEtB,CAAC;AAEF,eAAO,MAAM,cAAc,oHAE1B,CAAC;AAyEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,iFAU1B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,yBAEzB,CAAC;AAEF,eAAO,MAAM,QAAQ,2EAEpB,CAAC"}
@@ -4,6 +4,7 @@ const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
4
4
  const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
5
5
  const _concat = /*#__PURE__*/ NodeBuffer.concat;
6
6
  const _from = /*#__PURE__*/ NodeBuffer.from;
7
+ const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
7
8
  const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
8
9
  const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
9
10
  const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
@@ -35,9 +36,124 @@ export const encodeUtf8 = (str) => {
35
36
  export const encodeUtf8Into = (to, str, offset, length) => {
36
37
  return _utf8Write.call(to, str, offset, length);
37
38
  };
39
+ const _fromCharCode = String.fromCharCode;
40
+ // fully unrolled short string decoder, inspired by cbor-x
41
+ // returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
42
+ const _shortString = (from, p, length) => {
43
+ if (length < 4) {
44
+ if (length < 2) {
45
+ if (length === 0)
46
+ return '';
47
+ const a = from[p];
48
+ if (a & 0x80)
49
+ return null;
50
+ return _fromCharCode(a);
51
+ }
52
+ const a = from[p];
53
+ const b = from[p + 1];
54
+ if ((a | b) & 0x80)
55
+ return null;
56
+ if (length === 2)
57
+ return _fromCharCode(a, b);
58
+ const c = from[p + 2];
59
+ if (c & 0x80)
60
+ return null;
61
+ return _fromCharCode(a, b, c);
62
+ }
63
+ const a = from[p];
64
+ const b = from[p + 1];
65
+ const c = from[p + 2];
66
+ const d = from[p + 3];
67
+ if ((a | b | c | d) & 0x80)
68
+ return null;
69
+ if (length < 8) {
70
+ if (length === 4)
71
+ return _fromCharCode(a, b, c, d);
72
+ const e = from[p + 4];
73
+ if (e & 0x80)
74
+ return null;
75
+ if (length === 5)
76
+ return _fromCharCode(a, b, c, d, e);
77
+ const f = from[p + 5];
78
+ if (f & 0x80)
79
+ return null;
80
+ if (length === 6)
81
+ return _fromCharCode(a, b, c, d, e, f);
82
+ const g = from[p + 6];
83
+ if (g & 0x80)
84
+ return null;
85
+ return _fromCharCode(a, b, c, d, e, f, g);
86
+ }
87
+ const e = from[p + 4];
88
+ const f = from[p + 5];
89
+ const g = from[p + 6];
90
+ const h = from[p + 7];
91
+ if ((e | f | g | h) & 0x80)
92
+ return null;
93
+ if (length < 12) {
94
+ if (length === 8)
95
+ return _fromCharCode(a, b, c, d, e, f, g, h);
96
+ const i = from[p + 8];
97
+ if (i & 0x80)
98
+ return null;
99
+ if (length === 9)
100
+ return _fromCharCode(a, b, c, d, e, f, g, h, i);
101
+ const j = from[p + 9];
102
+ if (j & 0x80)
103
+ return null;
104
+ if (length === 10)
105
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
106
+ const k = from[p + 10];
107
+ if (k & 0x80)
108
+ return null;
109
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
110
+ }
111
+ const i = from[p + 8];
112
+ const j = from[p + 9];
113
+ const k = from[p + 10];
114
+ const l = from[p + 11];
115
+ if ((i | j | k | l) & 0x80)
116
+ return null;
117
+ if (length === 12)
118
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
119
+ const m = from[p + 12];
120
+ if (m & 0x80)
121
+ return null;
122
+ if (length === 13)
123
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
124
+ const n = from[p + 13];
125
+ if (n & 0x80)
126
+ return null;
127
+ if (length === 14)
128
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
129
+ const o = from[p + 14];
130
+ if (o & 0x80)
131
+ return null;
132
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
133
+ };
134
+ /**
135
+ * decodes a UTF-8 string from a given buffer
136
+ * @param from source buffer
137
+ * @param offset byte offset to start reading from
138
+ * @param length number of bytes to read
139
+ * @returns decoded string
140
+ */
38
141
  export const decodeUtf8From = (from, offset = 0, length = from.length) => {
142
+ if (length <= 15) {
143
+ const result = _shortString(from, offset, length);
144
+ if (result !== null)
145
+ return result;
146
+ }
39
147
  return _utf8Slice.call(from, offset, offset + length);
40
148
  };
149
+ /**
150
+ * calculates the UTF-8 byte length of a string
151
+ * @param str string to measure
152
+ * @returns byte length when encoded as UTF-8
153
+ */
154
+ export const getUtf8Length = (str) => {
155
+ return _byteLength(str, 'utf8');
156
+ };
41
157
  export const toSha256 = async (buffer) => {
42
158
  return toUint8Array(_hash('sha256', buffer, 'buffer'));
43
159
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.node.js","sourceRoot":"","sources":["../lib/index.node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,IAAI,IAAI,KAAK,EAAE,eAAe,IAAI,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEjF,MAAM,MAAM,GAAG,aAAa,CAAC,UAAU,CAAC,KAAK,CAAC;AAC9C,MAAM,YAAY,GAAG,aAAa,CAAC,UAAU,CAAC,WAAW,CAAC;AAC1D,MAAM,OAAO,GAAG,aAAa,CAAC,UAAU,CAAC,MAAM,CAAC;AAChD,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC;AAE5C,MAAM,QAAQ,GAAG,aAAa,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,CAAC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M,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAClD,IAAI,MAAM,KAAK,IAAI;YAAE,OAAO,MAAM,CAAC;IACpC,CAAC;IACD,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AAAA,CACtD,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC;IACrD,OAAO,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AAAA,CAChC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAoC,EAAE,CAAC;IACvF,OAAO,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAA4B,CAAC;AAAA,CAClF,CAAC"}
package/lib/index.bun.ts CHANGED
@@ -3,11 +3,13 @@ import { allocUnsafe as _allocUnsafe, concatArrayBuffers as _concat } from 'bun'
3
3
  import { Buffer as NodeBuffer } from 'node:buffer';
4
4
  import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
5
5
 
6
+ const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
7
+
6
8
  const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
7
9
  const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
10
+ const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
8
11
 
9
12
  const textEncoder = new TextEncoder();
10
- const textDecoder = new TextDecoder();
11
13
 
12
14
  const toUint8Array = (buffer: NodeBuffer) => {
13
15
  return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
@@ -54,56 +56,103 @@ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, len
54
56
  return result.written;
55
57
  };
56
58
 
57
- const fromCharCode = String.fromCharCode;
59
+ const _fromCharCode = String.fromCharCode;
60
+
61
+ // fully unrolled short string decoder, inspired by cbor-x
62
+ // returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
63
+ const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
64
+ if (length < 4) {
65
+ if (length < 2) {
66
+ if (length === 0) return '';
67
+ const a = from[p];
68
+ if (a & 0x80) return null;
69
+ return _fromCharCode(a);
70
+ }
71
+ const a = from[p];
72
+ const b = from[p + 1];
73
+ if ((a | b) & 0x80) return null;
74
+ if (length === 2) return _fromCharCode(a, b);
75
+ const c = from[p + 2];
76
+ if (c & 0x80) return null;
77
+ return _fromCharCode(a, b, c);
78
+ }
79
+ const a = from[p];
80
+ const b = from[p + 1];
81
+ const c = from[p + 2];
82
+ const d = from[p + 3];
83
+ if ((a | b | c | d) & 0x80) return null;
84
+ if (length < 8) {
85
+ if (length === 4) return _fromCharCode(a, b, c, d);
86
+ const e = from[p + 4];
87
+ if (e & 0x80) return null;
88
+ if (length === 5) return _fromCharCode(a, b, c, d, e);
89
+ const f = from[p + 5];
90
+ if (f & 0x80) return null;
91
+ if (length === 6) return _fromCharCode(a, b, c, d, e, f);
92
+ const g = from[p + 6];
93
+ if (g & 0x80) return null;
94
+ return _fromCharCode(a, b, c, d, e, f, g);
95
+ }
96
+ const e = from[p + 4];
97
+ const f = from[p + 5];
98
+ const g = from[p + 6];
99
+ const h = from[p + 7];
100
+ if ((e | f | g | h) & 0x80) return null;
101
+ if (length < 12) {
102
+ if (length === 8) return _fromCharCode(a, b, c, d, e, f, g, h);
103
+ const i = from[p + 8];
104
+ if (i & 0x80) return null;
105
+ if (length === 9) return _fromCharCode(a, b, c, d, e, f, g, h, i);
106
+ const j = from[p + 9];
107
+ if (j & 0x80) return null;
108
+ if (length === 10) return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
109
+ const k = from[p + 10];
110
+ if (k & 0x80) return null;
111
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
112
+ }
113
+ const i = from[p + 8];
114
+ const j = from[p + 9];
115
+ const k = from[p + 10];
116
+ const l = from[p + 11];
117
+ if ((i | j | k | l) & 0x80) return null;
118
+ if (length === 12) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
119
+ const m = from[p + 12];
120
+ if (m & 0x80) return null;
121
+ if (length === 13) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
122
+ const n = from[p + 13];
123
+ if (n & 0x80) return null;
124
+ if (length === 14) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
125
+ const o = from[p + 14];
126
+ if (o & 0x80) return null;
127
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
128
+ };
58
129
 
59
130
  /**
60
131
  * decodes a UTF-8 string from a given buffer
132
+ * @param from source buffer
133
+ * @param offset byte offset to start reading from
134
+ * @param length number of bytes to read
135
+ * @returns decoded string
61
136
  */
62
- export const decodeUtf8From = (from: Uint8Array, offset?: number, length?: number): string => {
63
- let buffer: Uint8Array;
64
-
65
- if (offset === undefined) {
66
- buffer = from;
67
- } else if (length === undefined) {
68
- buffer = from.subarray(offset);
69
- } else {
70
- buffer = from.subarray(offset, offset + length);
71
- }
72
-
73
- const end = buffer.length;
74
- if (end > 24) {
75
- return textDecoder.decode(buffer);
137
+ export const decodeUtf8From = (
138
+ from: Uint8Array,
139
+ offset: number = 0,
140
+ length: number = from.length,
141
+ ): string => {
142
+ if (length <= 15) {
143
+ const result = _shortString(from, offset, length);
144
+ if (result !== null) return result;
76
145
  }
146
+ return _utf8Slice.call(from, offset, offset + length);
147
+ };
77
148
 
78
- {
79
- let str = '';
80
- let idx = 0;
81
-
82
- for (; idx + 3 < end; idx += 4) {
83
- const a = buffer[idx];
84
- const b = buffer[idx + 1];
85
- const c = buffer[idx + 2];
86
- const d = buffer[idx + 3];
87
-
88
- if ((a | b | c | d) & 0x80) {
89
- return str + textDecoder.decode(buffer.subarray(idx));
90
- }
91
-
92
- str += fromCharCode(a, b, c, d);
93
- }
94
-
95
- for (; idx < end; idx++) {
96
- const x = buffer[idx];
97
-
98
- if (x & 0x80) {
99
- return str + textDecoder.decode(buffer.subarray(idx));
100
- }
101
-
102
- str += fromCharCode(x);
103
- }
104
-
105
- return str;
106
- }
149
+ /**
150
+ * calculates the UTF-8 byte length of a string
151
+ * @param str string to measure
152
+ * @returns byte length when encoded as UTF-8
153
+ */
154
+ export const getUtf8Length = (str: string): number => {
155
+ return _byteLength(str, 'utf8');
107
156
  };
108
157
 
109
158
  export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
package/lib/index.node.ts CHANGED
@@ -6,6 +6,8 @@ const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
6
6
  const _concat = /*#__PURE__*/ NodeBuffer.concat;
7
7
  const _from = /*#__PURE__*/ NodeBuffer.from;
8
8
 
9
+ const _byteLength = /*#__PURE__*/ NodeBuffer.byteLength;
10
+
9
11
  const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
10
12
  const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
11
13
  const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
@@ -47,14 +49,105 @@ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, len
47
49
  return _utf8Write.call(to, str, offset, length);
48
50
  };
49
51
 
52
+ const _fromCharCode = String.fromCharCode;
53
+
54
+ // fully unrolled short string decoder, inspired by cbor-x
55
+ // returns null if non-ASCII byte encountered, signaling fallback to utf8Slice
56
+ const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
57
+ if (length < 4) {
58
+ if (length < 2) {
59
+ if (length === 0) return '';
60
+ const a = from[p];
61
+ if (a & 0x80) return null;
62
+ return _fromCharCode(a);
63
+ }
64
+ const a = from[p];
65
+ const b = from[p + 1];
66
+ if ((a | b) & 0x80) return null;
67
+ if (length === 2) return _fromCharCode(a, b);
68
+ const c = from[p + 2];
69
+ if (c & 0x80) return null;
70
+ return _fromCharCode(a, b, c);
71
+ }
72
+ const a = from[p];
73
+ const b = from[p + 1];
74
+ const c = from[p + 2];
75
+ const d = from[p + 3];
76
+ if ((a | b | c | d) & 0x80) return null;
77
+ if (length < 8) {
78
+ if (length === 4) return _fromCharCode(a, b, c, d);
79
+ const e = from[p + 4];
80
+ if (e & 0x80) return null;
81
+ if (length === 5) return _fromCharCode(a, b, c, d, e);
82
+ const f = from[p + 5];
83
+ if (f & 0x80) return null;
84
+ if (length === 6) return _fromCharCode(a, b, c, d, e, f);
85
+ const g = from[p + 6];
86
+ if (g & 0x80) return null;
87
+ return _fromCharCode(a, b, c, d, e, f, g);
88
+ }
89
+ const e = from[p + 4];
90
+ const f = from[p + 5];
91
+ const g = from[p + 6];
92
+ const h = from[p + 7];
93
+ if ((e | f | g | h) & 0x80) return null;
94
+ if (length < 12) {
95
+ if (length === 8) return _fromCharCode(a, b, c, d, e, f, g, h);
96
+ const i = from[p + 8];
97
+ if (i & 0x80) return null;
98
+ if (length === 9) return _fromCharCode(a, b, c, d, e, f, g, h, i);
99
+ const j = from[p + 9];
100
+ if (j & 0x80) return null;
101
+ if (length === 10) return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
102
+ const k = from[p + 10];
103
+ if (k & 0x80) return null;
104
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
105
+ }
106
+ const i = from[p + 8];
107
+ const j = from[p + 9];
108
+ const k = from[p + 10];
109
+ const l = from[p + 11];
110
+ if ((i | j | k | l) & 0x80) return null;
111
+ if (length === 12) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
112
+ const m = from[p + 12];
113
+ if (m & 0x80) return null;
114
+ if (length === 13) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
115
+ const n = from[p + 13];
116
+ if (n & 0x80) return null;
117
+ if (length === 14) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
118
+ const o = from[p + 14];
119
+ if (o & 0x80) return null;
120
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
121
+ };
122
+
123
+ /**
124
+ * decodes a UTF-8 string from a given buffer
125
+ * @param from source buffer
126
+ * @param offset byte offset to start reading from
127
+ * @param length number of bytes to read
128
+ * @returns decoded string
129
+ */
50
130
  export const decodeUtf8From = (
51
131
  from: Uint8Array,
52
132
  offset: number = 0,
53
133
  length: number = from.length,
54
134
  ): string => {
135
+ if (length <= 15) {
136
+ const result = _shortString(from, offset, length);
137
+ if (result !== null) return result;
138
+ }
55
139
  return _utf8Slice.call(from, offset, offset + length);
56
140
  };
57
141
 
142
+ /**
143
+ * calculates the UTF-8 byte length of a string
144
+ * @param str string to measure
145
+ * @returns byte length when encoded as UTF-8
146
+ */
147
+ export const getUtf8Length = (str: string): number => {
148
+ return _byteLength(str, 'utf8');
149
+ };
150
+
58
151
  export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
59
152
  return toUint8Array(_hash('sha256', buffer, 'buffer')) as Uint8Array<ArrayBuffer>;
60
153
  };
package/lib/index.ts CHANGED
@@ -135,56 +135,142 @@ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, len
135
135
  return result.written;
136
136
  };
137
137
 
138
- const fromCharCode = String.fromCharCode;
138
+ const _fromCharCode = String.fromCharCode;
139
+
140
+ // fully unrolled short string decoder, inspired by cbor-x
141
+ // returns null if non-ASCII byte encountered, signaling fallback to TextDecoder
142
+ const _shortString = (from: Uint8Array, p: number, length: number): string | null => {
143
+ if (length < 4) {
144
+ if (length < 2) {
145
+ if (length === 0) return '';
146
+ const a = from[p];
147
+ if (a & 0x80) return null;
148
+ return _fromCharCode(a);
149
+ }
150
+ const a = from[p];
151
+ const b = from[p + 1];
152
+ if ((a | b) & 0x80) return null;
153
+ if (length === 2) return _fromCharCode(a, b);
154
+ const c = from[p + 2];
155
+ if (c & 0x80) return null;
156
+ return _fromCharCode(a, b, c);
157
+ }
158
+ const a = from[p];
159
+ const b = from[p + 1];
160
+ const c = from[p + 2];
161
+ const d = from[p + 3];
162
+ if ((a | b | c | d) & 0x80) return null;
163
+ if (length < 8) {
164
+ if (length === 4) return _fromCharCode(a, b, c, d);
165
+ const e = from[p + 4];
166
+ if (e & 0x80) return null;
167
+ if (length === 5) return _fromCharCode(a, b, c, d, e);
168
+ const f = from[p + 5];
169
+ if (f & 0x80) return null;
170
+ if (length === 6) return _fromCharCode(a, b, c, d, e, f);
171
+ const g = from[p + 6];
172
+ if (g & 0x80) return null;
173
+ return _fromCharCode(a, b, c, d, e, f, g);
174
+ }
175
+ const e = from[p + 4];
176
+ const f = from[p + 5];
177
+ const g = from[p + 6];
178
+ const h = from[p + 7];
179
+ if ((e | f | g | h) & 0x80) return null;
180
+ if (length < 12) {
181
+ if (length === 8) return _fromCharCode(a, b, c, d, e, f, g, h);
182
+ const i = from[p + 8];
183
+ if (i & 0x80) return null;
184
+ if (length === 9) return _fromCharCode(a, b, c, d, e, f, g, h, i);
185
+ const j = from[p + 9];
186
+ if (j & 0x80) return null;
187
+ if (length === 10) return _fromCharCode(a, b, c, d, e, f, g, h, i, j);
188
+ const k = from[p + 10];
189
+ if (k & 0x80) return null;
190
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k);
191
+ }
192
+ const i = from[p + 8];
193
+ const j = from[p + 9];
194
+ const k = from[p + 10];
195
+ const l = from[p + 11];
196
+ if ((i | j | k | l) & 0x80) return null;
197
+ if (length === 12) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l);
198
+ const m = from[p + 12];
199
+ if (m & 0x80) return null;
200
+ if (length === 13) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m);
201
+ const n = from[p + 13];
202
+ if (n & 0x80) return null;
203
+ if (length === 14) return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
204
+ const o = from[p + 14];
205
+ if (o & 0x80) return null;
206
+ return _fromCharCode(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o);
207
+ };
139
208
 
140
209
  /**
141
210
  * decodes a UTF-8 string from a given buffer
211
+ * @param from source buffer
212
+ * @param offset byte offset to start reading from
213
+ * @param length number of bytes to read
214
+ * @returns decoded string
142
215
  */
143
- export const decodeUtf8From = (from: Uint8Array, offset?: number, length?: number): string => {
144
- let buffer: Uint8Array;
145
-
146
- if (offset === undefined) {
147
- buffer = from;
148
- } else if (length === undefined) {
149
- buffer = from.subarray(offset);
150
- } else {
151
- buffer = from.subarray(offset, offset + length);
152
- }
153
-
154
- const end = buffer.length;
155
- if (end > 24) {
156
- return textDecoder.decode(buffer);
216
+ export const decodeUtf8From = (
217
+ from: Uint8Array,
218
+ offset: number = 0,
219
+ length: number = from.length,
220
+ ): string => {
221
+ if (length <= 15) {
222
+ const result = _shortString(from, offset, length);
223
+ if (result !== null) return result;
157
224
  }
225
+ return textDecoder.decode(from.subarray(offset, offset + length));
226
+ };
158
227
 
159
- {
160
- let str = '';
161
- let idx = 0;
228
+ /**
229
+ * calculates the UTF-8 byte length of a string
230
+ * @param str string to measure
231
+ * @returns byte length when encoded as UTF-8
232
+ */
233
+ export const getUtf8Length = (str: string): number => {
234
+ const len = str.length;
162
235
 
163
- for (; idx + 3 < end; idx += 4) {
164
- const a = buffer[idx];
165
- const b = buffer[idx + 1];
166
- const c = buffer[idx + 2];
167
- const d = buffer[idx + 3];
236
+ let u16pos = 0;
237
+ let u8pos = 0;
168
238
 
169
- if ((a | b | c | d) & 0x80) {
170
- return str + textDecoder.decode(buffer.subarray(idx));
171
- }
239
+ // ASCII fast-path: batch process 4 chars at a time
240
+ while (u16pos + 3 < len) {
241
+ const a = str.charCodeAt(u16pos);
242
+ const b = str.charCodeAt(u16pos + 1);
243
+ const c = str.charCodeAt(u16pos + 2);
244
+ const d = str.charCodeAt(u16pos + 3);
172
245
 
173
- str += fromCharCode(a, b, c, d);
246
+ if ((a | b | c | d) >= 0x80) {
247
+ break;
174
248
  }
175
249
 
176
- for (; idx < end; idx++) {
177
- const x = buffer[idx];
178
-
179
- if (x & 0x80) {
180
- return str + textDecoder.decode(buffer.subarray(idx));
181
- }
250
+ u16pos += 4;
251
+ u8pos += 4;
252
+ }
182
253
 
183
- str += fromCharCode(x);
254
+ // handle remaining chars
255
+ while (u16pos < len) {
256
+ const code = str.charCodeAt(u16pos);
257
+
258
+ if (code < 0x80) {
259
+ u16pos += 1;
260
+ u8pos += 1;
261
+ } else if (code < 0x800) {
262
+ u16pos += 1;
263
+ u8pos += 2;
264
+ } else if (code < 0xd800 || code > 0xdbff) {
265
+ u16pos += 1;
266
+ u8pos += 3;
267
+ } else {
268
+ u16pos += 2;
269
+ u8pos += 4;
184
270
  }
185
-
186
- return str;
187
271
  }
272
+
273
+ return u8pos;
188
274
  };
189
275
 
190
276
  /**
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@atcute/uint8array",
4
- "version": "1.0.4",
4
+ "version": "1.0.6",
5
5
  "description": "uint8array utilities",
6
6
  "license": "0BSD",
7
7
  "repository": {
8
8
  "url": "https://github.com/mary-ext/atcute",
9
- "directory": "packages/utilities/uint8array"
9
+ "directory": "packages/misc/uint8array"
10
10
  },
11
11
  "files": [
12
12
  "dist/",
@@ -23,11 +23,10 @@
23
23
  },
24
24
  "sideEffects": false,
25
25
  "devDependencies": {
26
- "@types/bun": "^1.2.21"
26
+ "@types/bun": "^1.3.3"
27
27
  },
28
28
  "scripts": {
29
- "build": "tsc --project tsconfig.build.json",
30
- "test": "bun test --coverage",
29
+ "build": "tsgo --project tsconfig.build.json",
31
30
  "prepublish": "rm -rf dist; pnpm run build"
32
31
  }
33
32
  }