mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(filters): wildcard slice matching optimizations (#1716)
* chore(tests): add more test cases * chore(tests): add code comments for matching patterns * chore(tests): fix typos --------- Co-authored-by: martylukyy <35452459+martylukyy@users.noreply.github.com>
This commit is contained in:
parent
3af06553e7
commit
e9f8730ca0
5 changed files with 279 additions and 48 deletions
|
@ -878,12 +878,14 @@ func sliceContainsSlice(tags []string, filters []string) bool {
|
|||
}
|
||||
|
||||
func containsMatchFuzzy(tags []string, filters []string) bool {
|
||||
advanced := make([]string, 0, len(filters))
|
||||
for _, tag := range tags {
|
||||
if tag == "" {
|
||||
continue
|
||||
}
|
||||
tag = strings.ToLower(tag)
|
||||
|
||||
clear(advanced)
|
||||
for _, filter := range filters {
|
||||
if filter == "" {
|
||||
continue
|
||||
|
@ -893,20 +895,22 @@ func containsMatchFuzzy(tags []string, filters []string) bool {
|
|||
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
|
||||
a := strings.ContainsAny(filter, "?|*")
|
||||
if a {
|
||||
match := wildcard.Match(filter, tag)
|
||||
if match {
|
||||
return true
|
||||
}
|
||||
advanced = append(advanced, filter)
|
||||
} else if strings.Contains(tag, filter) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if wildcard.MatchSlice(advanced, tag) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func containsMatch(tags []string, filters []string) bool {
|
||||
advanced := make([]string, 0, len(filters))
|
||||
for _, tag := range tags {
|
||||
if tag == "" {
|
||||
continue
|
||||
|
@ -914,6 +918,7 @@ func containsMatch(tags []string, filters []string) bool {
|
|||
tag = strings.ToLower(tag)
|
||||
tag = strings.Trim(tag, " ")
|
||||
|
||||
clear(advanced)
|
||||
for _, filter := range filters {
|
||||
if filter == "" {
|
||||
continue
|
||||
|
@ -923,14 +928,15 @@ func containsMatch(tags []string, filters []string) bool {
|
|||
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
|
||||
a := strings.ContainsAny(filter, "?|*")
|
||||
if a {
|
||||
match := wildcard.Match(filter, tag)
|
||||
if match {
|
||||
return true
|
||||
}
|
||||
advanced = append(advanced, filter)
|
||||
} else if tag == filter {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if wildcard.MatchSlice(advanced, tag) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
|
@ -945,6 +951,8 @@ func containsAllMatch(tags []string, filters []string) bool {
|
|||
filter = strings.Trim(filter, " ")
|
||||
found := false
|
||||
|
||||
wildFilter := strings.ContainsAny(filter, "?|*")
|
||||
|
||||
for _, tag := range tags {
|
||||
if tag == "" {
|
||||
continue
|
||||
|
@ -955,7 +963,7 @@ func containsAllMatch(tags []string, filters []string) bool {
|
|||
if tag == filter {
|
||||
found = true
|
||||
break
|
||||
} else if strings.ContainsAny(filter, "?|*") {
|
||||
} else if wildFilter {
|
||||
if wildcard.Match(filter, tag) {
|
||||
found = true
|
||||
break
|
||||
|
@ -994,11 +1002,13 @@ func containsMatchBasic(tags []string, filters []string) bool {
|
|||
}
|
||||
|
||||
func containsAnySlice(tags []string, filters []string) bool {
|
||||
advanced := make([]string, 0, len(filters))
|
||||
for _, tag := range tags {
|
||||
if tag == "" {
|
||||
continue
|
||||
}
|
||||
tag = strings.ToLower(tag)
|
||||
clear(advanced)
|
||||
|
||||
for _, filter := range filters {
|
||||
if filter == "" {
|
||||
|
@ -1007,16 +1017,17 @@ func containsAnySlice(tags []string, filters []string) bool {
|
|||
filter = strings.ToLower(filter)
|
||||
filter = strings.Trim(filter, " ")
|
||||
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
|
||||
wild := strings.ContainsAny(filter, "?|*")
|
||||
if wild {
|
||||
match := wildcard.Match(filter, tag)
|
||||
if match {
|
||||
return true
|
||||
}
|
||||
a := strings.ContainsAny(filter, "?|*")
|
||||
if a {
|
||||
advanced = append(advanced, filter)
|
||||
} else if tag == filter {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if wildcard.MatchSlice(advanced, tag) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
|
|
|
@ -42,7 +42,7 @@ func TestFilter_CheckFilter(t *testing.T) {
|
|||
args: args{
|
||||
filter: Filter{
|
||||
Enabled: true,
|
||||
MatchCategories: "Movies",
|
||||
MatchCategories: "TV*,Movies*",
|
||||
Freeleech: true,
|
||||
MinSize: "10 GB",
|
||||
MaxSize: "40GB",
|
||||
|
@ -1911,6 +1911,20 @@ func TestFilter_CheckFilter1(t *testing.T) {
|
|||
wantRejections: []string{"match release tags regex not matching. got: want: foreign - 17"},
|
||||
wantMatch: false,
|
||||
},
|
||||
{
|
||||
name: "test_43",
|
||||
fields: fields{
|
||||
Shows: ",Dutchess,Preacher",
|
||||
Seasons: "1",
|
||||
Episodes: "0",
|
||||
Resolutions: []string{"2160p"},
|
||||
Sources: []string{"WEB-DL"},
|
||||
Codecs: []string{"x265"},
|
||||
},
|
||||
args: args{&Release{TorrentName: "Preacher.S01.DV.2160p.ATVP.WEB-DL.DDPA5.1.x265-NOSiViD"}},
|
||||
wantRejections: nil,
|
||||
wantMatch: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue