From 10680584918550b3eb6bdc8728ebad42ad5385cd Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 20 Feb 2024 22:16:07 +1100 Subject: [PATCH] . --- src/Polyfill/Regex/RegexCache.cs | 22 +++++----------------- src/Polyfill/Regex/RegexPolyfill.cs | 4 ++-- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/Polyfill/Regex/RegexCache.cs b/src/Polyfill/Regex/RegexCache.cs index 33fa1589..e7597238 100644 --- a/src/Polyfill/Regex/RegexCache.cs +++ b/src/Polyfill/Regex/RegexCache.cs @@ -104,15 +104,12 @@ public static Regex GetOrAdd(string pattern) // a statically-reachable path to the 'new Regex(..., RegexOptions, ...)', which // will force the Regex compiler to be reachable and thus rooted for trimming. - Regex.ValidatePattern(pattern); - - CultureInfo culture = CultureInfo.CurrentCulture; - Key key = new Key(pattern, culture.ToString(), RegexOptions.None, Regex.s_defaultMatchTimeout); + Key key = new Key(pattern, RegexOptions.None, Regex.InfiniteMatchTimeout); Regex? regex = Get(key); if (regex is null) { - regex = new Regex(pattern, culture); + regex = new Regex(pattern); Add(key, regex); } @@ -121,17 +118,12 @@ public static Regex GetOrAdd(string pattern) public static Regex GetOrAdd(string pattern, RegexOptions options, TimeSpan matchTimeout) { - Regex.ValidatePattern(pattern); - Regex.ValidateOptions(options); - Regex.ValidateMatchTimeout(matchTimeout); - - CultureInfo culture = RegexParser.GetTargetCulture(options); - Key key = new Key(pattern, culture.ToString(), options, matchTimeout); + Key key = new Key(pattern, options, matchTimeout); Regex? regex = Get(key); if (regex is null) { - regex = new Regex(pattern, options, matchTimeout, culture); + regex = new Regex(pattern, options, matchTimeout); Add(key, regex); } @@ -258,17 +250,14 @@ private static void Add(Key key, Regex regex) internal readonly struct Key : IEquatable { private readonly string _pattern; - private readonly string _culture; private readonly RegexOptions _options; private readonly TimeSpan _matchTimeout; - public Key(string pattern, string culture, RegexOptions options, TimeSpan matchTimeout) + public Key(string pattern, RegexOptions options, TimeSpan matchTimeout) { Debug.Assert(pattern != null, "Pattern must be provided"); - Debug.Assert(culture != null, "Culture must be provided"); _pattern = pattern; - _culture = culture; _options = options; _matchTimeout = matchTimeout; } @@ -278,7 +267,6 @@ public override bool Equals([NotNullWhen(true)] object? obj) => public bool Equals(Key other) => _pattern.Equals(other._pattern) && - _culture.Equals(other._culture) && _options == other._options && _matchTimeout == other._matchTimeout; diff --git a/src/Polyfill/Regex/RegexPolyfill.cs b/src/Polyfill/Regex/RegexPolyfill.cs index e2e92e7d..0c8d9285 100644 --- a/src/Polyfill/Regex/RegexPolyfill.cs +++ b/src/Polyfill/Regex/RegexPolyfill.cs @@ -73,7 +73,7 @@ public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan input, st #if NET7_0_OR_GREATER return Regex.EnumerateMatches(input, pattern); #else - return new Regex(pattern).EnumerateMatches(input); + return RegexCache.GetOrAdd(pattern).EnumerateMatches(input); #endif } @@ -87,7 +87,7 @@ public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan input, st #if NET7_0_OR_GREATER return Regex.EnumerateMatches(input, pattern, options, timeout); #else - return new Regex(pattern, options, timeout).EnumerateMatches(input); + return RegexCache.GetOrAdd(pattern, options, timeout).EnumerateMatches(input); #endif }