diff --git a/TextFilter.Test/TextFilterTests.cs b/TextFilter.Test/TextFilterTests.cs index 49ef49e..ecbf067 100644 --- a/TextFilter.Test/TextFilterTests.cs +++ b/TextFilter.Test/TextFilterTests.cs @@ -1020,4 +1020,31 @@ public void RegexByWordAllDoesNotMatchTextOfOnlyNonSpaceWhitespace(string text) { Assert.IsFalse(TextFilter.IsMatch(text, "a*", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll)); } + + private static readonly string[] PaddingCandidates = ["hello world", "helium", "help", "yellow", "abcdef"]; + private static readonly string[] HelMatches = ["hello world", "helium", "help"]; + + [TestMethod] + [DataRow("hel ")] + [DataRow(" hel")] + [DataRow(" hel\t")] + public void FuzzyIsMatchIgnoresFilterPadding(string paddedFilter) + { + foreach (string text in PaddingCandidates) + { + Assert.AreEqual(TextFilter.IsMatch(text, "hel", TextFilterType.Fuzzy), TextFilter.IsMatch(text, paddedFilter, TextFilterType.Fuzzy), + $"Padding in '{paddedFilter}' should not change whether '{text}' fuzzy-matches."); + } + } + + [TestMethod] + [DataRow("hel ")] + [DataRow(" hel")] + public void FuzzyFilterIgnoresFilterPadding(string paddedFilter) + { + List strings = ["hello world", "helium", "help", "yellow"]; + List result = [.. TextFilter.Filter(strings, paddedFilter, TextFilterType.Fuzzy)]; + CollectionAssert.AreEquivalent(HelMatches, result, + $"Padding in '{paddedFilter}' should not hide items that 'hel' matches."); + } } diff --git a/TextFilter/TextFilter.cs b/TextFilter/TextFilter.cs index 913061a..2c0224c 100644 --- a/TextFilter/TextFilter.cs +++ b/TextFilter/TextFilter.cs @@ -240,7 +240,8 @@ public static bool IsMatch(string text, string filter, out int score, TextFilter { TextFilterType.Glob => DoesMatchGlob(text, filter, textFilterMatchOptions, caseSensitivity), TextFilterType.Regex => DoesMatchRegex(text, filter, textFilterMatchOptions, caseSensitivity), - TextFilterType.Fuzzy => Fuzzy.Contains(text.AsSpan(), filter.AsSpan(), out score), + // Padding would otherwise become a space the text must contain, so " hel" matched nothing. + TextFilterType.Fuzzy => Fuzzy.Contains(text.AsSpan(), filter.AsSpan().Trim(), out score), _ => throw new NotImplementedException($"{nameof(TextFilterType)}.{filterType} has not been implemented"), }; }