Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Feb 20, 2024
1 parent 059c215 commit 1068058
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
22 changes: 5 additions & 17 deletions src/Polyfill/Regex/RegexCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -258,17 +250,14 @@ private static void Add(Key key, Regex regex)
internal readonly struct Key : IEquatable<Key>
{
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;
}
Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/Polyfill/Regex/RegexPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<char> 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
}

Expand All @@ -87,7 +87,7 @@ public static ValueMatchEnumerator EnumerateMatches(ReadOnlySpan<char> 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
}

Expand Down

0 comments on commit 1068058

Please sign in to comment.