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 globbing pattern was malformed.
14 var ErrBadPattern
= errors
.New("syntax error in pattern")
16 // Match returns true if name matches the shell file name 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 strings
.Index(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 {
80 return len(name
) == 0, nil
83 // scanChunk gets the next segment of pattern, which is a non-star string
84 // possibly preceded by a star.
85 func scanChunk(pattern
string) (star
bool, chunk
, rest
string) {
86 for len(pattern
) > 0 && pattern
[0] == '*' {
93 for i
= 0; i
< len(pattern
); i
++ {
96 // error check handled in matchChunk: bad pattern.
97 if i
+1 < len(pattern
) {
110 return star
, pattern
[0:i
], pattern
[i
:]
113 // matchChunk checks whether chunk matches the beginning of s.
114 // If so, it returns the remainder of s (after the match).
115 // Chunk is all single-character operators: literals, char classes, and ?.
116 func matchChunk(chunk
, s
string) (rest
string, ok
bool, err error
) {
124 r
, n
:= utf8
.DecodeRuneInString(s
)
129 if len(chunk
) > 0 && chunk
[0] == '^' {
137 if len(chunk
) > 0 && chunk
[0] == ']' && nrange
> 0 {
142 if lo
, chunk
, err
= getEsc(chunk
); err
!= nil {
147 if hi
, chunk
, err
= getEsc(chunk
[1:]); err
!= nil {
151 if lo
<= r
&& r
<= hi
{
156 if match
!= notNegated
{
164 _
, n
:= utf8
.DecodeRuneInString(s
)
177 if chunk
[0] != s
[0] {
187 // getEsc gets a possibly-escaped character from chunk, for a character class.
188 func getEsc(chunk
string) (r rune
, nchunk
string, err error
) {
189 if len(chunk
) == 0 || chunk
[0] == '-' || chunk
[0] == ']' {
193 if chunk
[0] == '\\' {
200 r
, n
:= utf8
.DecodeRuneInString(chunk
)
201 if r
== utf8
.RuneError
&& n
== 1 {
205 if len(nchunk
) == 0 {