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

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

‎lib/internal/readline/utils.js‎

Lines changed: 25 additions & 0 deletions
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+
functioncharLengthLeft(str,i){
44+
if(i<=0)
45+
return0;
46+
if((i>1&&str.codePointAt(i-2)>=kUTF16SurrogateThreshold)||
47+
str.codePointAt(i-1)>=kUTF16SurrogateThreshold){
48+
return2;
49+
}
50+
return1;
51+
}
52+
53+
functioncharLengthAt(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+
return1;
58+
}
59+
returnstr.codePointAt(i)>=kUTF16SurrogateThreshold ? 2 : 1;
60+
}
61+
3962
if(internalBinding('config').hasIntl){
4063
consticu=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‎

Lines changed: 2 additions & 19 deletions
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
constEventEmitter=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-
functioncharLengthLeft(str,i){
595-
if(i<=0)
596-
return0;
597-
if((i>1&&str.codePointAt(i-2)>=kUTF16SurrogateThreshold)||
598-
str.codePointAt(i-1)>=kUTF16SurrogateThreshold){
599-
return2;
600-
}
601-
return1;
602-
}
603-
604-
functioncharLengthAt(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-
return1;
609-
}
610-
returnstr.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)