From 9ff7ccb798a096b139313023afb400495ae6ed23 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sun, 23 Jun 2019 17:19:51 -0400 Subject: [PATCH] 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. --- lib/internal/readline/interface.js | 7 ++++--- lib/internal/readline/utils.js | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 83679817549433..0a0197dff70546 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -9,6 +9,7 @@ const { getStringWidth, isFullWidthCodePoint, kEscapeCodeTimeout, + kUTF16SurrogateThreshold, moveCursor, stripVTControlCharacters } = require('internal/readline/utils'); @@ -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; } @@ -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; } diff --git a/lib/internal/readline/utils.js b/lib/internal/readline/utils.js index e4f9e83d499c00..5af2ea47ccd78a 100644 --- a/lib/internal/readline/utils.js +++ b/lib/internal/readline/utils.js @@ -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. @@ -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++; } @@ -580,6 +581,7 @@ module.exports = { getStringWidth, isFullWidthCodePoint, kEscapeCodeTimeout, + kUTF16SurrogateThreshold, moveCursor, stripVTControlCharacters, CSI // CSI is only exported for testing purposes.