lib: add primordials.SafeStringIterator
PR-URL: https://github.com/nodejs/node/pull/36526 Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
40fc395d7d
commit
bd6f2303f4
@ -242,6 +242,9 @@ primordials.SafeWeakSet = makeSafe(
|
|||||||
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
|
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
|
||||||
[
|
[
|
||||||
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
|
{ name: 'TypedArray', original: Reflect.getPrototypeOf(Uint8Array) },
|
||||||
|
{ name: 'StringIterator', original: {
|
||||||
|
prototype: Reflect.getPrototypeOf(String.prototype[Symbol.iterator]()),
|
||||||
|
} },
|
||||||
].forEach(({ name, original }) => {
|
].forEach(({ name, original }) => {
|
||||||
primordials[name] = original;
|
primordials[name] = original;
|
||||||
// The static %TypedArray% methods require a valid `this`, but can't be bound,
|
// The static %TypedArray% methods require a valid `this`, but can't be bound,
|
||||||
@ -250,5 +253,10 @@ primordials.SafeWeakSet = makeSafe(
|
|||||||
copyPrototype(original.prototype, primordials, `${name}Prototype`);
|
copyPrototype(original.prototype, primordials, `${name}Prototype`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
primordials.SafeStringIterator = createSafeIterator(
|
||||||
|
primordials.StringPrototypeSymbolIterator,
|
||||||
|
primordials.StringIteratorPrototypeNext
|
||||||
|
);
|
||||||
|
|
||||||
Object.setPrototypeOf(primordials, null);
|
Object.setPrototypeOf(primordials, null);
|
||||||
Object.freeze(primordials);
|
Object.freeze(primordials);
|
||||||
|
@ -9,6 +9,7 @@ const {
|
|||||||
MathMin,
|
MathMin,
|
||||||
RegExpPrototypeTest,
|
RegExpPrototypeTest,
|
||||||
SafeSet,
|
SafeSet,
|
||||||
|
SafeStringIterator,
|
||||||
StringPrototypeEndsWith,
|
StringPrototypeEndsWith,
|
||||||
StringPrototypeIndexOf,
|
StringPrototypeIndexOf,
|
||||||
StringPrototypeLastIndexOf,
|
StringPrototypeLastIndexOf,
|
||||||
@ -425,7 +426,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
|
|||||||
getStringWidth(inspected) > maxColumns) {
|
getStringWidth(inspected) > maxColumns) {
|
||||||
maxColumns -= 4 + (repl.useColors ? 0 : 3);
|
maxColumns -= 4 + (repl.useColors ? 0 : 3);
|
||||||
let res = '';
|
let res = '';
|
||||||
for (const char of inspected) {
|
for (const char of new SafeStringIterator(inspected)) {
|
||||||
maxColumns -= getStringWidth(char);
|
maxColumns -= getStringWidth(char);
|
||||||
if (maxColumns < 0)
|
if (maxColumns < 0)
|
||||||
break;
|
break;
|
||||||
|
@ -9,6 +9,7 @@ const {
|
|||||||
StringPrototypeSlice,
|
StringPrototypeSlice,
|
||||||
StringPrototypeSplit,
|
StringPrototypeSplit,
|
||||||
StringPrototypeStartsWith,
|
StringPrototypeStartsWith,
|
||||||
|
SafeStringIterator,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
|
let debug = require('internal/util/debuglog').debuglog('source_map', (fn) => {
|
||||||
@ -144,7 +145,8 @@ function getErrorSource(
|
|||||||
// Display ^ in appropriate position, regardless of whether tabs or
|
// Display ^ in appropriate position, regardless of whether tabs or
|
||||||
// spaces are used:
|
// spaces are used:
|
||||||
let prefix = '';
|
let prefix = '';
|
||||||
for (const character of StringPrototypeSlice(line, 0, originalColumn + 1)) {
|
for (const character of new SafeStringIterator(
|
||||||
|
StringPrototypeSlice(line, 0, originalColumn + 1))) {
|
||||||
prefix += (character === '\t') ? '\t' :
|
prefix += (character === '\t') ? '\t' :
|
||||||
StringPrototypeRepeat(' ', getStringWidth(character));
|
StringPrototypeRepeat(' ', getStringWidth(character));
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,7 @@ const {
|
|||||||
ReflectApply,
|
ReflectApply,
|
||||||
RegExp,
|
RegExp,
|
||||||
RegExpPrototypeToString,
|
RegExpPrototypeToString,
|
||||||
|
SafeStringIterator,
|
||||||
Set,
|
Set,
|
||||||
SetPrototypeGetSize,
|
SetPrototypeGetSize,
|
||||||
SetPrototypeValues,
|
SetPrototypeValues,
|
||||||
@ -2005,7 +2006,7 @@ if (internalBinding('config').hasIntl) {
|
|||||||
if (removeControlChars)
|
if (removeControlChars)
|
||||||
str = stripVTControlCharacters(str);
|
str = stripVTControlCharacters(str);
|
||||||
str = str.normalize('NFC');
|
str = str.normalize('NFC');
|
||||||
for (const char of str) {
|
for (const char of new SafeStringIterator(str)) {
|
||||||
const code = char.codePointAt(0);
|
const code = char.codePointAt(0);
|
||||||
if (isFullWidthCodePoint(code)) {
|
if (isFullWidthCodePoint(code)) {
|
||||||
width += 2;
|
width += 2;
|
||||||
|
@ -59,6 +59,7 @@ const {
|
|||||||
StringPrototypeTrim,
|
StringPrototypeTrim,
|
||||||
Symbol,
|
Symbol,
|
||||||
SymbolAsyncIterator,
|
SymbolAsyncIterator,
|
||||||
|
SafeStringIterator,
|
||||||
} = primordials;
|
} = primordials;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@ -760,7 +761,7 @@ Interface.prototype._getDisplayPos = function(str) {
|
|||||||
const col = this.columns;
|
const col = this.columns;
|
||||||
let rows = 0;
|
let rows = 0;
|
||||||
str = stripVTControlCharacters(str);
|
str = stripVTControlCharacters(str);
|
||||||
for (const char of str) {
|
for (const char of new SafeStringIterator(str)) {
|
||||||
if (char === '\n') {
|
if (char === '\n') {
|
||||||
// Rows must be incremented by 1 even if offset = 0 or col = +Infinity.
|
// Rows must be incremented by 1 even if offset = 0 or col = +Infinity.
|
||||||
rows += MathCeil(offset / col) || 1;
|
rows += MathCeil(offset / col) || 1;
|
||||||
@ -1181,7 +1182,7 @@ function emitKeypressEvents(stream, iface = {}) {
|
|||||||
iface.isCompletionEnabled = false;
|
iface.isCompletionEnabled = false;
|
||||||
|
|
||||||
let length = 0;
|
let length = 0;
|
||||||
for (const character of string) {
|
for (const character of new SafeStringIterator(string)) {
|
||||||
length += character.length;
|
length += character.length;
|
||||||
if (length === string.length) {
|
if (length === string.length) {
|
||||||
iface.isCompletionEnabled = true;
|
iface.isCompletionEnabled = true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user