diff --git a/PKHeX.Core/Legality/Restrictions/WordFilter.cs b/PKHeX.Core/Legality/Restrictions/WordFilter.cs index 952960807..3428b82c6 100644 --- a/PKHeX.Core/Legality/Restrictions/WordFilter.cs +++ b/PKHeX.Core/Legality/Restrictions/WordFilter.cs @@ -9,9 +9,21 @@ namespace PKHeX.Core public static class WordFilter { /// - /// Source pattern regexes to check with + /// Regex patterns to check against /// - private static readonly string[] Patterns = Util.GetStringList("badwords"); + /// No need to keep the original pattern strings around; the object retrieves this via + private static readonly Regex[] Regexes = LoadPatterns(Util.GetStringList("badwords")); + + // if you're running this as a server and don't mind a few extra seconds of startup, add RegexOptions.Compiled for slightly better checking. + private const RegexOptions Options = RegexOptions.CultureInvariant; + + private static Regex[] LoadPatterns(IReadOnlyList patterns) + { + var result = new Regex[patterns.Count]; + for (int i = 0; i < patterns.Count; i++) + result[i] = new Regex(patterns[i], Options); + return result; + } /// /// Due to some messages repeating (Trainer names), keep a list of repeated values for faster lookup. @@ -43,13 +55,13 @@ public static bool IsFiltered(string message, out string regMatch) } // not in dictionary, check patterns - foreach (var pattern in Patterns) + foreach (var regex in Regexes) { - if (!Regex.IsMatch(msg, pattern)) + if (!regex.IsMatch(msg)) continue; // match found, cache result - regMatch = pattern; + regMatch = regex.ToString(); // fetches from regex field lock (dictLock) Lookup[msg] = regMatch; return true;