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:
Kyle Sanderson 2024-09-14 02:31:26 -07:00 committed by GitHub
parent 3af06553e7
commit e9f8730ca0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 279 additions and 48 deletions

View file

@ -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