-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.go
57 lines (47 loc) · 869 Bytes
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package filter
import (
"regexp"
"strings"
)
// Filter represents a compiled filter, which can be used to match text.
type Filter interface {
Match(s string) bool
}
type andFilter struct {
queries []Filter
}
func (aq andFilter) Match(s string) bool {
for _, q := range aq.queries {
if !q.Match(s) {
return false
}
}
return true
}
type orFilter struct {
queries []Filter
}
func (oq orFilter) Match(s string) bool {
for _, q := range oq.queries {
if q.Match(s) {
return true
}
}
return false
}
type substringFilter struct {
pattern string
ignoreCase bool
}
func (sq substringFilter) Match(s string) bool {
if sq.ignoreCase {
s = strings.ToLower(s)
}
return strings.Contains(s, sq.pattern)
}
type regexpFilter struct {
pattern *regexp.Regexp
}
func (rq regexpFilter) Match(s string) bool {
return rq.pattern.MatchString(s)
}