Skip to content

Commit f0506c3

Browse files
BridgeARMylesBorins
authored andcommitted
readline: move charLengthLeft() and charLengthAt()
This moves the charLengthLeft() and charLengthAt() into the internal readline file. This allows sharing the functions internally with other code. PR-URL: #31112 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 7ba21d0 commit f0506c3

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

lib/internal/readline/utils.js

+25
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ CSI.kClearToLineEnd = CSI`0K`;
3636
CSI.kClearLine = CSI`2K`;
3737
CSI.kClearScreenDown = CSI`0J`;
3838

39+
// TODO(BridgeAR): Treat combined characters as single character, i.e,
40+
// 'a\u0301' and '\u0301a' (both have the same visual output).
41+
// Check Canonical_Combining_Class in
42+
// http://userguide.icu-project.org/strings/properties
43+
function charLengthLeft(str, i) {
44+
if (i <= 0)
45+
return 0;
46+
if ((i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold) ||
47+
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
48+
return 2;
49+
}
50+
return 1;
51+
}
52+
53+
function charLengthAt(str, i) {
54+
if (str.length <= i) {
55+
// Pretend to move to the right. This is necessary to autocomplete while
56+
// moving to the right.
57+
return 1;
58+
}
59+
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
60+
}
61+
3962
if (internalBinding('config').hasIntl) {
4063
const icu = internalBinding('icu');
4164
// icu.getStringWidth(string, ambiguousAsFullWidth, expandEmojiSequence)
@@ -452,6 +475,8 @@ function commonPrefix(strings) {
452475
}
453476

454477
module.exports = {
478+
charLengthAt,
479+
charLengthLeft,
455480
commonPrefix,
456481
emitKeys,
457482
getStringWidth,

lib/readline.js

+2-19
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ const { validateString } = require('internal/validators');
4949
const { inspect } = require('internal/util/inspect');
5050
const EventEmitter = require('events');
5151
const {
52+
charLengthAt,
53+
charLengthLeft,
5254
commonPrefix,
5355
CSI,
5456
emitKeys,
@@ -591,25 +593,6 @@ Interface.prototype._wordRight = function() {
591593
}
592594
};
593595

594-
function charLengthLeft(str, i) {
595-
if (i <= 0)
596-
return 0;
597-
if ((i > 1 && str.codePointAt(i - 2) >= kUTF16SurrogateThreshold) ||
598-
str.codePointAt(i - 1) >= kUTF16SurrogateThreshold) {
599-
return 2;
600-
}
601-
return 1;
602-
}
603-
604-
function charLengthAt(str, i) {
605-
if (str.length <= i) {
606-
// Pretend to move to the right. This is necessary to autocomplete while
607-
// moving to the right.
608-
return 1;
609-
}
610-
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
611-
}
612-
613596
Interface.prototype._deleteLeft = function() {
614597
if (this.cursor > 0 && this.line.length > 0) {
615598
// The number of UTF-16 units comprising the character to the left

0 commit comments

Comments
 (0)