blob: 6bbb11126257c8e4ee041a245e20a67139d93333 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// 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 Wang8c4e5cc2015-04-09 11:30:52 -07005// Package glob defines a globbing syntax and implements matching routines.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07006//
7// Globs match a slash separated series of glob expressions.
8//
Todd Wang8c4e5cc2015-04-09 11:30:52 -07009// // 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 Thellend40c01b22015-07-15 10:44:41 -070022//
23// This package is DEPRECATED. Use v.io/v23/glob instead.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070024package glob
25
26import (
Robin Thellend40c01b22015-07-15 10:44:41 -070027 "v.io/v23/glob"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070028)
29
Robin Thellend2f1878c2015-07-14 14:18:47 -070030// Glob represents a slash separated path glob pattern.
Robin Thellend40c01b22015-07-15 10:44:41 -070031// This type is DEPRECATED. Use v.io/v23/glob.Glob instead.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070032type Glob struct {
Robin Thellend40c01b22015-07-15 10:44:41 -070033 *glob.Glob
Jiri Simsa5293dcb2014-05-10 09:56:38 -070034}
35
Robin Thellend2f1878c2015-07-14 14:18:47 -070036// Parse returns a new Glob.
Robin Thellend40c01b22015-07-15 10:44:41 -070037// This function is DEPRECATED.
Robin Thellend2f1878c2015-07-14 14:18:47 -070038func Parse(pattern string) (*Glob, error) {
Robin Thellend40c01b22015-07-15 10:44:41 -070039 g, err := glob.Parse(pattern)
40 return &Glob{g}, err
Robin Thellend2f1878c2015-07-14 14:18:47 -070041}
42
43// Tail returns the suffix of g starting at the second element.
Robin Thellend40c01b22015-07-15 10:44:41 -070044// This method is DEPRECATED.
Robin Thellend2f1878c2015-07-14 14:18:47 -070045func (g *Glob) Tail() *Glob {
Robin Thellend40c01b22015-07-15 10:44:41 -070046 return &Glob{g.Glob.Tail()}
Robin Thellend2f1878c2015-07-14 14:18:47 -070047}
48
Jiri Simsa5293dcb2014-05-10 09:56:38 -070049// Finished returns true if the pattern cannot match anything.
Robin Thellend2f1878c2015-07-14 14:18:47 -070050// This method is DEPRECATED.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070051func (g *Glob) Finished() bool {
Robin Thellend2f1878c2015-07-14 14:18:47 -070052 return g.Empty()
David Why Use Two When One Will Do Presottoc28686e2014-11-05 11:19:29 -080053}
54
Adam Sadovsky654190e2015-05-15 10:31:43 -070055// Split returns the suffix of g starting at the path element corresponding to
56// start.
Robin Thellend2f1878c2015-07-14 14:18:47 -070057// This method is DEPRECATED.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070058func (g *Glob) Split(start int) *Glob {
Robin Thellend40c01b22015-07-15 10:44:41 -070059 suffix := g
60 for i := start - 1; i >= 0; i-- {
61 suffix = suffix.Tail()
Jiri Simsa5293dcb2014-05-10 09:56:38 -070062 }
Robin Thellend40c01b22015-07-15 10:44:41 -070063 return suffix
Jiri Simsa5293dcb2014-05-10 09:56:38 -070064}
65
66// MatchInitialSegment tries to match segment against the initial element of g.
Tilak Sharma577ce8d2014-09-22 10:25:00 -070067// 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 Thellend2f1878c2015-07-14 14:18:47 -070071// This method is DEPRECATED.
Tilak Sharma577ce8d2014-09-22 10:25:00 -070072func (g *Glob) MatchInitialSegment(segment string) (matched bool, exact bool, remainder *Glob) {
Robin Thellend2f1878c2015-07-14 14:18:47 -070073 m := g.Head()
74 matched = m.Match(segment)
75 _, exact = m.FixedPrefix()
76 remainder = g.Tail()
77 return
Jiri Simsa5293dcb2014-05-10 09:56:38 -070078}
79
80// PartialMatch tries matching elems against part of a glob pattern.
Tilak Sharma577ce8d2014-09-22 10:25:00 -070081// 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 Sharma9cb954f2014-10-09 09:49:36 -070084// exact, a boolean indicating whether elems matched a fixed string pattern.
85// <path> is considered an exact match for pattern <path>/...;
Tilak Sharma577ce8d2014-09-22 10:25:00 -070086// remainder, a Glob representing the unmatched remainder of g. remainder will
87// be empty if the pattern is completely matched.
Jiri Simsa5293dcb2014-05-10 09:56:38 -070088//
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 Thellend2f1878c2015-07-14 14:18:47 -070091// This method is DEPRECATED.
Tilak Sharma577ce8d2014-09-22 10:25:00 -070092func (g *Glob) PartialMatch(start int, elems []string) (matched bool, exact bool, remainder *Glob) {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070093 g = g.Split(start)
Tilak Sharma577ce8d2014-09-22 10:25:00 -070094 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 Simsa5293dcb2014-05-10 09:56:38 -0700101 }
102 }
Tilak Sharma577ce8d2014-09-22 10:25:00 -0700103 return true, allExact, g
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700104}
105
Robin Thellend40c01b22015-07-15 10:44:41 -0700106// 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.
109func (g *Glob) SplitFixedElements() ([]string, *Glob) {
110 prefix, left := g.Glob.SplitFixedElements()
111 return prefix, &Glob{left}
112}
113
Robin Thellend2f1878c2015-07-14 14:18:47 -0700114// 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 Simsa5293dcb2014-05-10 09:56:38 -0700117func (g *Glob) SplitFixedPrefix() ([]string, *Glob) {
Robin Thellend2f1878c2015-07-14 14:18:47 -0700118 return g.SplitFixedElements()
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700119}