35
const view = new Uint16Array(source.buffer, offset, len);
36
if (len > 0 && (view[0] === 0xFEFF || view[0] === 0xFFFE)) {
37
>
// UTF16 sometimes starts with a BOM https://de.wikipedia.org/wiki/Byte_Order_Mark
stringBuilder.ts
38
>
// It looks like TextDecoder.decode will eat up a leading BOM (0xFEFF or 0xFFFE)
39
>
// We don't want that behavior because we know the string is UTF16LE and the BOM should be maintained
40
>
// So we use the manual decoder
41
>
return compatDecodeUTF16LE(source, offset, len);
42
>
}
43
return getUTF16LE_TextDecoder().decode(view);
44
}
45
46
>
function compatDecodeUTF16LE(source: Uint8Array, offset: number, len: number): string {
stringBuilder.ts
47
>
const result: string[] = [];
48
>
let resultLen = 0;
49
>
for (let i = 0; i < len; i++) {
50
>
const charCode = buffer.readUInt16LE(source, offset); offset += 2;
51
>
result[resultLen++] = String.fromCharCode(charCode);
52
>
}
53
>
return result.join('');
54
>
}
55
56
export class StringBuilder {