未验证 提交 34c2c766 编写于 作者: A Alex Dima

Fixes #93370: Use TextDecoder to create a reverse string

上级 1ec43773
......@@ -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 {
......
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册