bin-serde 1.6.6 → 1.6.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,37 +1,11 @@
1
+ const textDecoder = new TextDecoder();
1
2
  export function unpack(buffer, start = 0, end = buffer.length) {
2
- const len = end - start;
3
- if (len < 1)
3
+ if (end - start < 1)
4
4
  return "";
5
- const chunks = [];
6
- const chunk = [];
7
- let i = 0;
8
- while (start < end) {
9
- let t = buffer[start++];
10
- if (t < 128) {
11
- chunk[i++] = t;
12
- }
13
- else if (t > 191 && t < 224) {
14
- chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
15
- }
16
- else if (t > 239 && t < 245) {
17
- t =
18
- (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
19
- 0x10000;
20
- chunk[i++] = 0xd800 + (t >> 10);
21
- chunk[i++] = 0xdc00 + (t & 1023);
22
- }
23
- else {
24
- chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
25
- }
26
- if (i > 8191) {
27
- chunks.push(String.fromCharCode.apply(String, chunk));
28
- i = 0;
29
- }
30
- }
31
- if (i > 0) {
32
- chunks.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
5
+ if (start === 0 && end === buffer.length) {
6
+ return textDecoder.decode(buffer);
33
7
  }
34
- return chunks.join("");
8
+ return textDecoder.decode(buffer.subarray(start, end));
35
9
  }
36
10
  export function pack(str, buffer, index = 0) {
37
11
  let c1, c2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.6.6",
3
+ "version": "1.6.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
@@ -1,36 +1,11 @@
1
- export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
2
- const len = end - start;
3
- if (len < 1) return "";
4
-
5
- const chunks: string[] = [];
6
- const chunk: number[] = [];
7
- let i = 0;
8
-
9
- while (start < end) {
10
- let t = buffer[start++];
11
- if (t < 128) {
12
- chunk[i++] = t;
13
- } else if (t > 191 && t < 224) {
14
- chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
15
- } else if (t > 239 && t < 245) {
16
- t =
17
- (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
18
- 0x10000;
19
- chunk[i++] = 0xd800 + (t >> 10);
20
- chunk[i++] = 0xdc00 + (t & 1023);
21
- } else {
22
- chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
23
- }
24
- if (i > 8191) {
25
- chunks.push(String.fromCharCode.apply(String, chunk));
26
- i = 0;
27
- }
28
- }
1
+ const textDecoder = new TextDecoder();
29
2
 
30
- if (i > 0) {
31
- chunks.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
3
+ export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
4
+ if (end - start < 1) return "";
5
+ if (start === 0 && end === buffer.length) {
6
+ return textDecoder.decode(buffer);
32
7
  }
33
- return chunks.join("");
8
+ return textDecoder.decode(buffer.subarray(start, end));
34
9
  }
35
10
 
36
11
  export function pack(str: string, buffer: Uint8Array, index = 0): number {