Debian Codegraph @

match.go

raw symbols SCIP
1 package mainscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/"
5 "path/filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/"
6 "regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/"
7 "regexp/syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/"
8 "stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/"
9 "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/"
10 )
11
12 // A Matcher decides whether some filename matches its set of patterns.
13 type Matcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher# interface {
14 // Match returns whether a filename matches.
15 Matchscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#Match.(name string) bool
16 // ExcludePrefix returns whether all paths with this prefix cannot match.
17 // It is allowed to return false negatives but not false positives.
18 // This is used as an optimization for skipping directory watches with
19 // inverted matches.
20 ExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#ExcludePrefix.(prefix string) bool
21 Stringscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#String.() string
22 }
23
24 // ParseMatchers combines multiple (possibly inverse) regex and glob patterns
25 // into a single Matcher.
26 func ParseMatchersscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/ParseMatchers().(regexes, inverseRegexes, globs, inverseGlobs []string) (m Matcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#, err error) {
27 var matchers multiMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#
28 if len(regexes) == 0 && len(globs) == 0 {
29 matchers = multiMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#{matchAllscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#{}}
30 }
31 for _, r := range regexes {
32 regex, err := regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Compilescip-go gomod github.com/golang/go/src go1.24.0 regexp/Compile().(r)
33 if err != nil {
34 return nil, err
35 }
36 matchers = append(matchers, newRegexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/newRegexMatcher().(regex, false))
37 }
38 for _, r := range inverseRegexes {
39 regex, err := regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Compilescip-go gomod github.com/golang/go/src go1.24.0 regexp/Compile().(r)
40 if err != nil {
41 return nil, err
42 }
43 matchers = append(matchers, newRegexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/newRegexMatcher().(regex, true))
44 }
45 for _, g := range globs {
46 matchers = append(matchers, &globMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#{globscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#glob.: g})
47 }
48 for _, g := range inverseGlobs {
49 matchers = append(matchers, &globMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#{
50 globscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#glob.: g,
51 inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#inverse.: true,
52 })
53 }
54 return matchers, nil
55 }
56
57 // matchAll is an all-accepting Matcher.
58 type matchAllscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll# struct{}
59
60 func (matchAllscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#) Matchscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#Match().(name string) bool { return true }
61 func (matchAllscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#) ExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#ExcludePrefix().(prefix string) bool { return false }
62 func (matchAllscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#) Stringscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/matchAll#String().() string { return "(Implicitly matching all non-excluded files)" }
63
64 type globMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher# struct {
65 globscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#glob. string
66 inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#inverse. bool
67 }
68
69 func (m *globMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#) Matchscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#Match().(name string) bool {
70 matches, err := filepathscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/.Matchscip-go gomod github.com/golang/go/src go1.24.0 `path/filepath`/Match().(m.globscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#glob., name)
71 if err != nil {
72 return false
73 }
74 return matches != m.inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#inverse.
75 }
76
77 func (m *globMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#) ExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#ExcludePrefix().(prefix string) bool { return false }
78
79 func (m *globMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#) Stringscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#String().() string {
80 s := "Glob"
81 if m.inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#inverse. {
82 s = "Inverted glob"
83 }
84 return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s match: %q", s, m.globscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/globMatcher#glob.)
85 }
86
87 type regexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher# struct {
88 regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex. *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#
89 inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#inverse. bool
90
91 muscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#mu. *syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex# // protects following
92 canExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#canExcludePrefix. bool // This regex has no $, \z, or \b -- see ExcludePrefix
93 excludeCheckedscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#excludeChecked. bool
94 }
95
96 func (m *regexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#) Matchscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#Match().(name string) bool {
97 return m.regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex..MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(name) != m.inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#inverse.
98 }
99
100 func newRegexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/newRegexMatcher().(regex *regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#, inverse bool) *regexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher# {
101 return &regexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#{
102 regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex.: regex,
103 inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#inverse.: inverse,
104 muscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#mu.: new(syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#),
105 }
106 }
107
108 // ExcludePrefix returns whether this matcher cannot possibly match any path
109 // with a particular prefix. The question is: given a regex r and some prefix p
110 // which r accepts, is there any string s that has p as a prefix that r does not
111 // accept?
112 //
113 // With a classic regular expression from CS, this can only be the case if r
114 // ends with $, the end-of-input token (because once the NFA is in an accepting
115 // state, adding more input will not change that). In Go's regular expressions,
116 // I think the only way to construct a regex that would not meet this criteria
117 // is by using zero-width lookahead. There is no arbitrary lookahead in Go, so
118 // the only zero-width lookahead is provided by $, \z, and \b. For instance, the
119 // following regular expressions match the "foo", but not "foobar":
120 //
121 // foo$
122 // foo\b
123 // (foo$)|(baz$)
124 //
125 // Thus, to choose whether we can exclude this prefix, m must be an inverse
126 // matcher that does not contain the zero-width ops $, \z, and \b.
127 func (m *regexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#) ExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#ExcludePrefix().(prefix string) bool {
128 if !m.inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#inverse. {
129 return false
130 }
131 if !m.regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex..MatchStringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#MatchString().(prefix) || m.regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex..Stringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#String().() == "" {
132 return false
133 }
134
135 m.muscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#mu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().()
136 defer m.muscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#mu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().()
137 if !m.excludeCheckedscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#excludeChecked. {
138 r, err := syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/.Parsescip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Parse().(m.regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex..Stringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#String().(), syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/.Perlscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Perl.)
139 if err != nil {
140 panic("Cannot compile regex, but it was previously compiled!?!")
141 }
142 r = r.Simplifyscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Regexp#Simplify().()
143 stack := []*syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/.Regexpscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Regexp#{r}
144 for len(stack) > 0 {
145 cur := stack[len(stack)-1]
146 stack = stack[:len(stack)-1]
147 switch cur.Opscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Regexp#Op. {
148 case syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/.OpEndLinescip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/OpEndLine., syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/.OpEndTextscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/OpEndText., syntaxscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/.OpWordBoundaryscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/OpWordBoundary.:
149 m.canExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#canExcludePrefix. = false
150 goto after
151 }
152 if cur.Sub0scip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Regexp#Sub0.[0] != nil {
153 stack = append(stack, cur.Sub0scip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Regexp#Sub0.[0])
154 }
155 stack = append(stack, cur.Subscip-go gomod github.com/golang/go/src go1.24.0 `regexp/syntax`/Regexp#Sub....)
156 }
157 m.canExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#canExcludePrefix. = true
158 after:
159 m.excludeCheckedscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#excludeChecked. = true
160 }
161 return m.canExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#canExcludePrefix.
162 }
163
164 func (m *regexMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#) Stringscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#String().() string {
165 s := "Regex"
166 if m.inversescip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#inverse. {
167 s = "Inverted regex"
168 }
169 return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s match: %q", s, m.regexscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/regexMatcher#regex..Stringscip-go gomod github.com/golang/go/src go1.24.0 regexp/Regexp#String().())
170 }
171
172 // A multiMatcher returns the logical AND of its sub-matchers.
173 type multiMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher# []Matcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#
174
175 func (m multiMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#) Matchscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#Match().(name string) bool {
176 for _, matcher := range m {
177 if !matcher.Matchscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#Match.(name) {
178 return false
179 }
180 }
181 return true
182 }
183
184 func (m multiMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#) ExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#ExcludePrefix().(prefix string) bool {
185 for _, matcher := range m {
186 if matcher.ExcludePrefixscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#ExcludePrefix.(prefix) {
187 return true
188 }
189 }
190 return false
191 }
192
193 func (m multiMatcherscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#) Stringscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/multiMatcher#String().() string {
194 var s []string
195 for _, matcher := range m {
196 s = append(s, matcher.Stringscip-go gomod github.com/cespare/reflex . `github.com/cespare/reflex`/Matcher#String.())
197 }
198 return stringsscip-go gomod github.com/golang/go/src go1.24.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.0 strings/Join().(s, "\n")
199 }