Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readline: refactor to use more primordials #36296

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 25 additions & 12 deletions lib/internal/readline/utils.js
@@ -1,7 +1,15 @@
'use strict';

const {
ArrayPrototypeSlice,
ArrayPrototypeSort,
RegExpPrototypeTest,
StringFromCharCode,
StringPrototypeCharCodeAt,
StringPrototypeCodePointAt,
StringPrototypeMatch,
StringPrototypeSlice,
StringPrototypeToLowerCase,
Symbol,
} = primordials;

Expand Down Expand Up @@ -32,8 +40,9 @@ CSI.kClearScreenDown = CSI`0J`;
function charLengthLeft(str, i) {
if (i <= 0)
return 0;
if ((i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold) ||
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
if ((i > 1 &&
StringPrototypeCodePointAt(str, i - 2) >= kUTF16SurrogateThreshold) ||
StringPrototypeCodePointAt(str, i - 1) >= kUTF16SurrogateThreshold) {
return 2;
}
return 1;
Expand All @@ -45,7 +54,7 @@ function charLengthAt(str, i) {
// moving to the right.
return 1;
}
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
return StringPrototypeCodePointAt(str, i) >= kUTF16SurrogateThreshold ? 2 : 1;
}

/*
Expand Down Expand Up @@ -178,13 +187,15 @@ function* emitKeys(stream) {
* We buffered enough data, now trying to extract code
* and modifier from it
*/
const cmd = s.slice(cmdStart);
const cmd = StringPrototypeSlice(s, cmdStart);
let match;

if ((match = cmd.match(/^(\d\d?)(;(\d))?([~^$])$/))) {
if ((match = StringPrototypeMatch(cmd, /^(\d\d?)(;(\d))?([~^$])$/))) {
code += match[1] + match[4];
modifier = (match[3] || 1) - 1;
} else if ((match = cmd.match(/^((\d;)?(\d))?([A-Za-z])$/))) {
} else if (
(match = StringPrototypeMatch(cmd, /^((\d;)?(\d))?([A-Za-z])$/))
) {
code += match[4];
modifier = (match[3] || 1) - 1;
} else {
Expand Down Expand Up @@ -325,12 +336,14 @@ function* emitKeys(stream) {
key.meta = escaped;
} else if (!escaped && ch <= '\x1a') {
// ctrl+letter
key.name = StringFromCharCode(ch.charCodeAt(0) + 'a'.charCodeAt(0) - 1);
key.name = StringFromCharCode(
StringPrototypeCharCodeAt(ch) + StringPrototypeCharCodeAt('a') - 1
);
key.ctrl = true;
} else if (/^[0-9A-Za-z]$/.test(ch)) {
} else if (RegExpPrototypeTest(/^[0-9A-Za-z]$/, ch)) {
// Letter, number, shift+letter
key.name = ch.toLowerCase();
key.shift = /^[A-Z]$/.test(ch);
key.name = StringPrototypeToLowerCase(ch);
key.shift = RegExpPrototypeTest(/^[A-Z]$/, ch);
key.meta = escaped;
} else if (escaped) {
// Escape sequence timeout
Expand All @@ -356,12 +369,12 @@ function commonPrefix(strings) {
if (strings.length === 1) {
return strings[0];
}
const sorted = strings.slice().sort();
const sorted = ArrayPrototypeSort(ArrayPrototypeSlice(strings));
const min = sorted[0];
const max = sorted[sorted.length - 1];
for (let i = 0; i < min.length; i++) {
if (min[i] !== max[i]) {
return min.slice(0, i);
return StringPrototypeSlice(min, 0, i);
}
}
return min;
Expand Down