perf(automod): also merge regexes in match_links, match_words
This commit is contained in:
parent
aea6999753
commit
44f5b77cc7
5 changed files with 85 additions and 43 deletions
17
backend/src/utils/mergeRegexes.ts
Normal file
17
backend/src/utils/mergeRegexes.ts
Normal 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;
|
||||
}
|
6
backend/src/utils/mergeWordsIntoRegex.ts
Normal file
6
backend/src/utils/mergeWordsIntoRegex.ts
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue