Skip to content

Commit

Permalink
readline: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36296
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 authored and targos committed May 16, 2021
1 parent d43e1ae commit efbec9d
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 57 deletions.
37 changes: 25 additions & 12 deletions lib/internal/readline/utils.js
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit efbec9d

Please sign in to comment.