Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium 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. |
| 4 | |
Todd Wang | 8c4e5cc | 2015-04-09 11:30:52 -0700 | [diff] [blame] | 5 | // Package glob defines a globbing syntax and implements matching routines. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | // |
| 7 | // Globs match a slash separated series of glob expressions. |
| 8 | // |
Todd Wang | 8c4e5cc | 2015-04-09 11:30:52 -0700 | [diff] [blame] | 9 | // // Patterns: |
| 10 | // term ['/' term]* |
| 11 | // term: |
| 12 | // '*' matches any sequence of non-Separator characters |
| 13 | // '?' matches any single non-Separator character |
| 14 | // '[' [ '^' ] { character-range } ']' |
| 15 | // // Character classes (must be non-empty): |
| 16 | // c matches character c (c != '*', '?', '\\', '[', '/') |
| 17 | // '\\' c matches character c |
| 18 | // // Character-ranges: |
| 19 | // c matches character c (c != '\\', '-', ']') |
| 20 | // '\\' c matches character c |
| 21 | // lo '-' hi matches character c for lo <= c <= hi |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 22 | // |
| 23 | // This package is DEPRECATED. Use v.io/v23/glob instead. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 24 | package glob |
| 25 | |
| 26 | import ( |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 27 | "v.io/v23/glob" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 30 | // Glob represents a slash separated path glob pattern. |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 31 | // This type is DEPRECATED. Use v.io/v23/glob.Glob instead. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 32 | type Glob struct { |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 33 | *glob.Glob |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 36 | // Parse returns a new Glob. |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 37 | // This function is DEPRECATED. |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 38 | func Parse(pattern string) (*Glob, error) { |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 39 | g, err := glob.Parse(pattern) |
| 40 | return &Glob{g}, err |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Tail returns the suffix of g starting at the second element. |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 44 | // This method is DEPRECATED. |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 45 | func (g *Glob) Tail() *Glob { |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 46 | return &Glob{g.Glob.Tail()} |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 49 | // Finished returns true if the pattern cannot match anything. |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 50 | // This method is DEPRECATED. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 51 | func (g *Glob) Finished() bool { |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 52 | return g.Empty() |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Adam Sadovsky | 654190e | 2015-05-15 10:31:43 -0700 | [diff] [blame] | 55 | // Split returns the suffix of g starting at the path element corresponding to |
| 56 | // start. |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 57 | // This method is DEPRECATED. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 58 | func (g *Glob) Split(start int) *Glob { |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 59 | suffix := g |
| 60 | for i := start - 1; i >= 0; i-- { |
| 61 | suffix = suffix.Tail() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 62 | } |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 63 | return suffix |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // MatchInitialSegment tries to match segment against the initial element of g. |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 67 | // Returns: |
| 68 | // matched, a boolean indicating whether the match was successful; |
| 69 | // exact, a boolean indicating whether segment matched a fixed string pattern; |
| 70 | // remainder, a Glob representing the unmatched remainder of g. |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 71 | // This method is DEPRECATED. |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 72 | func (g *Glob) MatchInitialSegment(segment string) (matched bool, exact bool, remainder *Glob) { |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 73 | m := g.Head() |
| 74 | matched = m.Match(segment) |
| 75 | _, exact = m.FixedPrefix() |
| 76 | remainder = g.Tail() |
| 77 | return |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // PartialMatch tries matching elems against part of a glob pattern. |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 81 | // Returns: |
| 82 | // matched, a boolean indicating whether each element e_i of elems matches the |
| 83 | // (start + i)th element of the glob pattern; |
Tilak Sharma | 9cb954f | 2014-10-09 09:49:36 -0700 | [diff] [blame] | 84 | // exact, a boolean indicating whether elems matched a fixed string pattern. |
| 85 | // <path> is considered an exact match for pattern <path>/...; |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 86 | // remainder, a Glob representing the unmatched remainder of g. remainder will |
| 87 | // be empty if the pattern is completely matched. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 88 | // |
| 89 | // Note that if the glob is recursive elems can have more elements then |
| 90 | // the glob pattern and still get a true result. |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 91 | // This method is DEPRECATED. |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 92 | 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] | 93 | g = g.Split(start) |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 94 | allExact := true |
| 95 | for i := 0; i < len(elems); i++ { |
| 96 | var matched, exact bool |
| 97 | if matched, exact, g = g.MatchInitialSegment(elems[i]); !matched { |
| 98 | return false, false, nil |
| 99 | } else if !exact { |
| 100 | allExact = false |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 103 | return true, allExact, g |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Robin Thellend | 40c01b2 | 2015-07-15 10:44:41 -0700 | [diff] [blame] | 106 | // SplitFixedElements returns the part of the glob pattern that contains only |
| 107 | // fixed elements, and the glob that follows it. |
| 108 | // This method is DEPRECATED. |
| 109 | func (g *Glob) SplitFixedElements() ([]string, *Glob) { |
| 110 | prefix, left := g.Glob.SplitFixedElements() |
| 111 | return prefix, &Glob{left} |
| 112 | } |
| 113 | |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 114 | // SplitFixedPrefix returns the part of the glob pattern that contains only |
| 115 | // fixed elements, and the glob that follows it. |
| 116 | // This method is DEPRECATED. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 117 | func (g *Glob) SplitFixedPrefix() ([]string, *Glob) { |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 118 | return g.SplitFixedElements() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 119 | } |