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
31 changes: 31 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,35 @@
new List<string> { "hello" },
TextFilter.Filter(["", "hello"], "hel", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll).ToList());
}

[TestMethod]
[DataRow("foo \t")]
[DataRow("foo \r\n")]
[DataRow("foo \u00a0")]
public void GlobWithNonSpaceWhitespaceDoesNotThrow(string filter)
{
// Splitting only on ' ' left a chunk of other whitespace that trimmed to an empty token, and
// the glob tokenizer then read its first character. A pasted trailing tab or newline is enough.
Assert.IsTrue(TextFilter.IsMatch("foo", filter));
CollectionAssert.AreEqual(
new List<string> { "foo" },
TextFilter.Filter(["foo"], filter).ToList());

Check warning on line 853 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=AaDiAZf6HLoqijiDlLhd&open=AaDiAZf6HLoqijiDlLhd&pullRequest=116
}

[TestMethod]
public void RegexByWordAllIgnoresNonSpaceWhitespaceBetweenWords()
{
Assert.IsTrue(TextFilter.IsMatch("hello \t world", "o", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll));
Assert.IsTrue(TextFilter.IsMatch("hello\tworld", "o", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll));
Assert.IsFalse(TextFilter.IsMatch("hello\tbye", "o", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll));
}

[TestMethod]
[DataRow(" \t ")]
[DataRow("\r\n")]
[DataRow("\u00a0")]
public void RegexByWordAllDoesNotMatchTextOfOnlyNonSpaceWhitespace(string text)
{
Assert.IsFalse(TextFilter.IsMatch(text, "a*", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll));
}
}
6 changes: 3 additions & 3 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 @@ -261,22 +261,22 @@
return textFilterMatchOptions switch
{
TextFilterMatchOptions.ByWholeString => [text],
TextFilterMatchOptions.ByWordAll => [.. text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim())],
TextFilterMatchOptions.ByWordAny => [.. text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim())],
TextFilterMatchOptions.ByWordAll => [.. text.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)],
TextFilterMatchOptions.ByWordAny => [.. text.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)],
_ => throw new NotImplementedException($"{nameof(TextFilterMatchOptions)}.{textFilterMatchOptions} has not been implemented"),
};
}

internal static Dictionary<TextFilterTokenType, HashSet<string>> ExtractGlobFilterTokens(string filter)
{
string[] filterTokens = [.. filter.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim())];
string[] filterTokens = [.. filter.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries)];
return filterTokens.GroupBy(t =>
{
char prefix = t.First();

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
Loading