bin-serde 1.7.6 → 1.7.7

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,17 +1,41 @@
1
1
  const textDecoder = new TextDecoder();
2
2
  const hasBuffer = typeof Buffer !== "undefined";
3
3
  export function unpack(buffer, start = 0, end = buffer.length) {
4
- if (end - start < 1)
4
+ const len = end - start;
5
+ if (len < 1)
5
6
  return "";
6
- // Fast path: use Buffer.toString() in Node.js (avoids subarray overhead)
7
+ // Node.js: use Buffer.toString()
7
8
  if (hasBuffer && Buffer.isBuffer(buffer)) {
8
9
  return buffer.toString("utf8", start, end);
9
10
  }
10
- // Standard path: TextDecoder
11
- if (start === 0 && end === buffer.length) {
12
- return textDecoder.decode(buffer);
11
+ // Long strings: use TextDecoder
12
+ if (len > 64) {
13
+ return textDecoder.decode(buffer.subarray(start, end));
13
14
  }
14
- return textDecoder.decode(buffer.subarray(start, end));
15
+ // Short strings: use pure JS
16
+ const chunks = [];
17
+ let i = 0;
18
+ let t;
19
+ while (start < end) {
20
+ t = buffer[start++];
21
+ if (t < 128) {
22
+ chunks[i++] = t;
23
+ }
24
+ else if (t > 191 && t < 224) {
25
+ chunks[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
26
+ }
27
+ else if (t > 239 && t < 365) {
28
+ t =
29
+ (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
30
+ 0x10000;
31
+ chunks[i++] = 0xd800 + (t >> 10);
32
+ chunks[i++] = 0xdc00 + (t & 1023);
33
+ }
34
+ else {
35
+ chunks[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
36
+ }
37
+ }
38
+ return String.fromCharCode.apply(String, chunks);
15
39
  }
16
40
  export function pack(str, buffer, index = 0) {
17
41
  let c1, c2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.7.6",
3
+ "version": "1.7.7",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
package/utf8-buffer.ts CHANGED
@@ -2,18 +2,40 @@ const textDecoder = new TextDecoder();
2
2
  const hasBuffer = typeof Buffer !== "undefined";
3
3
 
4
4
  export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
5
- if (end - start < 1) return "";
5
+ const len = end - start;
6
+ if (len < 1) return "";
6
7
 
7
- // Fast path: use Buffer.toString() in Node.js (avoids subarray overhead)
8
+ // Node.js: use Buffer.toString()
8
9
  if (hasBuffer && Buffer.isBuffer(buffer)) {
9
10
  return buffer.toString("utf8", start, end);
10
11
  }
11
12
 
12
- // Standard path: TextDecoder
13
- if (start === 0 && end === buffer.length) {
14
- return textDecoder.decode(buffer);
13
+ // Long strings: use TextDecoder
14
+ if (len > 64) {
15
+ return textDecoder.decode(buffer.subarray(start, end));
15
16
  }
16
- return textDecoder.decode(buffer.subarray(start, end));
17
+
18
+ // Short strings: use pure JS
19
+ const chunks: number[] = [];
20
+ let i = 0;
21
+ let t: number;
22
+ while (start < end) {
23
+ t = buffer[start++]!;
24
+ if (t < 128) {
25
+ chunks[i++] = t;
26
+ } else if (t > 191 && t < 224) {
27
+ chunks[i++] = ((t & 31) << 6) | (buffer[start++]! & 63);
28
+ } else if (t > 239 && t < 365) {
29
+ t =
30
+ (((t & 7) << 18) | ((buffer[start++]! & 63) << 12) | ((buffer[start++]! & 63) << 6) | (buffer[start++]! & 63)) -
31
+ 0x10000;
32
+ chunks[i++] = 0xd800 + (t >> 10);
33
+ chunks[i++] = 0xdc00 + (t & 1023);
34
+ } else {
35
+ chunks[i++] = ((t & 15) << 12) | ((buffer[start++]! & 63) << 6) | (buffer[start++]! & 63);
36
+ }
37
+ }
38
+ return String.fromCharCode.apply(String, chunks);
17
39
  }
18
40
 
19
41
  export function pack(str: string, buffer: Uint8Array, index = 0): number {