Skip to content
Open
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
33 changes: 33 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,39 @@ static int ScoreOf(string s)
Assert.IsNotEmpty(result);
}

[TestMethod]
[DataRow("foo bar", "foo bar")]
[DataRow("my file.txt", "my file.txt")]
[DataRow("my file.txt", " my file.txt ")]
[DataRow("my file.txt", "my *.txt")]
public void GlobByWholeStringMatchesTextContainingASpace(string text, string filter)
{
Assert.IsTrue(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
}

[TestMethod]
public void GlobByWholeStringWithASpaceStillNeedsTheWholeTextToMatch()
{
Assert.IsFalse(TextFilter.IsMatch("foo bar baz", "foo bar", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsFalse(TextFilter.IsMatch("foo bar", "foo bar", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsFalse(TextFilter.IsMatch("Foo bar", "foo bar", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsTrue(TextFilter.IsMatch("Foo Bar", "foo bar", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString, TextFilterCaseSensitivity.CaseInsensitive));
}

[TestMethod]
public void GlobByWholeStringKeepsMultiplePatternsAndPrefixedTokens()
{
// #113: each pattern is still tried on its own.
Assert.IsTrue(TextFilter.IsMatch("a.png", "*.jpg *.png", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsFalse(TextFilter.IsMatch("a.gif", "*.jpg *.png", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));

// A filter with an excluded or required token is not tried whole, so those tokens behave as before.
Assert.IsFalse(TextFilter.IsMatch("a.png", "*.png -a.*", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsTrue(TextFilter.IsMatch("b.png", "*.png -a.*", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsTrue(TextFilter.IsMatch("a.png", "*.png +a.*", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsFalse(TextFilter.IsMatch("b.png", "*.png +a.*", TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
}

// Assemblies compiled against 1.6 or earlier bind to these exact signatures, so each must still
// exist in the binary even though source callers now resolve to the case-sensitivity overloads.
[TestMethod]
Expand Down
14 changes: 14 additions & 0 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@

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

Check warning on line 306 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 306 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.
? TextFilterTokenType.Required
: TextFilterTokenType.Optional;
})
Expand Down Expand Up @@ -334,7 +334,7 @@
/// matches nor rejects anything; as a required token it matches everything; and as an excluded
/// token it excludes nothing.
/// </remarks>
public static bool DoesMatchGlob(string text, string filter, TextFilterMatchOptions textFilterMatchOptions, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 337 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 337 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand All @@ -347,6 +347,20 @@
return true; // empty filter matches all text
}

// The filter is split on whitespace, so under ByWholeString no single token can equal a text that
// contains a space ("my file.txt"). With only plain tokens, try the whole filter as one glob first;
// the per-token pass below still runs, so "*.jpg *.png" keeps matching "a.png" (#113).
string wholeFilter = filter.Trim();
if (textFilterMatchOptions is TextFilterMatchOptions.ByWholeString
&& filterTokens.Count == 1
&& filterTokens.ContainsKey(TextFilterTokenType.Optional)
&& wholeFilter.Any(char.IsWhiteSpace)
&& ResolveGlob(wholeFilter, caseSensitivity) is Glob[] wholeFilterGlobs
&& IsGlobMatch(wholeFilterGlobs, text))
{
return true;
}

if (!filterTokens.TryGetValue(TextFilterTokenType.Excluded, out HashSet<string>? excludedTokens))
{
excludedTokens = [];
Expand Down Expand Up @@ -408,7 +422,7 @@
/// <param name="textTokens">The set of text tokens to match against.</param>
/// <param name="caseSensitivity">Whether the match distinguishes uppercase from lowercase.</param>
/// <returns><c>true</c> if any token matches the glob filter token; otherwise, <c>false</c>.</returns>
public static bool AnyTokenMatchesGlobFilter(string filterToken, HashSet<string> textTokens, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 425 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 425 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand All @@ -425,7 +439,7 @@
/// <param name="textTokens">The set of text tokens to match against.</param>
/// <param name="caseSensitivity">Whether the match distinguishes uppercase from lowercase.</param>
/// <returns><c>true</c> if all tokens match the glob filter token; otherwise, <c>false</c>.</returns>
public static bool AllTokensMatchGlobFilter(string filterToken, HashSet<string> textTokens, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 442 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 442 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand Down Expand Up @@ -816,7 +830,7 @@
/// <see cref="TextFilterMatchOptions.ByWholeString"/> for those.
/// </para>
/// </remarks>
public static bool DoesMatchRegex(string text, string filter, TextFilterMatchOptions textFilterMatchOptions, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 833 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 40 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 833 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 40 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down
Loading