wildcard: match expanded variable text literally

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-04 16:22:51 -08:00
1 parent d84235e74f
commit 3be832a12b
2 files changed
+85 -8

No files matched your search

+46 -7
View File
@@ -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)
+39 -1
View File
@@ -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)
}
}
}