diff --git a/src/main/java/dev/khloeleclair/create/additionallogistics/common/SafeRegex.java b/src/main/java/dev/khloeleclair/create/additionallogistics/common/SafeRegex.java index e887cb6..fe10670 100644 --- a/src/main/java/dev/khloeleclair/create/additionallogistics/common/SafeRegex.java +++ b/src/main/java/dev/khloeleclair/create/additionallogistics/common/SafeRegex.java @@ -31,7 +31,11 @@ public class SafeRegex { .build(); - private record CachedGlobResult(@Nullable String regex, @Nullable PatternSyntaxException error) {} + private record CachedGlobResult(@Nullable String regex, @Nullable PatternSyntaxException error) { + public boolean isError() { + return this.error != null; + } + } private record CachedReplacementResult(@Nullable PatternSyntaxException error) { boolean isError() { return this.error != null; } @@ -154,6 +158,21 @@ public static void assertReplacementSafe(@NotNull String regex, @NotNull String throw cached.error; } + @NotNull + private static CachedRegexResult processRegex(@NotNull CachedGlobResult cachedResult) { + CachedRegexResult cached; + + if (cachedResult.isError()) { // return a cached regex result with the error from cachedResult + cached = new CachedRegexResult(cachedResult.error); + } else { + String regex = cachedResult.regex; + + cached = processRegex(regex); + } + + return cached; + } + @NotNull private static CachedRegexResult processRegex(@NotNull String regex) { CachedRegexResult cached = REGEX_CACHE.getIfPresent(regex); @@ -217,7 +236,7 @@ public static boolean isSafe(String regex, int starHeightLimit, int repetitionLi } - private static String cachedToRegexPattern(String address) { + private static CachedGlobResult cachedToRegexPattern(String address) { var cached = GLOB_CACHE.getIfPresent(address); if (cached == null) { try { @@ -228,7 +247,7 @@ private static String cachedToRegexPattern(String address) { GLOB_CACHE.put(address, cached); } - return cached.regex; + return cached; } /// An optimized version of PackageItem.matchAddress that uses caching and optimized logic. @@ -261,4 +280,4 @@ else if (info.pattern != null && info.pattern.matcher(address).matches()) return false; } -} +} \ No newline at end of file