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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
package main
import (
"fmt"
"path/filepath"
"regexp"
"regexp/syntax"
"strings"
"sync"
)
// A Matcher decides whether some filename matches its set of patterns.
type Matcher interface {
// Match returns whether a filename matches.
Match(name string) bool
// ExcludePrefix returns whether all paths with this prefix cannot match.
// It is allowed to return false negatives but not false positives.
// This is used as an optimization for skipping directory watches with
// inverted matches.
ExcludePrefix(prefix string) bool
String() string
}
// ParseMatchers combines multiple (possibly inverse) regex and glob patterns
// into a single Matcher.
func ParseMatchers(regexes, inverseRegexes, globs, inverseGlobs []string) (m Matcher, err error) {
var matchers multiMatcher
if len(regexes) == 0 && len(globs) == 0 {
matchers = multiMatcher{matchAll{}}
}
for _, r := range regexes {
regex, err := regexp.Compile(r)
if err != nil {
return nil, err
}
matchers = append(matchers, newRegexMatcher(regex, false))
}
for _, r := range inverseRegexes {
regex, err := regexp.Compile(r)
if err != nil {
return nil, err
}
matchers = append(matchers, newRegexMatcher(regex, true))
}
for _, g := range globs {
matchers = append(matchers, &globMatcher{glob: g})
}
for _, g := range inverseGlobs {
matchers = append(matchers, &globMatcher{
glob: g,
inverse: true,
})
}
return matchers, nil
}
// matchAll is an all-accepting Matcher.
type matchAll struct{}
func (matchAll) Match(name string) bool { return true }
func (matchAll) ExcludePrefix(prefix string) bool { return false }
func (matchAll) String() string { return "(Implicitly matching all non-excluded files)" }
type globMatcher struct {
glob string
inverse bool
}
func (m *globMatcher) Match(name string) bool {
matches, err := filepath.Match(m.glob, name)
if err != nil {
return false
}
return matches != m.inverse
}
func (m *globMatcher) ExcludePrefix(prefix string) bool { return false }
func (m *globMatcher) String() string {
s := "Glob"
if m.inverse {
s = "Inverted glob"
}
return fmt.Sprintf("%s match: %q", s, m.glob)
}
type regexMatcher struct {
regex *regexp.Regexp
inverse bool
mu *sync.Mutex // protects following
canExcludePrefix bool // This regex has no $, \z, or \b -- see ExcludePrefix
excludeChecked bool
}
func (m *regexMatcher) Match(name string) bool {
return m.regex.MatchString(name) != m.inverse
}
func newRegexMatcher(regex *regexp.Regexp, inverse bool) *regexMatcher {
return ®exMatcher{
regex: regex,
inverse: inverse,
mu: new(sync.Mutex),
}
}
// ExcludePrefix returns whether this matcher cannot possibly match any path
// with a particular prefix. The question is: given a regex r and some prefix p
// which r accepts, is there any string s that has p as a prefix that r does not
// accept?
//
// With a classic regular expression from CS, this can only be the case if r
// ends with $, the end-of-input token (because once the NFA is in an accepting
// state, adding more input will not change that). In Go's regular expressions,
// I think the only way to construct a regex that would not meet this criteria
// is by using zero-width lookahead. There is no arbitrary lookahead in Go, so
// the only zero-width lookahead is provided by $, \z, and \b. For instance, the
// following regular expressions match the "foo", but not "foobar":
//
// foo$
// foo\b
// (foo$)|(baz$)
//
// Thus, to choose whether we can exclude this prefix, m must be an inverse
// matcher that does not contain the zero-width ops $, \z, and \b.
func (m *regexMatcher) ExcludePrefix(prefix string) bool {
if !m.inverse {
return false
}
if !m.regex.MatchString(prefix) || m.regex.String() == "" {
return false
}
m.mu.Lock()
defer m.mu.Unlock()
if !m.excludeChecked {
r, err := syntax.Parse(m.regex.String(), syntax.Perl)
if err != nil {
panic("Cannot compile regex, but it was previously compiled!?!")
}
r = r.Simplify()
stack := []*syntax.Regexp{r}
for len(stack) > 0 {
cur := stack[len(stack)-1]
stack = stack[:len(stack)-1]
switch cur.Op {
case syntax.OpEndLine, syntax.OpEndText, syntax.OpWordBoundary:
m.canExcludePrefix = false
goto after
}
if cur.Sub0[0] != nil {
stack = append(stack, cur.Sub0[0])
}
stack = append(stack, cur.Sub...)
}
m.canExcludePrefix = true
after:
m.excludeChecked = true
}
return m.canExcludePrefix
}
func (m *regexMatcher) String() string {
s := "Regex"
if m.inverse {
s = "Inverted regex"
}
return fmt.Sprintf("%s match: %q", s, m.regex.String())
}
// A multiMatcher returns the logical AND of its sub-matchers.
type multiMatcher []Matcher
func (m multiMatcher) Match(name string) bool {
for _, matcher := range m {
if !matcher.Match(name) {
return false
}
}
return true
}
func (m multiMatcher) ExcludePrefix(prefix string) bool {
for _, matcher := range m {
if matcher.ExcludePrefix(prefix) {
return true
}
}
return false
}
func (m multiMatcher) String() string {
var s []string
for _, matcher := range m {
s = append(s, matcher.String())
}
return strings.Join(s, "\n")
}
|