Skip to content

Commit 9ff7ccb

Browse files
committed
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.
1 parent f763ff0 commit 9ff7ccb

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

lib/internal/readline/interface.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
getStringWidth,
1010
isFullWidthCodePoint,
1111
kEscapeCodeTimeout,
12+
kUTF16SurrogateThreshold,
1213
moveCursor,
1314
stripVTControlCharacters
1415
} = require('internal/readline/utils');
@@ -1051,8 +1052,8 @@ function charLengthLeft(str, i) {
10511052
if (i <= 0)
10521053
return 0;
10531054

1054-
if (i > 1 && str.codePointAt(i - 2) >= 2 ** 16 ||
1055-
str.codePointAt(i - 1) >= 2 ** 16) {
1055+
if (i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold ||
1056+
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
10561057
return 2;
10571058
}
10581059

@@ -1063,7 +1064,7 @@ function charLengthLeft(str, i) {
10631064
function charLengthAt(str, i) {
10641065
if (str.length <= i)
10651066
return 0;
1066-
return str.codePointAt(i) >= 2 ** 16 ? 2 : 1;
1067+
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
10671068
}
10681069

10691070

lib/internal/readline/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const kEscapeCodeTimeout = 500;
1717

1818
const kKeypressDecoder = Symbol('keypress-decoder');
1919
const kEscapeDecoder = Symbol('escape-decoder');
20+
const kUTF16SurrogateThreshold = 0x10000; // 2 ** 16
2021
const kEscape = '\x1b';
2122

2223
let StringDecoder; // Lazy loaded.
@@ -72,7 +73,7 @@ if (internalBinding('config').hasIntl) {
7273
for (var i = 0; i < str.length; i++) {
7374
const code = str.codePointAt(i);
7475

75-
if (code >= 0x10000) { // surrogates
76+
if (code >= kUTF16SurrogateThreshold) { // Surrogates.
7677
i++;
7778
}
7879

@@ -580,6 +581,7 @@ module.exports = {
580581
getStringWidth,
581582
isFullWidthCodePoint,
582583
kEscapeCodeTimeout,
584+
kUTF16SurrogateThreshold,
583585
moveCursor,
584586
stripVTControlCharacters,
585587
CSI // CSI is only exported for testing purposes.

0 commit comments

Comments
 (0)