blob: 94453f902fd14e54d6565620ef9a67e266882693 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001// glob implements a glob language.
2//
3// Globs match a slash separated series of glob expressions.
4//
5// pattern:
6// term ['/' term]*
7// term:
8// '*' matches any sequence of non-Separator characters
9// '?' matches any single non-Separator character
10// '[' [ '^' ] { character-range } ']'
11// character class (must be non-empty)
12// c matches character c (c != '*', '?', '\\', '[', '/')
13// '\\' c matches character c
14// character-range:
15// c matches character c (c != '\\', '-', ']')
16// '\\' c matches character c
17// lo '-' hi matches character c for lo <= c <= hi
18
19package glob
20
21import (
22 "path/filepath"
23 "strings"
24)
25
26// Glob represents a slash separated path glob expression.
27type Glob struct {
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -080028 elems []string
29 recursive bool
30 restricted bool
Jiri Simsa5293dcb2014-05-10 09:56:38 -070031}
32
33// Parse returns a new Glob.
34func Parse(pattern string) (*Glob, error) {
35 if len(pattern) > 0 && pattern[0] == '/' {
36 return nil, filepath.ErrBadPattern
37 }
38
39 g := &Glob{}
40 if pattern != "" {
41 g.elems = strings.Split(pattern, "/")
42 }
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -080043 if last := len(g.elems) - 1; last >= 0 {
44 if g.elems[last] == "..." {
45 g.elems = g.elems[:last]
46 g.recursive = true
47 } else if g.elems[last] == "***" {
48 g.elems = g.elems[:last]
49 g.recursive = true
50 g.restricted = true
51 }
Jiri Simsa5293dcb2014-05-10 09:56:38 -070052 }
53
54 // The only error we can get from the filepath library is badpattern.
55 // A future implementation would most likely recognize that here, so for now
56 // I'll just check every part to make sure it's error free.
Robin Thellendea11fa42014-10-17 11:37:18 -070057 // Note: Match never returns an error when matching against an empty string.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070058 for _, elem := range g.elems {
Robin Thellendea11fa42014-10-17 11:37:18 -070059 if _, err := filepath.Match(elem, "test"); err != nil {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070060 return nil, err
61 }
62 }
63
64 return g, nil
65}
66
67// Len returns the number of path elements represented by the glob expression.
68func (g *Glob) Len() int {
69 return len(g.elems)
70}
71
72// Finished returns true if the pattern cannot match anything.
73func (g *Glob) Finished() bool {
74 return !g.recursive && len(g.elems) == 0
75}
76
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -080077// Restricted returns true if recursion is restricted (up to the caller to
78// know what that means).
79func (g *Glob) Restricted() bool {
80 return g.restricted
81}
82
Jiri Simsa5293dcb2014-05-10 09:56:38 -070083// Split returns the suffix of g starting at the path element corresponding to start.
84func (g *Glob) Split(start int) *Glob {
85 if start >= len(g.elems) {
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -080086 return &Glob{elems: nil, recursive: g.recursive, restricted: g.restricted}
Jiri Simsa5293dcb2014-05-10 09:56:38 -070087 }
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -080088 return &Glob{elems: g.elems[start:], recursive: g.recursive, restricted: g.restricted}
Jiri Simsa5293dcb2014-05-10 09:56:38 -070089}
90
91// MatchInitialSegment tries to match segment against the initial element of g.
Tilak Sharma577ce8d2014-09-22 10:25:00 -070092// Returns:
93// matched, a boolean indicating whether the match was successful;
94// exact, a boolean indicating whether segment matched a fixed string pattern;
95// remainder, a Glob representing the unmatched remainder of g.
96func (g *Glob) MatchInitialSegment(segment string) (matched bool, exact bool, remainder *Glob) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070097 if len(g.elems) == 0 {
98 if !g.recursive {
Tilak Sharma577ce8d2014-09-22 10:25:00 -070099 return false, false, nil
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700100 }
Tilak Sharma9cb954f2014-10-09 09:49:36 -0700101 // The segment matches "...". This is not an exact match.
102 return true, false, g
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700103 }
104
105 if matches, err := filepath.Match(g.elems[0], segment); err != nil {
106 panic("Error in glob pattern found.")
107 } else if matches {
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700108 _, fixed := isFixed(g.elems[0])
109 return true, fixed, g.Split(1)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700110 }
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700111 return false, false, nil
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700112}
113
114// PartialMatch tries matching elems against part of a glob pattern.
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700115// Returns:
116// matched, a boolean indicating whether each element e_i of elems matches the
117// (start + i)th element of the glob pattern;
Tilak Sharma9cb954f2014-10-09 09:49:36 -0700118// exact, a boolean indicating whether elems matched a fixed string pattern.
119// <path> is considered an exact match for pattern <path>/...;
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700120// remainder, a Glob representing the unmatched remainder of g. remainder will
121// be empty if the pattern is completely matched.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700122//
123// Note that if the glob is recursive elems can have more elements then
124// the glob pattern and still get a true result.
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700125func (g *Glob) PartialMatch(start int, elems []string) (matched bool, exact bool, remainder *Glob) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700126 g = g.Split(start)
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700127 allExact := true
128 for i := 0; i < len(elems); i++ {
129 var matched, exact bool
130 if matched, exact, g = g.MatchInitialSegment(elems[i]); !matched {
131 return false, false, nil
132 } else if !exact {
133 allExact = false
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700134 }
135 }
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700136 return true, allExact, g
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700137}
138
139// isFixed returns the unescaped string and true if 's' is a pattern specifying
140// a fixed string. Otherwise it returns the original string and false.
141func isFixed(s string) (string, bool) {
142 // No special characters.
143 if !strings.ContainsAny(s, "*?[") {
144 return s, true
145 }
146 // Special characters and no backslash.
147 if !strings.ContainsAny(s, "\\") {
148 return "", false
149 }
150 unescaped := ""
151 escape := false
152 for _, c := range s {
153 if escape {
154 escape = false
155 unescaped += string(c)
156 } else if strings.ContainsRune("*?[", c) {
157 // S contains an unescaped special character.
158 return s, false
159 } else if c == '\\' {
160 escape = true
161 } else {
162 unescaped += string(c)
163 }
164 }
165 return unescaped, true
166}
167
168func (g *Glob) SplitFixedPrefix() ([]string, *Glob) {
169 var prefix []string
170 start := 0
171 for _, elem := range g.elems {
172 if u, q := isFixed(elem); q {
173 prefix = append(prefix, u)
174 start++
175 } else {
176 break
177 }
178 }
179 return prefix, g.Split(start)
180}
181
182func (g *Glob) String() string {
183 e := g.elems
184 if g.recursive {
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -0800185 if g.restricted {
186 e = append(e, "***")
187 } else {
188 e = append(e, "...")
189 }
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700190 }
191 return filepath.Join(e...)
192}