MCPcopy Create free account
hub / github.com/p2r3/convert / encodeUTF32

Function encodeUTF32

src/handlers/textEncoding.ts:66–81  ·  view source on GitHub ↗
(str: string, littleEndian: boolean, addBOM = false)

Source from the content-addressed store, hash-verified

64}
65
66function encodeUTF32(str: string, littleEndian: boolean, addBOM = false) {
67 const codepoints = Array.from(str, (ch) => ch.codePointAt(0) || 0);
68 const buf = new ArrayBuffer(codepoints.length * 4 + (addBOM ? 4 : 0));
69 const dv = new DataView(buf);
70 let offset = 0;
71 if (addBOM) {
72 if (littleEndian) dv.setUint32(0, 0xFF_FE_00_00, true);
73 else dv.setUint32(0, 0x00_00_FE_FF, false);
74 offset += 4;
75 }
76 for (const cp of codepoints) {
77 dv.setUint32(offset, cp, littleEndian);
78 offset += 4;
79 }
80 return new Uint8Array(buf, 0, offset);
81}
82
83function decodeUsingTextDecoder(bytes: Uint8Array, label: string) {
84 try {

Callers 1

doConvertMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected