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
39 changes: 38 additions & 1 deletion TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,6 @@
[DataRow("a[b-")]
[DataRow("[!a-")]
[DataRow("*[0-")]
[DataRow("[a-]")]
[DataRow("-file[0-")]
[DataRow("!file[0-")]
[DataRow("^file[0-")]
Expand All @@ -940,6 +939,44 @@
}
}

[TestMethod]
public void GlobClassWithATrailingDashMatchesTheDashLiterally()
{
// A "-" just before "]" is a literal dash, so "file[a-]" is a finished pattern matching "filea"
// and "file-". DotNet.Glob's tokeniser threw on it, and the token was then ignored as if half
// typed: the filter kept every item and the exclusion removed none.
List<string> items = ["filea", "file-", "fileZ", "report"];
foreach (TextFilterCaseSensitivity caseSensitivity in new[] { TextFilterCaseSensitivity.CaseSensitive, TextFilterCaseSensitivity.CaseInsensitive })
{
CollectionAssert.AreEqual(
new List<string> { "filea", "file-" },
TextFilter.Filter(items, "file[a-]", TextFilterType.Glob, TextFilterMatchOptions.ByWordAny, caseSensitivity).ToList());

Check warning on line 953 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaE0AOz8plQx4e6V07YH&open=AaE0AOz8plQx4e6V07YH&pullRequest=149
CollectionAssert.AreEqual(
new List<string> { "fileZ", "report" },
TextFilter.Filter(items, "-file[a-]", TextFilterType.Glob, TextFilterMatchOptions.ByWordAny, caseSensitivity).ToList());
CollectionAssert.AreEqual(
new List<string> { "fileZ" },
TextFilter.Filter(items, "file[!a-]", TextFilterType.Glob, TextFilterMatchOptions.ByWordAny, caseSensitivity).ToList());

Check warning on line 959 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaE0AOz8plQx4e6V07YI&open=AaE0AOz8plQx4e6V07YI&pullRequest=149
}

// A half-typed range is still unparseable and still ignored.
CollectionAssert.AreEqual(items, TextFilter.Filter(items, "file[0-", TextFilterType.Glob).ToList());

Check warning on line 963 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaE0AOz8plQx4e6V07YJ&open=AaE0AOz8plQx4e6V07YJ&pullRequest=149
CollectionAssert.AreEqual(items, TextFilter.Filter(items, "-file[0-", TextFilterType.Glob).ToList());

Check warning on line 964 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaE0AOz8plQx4e6V07YK&open=AaE0AOz8plQx4e6V07YK&pullRequest=149
}

[TestMethod]
[DataRow("file[a-]", "file[-a]")]
[DataRow("file[!a-]", "file[!-a]")]
[DataRow("[0-9a-]x[b-]", "[-0-9a]x[-b]")]
[DataRow("[]-]", "[-]]")]
[DataRow("[-]", "[-]")]
[DataRow("[a-z]", "[a-z]")]
[DataRow("file[0-", "file[0-")]
[DataRow("a]b", "a]b")]
[DataRow("[a-]x[0-", "[-a]x[0-")]
public void MoveTrailingClassDashesRewritesOnlyClosedClasses(string token, string expected) =>
Assert.AreEqual(expected, TextFilter.MoveTrailingClassDashes(token));

[TestMethod]
[DataRow("-readme[a-")]
[DataRow("!readme[a-")]
Expand All @@ -951,9 +988,9 @@
List<string> items = ["file1.txt", "file2.txt", "readme.md"];
foreach (TextFilterCaseSensitivity caseSensitivity in new[] { TextFilterCaseSensitivity.CaseSensitive, TextFilterCaseSensitivity.CaseInsensitive })
{
CollectionAssert.AreEqual(
items,
TextFilter.Filter(items, pattern, TextFilterType.Glob, TextFilterMatchOptions.ByWordAny, caseSensitivity).ToList());

Check warning on line 993 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaE0AOz8plQx4e6V07YG&open=AaE0AOz8plQx4e6V07YG&pullRequest=149
}
}

Expand Down
53 changes: 52 additions & 1 deletion TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ktsu.TextFilter;

using System.Collections.Concurrent;
using System.Text;
using System.Text.RegularExpressions;

using DotNet.Globbing;
Expand Down Expand Up @@ -124,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.

// 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 @@ -285,7 +286,7 @@

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

Check warning on line 289 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 289 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 289 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 289 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 289 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 289 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 @@ -466,6 +467,56 @@
return [.. expansions.Select(prefix => prefix + tail).Distinct(StringComparer.Ordinal)];
}

// In glob syntax a "-" just before the closing "]" is a literal dash, so "[a-]" matches "a" or "-".
// DotNet.Glob's tokeniser reads it as a range left open and throws, as it does for the half-typed
// "[a-", so the dash is moved to the front of the class ("[-a]", "[!-a]"), a form it does parse.
// A class with no closing "]" is left alone and stays unparseable.
internal static string MoveTrailingClassDashes(string token)
{
if (!token.Contains(']'))
{
return token;
}

StringBuilder result = new(token.Length);
int index = 0;
while (index < token.Length)
{
int open = token.IndexOf('[', index);
if (open < 0)
{
break;
}

int contentStart = open + 1;
string negation = contentStart < token.Length && token[contentStart] == '!' ? "!" : string.Empty;
contentStart += negation.Length;

// A "]" straight after the opening bracket is a member of the class rather than its end.
int close = contentStart < token.Length ? token.IndexOf(']', contentStart + 1) : -1;
if (close < 0)
{
break;
}

string content = token[contentStart..close];
result.Append(token, index, open - index).Append('[').Append(negation);
if (content.Length > 1 && content[^1] == '-')
{
result.Append('-').Append(content, 0, content.Length - 1);
}
else
{
result.Append(content);
}

result.Append(']');
index = close + 1;
}

return result.Append(token, index, token.Length - index).ToString();
}

// Returns null for a token that cannot be parsed, so each caller can decide what ignoring it means.
private static Glob[]? ResolveGlob(string filterToken, TextFilterCaseSensitivity caseSensitivity)
{
Expand All @@ -475,7 +526,7 @@
{
try
{
string maskedToken = MaskPathSeparators(filterToken);
string maskedToken = MaskPathSeparators(MoveTrailingClassDashes(filterToken));
globs = [.. ExpandGlobStarSegments(maskedToken).Select(pattern => caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
? Glob.Parse(pattern, CaseInsensitiveGlobOptions)
: Glob.Parse(pattern))];
Expand Down
Loading