From 34c2c766cabae9c32ab221ad225250a986db996b Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Sun, 29 Mar 2020 17:29:47 +0200 Subject: [PATCH] Fixes #93370: Use TextDecoder to create a reverse string --- src/vs/editor/common/core/stringBuilder.ts | 5 +++-- .../common/modes/supports/richEditBrackets.ts | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/vs/editor/common/core/stringBuilder.ts b/src/vs/editor/common/core/stringBuilder.ts index 1ae6347c00e..8a577ab8de2 100644 --- a/src/vs/editor/common/core/stringBuilder.ts +++ b/src/vs/editor/common/core/stringBuilder.ts @@ -24,17 +24,18 @@ export interface IStringBuilder { } let _platformTextDecoder: TextDecoder | null; -function getPlatformTextDecoder(): TextDecoder { +export function getPlatformTextDecoder(): TextDecoder { if (!_platformTextDecoder) { _platformTextDecoder = new TextDecoder(platform.isLittleEndian() ? 'UTF-16LE' : 'UTF-16BE'); } return _platformTextDecoder; } +export const hasTextDecoder = (typeof TextDecoder !== 'undefined'); export let createStringBuilder: (capacity: number) => IStringBuilder; export let decodeUTF16LE: (source: Uint8Array, offset: number, len: number) => string; -if (typeof TextDecoder !== 'undefined') { +if (hasTextDecoder) { createStringBuilder = (capacity) => new StringBuilder(capacity); decodeUTF16LE = standardDecodeUTF16LE; } else { diff --git a/src/vs/editor/common/modes/supports/richEditBrackets.ts b/src/vs/editor/common/modes/supports/richEditBrackets.ts index ae10537c82e..03a1d102dc1 100644 --- a/src/vs/editor/common/modes/supports/richEditBrackets.ts +++ b/src/vs/editor/common/modes/supports/richEditBrackets.ts @@ -4,6 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import * as strings from 'vs/base/common/strings'; +import * as stringBuilder from 'vs/editor/common/core/stringBuilder'; import { Range } from 'vs/editor/common/core/range'; import { LanguageIdentifier } from 'vs/editor/common/modes'; import { CharacterPair } from 'vs/editor/common/modes/languageConfiguration'; @@ -264,14 +265,23 @@ function createBracketOrRegExp(pieces: string[]): RegExp { return strings.createRegExp(regexStr, true); } -let toReversedString = (function () { +const toReversedString = (function () { function reverse(str: string): string { - let reversedStr = ''; - for (let i = str.length - 1; i >= 0; i--) { - reversedStr += str.charAt(i); + if (stringBuilder.hasTextDecoder) { + // create a Uint16Array and then use a TextDecoder to create a string + const arr = new Uint16Array(str.length); + for (let i = str.length - 1; i >= 0; i--) { + arr[i] = str.charCodeAt(i); + } + return stringBuilder.getPlatformTextDecoder().decode(arr); + } else { + let result: string[] = [], resultLen = 0; + for (let i = str.length - 1; i >= 0; i--) { + result[resultLen++] = str.charAt(i); + } + return result.join(''); } - return reversedStr; } let lastInput: string | null = null; -- GitLab