@atcute/uint8array 1.0.1 → 1.0.3
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/dist/index.bun.d.ts +4 -0
- package/dist/index.bun.js +56 -5
- package/dist/index.bun.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +35 -4
- package/dist/index.js.map +1 -1
- package/dist/index.node.d.ts +1 -0
- package/dist/index.node.js +4 -0
- package/dist/index.node.js.map +1 -1
- package/lib/index.bun.ts +68 -9
- package/lib/index.node.ts +5 -0
- package/lib/index.ts +44 -4
- package/package.json +2 -2
package/dist/index.bun.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
|
|
|
4
4
|
export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
|
|
5
5
|
export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
|
|
6
6
|
export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array;
|
|
7
|
+
export declare const encodeUtf8: (str: string) => Uint8Array;
|
|
7
8
|
export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
|
|
9
|
+
/**
|
|
10
|
+
* decodes a UTF-8 string from a given buffer
|
|
11
|
+
*/
|
|
8
12
|
export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
|
|
9
13
|
export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array>;
|
package/dist/index.bun.js
CHANGED
|
@@ -3,8 +3,8 @@ import { Buffer as NodeBuffer } from 'node:buffer';
|
|
|
3
3
|
import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto';
|
|
4
4
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
5
5
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const textEncoder = new TextEncoder();
|
|
7
|
+
const textDecoder = new TextDecoder();
|
|
8
8
|
const toUint8Array = (buffer) => {
|
|
9
9
|
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
10
10
|
};
|
|
@@ -25,11 +25,62 @@ export const concat = (arrays, size) => {
|
|
|
25
25
|
// Bun's typings is slightly wrong, *you can* pass `size: undefined` with `asUint8Array: true`
|
|
26
26
|
return _concat(arrays, size, true);
|
|
27
27
|
};
|
|
28
|
+
export const encodeUtf8 = textEncoder.encode.bind(textEncoder);
|
|
28
29
|
export const encodeUtf8Into = (to, str, offset, length) => {
|
|
29
|
-
|
|
30
|
+
let buffer;
|
|
31
|
+
if (offset === undefined) {
|
|
32
|
+
buffer = to;
|
|
33
|
+
}
|
|
34
|
+
else if (length === undefined) {
|
|
35
|
+
buffer = to.subarray(offset);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
buffer = to.subarray(offset, offset + length);
|
|
39
|
+
}
|
|
40
|
+
const result = textEncoder.encodeInto(str, buffer);
|
|
41
|
+
return result.written;
|
|
30
42
|
};
|
|
31
|
-
|
|
32
|
-
|
|
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);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
buffer = from.subarray(offset, offset + length);
|
|
57
|
+
}
|
|
58
|
+
const end = buffer.length;
|
|
59
|
+
if (end > 24) {
|
|
60
|
+
return textDecoder.decode(buffer);
|
|
61
|
+
}
|
|
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;
|
|
83
|
+
}
|
|
33
84
|
};
|
|
34
85
|
export const toSha256 = async (buffer) => {
|
|
35
86
|
return toUint8Array(_hash('sha256', buffer, 'buffer'));
|
package/dist/index.bun.js.map
CHANGED
|
@@ -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;
|
|
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,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;QAED,OAAO,GAAG,CAAC;IACZ,CAAC;AACF,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAuB,EAAE;IACzE,OAAO,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;AACxD,CAAC,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -24,11 +24,15 @@ export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean
|
|
|
24
24
|
*/
|
|
25
25
|
export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array;
|
|
26
26
|
/**
|
|
27
|
-
* encodes a UTF-8 string
|
|
27
|
+
* encodes a UTF-8 string
|
|
28
|
+
*/
|
|
29
|
+
export declare const encodeUtf8: (str: string) => Uint8Array;
|
|
30
|
+
/**
|
|
31
|
+
* encodes a UTF-8 string into a given buffer
|
|
28
32
|
*/
|
|
29
33
|
export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
|
|
30
34
|
/**
|
|
31
|
-
* decodes a UTF-8 string from a buffer
|
|
35
|
+
* decodes a UTF-8 string from a given buffer
|
|
32
36
|
*/
|
|
33
37
|
export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
|
|
34
38
|
/**
|
package/dist/index.js
CHANGED
|
@@ -88,7 +88,13 @@ export const concat = (arrays, size) => {
|
|
|
88
88
|
return buffer;
|
|
89
89
|
};
|
|
90
90
|
/**
|
|
91
|
-
* encodes a UTF-8 string
|
|
91
|
+
* encodes a UTF-8 string
|
|
92
|
+
*/
|
|
93
|
+
export const encodeUtf8 = (str) => {
|
|
94
|
+
return textEncoder.encode(str);
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* encodes a UTF-8 string into a given buffer
|
|
92
98
|
*/
|
|
93
99
|
export const encodeUtf8Into = (to, str, offset, length) => {
|
|
94
100
|
let buffer;
|
|
@@ -104,8 +110,9 @@ export const encodeUtf8Into = (to, str, offset, length) => {
|
|
|
104
110
|
const result = textEncoder.encodeInto(str, buffer);
|
|
105
111
|
return result.written;
|
|
106
112
|
};
|
|
113
|
+
const fromCharCode = String.fromCharCode;
|
|
107
114
|
/**
|
|
108
|
-
* decodes a UTF-8 string from a buffer
|
|
115
|
+
* decodes a UTF-8 string from a given buffer
|
|
109
116
|
*/
|
|
110
117
|
export const decodeUtf8From = (from, offset, length) => {
|
|
111
118
|
let buffer;
|
|
@@ -118,8 +125,32 @@ export const decodeUtf8From = (from, offset, length) => {
|
|
|
118
125
|
else {
|
|
119
126
|
buffer = from.subarray(offset, offset + length);
|
|
120
127
|
}
|
|
121
|
-
const
|
|
122
|
-
|
|
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);
|
|
144
|
+
}
|
|
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);
|
|
151
|
+
}
|
|
152
|
+
return str;
|
|
153
|
+
}
|
|
123
154
|
};
|
|
124
155
|
/**
|
|
125
156
|
* get a SHA-256 digest of this buffer
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../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,CAAC,GAAG,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;YAE1B,IAAI,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC;gBAC5B,OAAO,GAAG,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;YACvD,CAAC;YAED,GAAG,IAAI,YAAY,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACjC,CAAC;QAED,OAAO,GAAG,GAAG,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC;YACzB,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YAEtB,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC;gBACd,OAAO,GAAG,GAAG,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;YACvD,CAAC;YAED,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC;QAED,OAAO,GAAG,CAAC;IACZ,CAAC;AACF,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAuB,EAAE;IACzE,OAAO,IAAI,UAAU,CAAC,MAAM,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;AAC/D,CAAC,CAAC"}
|
package/dist/index.node.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export declare const compare: (a: Uint8Array, b: Uint8Array) => number;
|
|
|
4
4
|
export declare const equals: (a: Uint8Array, b: Uint8Array) => boolean;
|
|
5
5
|
export declare const timingSafeEquals: (a: Uint8Array, b: Uint8Array) => boolean;
|
|
6
6
|
export declare const concat: (arrays: Uint8Array[], size?: number) => Uint8Array;
|
|
7
|
+
export declare const encodeUtf8: (str: string) => Uint8Array;
|
|
7
8
|
export declare const encodeUtf8Into: (to: Uint8Array, str: string, offset?: number, length?: number) => number;
|
|
8
9
|
export declare const decodeUtf8From: (from: Uint8Array, offset?: number, length?: number) => string;
|
|
9
10
|
export declare const toSha256: (buffer: Uint8Array) => Promise<Uint8Array>;
|
package/dist/index.node.js
CHANGED
|
@@ -3,6 +3,7 @@ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto'
|
|
|
3
3
|
const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
|
|
4
4
|
const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
|
|
5
5
|
const _concat = /*#__PURE__*/ NodeBuffer.concat;
|
|
6
|
+
const _from = /*#__PURE__*/ NodeBuffer.from;
|
|
6
7
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
7
8
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
8
9
|
const _utf8Slice = /*#__PURE__*/ NodeBuffer.prototype.utf8Slice;
|
|
@@ -28,6 +29,9 @@ export const timingSafeEquals = (a, b) => {
|
|
|
28
29
|
export const concat = (arrays, size) => {
|
|
29
30
|
return toUint8Array(_concat(arrays, size));
|
|
30
31
|
};
|
|
32
|
+
export const encodeUtf8 = (str) => {
|
|
33
|
+
return toUint8Array(_from(str, 'utf8'));
|
|
34
|
+
};
|
|
31
35
|
export const encodeUtf8Into = (to, str, offset, length) => {
|
|
32
36
|
return _utf8Write.call(to, str, offset, length);
|
|
33
37
|
};
|
package/dist/index.node.js.map
CHANGED
|
@@ -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,CAA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O,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AACvD,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,EAAE,MAAkB,EAAuB,EAAE;IACzE,OAAO,YAAY,CAAC,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC;AACxD,CAAC,CAAC"}
|
package/lib/index.bun.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto'
|
|
|
5
5
|
|
|
6
6
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
7
7
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
8
|
-
|
|
9
|
-
const
|
|
8
|
+
|
|
9
|
+
const textEncoder = new TextEncoder();
|
|
10
|
+
const textDecoder = new TextDecoder();
|
|
10
11
|
|
|
11
12
|
const toUint8Array = (buffer: NodeBuffer) => {
|
|
12
13
|
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
@@ -35,16 +36,74 @@ export const concat = (arrays: Uint8Array[], size?: number): Uint8Array => {
|
|
|
35
36
|
return _concat(arrays, size as number, true);
|
|
36
37
|
};
|
|
37
38
|
|
|
39
|
+
export const encodeUtf8: (str: string) => Uint8Array = textEncoder.encode.bind(textEncoder);
|
|
40
|
+
|
|
38
41
|
export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, length?: number): number => {
|
|
39
|
-
|
|
42
|
+
let buffer: Uint8Array;
|
|
43
|
+
|
|
44
|
+
if (offset === undefined) {
|
|
45
|
+
buffer = to;
|
|
46
|
+
} else if (length === undefined) {
|
|
47
|
+
buffer = to.subarray(offset);
|
|
48
|
+
} else {
|
|
49
|
+
buffer = to.subarray(offset, offset + length);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const result = textEncoder.encodeInto(str, buffer);
|
|
53
|
+
|
|
54
|
+
return result.written;
|
|
40
55
|
};
|
|
41
56
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
57
|
+
const fromCharCode = String.fromCharCode;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* decodes a UTF-8 string from a given buffer
|
|
61
|
+
*/
|
|
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);
|
|
76
|
+
}
|
|
77
|
+
|
|
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
|
+
}
|
|
48
107
|
};
|
|
49
108
|
|
|
50
109
|
export const toSha256 = async (buffer: Uint8Array): Promise<Uint8Array> => {
|
package/lib/index.node.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { hash as _hash, timingSafeEqual as _timingSafeEqual } from 'node:crypto'
|
|
|
4
4
|
const _alloc = /*#__PURE__*/ NodeBuffer.alloc;
|
|
5
5
|
const _allocUnsafe = /*#__PURE__*/ NodeBuffer.allocUnsafe;
|
|
6
6
|
const _concat = /*#__PURE__*/ NodeBuffer.concat;
|
|
7
|
+
const _from = /*#__PURE__*/ NodeBuffer.from;
|
|
7
8
|
|
|
8
9
|
const _compare = /*#__PURE__*/ NodeBuffer.prototype.compare;
|
|
9
10
|
const _equals = /*#__PURE__*/ NodeBuffer.prototype.equals;
|
|
@@ -38,6 +39,10 @@ export const concat = (arrays: Uint8Array[], size?: number): Uint8Array => {
|
|
|
38
39
|
return toUint8Array(_concat(arrays, size));
|
|
39
40
|
};
|
|
40
41
|
|
|
42
|
+
export const encodeUtf8 = (str: string): Uint8Array => {
|
|
43
|
+
return toUint8Array(_from(str, 'utf8'));
|
|
44
|
+
};
|
|
45
|
+
|
|
41
46
|
export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, length?: number): number => {
|
|
42
47
|
return _utf8Write.call(to, str, offset, length);
|
|
43
48
|
};
|
package/lib/index.ts
CHANGED
|
@@ -110,7 +110,14 @@ export const concat = (arrays: Uint8Array[], size?: number): Uint8Array => {
|
|
|
110
110
|
};
|
|
111
111
|
|
|
112
112
|
/**
|
|
113
|
-
* encodes a UTF-8 string
|
|
113
|
+
* encodes a UTF-8 string
|
|
114
|
+
*/
|
|
115
|
+
export const encodeUtf8 = (str: string): Uint8Array => {
|
|
116
|
+
return textEncoder.encode(str);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* encodes a UTF-8 string into a given buffer
|
|
114
121
|
*/
|
|
115
122
|
export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, length?: number): number => {
|
|
116
123
|
let buffer: Uint8Array;
|
|
@@ -128,8 +135,10 @@ export const encodeUtf8Into = (to: Uint8Array, str: string, offset?: number, len
|
|
|
128
135
|
return result.written;
|
|
129
136
|
};
|
|
130
137
|
|
|
138
|
+
const fromCharCode = String.fromCharCode;
|
|
139
|
+
|
|
131
140
|
/**
|
|
132
|
-
* decodes a UTF-8 string from a buffer
|
|
141
|
+
* decodes a UTF-8 string from a given buffer
|
|
133
142
|
*/
|
|
134
143
|
export const decodeUtf8From = (from: Uint8Array, offset?: number, length?: number): string => {
|
|
135
144
|
let buffer: Uint8Array;
|
|
@@ -142,9 +151,40 @@ export const decodeUtf8From = (from: Uint8Array, offset?: number, length?: numbe
|
|
|
142
151
|
buffer = from.subarray(offset, offset + length);
|
|
143
152
|
}
|
|
144
153
|
|
|
145
|
-
const
|
|
154
|
+
const end = buffer.length;
|
|
155
|
+
if (end > 24) {
|
|
156
|
+
return textDecoder.decode(buffer);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
{
|
|
160
|
+
let str = '';
|
|
161
|
+
let idx = 0;
|
|
162
|
+
|
|
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];
|
|
168
|
+
|
|
169
|
+
if ((a | b | c | d) & 0x80) {
|
|
170
|
+
return str + textDecoder.decode(buffer.subarray(idx));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
str += fromCharCode(a, b, c, d);
|
|
174
|
+
}
|
|
146
175
|
|
|
147
|
-
|
|
176
|
+
for (; idx < end; idx++) {
|
|
177
|
+
const x = buffer[idx];
|
|
178
|
+
|
|
179
|
+
if (x & 0x80) {
|
|
180
|
+
return str + textDecoder.decode(buffer.subarray(idx));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
str += fromCharCode(x);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return str;
|
|
187
|
+
}
|
|
148
188
|
};
|
|
149
189
|
|
|
150
190
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@atcute/uint8array",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.3",
|
|
5
5
|
"description": "uint8array utilities",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"sideEffects": false,
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@types/bun": "^1.
|
|
26
|
+
"@types/bun": "^1.2.13"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"build": "tsc --project tsconfig.build.json",
|