Skip to content

Commit e0c5e7a

Browse files
cjihrigtargos
authored andcommitted
readline: use named constant for surrogate checks
This commit defines a named constant instead of using a mix of 2 ** 16 and 0x10000 throughout the code. PR-URL: #28638 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent f688122 commit e0c5e7a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

lib/internal/readline.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
const ansi =
99
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
1010
/* eslint-enable no-control-regex */
11-
11+
const kUTF16SurrogateThreshold = 0x10000; // 2 ** 16
1212
const kEscape = '\x1b';
1313

1414
let getStringWidth;
@@ -63,7 +63,7 @@ if (internalBinding('config').hasIntl) {
6363
for (var i = 0; i < str.length; i++) {
6464
const code = str.codePointAt(i);
6565

66-
if (code >= 0x10000) { // surrogates
66+
if (code >= kUTF16SurrogateThreshold) { // Surrogates.
6767
i++;
6868
}
6969

@@ -434,6 +434,7 @@ module.exports = {
434434
emitKeys,
435435
getStringWidth,
436436
isFullWidthCodePoint,
437+
kUTF16SurrogateThreshold,
437438
stripVTControlCharacters,
438439
CSI
439440
};

lib/readline.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const {
4141
emitKeys,
4242
getStringWidth,
4343
isFullWidthCodePoint,
44+
kUTF16SurrogateThreshold,
4445
stripVTControlCharacters
4546
} = require('internal/readline');
4647

@@ -600,8 +601,8 @@ Interface.prototype._wordRight = function() {
600601
function charLengthLeft(str, i) {
601602
if (i <= 0)
602603
return 0;
603-
if (i > 1 && str.codePointAt(i - 2) >= 2 ** 16 ||
604-
str.codePointAt(i - 1) >= 2 ** 16) {
604+
if (i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold ||
605+
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
605606
return 2;
606607
}
607608
return 1;
@@ -610,7 +611,7 @@ function charLengthLeft(str, i) {
610611
function charLengthAt(str, i) {
611612
if (str.length <= i)
612613
return 0;
613-
return str.codePointAt(i) >= 2 ** 16 ? 2 : 1;
614+
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
614615
}
615616

616617
Interface.prototype._deleteLeft = function() {
@@ -728,7 +729,7 @@ Interface.prototype._getDisplayPos = function(str) {
728729
str = stripVTControlCharacters(str);
729730
for (var i = 0, len = str.length; i < len; i++) {
730731
code = str.codePointAt(i);
731-
if (code >= 0x10000) { // surrogates
732+
if (code >= kUTF16SurrogateThreshold) { // Surrogates.
732733
i++;
733734
}
734735
if (code === 0x0a) { // new line \n

0 commit comments

Comments
 (0)