-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.go
74 lines (63 loc) · 1.37 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package news
import (
"strings"
"github.com/dlepex/newsmaker/words"
)
type Filter struct { //nolint
Cond string
Sources []string // this are "globs" (glob is a simplified pattern, right now it's either prefix or suffix match)
Pubs []string // same
dnf *words.Expr // news title match condition
pubs []string
}
func (f *Filter) init() error {
dnf, e := words.NewExpr(f.Cond)
if e != nil {
return e
}
f.dnf = dnf
return nil
}
// matchAnyGlob matches string s against "globs"
// true, if glob is either prefix or suffix of s.
// todo use glob instead of simple prefix/suffix match?
func matchAnyGlob(s string, globs []string) bool {
if len(globs) == 0 {
return true
}
for _, g := range globs {
if strings.HasPrefix(s, g) || strings.HasSuffix(s, g) {
return true
}
}
return false
}
func matchAnyGlobAny(ss []string, globs []string) bool {
if len(ss) == 0 {
return true
}
if len(globs) == 0 {
return true
}
for _, s := range ss {
if matchAnyGlob(s, globs) {
return true
}
}
return false
}
func (f *Filter) matchSrc(src *SourceInfo) bool {
return matchAnyGlob(src.Name, f.Sources)
}
func (f *Filter) matchPub(pub *PubInfo) bool {
return matchAnyGlob(pub.Name, f.Pubs)
}
func chooseFilters(ff []*Filter, match func(*Filter) bool) []int {
ind := []int{}
for i, f := range ff {
if match(f) {
ind = append(ind, i)
}
}
return ind
}