Skip to content

Commit

Permalink
readline: use named constant for surrogate checks
Browse files Browse the repository at this point in the history
This commit defines a named constant instead of using a mix of
2 ** 16 and 0X10000 throughout the code.
  • Loading branch information
cjihrig committed Jul 10, 2019
1 parent f763ff0 commit 9ff7ccb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
getStringWidth,
isFullWidthCodePoint,
kEscapeCodeTimeout,
kUTF16SurrogateThreshold,
moveCursor,
stripVTControlCharacters
} = require('internal/readline/utils');
Expand Down Expand Up @@ -1051,8 +1052,8 @@ function charLengthLeft(str, i) {
if (i <= 0)
return 0;

if (i > 1 && str.codePointAt(i - 2) >= 2 ** 16 ||
str.codePointAt(i - 1) >= 2 ** 16) {
if (i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold ||
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
return 2;
}

Expand All @@ -1063,7 +1064,7 @@ function charLengthLeft(str, i) {
function charLengthAt(str, i) {
if (str.length <= i)
return 0;
return str.codePointAt(i) >= 2 ** 16 ? 2 : 1;
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
}


Expand Down
4 changes: 3 additions & 1 deletion lib/internal/readline/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const kEscapeCodeTimeout = 500;

const kKeypressDecoder = Symbol('keypress-decoder');
const kEscapeDecoder = Symbol('escape-decoder');
const kUTF16SurrogateThreshold = 0x10000; // 2 ** 16
const kEscape = '\x1b';

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

if (code >= 0x10000) { // surrogates
if (code >= kUTF16SurrogateThreshold) { // Surrogates.
i++;
}

Expand Down Expand Up @@ -580,6 +581,7 @@ module.exports = {
getStringWidth,
isFullWidthCodePoint,
kEscapeCodeTimeout,
kUTF16SurrogateThreshold,
moveCursor,
stripVTControlCharacters,
CSI // CSI is only exported for testing purposes.
Expand Down

0 comments on commit 9ff7ccb

Please sign in to comment.