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
38 func Match(pattern
, name
string) (matched
bool, err error
) {
40 for len(pattern
) > 0 {
43 star
, chunk
, pattern
= scanChunk(pattern
)
44 if star
&& chunk
== "" {
45 // Trailing * matches rest of string unless it has a /.
46 return bytealg
.IndexByteString(name
, '/') < 0, nil
48 // Look for match at current position.
49 t
, ok
, err
:= matchChunk(chunk
, name
)
50 // if we're the last chunk, make sure we've exhausted the name
51 // otherwise we'll give a false result even if we could still match
53 if ok
&& (len(t
) == 0 ||
len(pattern
) > 0) {
61 // Look for match skipping i+1 bytes.
63 for i
:= 0; i
< len(name
) && name
[i
] != '/'; i
++ {
64 t
, ok
, err
:= matchChunk(chunk
, name
[i
+1:])
66 // if we're the last chunk, make sure we exhausted the name
67 if len(pattern
) == 0 && len(t
) > 0 {
78 // Before returning false with no error,
79 // check that the remainder of the pattern is syntactically valid.
80 for len(pattern
) > 0 {
81 _
, chunk
, pattern
= scanChunk(pattern
)
82 if _
, _
, err
:= matchChunk(chunk
, ""); err
!= nil {
88 return len(name
) == 0, nil
91 // scanChunk gets the next segment of pattern, which is a non-star string
92 // possibly preceded by a star.
93 func scanChunk(pattern
string) (star
bool, chunk
, rest
string) {
94 for len(pattern
) > 0 && pattern
[0] == '*' {
101 for i
= 0; i
< len(pattern
); i
++ {
104 // error check handled in matchChunk: bad pattern.
105 if i
+1 < len(pattern
) {
118 return star
, pattern
[0:i
], pattern
[i
:]
121 // matchChunk checks whether chunk matches the beginning of s.
122 // If so, it returns the remainder of s (after the match).
123 // Chunk is all single-character operators: literals, char classes, and ?.
124 func matchChunk(chunk
, s
string) (rest
string, ok
bool, err error
) {
125 // failed records whether the match has failed.
126 // After the match fails, the loop continues on processing chunk,
127 // checking that the pattern is well-formed but no longer reading s.
130 if !failed
&& len(s
) == 0 {
139 r
, n
= utf8
.DecodeRuneInString(s
)
145 if len(chunk
) > 0 && chunk
[0] == '^' {
153 if len(chunk
) > 0 && chunk
[0] == ']' && nrange
> 0 {
158 if lo
, chunk
, err
= getEsc(chunk
); err
!= nil {
159 return "", false, err
163 if hi
, chunk
, err
= getEsc(chunk
[1:]); err
!= nil {
164 return "", false, err
167 if lo
<= r
&& r
<= hi
{
172 if match
== negated
{
181 _
, n
:= utf8
.DecodeRuneInString(s
)
189 return "", false, ErrBadPattern
195 if chunk
[0] != s
[0] {
204 return "", false, nil
209 // getEsc gets a possibly-escaped character from chunk, for a character class.
210 func getEsc(chunk
string) (r rune
, nchunk
string, err error
) {
211 if len(chunk
) == 0 || chunk
[0] == '-' || chunk
[0] == ']' {
215 if chunk
[0] == '\\' {
222 r
, n
:= utf8
.DecodeRuneInString(chunk
)
223 if r
== utf8
.RuneError
&& n
== 1 {
227 if len(nchunk
) == 0 {