diff --git a/pkg/wildcard/match.go b/pkg/wildcard/match.go index a445465..ded4bd7 100644 --- a/pkg/wildcard/match.go +++ b/pkg/wildcard/match.go @@ -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) diff --git a/pkg/wildcard/match_test.go b/pkg/wildcard/match_test.go index 99beaa9..2134d3b 100644 --- a/pkg/wildcard/match_test.go +++ b/pkg/wildcard/match_test.go @@ -52,11 +52,26 @@ func TestMatch(t *testing.T) { text: "tv", matched: true, }, + { + pattern: "t?", + text: "tv", + matched: true, + }, + { + pattern: "?", + text: "z", + matched: true, + }, { pattern: "*EPUB*", text: "Translated (Group) / EPUB", matched: true, }, + { + pattern: "*EP?B*", + text: "Translated (Group) / EPUB", + matched: true, + }, { pattern: "*shift*", text: "Good show shift S02 2160p ATVP WEB-DL DDP 5.1 Atmos DV HEVC-GROUP", @@ -110,6 +125,8 @@ func TestMatchSimple(t *testing.T) { {"t?st", "test", false}, {"t?st", "tast", false}, {"test", "test", true}, + {"*te?t*", "test", false}, + {"*test*", "test", true}, {"test", "toast", false}, {"", "non-empty", false}, {"*", "", true}, @@ -160,6 +177,7 @@ func TestMatchSlice(t *testing.T) { }{ {[]string{"*", "test", "t?st"}, "test", true}, {[]string{"te?t", "t?st", "random"}, "tost", true}, + {[]string{"te?t", "t??e?", "random"}, "toser", true}, {[]string{"*st", "n?st", "l*st"}, "list", true}, {[]string{"?", "??", "???"}, "t", true}, {[]string{"a", "b", "c"}, "d", false}, @@ -190,3 +208,11 @@ func TestMatchSlice(t *testing.T) { } } } + +func Benchmark_Regex(b *testing.B) { + for i := 0; i < b.N; i++ { + b.StartTimer() + TestMatchSlice(nil) + b.StopTimer() + } +}