Skip to content

Commit

Permalink
readline: refactor to avoid unsafe regex primordials
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 authored and guangwong committed Jan 3, 2023
1 parent aefcea4 commit fbcadfe
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const {
NumberIsNaN,
ObjectDefineProperty,
ObjectSetPrototypeOf,
RegExpPrototypeTest,
RegExpPrototypeExec,
RegExpPrototypeSymbolReplace,
RegExpPrototypeSymbolSplit,
StringPrototypeCodePointAt,
StringPrototypeEndsWith,
StringPrototypeMatch,
StringPrototypeRepeat,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
Expand Down Expand Up @@ -642,12 +642,12 @@ Interface.prototype._normalWrite = function(b) {
let string = this._decoder.write(b);
if (this._sawReturnAt &&
DateNow() - this._sawReturnAt <= this.crlfDelay) {
string = StringPrototypeReplace(string, /^\n/, '');
string = RegExpPrototypeSymbolReplace(/^\n/, string, '');
this._sawReturnAt = 0;
}

// Run test() on the new string chunk, not on the entire line buffer.
const newPartContainsEnding = RegExpPrototypeTest(lineEnding, string);
const newPartContainsEnding = RegExpPrototypeExec(lineEnding, string) !== null;

if (this._line_buffer) {
string = this._line_buffer + string;
Expand Down Expand Up @@ -773,7 +773,7 @@ Interface.prototype._wordLeft = function() {
const leading = StringPrototypeSlice(this.line, 0, this.cursor);
const reversed = ArrayPrototypeJoin(
ArrayPrototypeReverse(ArrayFrom(leading)), '');
const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/);
const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed);
this._moveCursor(-match[0].length);
}
};
Expand All @@ -782,7 +782,7 @@ Interface.prototype._wordLeft = function() {
Interface.prototype._wordRight = function() {
if (this.cursor < this.line.length) {
const trailing = StringPrototypeSlice(this.line, this.cursor);
const match = StringPrototypeMatch(trailing, /^(?:\s+|[^\w\s]+|\w+)\s*/);
const match = RegExpPrototypeExec(/^(?:\s+|[^\w\s]+|\w+)\s*/, trailing);
this._moveCursor(match[0].length);
}
};
Expand Down Expand Up @@ -818,7 +818,7 @@ Interface.prototype._deleteWordLeft = function() {
let leading = StringPrototypeSlice(this.line, 0, this.cursor);
const reversed = ArrayPrototypeJoin(
ArrayPrototypeReverse(ArrayFrom(leading)), '');
const match = StringPrototypeMatch(reversed, /^\s*(?:[^\w\s]+|\w+)?/);
const match = RegExpPrototypeExec(/^\s*(?:[^\w\s]+|\w+)?/, reversed);
leading = StringPrototypeSlice(leading, 0,
leading.length - match[0].length);
this.line = leading + StringPrototypeSlice(this.line, this.cursor,
Expand All @@ -832,7 +832,7 @@ Interface.prototype._deleteWordLeft = function() {
Interface.prototype._deleteWordRight = function() {
if (this.cursor < this.line.length) {
const trailing = StringPrototypeSlice(this.line, this.cursor);
const match = StringPrototypeMatch(trailing, /^(?:\s+|\W+|\w+)\s*/);
const match = RegExpPrototypeExec(/^(?:\s+|\W+|\w+)\s*/, trailing);
this.line = StringPrototypeSlice(this.line, 0, this.cursor) +
StringPrototypeSlice(trailing, match[0].length);
this._refreshLine();
Expand Down Expand Up @@ -1272,7 +1272,7 @@ Interface.prototype._ttyWrite = function(s, key) {
// falls through
default:
if (typeof s === 'string' && s) {
const lines = StringPrototypeSplit(s, /\r\n|\n|\r/);
const lines = RegExpPrototypeSymbolSplit(/\r\n|\n|\r/, s);
for (let i = 0, len = lines.length; i < len; i++) {
if (i > 0) {
this._line();
Expand Down

0 comments on commit fbcadfe

Please sign in to comment.