perf(automod): also merge regexes in match_links, match_words

This commit is contained in:
Dragory 2021-10-17 08:03:38 +03:00
parent aea6999753
commit 44f5b77cc7
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
5 changed files with 85 additions and 43 deletions

View file

@ -0,0 +1,17 @@
import { categorize } from "./categorize";
const hasBackreference = /(?:^|[^\\]|[\\]{2})\\\d+/;
export function mergeRegexes(sourceRegexes: RegExp[], flags: string): RegExp[] {
const categories = categorize(sourceRegexes, {
hasBackreferences: (regex) => hasBackreference.exec(regex.source) !== null,
safeToMerge: () => true,
});
const regexes: RegExp[] = [];
if (categories.safeToMerge.length) {
const merged = categories.safeToMerge.map((r) => `(?:${r.source})`).join("|");
regexes.push(new RegExp(merged, flags));
}
regexes.push(...categories.hasBackreferences);
return regexes;
}

View file

@ -0,0 +1,6 @@
import escapeStringRegexp from "escape-string-regexp";
export function mergeWordsIntoRegex(words: string[], flags?: string) {
const source = words.map((word) => `(?:${escapeStringRegexp(word)})`).join("|");
return new RegExp(source, flags);
}