Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
readline: use built in codePointAt
  • Loading branch information
Avi-D-coder committed Feb 6, 2019
commit 756ebea867bc551f61d0d35bff2827881625a426
45 changes: 16 additions & 29 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,35 +596,26 @@ Interface.prototype._wordRight = function() {
}
};

function codePointLeftOf(i, str) {
// obtain the code point to the left
let previousChar;
let len = 0;
for (const char of str) {
len += char.length;
previousChar = char;
if (len === i) {
return previousChar;
}
function charLengthLeft(str, i) {
if (i <= 0)
return 0;
if (i > 1 && str.codePointAt(i - 2) > 2 ** 16 ||
str.codePointAt(i - 1) > 2 ** 16) {
Comment thread
Avi-D-coder marked this conversation as resolved.
Outdated
return 2;
}
return 1;
}
Comment thread
Avi-D-coder marked this conversation as resolved.

function codePointRightOf(i, str) {
// obtain the code point to the right
let len = 0;
for (const char of str) {
len += char.length;
if (len > i) {
// UTF-16 units comprising the character
return char;
}
}
function charLengthAt(str, i) {
if (str.length <= i)
return 0;
return str.codePointAt(i) > 2 ** 16 ? 2 : 1;
}
Comment thread
Avi-D-coder marked this conversation as resolved.

Interface.prototype._deleteLeft = function() {
if (this.cursor > 0 && this.line.length > 0) {
// The number of UTF-16 units comprising the character to the left
const charSize = codePointLeftOf(this.cursor, this.line).length;
const charSize = charLengthLeft(this.line, this.cursor);
this.line = this.line.slice(0, this.cursor - charSize) +
this.line.slice(this.cursor, this.line.length);

Expand All @@ -637,7 +628,7 @@ Interface.prototype._deleteLeft = function() {
Interface.prototype._deleteRight = function() {
if (this.cursor < this.line.length) {
// The number of UTF-16 units comprising the character to the left
const charSize = codePointRightOf(this.cursor, this.line).length;
const charSize = charLengthAt(this.line, this.cursor);
this.line = this.line.slice(0, this.cursor) +
this.line.slice(this.cursor + charSize, this.line.length);
this._refreshLine();
Expand Down Expand Up @@ -993,16 +984,12 @@ Interface.prototype._ttyWrite = function(s, key) {
break;

case 'left':
if (this.cursor > 0) {
// obtain the code point to the left
this._moveCursor(-codePointLeftOf(this.cursor, this.line).length);
}
// obtain the code point to the left
this._moveCursor(-charLengthLeft(this.line, this.cursor));
break;

case 'right':
if (this.cursor < this.line.length) {
this._moveCursor(+codePointRightOf(this.cursor, this.line).length);
}
this._moveCursor(+charLengthAt(this.line, this.cursor));
break;

case 'home':
Expand Down