1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // ErrBadPattern indicates a pattern was malformed.
14 var ErrBadPattern = errors.New("syntax error in pattern")
16 // Match reports whether name matches the shell pattern.
17 // The pattern syntax is:
22 // '*' matches any sequence of non-/ characters
23 // '?' matches any single non-/ character
24 // '[' [ '^' ] { character-range } ']'
25 // character class (must be non-empty)
26 // c matches character c (c != '*', '?', '\\', '[')
27 // '\\' c matches character c
30 // c matches character c (c != '\\', '-', ']')
31 // '\\' c matches character c
32 // lo '-' hi matches character c for lo <= c <= hi
34 // Match requires pattern to match all of name, not just a substring.
35 // The only possible returned error is ErrBadPattern, when pattern
37 func Match(pattern, name string) (matched bool, err error) {
39 for len(pattern) > 0 {
42 star, chunk, pattern = scanChunk(pattern)
43 if star && chunk == "" {
44 // Trailing * matches rest of string unless it has a /.
45 return bytealg.IndexByteString(name, '/') < 0, nil
47 // Look for match at current position.
48 t, ok, err := matchChunk(chunk, name)
49 // if we're the last chunk, make sure we've exhausted the name
50 // otherwise we'll give a false result even if we could still match
52 if ok && (len(t) == 0 || len(pattern) > 0) {
60 // Look for match skipping i+1 bytes.
62 for i := 0; i < len(name) && name[i] != '/'; i++ {
63 t, ok, err := matchChunk(chunk, name[i+1:])
65 // if we're the last chunk, make sure we exhausted the name
66 if len(pattern) == 0 && len(t) > 0 {
77 // Before returning false with no error,
78 // check that the remainder of the pattern is syntactically valid.
79 for len(pattern) > 0 {
80 _, chunk, pattern = scanChunk(pattern)
81 if _, _, err := matchChunk(chunk, ""); err != nil {
87 return len(name) == 0, nil
90 // scanChunk gets the next segment of pattern, which is a non-star string
91 // possibly preceded by a star.
92 func scanChunk(pattern string) (star bool, chunk, rest string) {
93 for len(pattern) > 0 && pattern[0] == '*' {
100 for i = 0; i < len(pattern); i++ {
103 // error check handled in matchChunk: bad pattern.
104 if i+1 < len(pattern) {
117 return star, pattern[0:i], pattern[i:]
120 // matchChunk checks whether chunk matches the beginning of s.
121 // If so, it returns the remainder of s (after the match).
122 // Chunk is all single-character operators: literals, char classes, and ?.
123 func matchChunk(chunk, s string) (rest string, ok bool, err error) {
124 // failed records whether the match has failed.
125 // After the match fails, the loop continues on processing chunk,
126 // checking that the pattern is well-formed but no longer reading s.
129 if !failed && len(s) == 0 {
138 r, n = utf8.DecodeRuneInString(s)
144 if len(chunk) > 0 && chunk[0] == '^' {
152 if len(chunk) > 0 && chunk[0] == ']' && nrange > 0 {
157 if lo, chunk, err = getEsc(chunk); err != nil {
158 return "", false, err
162 if hi, chunk, err = getEsc(chunk[1:]); err != nil {
163 return "", false, err
166 if lo <= r && r <= hi {
171 if match == negated {
180 _, n := utf8.DecodeRuneInString(s)
188 return "", false, ErrBadPattern
194 if chunk[0] != s[0] {
203 return "", false, nil
208 // getEsc gets a possibly-escaped character from chunk, for a character class.
209 func getEsc(chunk string) (r rune, nchunk string, err error) {
210 if len(chunk) == 0 || chunk[0] == '-' || chunk[0] == ']' {
214 if chunk[0] == '\\' {
221 r, n := utf8.DecodeRuneInString(chunk)
222 if r == utf8.RuneError && n == 1 {
226 if len(nchunk) == 0 {