Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,35 @@ public void ARegexCompiledUnderOneCultureAnswersTheSameUnderAnother()
}
}

[TestMethod]
public void AnInlineIgnoreCaseFoldsTheSameWhateverCultureCompiledIt()
{
// Under CaseSensitive the pattern can still ask for a fold itself with (?i), and that fold is
// culture-dependent unless CultureInvariant is set. Compiled under tr-TR first, "(?i)img" would
// then miss "IMG_1234.JPG" for every later caller, because the cache keeps the first compile.
CultureInfo original = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
if (CultureInfo.CurrentCulture.TextInfo.ToUpper("i") == "I")
{
Assert.Inconclusive("This runtime does not apply Turkish case mapping, so the regression cannot be provoked here.");
}

bool underTurkish = TextFilter.IsMatch("IMG_1234.JPG", "(?i)img", TextFilterType.Regex, TextFilterMatchOptions.ByWholeString);

CultureInfo.CurrentCulture = new CultureInfo("en-US");
bool underEnglish = TextFilter.IsMatch("IMG_1234.JPG", "(?i)img", TextFilterType.Regex, TextFilterMatchOptions.ByWholeString);

Assert.IsTrue(underTurkish, "An inline (?i) should fold i and I whatever locale compiled the pattern.");
Assert.IsTrue(underEnglish, "The first caller's culture should not decide the answer for later callers.");
}
finally
{
CultureInfo.CurrentCulture = original;
}
}

[TestMethod]
public void TheTwoSensitivitiesDoNotCollideInTheRegexCache()
{
Expand Down
6 changes: 4 additions & 2 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(1);

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "SYSLIB1045:Convert to 'GeneratedRegexAttribute'.", Justification = "Not available in older frameworks")]
private static Regex RegexMatchAnything() => new(".*", RegexOptions.Compiled);

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 127 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

/// <summary>
/// Gets a hint for the specified filter type.
Expand Down Expand Up @@ -276,7 +276,7 @@

return ExcludedTokenPrefixes.Contains(prefix)
? TextFilterTokenType.Excluded
: RequiredTokenPrefixes.Contains(prefix)

Check warning on line 279 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 279 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 279 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 279 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 279 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 279 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.
? TextFilterTokenType.Required
: TextFilterTokenType.Optional;
})
Expand Down Expand Up @@ -458,10 +458,12 @@
// matches "IMG_1234.JPG" -- and the cache below is keyed by pattern and sensitivity only,
// so whichever culture happened to compile the pattern first decides the answer for every
// later caller on any thread. A filter pattern is machine text, not prose, and the glob
// path already folds invariantly, so the two paths now agree.
// path already folds invariantly, so the two paths now agree. It applies under CaseSensitive
// too, because the pattern itself can ask for a fold with an inline (?i), and that fold is
// just as culture-dependent and just as cached as the one the option asks for.
RegexOptions regexOptions = caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
? RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
: RegexOptions.Compiled;
: RegexOptions.Compiled | RegexOptions.CultureInvariant;

try
{
Expand Down
Loading