Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | // 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 | |
| 19 | package glob |
| 20 | |
| 21 | import ( |
| 22 | "path/filepath" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | // Glob represents a slash separated path glob expression. |
| 27 | type Glob struct { |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 28 | elems []string |
| 29 | recursive bool |
| 30 | restricted bool |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Parse returns a new Glob. |
| 34 | func 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 Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 43 | 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 Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 52 | } |
| 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 Thellend | ea11fa4 | 2014-10-17 11:37:18 -0700 | [diff] [blame] | 57 | // Note: Match never returns an error when matching against an empty string. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 58 | for _, elem := range g.elems { |
Robin Thellend | ea11fa4 | 2014-10-17 11:37:18 -0700 | [diff] [blame] | 59 | if _, err := filepath.Match(elem, "test"); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 60 | 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. |
| 68 | func (g *Glob) Len() int { |
| 69 | return len(g.elems) |
| 70 | } |
| 71 | |
| 72 | // Finished returns true if the pattern cannot match anything. |
| 73 | func (g *Glob) Finished() bool { |
| 74 | return !g.recursive && len(g.elems) == 0 |
| 75 | } |
| 76 | |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 77 | // Restricted returns true if recursion is restricted (up to the caller to |
| 78 | // know what that means). |
| 79 | func (g *Glob) Restricted() bool { |
| 80 | return g.restricted |
| 81 | } |
| 82 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 83 | // Split returns the suffix of g starting at the path element corresponding to start. |
| 84 | func (g *Glob) Split(start int) *Glob { |
| 85 | if start >= len(g.elems) { |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 86 | return &Glob{elems: nil, recursive: g.recursive, restricted: g.restricted} |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | } |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 88 | return &Glob{elems: g.elems[start:], recursive: g.recursive, restricted: g.restricted} |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // MatchInitialSegment tries to match segment against the initial element of g. |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 92 | // 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. |
| 96 | func (g *Glob) MatchInitialSegment(segment string) (matched bool, exact bool, remainder *Glob) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 97 | if len(g.elems) == 0 { |
| 98 | if !g.recursive { |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 99 | return false, false, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 100 | } |
Tilak Sharma | 9cb954f | 2014-10-09 09:49:36 -0700 | [diff] [blame] | 101 | // The segment matches "...". This is not an exact match. |
| 102 | return true, false, g |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 103 | } |
| 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 Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 108 | _, fixed := isFixed(g.elems[0]) |
| 109 | return true, fixed, g.Split(1) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 110 | } |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 111 | return false, false, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // PartialMatch tries matching elems against part of a glob pattern. |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 115 | // 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 Sharma | 9cb954f | 2014-10-09 09:49:36 -0700 | [diff] [blame] | 118 | // exact, a boolean indicating whether elems matched a fixed string pattern. |
| 119 | // <path> is considered an exact match for pattern <path>/...; |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 120 | // remainder, a Glob representing the unmatched remainder of g. remainder will |
| 121 | // be empty if the pattern is completely matched. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 122 | // |
| 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 Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 125 | func (g *Glob) PartialMatch(start int, elems []string) (matched bool, exact bool, remainder *Glob) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 126 | g = g.Split(start) |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 127 | 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 Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 136 | return true, allExact, g |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 137 | } |
| 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. |
| 141 | func 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 | |
| 168 | func (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 | |
| 182 | func (g *Glob) String() string { |
| 183 | e := g.elems |
| 184 | if g.recursive { |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 185 | if g.restricted { |
| 186 | e = append(e, "***") |
| 187 | } else { |
| 188 | e = append(e, "...") |
| 189 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 190 | } |
| 191 | return filepath.Join(e...) |
| 192 | } |