mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(wildcard): fast-path some cases (#1747)
* feat(wildcard): fast-path some cases
This commit is contained in:
parent
737184a985
commit
caccaf3e09
2 changed files with 64 additions and 2 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue