-
Notifications
You must be signed in to change notification settings - Fork 0
/
matchers.go
69 lines (54 loc) · 1.38 KB
/
matchers.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
package main
import (
"regexp"
"path/filepath"
"strings"
)
type FileMatcher interface {
match(path string) (bool)
}
type BaseMatcher struct {
pattern string
insensitive bool
}
type RegexMatcher struct {
regexp *regexp.Regexp
BaseMatcher
}
func (m RegexMatcher) match(path string) (bool) {
return m.regexp.MatchString(path)
}
func newRegexMatcher(pattern string, insensitive bool) RegexMatcher {
// If this is case-insensitive, prepend '(?i)' to the regex. Likely NOT the
// best way to do this!
if insensitive {
// This assignment is not ideal, but easy.
prefix := "(?i)"
pattern = prefix + pattern
}
regexp, err := regexp.Compile(pattern)
check_error(err)
return RegexMatcher{regexp, BaseMatcher{pattern, insensitive}}
}
type FilepathMatcher struct {
BaseMatcher
}
func (m FilepathMatcher) match(path string) (bool) {
// Poor man's case insensitivity.
if m.insensitive {
path = strings.ToLower(path)
}
match, _ := filepath.Match(m.pattern, path)
return match
}
func newFilepathMatcher(pattern string, insensitive bool) FilepathMatcher {
return FilepathMatcher{BaseMatcher{pattern, insensitive}}
}
// An empty matcher always returns true. This is selected when pattern is empty.
type EmptyMatcher struct {}
func (m EmptyMatcher) match(path string) (bool) {
return true;
}
func newEmptyMatcher() EmptyMatcher {
return EmptyMatcher{}
}