fix(wildcard): match on multi-line data (#1780)

* fix(wildcard): match on multi-line data

* fix(wildcard): remove duplicate block
This commit is contained in:
ze0s 2024-10-23 16:33:49 +02:00 committed by GitHub
parent f029de233f
commit 8cd7d67cee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 21 deletions

View file

@ -8,6 +8,7 @@ import (
"strings"
"github.com/autobrr/autobrr/pkg/regexcache"
"github.com/rs/zerolog/log"
)
@ -29,22 +30,26 @@ func Match(pattern, name string) (matched bool) {
func match(pattern, name string, simple bool) (matched bool) {
if pattern == "" { //
return name == ""
} 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?
// egg?bert*
} else if wildEnd := pattern[len(pattern)-1] == '*'; !simple && ((wildEnd && strings.Count(pattern, "*") == 1) || (len(pattern) == len(name) && !strings.Contains(pattern, "*"))) { // egg?bert?
return matchComplex(name, pattern, wildEnd)
} 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.
// *egg*
// simple is fine, if not we need to check for ? and skip if so.
} else if strings.HasPrefix(pattern, "*") && strings.HasSuffix(pattern, "*") && (simple || (!simple && !strings.Contains(pattern, "?"))) && strings.Count(pattern, "*") == 2 { // make sure that we have no other wildcards.
return strings.Contains(name, pattern[1:len(pattern)-1])
}
@ -146,7 +151,7 @@ func cleanForRegex(pattern string, simple bool) string {
}
func prepForRegex(pattern string) string {
return `^` + regexp.QuoteMeta(pattern) + `$`
return `(?m)^` + regexp.QuoteMeta(pattern) + `$`
}
func deepMatchRune(str, pattern string, simple bool, original string, bulk bool) bool {
@ -174,10 +179,5 @@ func deepMatchRune(str, pattern string, simple bool, original string, bulk bool)
regexcache.SubmitOriginal(original+salt, user)
}
idx := user.FindStringIndex(str)
if idx == nil {
return false
}
return idx[1] == len(str)
return user.MatchString(str)
}