switch to regexp2 for parsed filter
This commit is contained in:
@@ -148,7 +148,7 @@ func filterItemsByTag(items []*Item, filter string) ([]*Item, error) {
|
||||
}
|
||||
for _, t := range tags {
|
||||
if re != nil {
|
||||
if re.MatchString(t) {
|
||||
if match, err := re.MatchString(t); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
break
|
||||
}
|
||||
@@ -173,7 +173,7 @@ func filterItemsByQuery(items []*Item, filter string) ([]*Item, error) {
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
@@ -212,7 +212,7 @@ func filterAppsByQuery(items []Application, filter string) ([]Application, error
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
@@ -251,7 +251,7 @@ func filterHostsByQuery(items []Host, filter string) ([]Host, error) {
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
@@ -282,7 +282,7 @@ func filterGroupsByQuery(items []Group, filter string) ([]Group, error) {
|
||||
for _, i := range items {
|
||||
name := i.Name
|
||||
if re != nil {
|
||||
if re.MatchString(name) {
|
||||
if match, err := re.MatchString(name); match && err != nil {
|
||||
filteredItems = append(filteredItems, i)
|
||||
}
|
||||
} else if name == filter {
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/dlclark/regexp2"
|
||||
)
|
||||
|
||||
func (item *Item) ExpandItemName() string {
|
||||
@@ -63,9 +65,10 @@ func splitKeyParams(paramStr string) []string {
|
||||
return params
|
||||
}
|
||||
|
||||
func parseFilter(filter string) (*regexp.Regexp, error) {
|
||||
regex := regexp.MustCompile(`^/(.+)/([imsU]*)$`)
|
||||
flagRE := regexp.MustCompile("[imsU]+")
|
||||
func parseFilter(filter string) (*regexp2.Regexp, error) {
|
||||
vaildREModifiers := "imncsxrde"
|
||||
regex := regexp.MustCompile(`^/(.+)/(\w*)$`)
|
||||
flagRE := regexp.MustCompile("[" + vaildREModifiers + "]+")
|
||||
|
||||
matches := regex.FindStringSubmatch(filter)
|
||||
if len(matches) <= 1 {
|
||||
@@ -77,12 +80,12 @@ func parseFilter(filter string) (*regexp.Regexp, error) {
|
||||
if flagRE.MatchString(matches[2]) {
|
||||
pattern += "(?" + matches[2] + ")"
|
||||
} else {
|
||||
return nil, fmt.Errorf("error parsing regexp: unsupported flags `%s` (expected [imsU])", matches[2])
|
||||
return nil, fmt.Errorf("error parsing regexp: unsupported flags `%s` (expected [%s])", matches[2], vaildREModifiers)
|
||||
}
|
||||
}
|
||||
pattern += matches[1]
|
||||
|
||||
return regexp.Compile(pattern)
|
||||
return regexp2.Compile(pattern, regexp2.RE2)
|
||||
}
|
||||
|
||||
func itemTagToString(tag ItemTag) string {
|
||||
|
||||
Reference in New Issue
Block a user