Skip to content

Commit

Permalink
More efficient regex replace (#2411)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Dec 15, 2023
1 parent 2b4f865 commit f6c63b9
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/common/types/chainner-builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,23 @@ const regexReplaceImpl = (

// rregex currently only supports byte offsets in matches. So we have to
// match spans on UTF8 and then convert it back to Unicode.
const utf8 = Buffer.from(text, 'utf8');
const toUTF16 = (offset: number) => {
return utf8.toString('utf8', 0, offset).length;
};
const utf8 = new TextEncoder().encode(text);
const decoder = new TextDecoder();

let result = '';
let lastIndex = 0;
let lastByteIndex = 0;
for (const match of matches) {
const full = match.get[0];
result += text.slice(lastIndex, toUTF16(full.start));
result += decoder.decode(utf8.slice(lastByteIndex, full.start));

const replacements = new Map<string, string>();
match.get.forEach((m, i) => replacements.set(String(i), m.value));
Object.entries(match.name).forEach(([name, m]) => replacements.set(name, m.value));
result += replacement.replace(replacements);

lastIndex = toUTF16(full.end);
lastByteIndex = full.end;
}
result += text.slice(lastIndex);
result += decoder.decode(utf8.slice(lastByteIndex));

return result;
};
Expand Down

0 comments on commit f6c63b9

Please sign in to comment.