diff --git a/internal/finder.go b/internal/finder.go index a957b6e..ccec417 100644 --- a/internal/finder.go +++ b/internal/finder.go @@ -18,7 +18,6 @@ func newFinder(queries ...string) *_Finder { for _, query := range queries { matcher, err := newMatcher(query) if err != nil { - println(err) continue } matchers = append(matchers, matcher) diff --git a/internal/matcher.go b/internal/matcher.go index fec3a88..9c2b0a8 100644 --- a/internal/matcher.go +++ b/internal/matcher.go @@ -1,6 +1,7 @@ package internal import ( + "errors" "regexp" "strings" ) @@ -13,11 +14,28 @@ type _StringMatcher struct { pattern string } -func newMatcher(pattern string) (_Matcher, error) { +func newMatcher(pattern string) (matcher _Matcher, err error) { + negate := false + + if strings.HasPrefix(pattern, "-") { + if len(pattern) == 1 { + return nil, errors.New("incomplete negate matcher") + } + negate = true + pattern = pattern[1:] + } + if len(pattern) > 2 && strings.HasPrefix(pattern, "/") && strings.HasSuffix(pattern, "/") { - return newRegexpMatcher(pattern) + matcher, err = newRegexpMatcher(pattern) + } else { + matcher = newStringMatcher(pattern) } - return newStringMatcher(pattern), nil + + if negate { + matcher = newNegateMatcher(matcher) + } + + return matcher, err } func newStringMatcher(pattern string) _StringMatcher { @@ -46,3 +64,15 @@ func newRegexpMatcher(pattern string) (*_RegexpMatcher, error) { func (m _RegexpMatcher) Matches(input string) bool { return m.pattern.FindStringSubmatch(input) != nil } + +type _NegateMatcher struct { + original _Matcher +} + +func newNegateMatcher(matcher _Matcher) *_NegateMatcher { + return &_NegateMatcher{matcher} +} + +func (m _NegateMatcher) Matches(input string) bool { + return !m.original.Matches(input) +}