Debian Codegraph @

node/@xterm/xterm/src/common/input/TextDecoder.ts

raw
1 /**
2 * Copyright (c) 2019 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 /**
7 * Polyfill - Convert UTF32 codepoint into JS string.
8 * Note: The built-in String.fromCodePoint happens to be much slower
9 * due to additional sanity checks. We can avoid them since
10 * we always operate on legal UTF32 (granted by the input decoders)
11 * and use this faster version instead.
12 */
13 export function stringFromCodePoint(codePoint: number): string {
14 if (codePoint > 0xFFFF) {
15 codePoint -= 0x10000;
16 return String.fromCharCode((codePoint >> 10) + 0xD800) + String.fromCharCode((codePoint % 0x400) + 0xDC00);
17 }
18 return String.fromCharCode(codePoint);
19 }
20
21 /**
22 * Convert UTF32 char codes into JS string.
23 * Basically the same as `stringFromCodePoint` but for multiple codepoints
24 * in a loop (which is a lot faster).
25 */
26 export function utf32ToString(data: Uint32Array, start: number = 0, end: number = data.length): string {
27 let result = '';
28 for (let i = start; i < end; ++i) {
29 let codepoint = data[i];
30 if (codepoint > 0xFFFF) {
31 // JS strings are encoded as UTF16, thus a non BMP codepoint gets converted into a surrogate
32 // pair conversion rules:
33 // - subtract 0x10000 from code point, leaving a 20 bit number
34 // - add high 10 bits to 0xD800 --> first surrogate
35 // - add low 10 bits to 0xDC00 --> second surrogate
36 codepoint -= 0x10000;
37 result += String.fromCharCode((codepoint >> 10) + 0xD800) + String.fromCharCode((codepoint % 0x400) + 0xDC00);
38 } else {
39 result += String.fromCharCode(codepoint);
40 }
41 }
42 return result;
43 }
44
45 /**
46 * StringToUtf32 - decodes UTF16 sequences into UTF32 codepoints.
47 * To keep the decoder in line with JS strings it handles single surrogates as UCS2.
48 */
49 export class StringToUtf32 {
50 private _interim: number = 0;
51
52 /**
53 * Clears interim and resets decoder to clean state.
54 */
55 public clear(): void {
56 this._interim = 0;
57 }
58
59 /**
60 * Decode JS string to UTF32 codepoints.
61 * The methods assumes stream input and will store partly transmitted
62 * surrogate pairs and decode them with the next data chunk.
63 * Note: The method does no bound checks for target, therefore make sure
64 * the provided input data does not exceed the size of `target`.
65 * Returns the number of written codepoints in `target`.
66 */
67 public decode(input: string, target: Uint32Array): number {
68 const length = input.length;
69
70 if (!length) {
71 return 0;
72 }
73
74 let size = 0;
75 let startPos = 0;
76
77 // handle leftover surrogate high
78 if (this._interim) {
79 const second = input.charCodeAt(startPos++);
80 if (0xDC00 <= second && second <= 0xDFFF) {
81 target[size++] = (this._interim - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
82 } else {
83 // illegal codepoint (USC2 handling)
84 target[size++] = this._interim;
85 target[size++] = second;
86 }
87 this._interim = 0;
88 }
89
90 for (let i = startPos; i < length; ++i) {
91 const code = input.charCodeAt(i);
92 // surrogate pair first
93 if (0xD800 <= code && code <= 0xDBFF) {
94 if (++i >= length) {
95 this._interim = code;
96 return size;
97 }
98 const second = input.charCodeAt(i);
99 if (0xDC00 <= second && second <= 0xDFFF) {
100 target[size++] = (code - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
101 } else {
102 // illegal codepoint (USC2 handling)
103 target[size++] = code;
104 target[size++] = second;
105 }
106 continue;
107 }
108 if (code === 0xFEFF) {
109 // BOM
110 continue;
111 }
112 target[size++] = code;
113 }
114 return size;
115 }
116 }
117
118 /**
119 * Utf8Decoder - decodes UTF8 byte sequences into UTF32 codepoints.
120 */
121 export class Utf8ToUtf32 {
122 public interim: Uint8Array = new Uint8Array(3);
123
124 /**
125 * Clears interim bytes and resets decoder to clean state.
126 */
127 public clear(): void {
128 this.interim.fill(0);
129 }
130
131 /**
132 * Decodes UTF8 byte sequences in `input` to UTF32 codepoints in `target`.
133 * The methods assumes stream input and will store partly transmitted bytes
134 * and decode them with the next data chunk.
135 * Note: The method does no bound checks for target, therefore make sure
136 * the provided data chunk does not exceed the size of `target`.
137 * Returns the number of written codepoints in `target`.
138 */
139 public decode(input: Uint8Array, target: Uint32Array): number {
140 const length = input.length;
141
142 if (!length) {
143 return 0;
144 }
145
146 let size = 0;
147 let byte1: number;
148 let byte2: number;
149 let byte3: number;
150 let byte4: number;
151 let codepoint = 0;
152 let startPos = 0;
153
154 // handle leftover bytes
155 if (this.interim[0]) {
156 let discardInterim = false;
157 let cp = this.interim[0];
158 cp &= ((((cp & 0xE0) === 0xC0)) ? 0x1F : (((cp & 0xF0) === 0xE0)) ? 0x0F : 0x07);
159 let pos = 0;
160 let tmp: number;
161 while ((tmp = this.interim[++pos] & 0x3F) && pos < 4) {
162 cp <<= 6;
163 cp |= tmp;
164 }
165 // missing bytes - read ahead from input
166 const type = (((this.interim[0] & 0xE0) === 0xC0)) ? 2 : (((this.interim[0] & 0xF0) === 0xE0)) ? 3 : 4;
167 const missing = type - pos;
168 while (startPos < missing) {
169 if (startPos >= length) {
170 return 0;
171 }
172 tmp = input[startPos++];
173 if ((tmp & 0xC0) !== 0x80) {
174 // wrong continuation, discard interim bytes completely
175 startPos--;
176 discardInterim = true;
177 break;
178 } else {
179 // need to save so we can continue short inputs in next call
180 this.interim[pos++] = tmp;
181 cp <<= 6;
182 cp |= tmp & 0x3F;
183 }
184 }
185 if (!discardInterim) {
186 // final test is type dependent
187 if (type === 2) {
188 if (cp < 0x80) {
189 // wrong starter byte
190 startPos--;
191 } else {
192 target[size++] = cp;
193 }
194 } else if (type === 3) {
195 if (cp < 0x0800 || (cp >= 0xD800 && cp <= 0xDFFF) || cp === 0xFEFF) {
196 // illegal codepoint or BOM
197 } else {
198 target[size++] = cp;
199 }
200 } else {
201 if (cp < 0x010000 || cp > 0x10FFFF) {
202 // illegal codepoint
203 } else {
204 target[size++] = cp;
205 }
206 }
207 }
208 this.interim.fill(0);
209 }
210
211 // loop through input
212 const fourStop = length - 4;
213 let i = startPos;
214 while (i < length) {
215 /**
216 * ASCII shortcut with loop unrolled to 4 consecutive ASCII chars.
217 * This is a compromise between speed gain for ASCII
218 * and penalty for non ASCII:
219 * For best ASCII performance the char should be stored directly into target,
220 * but even a single attempt to write to target and compare afterwards
221 * penalizes non ASCII really bad (-50%), thus we load the char into byteX first,
222 * which reduces ASCII performance by ~15%.
223 * This trial for ASCII reduces non ASCII performance by ~10% which seems acceptible
224 * compared to the gains.
225 * Note that this optimization only takes place for 4 consecutive ASCII chars,
226 * for any shorter it bails out. Worst case - all 4 bytes being read but
227 * thrown away due to the last being a non ASCII char (-10% performance).
228 */
229 while (i < fourStop
230 && !((byte1 = input[i]) & 0x80)
231 && !((byte2 = input[i + 1]) & 0x80)
232 && !((byte3 = input[i + 2]) & 0x80)
233 && !((byte4 = input[i + 3]) & 0x80))
234 {
235 target[size++] = byte1;
236 target[size++] = byte2;
237 target[size++] = byte3;
238 target[size++] = byte4;
239 i += 4;
240 }
241
242 // reread byte1
243 byte1 = input[i++];
244
245 // 1 byte
246 if (byte1 < 0x80) {
247 target[size++] = byte1;
248
249 // 2 bytes
250 } else if ((byte1 & 0xE0) === 0xC0) {
251 if (i >= length) {
252 this.interim[0] = byte1;
253 return size;
254 }
255 byte2 = input[i++];
256 if ((byte2 & 0xC0) !== 0x80) {
257 // wrong continuation
258 i--;
259 continue;
260 }
261 codepoint = (byte1 & 0x1F) << 6 | (byte2 & 0x3F);
262 if (codepoint < 0x80) {
263 // wrong starter byte
264 i--;
265 continue;
266 }
267 target[size++] = codepoint;
268
269 // 3 bytes
270 } else if ((byte1 & 0xF0) === 0xE0) {
271 if (i >= length) {
272 this.interim[0] = byte1;
273 return size;
274 }
275 byte2 = input[i++];
276 if ((byte2 & 0xC0) !== 0x80) {
277 // wrong continuation
278 i--;
279 continue;
280 }
281 if (i >= length) {
282 this.interim[0] = byte1;
283 this.interim[1] = byte2;
284 return size;
285 }
286 byte3 = input[i++];
287 if ((byte3 & 0xC0) !== 0x80) {
288 // wrong continuation
289 i--;
290 continue;
291 }
292 codepoint = (byte1 & 0x0F) << 12 | (byte2 & 0x3F) << 6 | (byte3 & 0x3F);
293 if (codepoint < 0x0800 || (codepoint >= 0xD800 && codepoint <= 0xDFFF) || codepoint === 0xFEFF) {
294 // illegal codepoint or BOM, no i-- here
295 continue;
296 }
297 target[size++] = codepoint;
298
299 // 4 bytes
300 } else if ((byte1 & 0xF8) === 0xF0) {
301 if (i >= length) {
302 this.interim[0] = byte1;
303 return size;
304 }
305 byte2 = input[i++];
306 if ((byte2 & 0xC0) !== 0x80) {
307 // wrong continuation
308 i--;
309 continue;
310 }
311 if (i >= length) {
312 this.interim[0] = byte1;
313 this.interim[1] = byte2;
314 return size;
315 }
316 byte3 = input[i++];
317 if ((byte3 & 0xC0) !== 0x80) {
318 // wrong continuation
319 i--;
320 continue;
321 }
322 if (i >= length) {
323 this.interim[0] = byte1;
324 this.interim[1] = byte2;
325 this.interim[2] = byte3;
326 return size;
327 }
328 byte4 = input[i++];
329 if ((byte4 & 0xC0) !== 0x80) {
330 // wrong continuation
331 i--;
332 continue;
333 }
334 codepoint = (byte1 & 0x07) << 18 | (byte2 & 0x3F) << 12 | (byte3 & 0x3F) << 6 | (byte4 & 0x3F);
335 if (codepoint < 0x010000 || codepoint > 0x10FFFF) {
336 // illegal codepoint, no i-- here
337 continue;
338 }
339 target[size++] = codepoint;
340 } else {
341 // illegal byte, just skip
342 }
343 }
344 return size;
345 }
346 }