MCPcopy Create free account
hub / github.com/LibPDF-js/core / decodeUtf16BE

Function decodeUtf16BE

src/helpers/encoding.ts:103–129  ·  view source on GitHub ↗
(bytes: Uint8Array)

Source from the content-addressed store, hash-verified

101 * Decode UTF-16BE bytes to string (skips BOM if present).
102 */
103export function decodeUtf16BE(bytes: Uint8Array): string {
104 const start = hasUtf16BOM(bytes) ? 2 : 0;
105
106 const chars: string[] = [];
107
108 for (let i = start; i < bytes.length - 1; i += 2) {
109 const code = (bytes[i] << 8) | bytes[i + 1];
110
111 // Handle surrogate pairs
112 if (code >= 0xd800 && code <= 0xdbff && i + 3 < bytes.length) {
113 const low = (bytes[i + 2] << 8) | bytes[i + 3];
114
115 if (low >= 0xdc00 && low <= 0xdfff) {
116 const codePoint = 0x10000 + ((code - 0xd800) << 10) + (low - 0xdc00);
117 chars.push(String.fromCodePoint(codePoint));
118
119 i += 2;
120
121 continue;
122 }
123 }
124
125 chars.push(String.fromCharCode(code));
126 }
127
128 return chars.join("");
129}
130
131/**
132 * Decode PDFDocEncoding bytes to string.

Callers 2

encoding.test.tsFile · 0.90
decodeTextStringFunction · 0.85

Calls 2

hasUtf16BOMFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected