bin-serde 1.7.8 → 1.7.9
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 +14 -1
- package/package.json +1 -1
- package/utf8-buffer.ts +16 -1
package/lib/utf8-buffer.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const textDecoder = new TextDecoder();
|
|
2
|
+
const textEncoder = new TextEncoder();
|
|
2
3
|
const hasBuffer = typeof Buffer !== "undefined";
|
|
3
4
|
export function unpack(buffer, start = 0, end = buffer.length) {
|
|
4
5
|
const len = end - start;
|
|
@@ -38,8 +39,20 @@ export function unpack(buffer, start = 0, end = buffer.length) {
|
|
|
38
39
|
return String.fromCharCode.apply(String, chunks);
|
|
39
40
|
}
|
|
40
41
|
export function pack(str, buffer, index = 0) {
|
|
42
|
+
const len = str.length;
|
|
43
|
+
if (len < 1)
|
|
44
|
+
return index;
|
|
45
|
+
// Node.js: use Buffer.write()
|
|
46
|
+
if (hasBuffer && Buffer.isBuffer(buffer)) {
|
|
47
|
+
return index + buffer.write(str, index, "utf8");
|
|
48
|
+
}
|
|
49
|
+
// Long strings: use TextEncoder
|
|
50
|
+
if (len > 64) {
|
|
51
|
+
return index + textEncoder.encodeInto(str, index === 0 ? buffer : buffer.subarray(index)).written;
|
|
52
|
+
}
|
|
53
|
+
// Short strings: use pure JS
|
|
41
54
|
let c1, c2;
|
|
42
|
-
for (let i = 0; i <
|
|
55
|
+
for (let i = 0; i < len; i++) {
|
|
43
56
|
c1 = str.charCodeAt(i);
|
|
44
57
|
if (c1 < 128) {
|
|
45
58
|
buffer[index++] = c1;
|
package/package.json
CHANGED
package/utf8-buffer.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const textDecoder = new TextDecoder();
|
|
2
|
+
const textEncoder = new TextEncoder();
|
|
2
3
|
const hasBuffer = typeof Buffer !== "undefined";
|
|
3
4
|
|
|
4
5
|
export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
|
|
@@ -39,8 +40,22 @@ export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): stri
|
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
export function pack(str: string, buffer: Uint8Array, index = 0): number {
|
|
43
|
+
const len = str.length;
|
|
44
|
+
if (len < 1) return index;
|
|
45
|
+
|
|
46
|
+
// Node.js: use Buffer.write()
|
|
47
|
+
if (hasBuffer && Buffer.isBuffer(buffer)) {
|
|
48
|
+
return index + buffer.write(str, index, "utf8");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Long strings: use TextEncoder
|
|
52
|
+
if (len > 64) {
|
|
53
|
+
return index + textEncoder.encodeInto(str, index === 0 ? buffer : buffer.subarray(index)).written;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Short strings: use pure JS
|
|
42
57
|
let c1: number, c2: number;
|
|
43
|
-
for (let i = 0; i <
|
|
58
|
+
for (let i = 0; i < len; i++) {
|
|
44
59
|
c1 = str.charCodeAt(i);
|
|
45
60
|
if (c1 < 128) {
|
|
46
61
|
buffer[index++] = c1;
|