Skip to content

Commit

Permalink
Improve worst case regex performance (#51)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
adam-arthur and sindresorhus committed Oct 28, 2023
1 parent 1150902 commit d989bc4
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ const wrapWord = (rows, word, columns) => {

if (ESCAPES.has(character)) {
isInsideEscape = true;
isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK);

const ansiEscapeLinkCandidate = characters.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join('');
isInsideLinkEscape = ansiEscapeLinkCandidate === ANSI_ESCAPE_LINK;
}

if (isInsideEscape) {
Expand Down Expand Up @@ -164,13 +166,17 @@ const exec = (string, columns, options = {}) => {
rows = rows.map(row => stringVisibleTrimSpacesRight(row));
}

const pre = [...rows.join('\n')];
const preString = rows.join('\n');
const pre = [...preString];

// We need to keep a separate index as `String#slice()` works on Unicode code units, while `pre` is an array of codepoints.
let preStringIndex = 0;

for (const [index, character] of pre.entries()) {
returnValue += character;

if (ESCAPES.has(character)) {
const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(pre.slice(index).join('')) || {groups: {}};
const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(preString.slice(preStringIndex)) || {groups: {}};
if (groups.code !== undefined) {
const code = Number.parseFloat(groups.code);
escapeCode = code === END_CODE ? undefined : code;
Expand Down Expand Up @@ -198,6 +204,8 @@ const exec = (string, columns, options = {}) => {
returnValue += wrapAnsiHyperlink(escapeUrl);
}
}

preStringIndex += character.length;
}

return returnValue;
Expand Down

0 comments on commit d989bc4

Please sign in to comment.