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.
16 // ErrBadPattern indicates a globbing pattern was malformed.
17 var ErrBadPattern
= errors
.New("syntax error in pattern")
19 // Match returns true if name matches the shell file name pattern.
20 // The pattern syntax is:
25 // '*' matches any sequence of non-Separator characters
26 // '?' matches any single non-Separator character
27 // '[' [ '^' ] { character-range } ']'
28 // character class (must be non-empty)
29 // c matches character c (c != '*', '?', '\\', '[')
30 // '\\' c matches character c
33 // c matches character c (c != '\\', '-', ']')
34 // '\\' c matches character c
35 // lo '-' hi matches character c for lo <= c <= hi
37 // Match requires pattern to match all of name, not just a substring.
38 // The only possible returned error is ErrBadPattern, when pattern
41 // On Windows, escaping is disabled. Instead, '\\' is treated as
44 func Match(pattern
, name
string) (matched
bool, err error
) {
46 for len(pattern
) > 0 {
49 star
, chunk
, pattern
= scanChunk(pattern
)
50 if star
&& chunk
== "" {
51 // Trailing * matches rest of string unless it has a /.
52 return strings
.Index(name
, string(Separator
)) < 0, nil
54 // Look for match at current position.
55 t
, ok
, err
:= matchChunk(chunk
, name
)
56 // if we're the last chunk, make sure we've exhausted the name
57 // otherwise we'll give a false result even if we could still match
59 if ok
&& (len(t
) == 0 ||
len(pattern
) > 0) {
67 // Look for match skipping i+1 bytes.
69 for i
:= 0; i
< len(name
) && name
[i
] != Separator
; i
++ {
70 t
, ok
, err
:= matchChunk(chunk
, name
[i
+1:])
72 // if we're the last chunk, make sure we exhausted the name
73 if len(pattern
) == 0 && len(t
) > 0 {
86 return len(name
) == 0, nil
89 // scanChunk gets the next segment of pattern, which is a non-star string
90 // possibly preceded by a star.
91 func scanChunk(pattern
string) (star
bool, chunk
, rest
string) {
92 for len(pattern
) > 0 && pattern
[0] == '*' {
99 for i
= 0; i
< len(pattern
); i
++ {
102 if runtime
.GOOS
!= "windows" {
103 // error check handled in matchChunk: bad pattern.
104 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
) {
132 r
, n
:= utf8
.DecodeRuneInString(s
)
135 // We can't end right after '[', we're expecting at least
136 // a closing bracket and possibly a caret.
142 negated
:= chunk
[0] == '^'
150 if len(chunk
) > 0 && chunk
[0] == ']' && nrange
> 0 {
155 if lo
, chunk
, err
= getEsc(chunk
); err
!= nil {
160 if hi
, chunk
, err
= getEsc(chunk
[1:]); err
!= nil {
164 if lo
<= r
&& r
<= hi
{
169 if match
== negated
{
174 if s
[0] == Separator
{
177 _
, n
:= utf8
.DecodeRuneInString(s
)
182 if runtime
.GOOS
!= "windows" {
192 if chunk
[0] != s
[0] {
202 // getEsc gets a possibly-escaped character from chunk, for a character class.
203 func getEsc(chunk
string) (r rune
, nchunk
string, err error
) {
204 if len(chunk
) == 0 || chunk
[0] == '-' || chunk
[0] == ']' {
208 if chunk
[0] == '\\' && runtime
.GOOS
!= "windows" {
215 r
, n
:= utf8
.DecodeRuneInString(chunk
)
216 if r
== utf8
.RuneError
&& n
== 1 {
220 if len(nchunk
) == 0 {
226 // Glob returns the names of all files matching pattern or nil
227 // if there is no matching file. The syntax of patterns is the same
228 // as in Match. The pattern may describe hierarchical names such as
229 // /usr/*/bin/ed (assuming the Separator is '/').
231 func Glob(pattern
string) (matches
[]string, err error
) {
232 if !hasMeta(pattern
) {
233 if _
, err
= os
.Lstat(pattern
); err
!= nil {
236 return []string{pattern
}, nil
239 dir
, file
:= Split(pattern
)
243 case string(Separator
):
246 dir
= dir
[0 : len(dir
)-1] // chop off trailing separator
250 return glob(dir
, file
, nil)
258 for _
, d
:= range m
{
259 matches
, err
= glob(d
, file
, matches
)
267 // glob searches for files matching pattern in the directory dir
268 // and appends them to matches. If the directory cannot be
269 // opened, it returns the existing matches. New matches are
270 // added in lexicographical order.
271 func glob(dir
, pattern
string, matches
[]string) (m
[]string, e error
) {
273 fi
, err
:= os
.Stat(dir
)
280 d
, err
:= os
.Open(dir
)
286 names
, err
:= d
.Readdirnames(-1)
292 for _
, n
:= range names
{
293 matched
, err
:= Match(pattern
, n
)
298 m
= append(m
, Join(dir
, n
))
304 // hasMeta returns true if path contains any of the magic characters
305 // recognized by Match.
306 func hasMeta(path
string) bool {
307 // TODO(niemeyer): Should other magic characters be added here?
308 return strings
.IndexAny(path
, "*?[") >= 0