From 204f45b629594664a15806b18f37874a8cbfc84f Mon Sep 17 00:00:00 2001 From: SillyStar-Github <90102598+SillyStar-Github@users.noreply.github.com> Date: Wed, 15 Oct 2025 03:08:27 -0400 Subject: [PATCH] hopeful regex performance improvement? --- BepInEx/AssetStore/StringStore.cs | 15 ++++++++++----- MelonLoader/AssetStore/StringStore.cs | 9 +++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/BepInEx/AssetStore/StringStore.cs b/BepInEx/AssetStore/StringStore.cs index a24d6fc..5be9b0b 100644 --- a/BepInEx/AssetStore/StringStore.cs +++ b/BepInEx/AssetStore/StringStore.cs @@ -147,11 +147,11 @@ namespace PvZ_Fusion_Translator__BepInEx_.AssetStore // Regex-based dynamic translation foreach (var entry in translationStringRegex) { - var regex = new Regex(entry.Key); - if (regex.IsMatch(originalText)) + if (TestRegex(originalText, entry.Key)) { - // Extract dynamic parts from the original text - var match = regex.Match(originalText); + // Extract dynamic parts from the original text + var regex = new Regex(entry.Key); + var match = regex.Match(originalText); int groupCount = match.Groups.Count; if (isLog) @@ -180,7 +180,12 @@ namespace PvZ_Fusion_Translator__BepInEx_.AssetStore return originalText; } - private static TextAlignmentOptions TextAnchorToTMPAlignment(TextAnchor anchor) + private static bool TestRegex(string originalText, string pattern) + { + return Regex.IsMatch(originalText, pattern); + } + + private static TextAlignmentOptions TextAnchorToTMPAlignment(TextAnchor anchor) { return anchor switch { diff --git a/MelonLoader/AssetStore/StringStore.cs b/MelonLoader/AssetStore/StringStore.cs index c22f781..ce75493 100644 --- a/MelonLoader/AssetStore/StringStore.cs +++ b/MelonLoader/AssetStore/StringStore.cs @@ -145,10 +145,10 @@ namespace PvZ_Fusion_Translator.AssetStore // Regex-based dynamic translation foreach (var entry in translationStringRegex) { - var regex = new Regex(entry.Key); - if (regex.IsMatch(originalText)) + if (TestRegex(originalText, entry.Key)) { // Extract dynamic parts from the original text + var regex = new Regex(entry.Key); var match = regex.Match(originalText); int groupCount = match.Groups.Count; @@ -178,6 +178,11 @@ namespace PvZ_Fusion_Translator.AssetStore return originalText; } + private static bool TestRegex(string originalText, string pattern) + { + return Regex.IsMatch(originalText, pattern); + } + private static TextAlignmentOptions TextAnchorToTMPAlignment(TextAnchor anchor) { return anchor switch