From cfd05571dd7fd63f03beffd69d8c66f33dc5209a Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 28 Sep 2026 18:28:36 +0000 Subject: [PATCH] [patch] Ignore leading and trailing spaces in a fuzzy filter The Fuzzy arm of IsMatch passed the filter through as typed, so padding became a space the text had to contain: " hel" matched nothing and "hel " matched only items with a space. The filter is now trimmed first, as Glob and Regex already effectively do by splitting on whitespace. Fixes #115 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RpyD4tJtfe5diTxv8UEw2x --- TextFilter.Test/TextFilterTests.cs | 27 +++++++++++++++++++++++++++ TextFilter/TextFilter.cs | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) 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"), }; }