From 3be832a12b5e033a1228a81fe2cbbb961e40fef6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 16:20:58 -0800 Subject: [PATCH] wildcard: match expanded variable text literally Co-Authored-By: Claude Opus 5.5 --- weed/util/wildcard/wildcard_matcher.go | 53 ++++++++++++++++++--- weed/util/wildcard/wildcard_matcher_test.go | 40 +++++++++++++++- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/weed/util/wildcard/wildcard_matcher.go b/weed/util/wildcard/wildcard_matcher.go index 5b2b85f6da..2c07d1b4ce 100644 --- a/weed/util/wildcard/wildcard_matcher.go +++ b/weed/util/wildcard/wildcard_matcher.go @@ -117,12 +117,47 @@ func (m *WildcardMatcher) Match(str string) bool { if m.useRegex { return m.regex.MatchString(str) } - return matchWildcardString(m.pattern, str) + return matchWildcardString(m.pattern, nil, str) } // MatchesWildcard provides a simple function interface for wildcard matching. func MatchesWildcard(pattern, str string) bool { - return matchWildcardString(pattern, str) + return matchWildcardString(pattern, nil, str) +} + +// MatchesExpandedWildcard matches str against pattern after replacing each +// match of vars with expand(match). The replacement text is matched literally, +// so only '*' and '?' written in pattern itself act as wildcards. +func MatchesExpandedWildcard(pattern string, vars *regexp.Regexp, expand func(string) string, str string, ignoreCase bool) bool { + if ignoreCase { + str = strings.ToLower(str) + } + locs := vars.FindAllStringIndex(pattern, -1) + if len(locs) == 0 { + if ignoreCase { + pattern = strings.ToLower(pattern) + } + return matchWildcardString(pattern, nil, str) + } + var expanded strings.Builder + var literal []bool + add := func(s string, isLiteral bool) { + if ignoreCase { + s = strings.ToLower(s) + } + expanded.WriteString(s) + for range len(s) { + literal = append(literal, isLiteral) + } + } + prev := 0 + for _, loc := range locs { + add(pattern[prev:loc[0]], false) + add(expand(pattern[loc[0]:loc[1]]), true) + prev = loc[1] + } + add(pattern[prev:], false) + return matchWildcardString(expanded.String(), literal, str) } // FastMatchesWildcard uses a cached WildcardMatcher for repeated pattern matching. @@ -140,8 +175,12 @@ func CompileWildcardPattern(pattern string) (*regexp.Regexp, error) { return compileWildcardPattern(pattern) } -func matchWildcardString(pattern, str string) bool { - if pattern == "*" { +func isWildcardAt(pattern string, literal []bool, i int, c byte) bool { + return pattern[i] == c && (literal == nil || !literal[i]) +} + +func matchWildcardString(pattern string, literal []bool, str string) bool { + if len(pattern) == 1 && isWildcardAt(pattern, literal, 0, '*') { return true } if pattern == str { @@ -157,11 +196,11 @@ func matchWildcardString(pattern, str string) bool { switch { // '*' must be checked before literal equality so a '*' in the target // cannot consume the pattern wildcard without a backtrack point - case patternIndex < len(pattern) && pattern[patternIndex] == '*': + case patternIndex < len(pattern) && isWildcardAt(pattern, literal, patternIndex, '*'): lastStarIndex = patternIndex lastStarMatchIndex = targetIndex patternIndex++ - case patternIndex < len(pattern) && (pattern[patternIndex] == '?' || pattern[patternIndex] == str[targetIndex]): + case patternIndex < len(pattern) && (isWildcardAt(pattern, literal, patternIndex, '?') || pattern[patternIndex] == str[targetIndex]): targetIndex++ patternIndex++ case lastStarIndex != -1: @@ -173,7 +212,7 @@ func matchWildcardString(pattern, str string) bool { } } - for patternIndex < len(pattern) && pattern[patternIndex] == '*' { + for patternIndex < len(pattern) && isWildcardAt(pattern, literal, patternIndex, '*') { patternIndex++ } return patternIndex == len(pattern) diff --git a/weed/util/wildcard/wildcard_matcher_test.go b/weed/util/wildcard/wildcard_matcher_test.go index d318206a38..c4394e24f8 100644 --- a/weed/util/wildcard/wildcard_matcher_test.go +++ b/weed/util/wildcard/wildcard_matcher_test.go @@ -1,6 +1,9 @@ package wildcard -import "testing" +import ( + "regexp" + "testing" +) func TestMatchesWildcard(t *testing.T) { tests := []struct { @@ -215,3 +218,38 @@ func BenchmarkFastMatchesWildcard(b *testing.B) { FastMatchesWildcard("s3:Get*", "s3:GetObject") } } + +func TestMatchesExpandedWildcard(t *testing.T) { + vars := regexp.MustCompile(`\$\{([^}]+)\}`) + values := map[string]string{"${user}": "", "${star}": "*", "${q}": "?lice", "${mixed}": "Al*"} + expand := func(v string) string { + if s, ok := values[v]; ok { + return s + } + return v + } + tests := []struct { + pattern string + str string + ignoreCase bool + want bool + }{ + {"home/${star}/*", "home/alice/secret", false, false}, + {"home/${star}/*", "home/*/own", false, true}, + {"${star}", "anything", false, false}, + {"${star}", "*", false, true}, + {"home/${q}/*", "home/alice/x", false, false}, + {"home/${q}/*", "home/?lice/x", false, true}, + {"home/${user}*", "home/anyone", false, true}, + {"home/${unset}/*", "home/${unset}/x", false, true}, + {"home/${unset}/*", "home/alice/x", false, false}, + {"Home/${mixed}/?", "home/al*/x", true, true}, + {"Home/${mixed}/?", "home/alice/x", true, false}, + {"plain*", "plain-text", false, true}, + } + for _, tt := range tests { + if got := MatchesExpandedWildcard(tt.pattern, vars, expand, tt.str, tt.ignoreCase); got != tt.want { + t.Errorf("MatchesExpandedWildcard(%q, %q, ignoreCase=%v) = %v, want %v", tt.pattern, tt.str, tt.ignoreCase, got, tt.want) + } + } +}