From 27b91f6b00c3a8d8d25bb0850518b8ccb75d8485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 3 Jan 2019 02:55:11 +0100 Subject: [PATCH] speedup utf32ToString conversion --- src/EscapeSequenceParser.ts | 2 +- src/common/TypedArrayUtils.ts | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/EscapeSequenceParser.ts b/src/EscapeSequenceParser.ts index 7bd425d4..ec7a9da7 100644 --- a/src/EscapeSequenceParser.ts +++ b/src/EscapeSequenceParser.ts @@ -567,7 +567,7 @@ export class EscapeSequenceParser extends Disposable implements IEscapeSequenceP if (j >= length || (code = data[j]) < 0x20 || (code > 0x7f && code <= 0x9f)) { - osc += utf32ToString(data.subarray(i, j)); + osc += utf32ToString(data, i, j); i = j - 1; break; } diff --git a/src/common/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts index 5ec1fd48..5f5be782 100644 --- a/src/common/TypedArrayUtils.ts +++ b/src/common/TypedArrayUtils.ts @@ -1,5 +1,3 @@ -import { stringFromCodePoint } from '../core/input/TextDecoder'; - /** * Copyright (c) 2018 The xterm.js authors. All rights reserved. * @license MIT @@ -54,17 +52,17 @@ export function concat(a: T, b: T): T { /** * Convert UTF32 char codes into JS string. - * Note the typed array is not limited to Uint16Array, make sure to align - * the values to 0-65535 integer for other typed array types, otherwise - * the conversion will fail. */ -export function utf32ToString(data: T): string { - if ((String as any).fromCodePoint) { - return (String as any).fromCodePoint.apply(null, data); - } +export function utf32ToString(data: T, start: number = 0, end: number = data.length): string { let result = ''; - for (let i = 0; i < data.length; ++i) { - result += stringFromCodePoint(data[i]); + let cp; + for (let i = start; i < end; ++i) { + if ((cp = data[i]) > 0xFFFF) { + cp -= 0x10000; + result += String.fromCharCode((cp >> 10) + 0xD800) + String.fromCharCode((cp % 0x400) + 0xDC00); + } else { + result += String.fromCharCode(cp); + } } return result; }