MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / decodeUTF32

Function decodeUTF32

src/pkg/utils/encoding.ts:12–34  ·  view source on GitHub ↗
(utf32Bytes: Uint8Array, isLE: boolean = true)

Source from the content-addressed store, hash-verified

10};
11
12export const decodeUTF32 = (utf32Bytes: Uint8Array, isLE: boolean = true): string => {
13 if (!(utf32Bytes instanceof Uint8Array)) {
14 throw new TypeError("utf32Bytes must be a Uint8Array");
15 }
16 const byteLen = utf32Bytes.byteLength;
17 if (byteLen % 4 !== 0) {
18 throw new RangeError("UTF-32 byte length must be a multiple of 4");
19 }
20 const view = new DataView(utf32Bytes.buffer, utf32Bytes.byteOffset, byteLen);
21 let out = "";
22 let chunk: number[] = [];
23 for (let i = 0; i < byteLen; i += 4) {
24 const codePoint = view.getUint32(i, isLE);
25 if (i === 0 && codePoint === 0x0000feff) continue;
26 chunk.push(codePoint);
27 if (chunk.length >= 16384) {
28 out += String.fromCodePoint(...chunk);
29 chunk = [];
30 }
31 }
32 if (chunk.length) out += String.fromCodePoint(...chunk);
33 return out;
34};
35
36export const bytesDecode = (charset: string, bytes: Uint8Array): string => {
37 const normalizedCharset = charset.toLowerCase();

Callers 2

encoding.test.tsFile · 0.90
bytesDecodeFunction · 0.85

Calls 1

pushMethod · 0.80

Tested by

no test coverage detected