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.
- package/lib/utf8-buffer.js +30 -6
- package/package.json +1 -1
- package/utf8-buffer.ts +28 -6
package/lib/utf8-buffer.js
CHANGED
|
@@ -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
|
-
|
|
4
|
+
const len = end - start;
|
|
5
|
+
if (len < 1)
|
|
5
6
|
return "";
|
|
6
|
-
//
|
|
7
|
+
// Node.js: use Buffer.toString()
|
|
7
8
|
if (hasBuffer && Buffer.isBuffer(buffer)) {
|
|
8
9
|
return buffer.toString("utf8", start, end);
|
|
9
10
|
}
|
|
10
|
-
//
|
|
11
|
-
if (
|
|
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
|
-
|
|
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
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
|
-
|
|
5
|
+
const len = end - start;
|
|
6
|
+
if (len < 1) return "";
|
|
6
7
|
|
|
7
|
-
//
|
|
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
|
-
//
|
|
13
|
-
if (
|
|
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
|
-
|
|
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 {
|