feat(wildcard): fast-path some cases (#1747)

* feat(wildcard): fast-path some cases
This commit is contained in:
Kyle Sanderson 2024-10-06 05:29:18 -07:00 committed by GitHub
parent 737184a985
commit caccaf3e09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 2 deletions

View file

@ -27,10 +27,46 @@ func Match(pattern, name string) (matched bool) {
}
func match(pattern, name string, simple bool) (matched bool) {
if pattern == "" {
if pattern == "" { //
return name == ""
} else if pattern == "*" {
} else if pattern == "*" { // *
return true
} else if !simple && pattern == "?" { // ?
return len(name) == 1
} else if idx := strings.IndexAny(pattern, "*?"); idx == -1 || (simple && pattern[idx] == '?' && !strings.Contains(pattern, "*")) { // egg
return name == pattern
} else if idx == len(pattern)-1 && pattern[idx] == '*' { // egg*
return strings.HasPrefix(name, pattern[:idx-1])
} else if wildEnd := pattern[len(pattern)-1] == '*'; !simple &&
((wildEnd && strings.Count(pattern, "*") == 1) || // egg?bert*
(len(pattern) == len(name) && !strings.Contains(pattern, "*"))) { // egg?bert?
base := 0
for base < len(name) {
i := strings.IndexRune(pattern[base:], '?')
if i == -1 {
if (wildEnd && !strings.HasPrefix(name[base:], pattern[base:len(pattern)-1])) || // egg*
(!wildEnd && name[base:] != pattern[base:]) { // egg
break
}
base = len(name)
continue
}
offset := base + i
if name[base:offset] != pattern[base:offset] {
break
}
base = offset + 1
}
return base == len(name)
} else if strings.HasPrefix(pattern, "*") && strings.HasSuffix(pattern, "*") && // *egg*
(simple || (!simple && !strings.Contains(pattern, "?"))) && // simple is fine, if not we need to check for ? and skip if so.
strings.Count(pattern, "*") == 2 { // make sure that we have no other wildcards.
return strings.Contains(name, pattern[1:len(pattern)-1])
}
return deepMatchRune(name, pattern, simple, pattern, false)