From 92e6bd43f312e66c4c5b5c158d99bb2dc0c52141 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 06:23:13 +0000 Subject: [PATCH] Require a separator after a glob ** joined to a preceding literal [patch] Expanding a masked "**/" for the zero-segment case dropped it even when the ** followed a literal, so "foo**/bar" spliced into "foobar" and matched text with no separator. The drop expansion is now added only when the ** starts the token or follows a separator; otherwise ** is just *. Fixes #170 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017vfJW5XsPdvSDDViUMN9s2 --- TextFilter.Test/TextFilterTests.cs | 20 ++++++++++++++++++++ TextFilter/TextFilter.cs | 13 +++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/TextFilter.Test/TextFilterTests.cs b/TextFilter.Test/TextFilterTests.cs index 3250a64..7d1f836 100644 --- a/TextFilter.Test/TextFilterTests.cs +++ b/TextFilter.Test/TextFilterTests.cs @@ -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() { diff --git a/TextFilter/TextFilter.cs b/TextFilter/TextFilter.cs index 927c00c..a49d1c6 100644 --- a/TextFilter/TextFilter.cs +++ b/TextFilter/TextFilter.cs @@ -449,7 +449,8 @@ private static bool IsGlobMatch(Glob[] globs, string textToken) // 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 @@ -471,10 +472,14 @@ internal static IReadOnlyList ExpandGlobStarSegments(string maskedToken) 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); } }