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
20 changes: 20 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,26 @@ public void GlobStarSlashStillRejectsNonMatchingPaths(string text, string filter
Assert.IsFalse(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
}

[TestMethod]
[DataRow("foobar", "foo**/bar")]
[DataRow("ab", "a**/b")]
[DataRow("buildout", "build**/out")]
public void GlobStarJoinedToLiteralRequiresSeparator(string text, string filter)
{
Assert.IsFalse(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsFalse(TextFilter.IsMatch(text, filter));
}

[TestMethod]
[DataRow("foo/bar", "foo**/bar")]
[DataRow("fooX/bar", "foo**/bar")]
[DataRow("bar", "**/bar")]
[DataRow("a/b", "a/**/b")]
public void GlobStarJoinedToLiteralStillMatchesWithSeparator(string text, string filter)
{
Assert.IsTrue(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
}

[TestMethod]
public void GlobStarSlashFiltersPathItems()
{
Expand Down
13 changes: 9 additions & 4 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

// Stands in for a pattern that has already run out of time once. The timeout bounds a single
// IsMatch call, so without this the same pathological pattern would pay it again for every word
Expand Down Expand Up @@ -300,7 +300,7 @@

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

Check warning on line 303 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 303 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 303 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 303 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 303 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 @@ -449,7 +449,8 @@
// Masking also hides the separators DotNet.Glob needs to recognise "**/" as "zero or more path
// segments", leaving "**" followed by a literal character. Since * already crosses the masked
// separator, each "**/" is expanded here instead: dropped, for the zero-segment case, or kept as
// "*/", for "anything ending at a separator". A token matches if any expansion does.
// "*/", for "anything ending at a separator". A token matches if any expansion does. Only a "**/"
// that starts the token or follows a separator is dropped, since one joined to a literal is a "*".
private const string MaskedGlobStarSegment = "**\uE000";

// Each "**/" doubles the expansions, so only this many are expanded both ways; any beyond it keep
Expand All @@ -471,10 +472,14 @@

foreach (string prefix in expansions)
{
next.Add(prefix + literal + "*" + MaskedPathSeparator);
if (expandBothWays)
string before = prefix + literal;
next.Add(before + "*" + MaskedPathSeparator);

// Only a "**" that is a whole segment can match zero segments; joined to a literal, as
// in "foo**/bar", it is just "*", and dropping it would splice "foo" onto "bar".
if (expandBothWays && (before.Length == 0 || before[^1] == MaskedPathSeparator))
{
next.Add(prefix + literal);
next.Add(before);
}
}

Expand Down
Loading