Skip to content

Case-insensitive regex matching gives culture-dependent results (Turkish "İ/i" breaks IgnoreCase) #102

Description

@matt-edmondson

What's wrong

TextFilter.cs:411-413, inside DoesMatchRegex:

RegexOptions regexOptions = caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
    ? RegexOptions.Compiled | RegexOptions.IgnoreCase
    : RegexOptions.Compiled;

RegexOptions.IgnoreCase without RegexOptions.CultureInvariant folds case using the current thread culture, not an invariant/ordinal fold. Under a Turkish culture (tr-TR), uppercase I and lowercase i are not case-equivalent (the dotted/dotless I problem: i folds to İ, not I).

Concrete failure scenario

CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
new Regex("i", RegexOptions.IgnoreCase).IsMatch("I");   // False
new Regex("I", RegexOptions.IgnoreCase).IsMatch("i");   // False
// same pattern with RegexOptions.CultureInvariant added -> True

So TextFilter.DoesMatchRegex("IMG_1234.JPG", "img", opts, TextFilterCaseSensitivity.CaseInsensitive) returns a different answer depending on the calling thread's CurrentCulture — a silent, hard-to-reproduce bug for any app whose UI thread runs under a Turkish locale (not an exotic case). This is a live gap in newly-added code: the case-insensitive matching feature was added in commit 7b59874 (closing #97), and neither the README's "Case Sensitivity" section nor the tests around TextFilterTests.cs:582-625 mention or test culture, so this isn't a documented/intentional limitation.

The glob path (DotNet.Glob's CaseInsensitive option, used in ResolveGlob) does not have this problem — it matches correctly regardless of culture. The bug is isolated to the regex branch.

Suggested fix

Add RegexOptions.CultureInvariant alongside RegexOptions.IgnoreCase at line 412:

RegexOptions regexOptions = caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
    ? RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant
    : RegexOptions.Compiled;

Add a regression test that sets CurrentCulture to tr-TR (or uses CultureInfo.InvariantCulture vs tr-TR explicitly) and asserts img matches IMG_1234.JPG case-insensitively regardless of culture.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions