(lib|runtime|services): Use v.io/v23/glob
With this change, everything now uses the v.io/v23/glob package instead
of v.io/x/ref/lib/glob.
The old glob package is now deprecated and will be removed soon.
Parly addresses: v.io/i/532
MultiPart: 2/2
Change-Id: I1189f66df2163d8c0995b4f755c023f70852012b
diff --git a/lib/glob/glob.go b/lib/glob/glob.go
index d7fa168..6bbb111 100644
--- a/lib/glob/glob.go
+++ b/lib/glob/glob.go
@@ -19,199 +19,33 @@
// c matches character c (c != '\\', '-', ']')
// '\\' c matches character c
// lo '-' hi matches character c for lo <= c <= hi
+//
+// This package is DEPRECATED. Use v.io/v23/glob instead.
package glob
import (
- "path"
- "strings"
+ "v.io/v23/glob"
)
// Glob represents a slash separated path glob pattern.
+// This type is DEPRECATED. Use v.io/v23/glob.Glob instead.
type Glob struct {
- elems []*Element
- recursive bool
- restricted bool
+ *glob.Glob
}
// Parse returns a new Glob.
+// This function is DEPRECATED.
func Parse(pattern string) (*Glob, error) {
- if len(pattern) > 0 && pattern[0] == '/' {
- return nil, path.ErrBadPattern
- }
-
- g := &Glob{}
- if pattern == "" {
- return g, nil
- }
-
- elems := strings.Split(pattern, "/")
- if last := len(elems) - 1; last >= 0 {
- if elems[last] == "..." {
- elems = elems[:last]
- g.recursive = true
- } else if elems[last] == "***" {
- elems = elems[:last]
- g.recursive = true
- g.restricted = true
- }
- }
- g.elems = make([]*Element, len(elems))
- for i, elem := range elems {
- g.elems[i] = &Element{pattern: elem}
- if err := g.elems[i].validate(); err != nil {
- return nil, err
- }
- }
-
- return g, nil
-}
-
-// Len returns the number of path elements represented by the glob expression.
-func (g *Glob) Len() int {
- return len(g.elems)
-}
-
-// Empty returns true if the pattern cannot match anything.
-func (g *Glob) Empty() bool {
- return !g.recursive && len(g.elems) == 0
-}
-
-// Recursive returns true if the pattern is recursive.
-func (g *Glob) Recursive() bool {
- return g.recursive
-}
-
-// Restricted returns true if recursion is restricted (up to the caller to
-// know what that means).
-func (g *Glob) Restricted() bool {
- return g.restricted
+ g, err := glob.Parse(pattern)
+ return &Glob{g}, err
}
// Tail returns the suffix of g starting at the second element.
+// This method is DEPRECATED.
func (g *Glob) Tail() *Glob {
- if len(g.elems) <= 1 {
- return &Glob{elems: nil, recursive: g.recursive, restricted: g.restricted}
- }
- return &Glob{elems: g.elems[1:], recursive: g.recursive, restricted: g.restricted}
+ return &Glob{g.Glob.Tail()}
}
-// Head returns an Element for the first element of the glob pattern.
-func (g *Glob) Head() *Element {
- if len(g.elems) == 0 {
- if g.recursive {
- return &Element{alwaysMatch: true}
- }
- return &Element{neverMatch: true}
- }
- return g.elems[0]
-}
-
-// SplitFixedElements returns the part of the glob pattern that contains only
-// fixed elements, and the glob that follows it.
-func (g *Glob) SplitFixedElements() ([]string, *Glob) {
- var prefix []string
- tail := g
- for _, elem := range g.elems {
- if pfx, fixed := elem.FixedPrefix(); fixed {
- prefix = append(prefix, pfx)
- tail = tail.Tail()
- } else {
- break
- }
- }
- return prefix, tail
-}
-
-// String returns the string representation of the glob pattern.
-func (g *Glob) String() string {
- elems := make([]string, len(g.elems))
- for i, e := range g.elems {
- elems[i] = e.pattern
- }
- if g.recursive {
- if g.restricted {
- elems = append(elems, "***")
- } else {
- elems = append(elems, "...")
- }
- }
- return path.Join(elems...)
-}
-
-// Element represents a single element of a glob pattern.
-type Element struct {
- pattern string
- alwaysMatch bool
- neverMatch bool
-}
-
-// Match returns true iff this pattern element matches the given segment.
-func (m *Element) Match(segment string) bool {
- if m.neverMatch {
- return false
- }
- if m.alwaysMatch {
- return true
- }
- matches, err := path.Match(m.pattern, segment)
- return err == nil && matches
-}
-
-// FixedPrefix returns the unescaped fixed part of the pattern, and whether the
-// prefix is the whole pattern. The fixed part does not contain any wildcards.
-func (m *Element) FixedPrefix() (string, bool) {
- if m.neverMatch {
- return "", true
- }
- if m.alwaysMatch {
- return "", false
- }
- unescaped := ""
- escape := false
- for _, c := range m.pattern {
- if escape {
- escape = false
- unescaped += string(c)
- } else if strings.ContainsRune("*?[", c) {
- return unescaped, false
- } else if c == '\\' {
- escape = true
- } else {
- unescaped += string(c)
- }
- }
- return unescaped, true
-}
-
-func (m *Element) validate() error {
- if len(m.pattern) == 0 {
- return path.ErrBadPattern
- }
- escape := false
- inrange := false
- for _, c := range m.pattern {
- if escape {
- escape = false
- continue
- }
- switch c {
- case '\\':
- escape = true
- case '[':
- inrange = true
- case ']':
- inrange = false
- }
- }
- // If we are in the middle of an escape or character range, the expression is incomplete.
- if escape || inrange {
- return path.ErrBadPattern
- }
- return nil
-}
-
-// DEPRECATED CODE
-
// Finished returns true if the pattern cannot match anything.
// This method is DEPRECATED.
func (g *Glob) Finished() bool {
@@ -222,10 +56,11 @@
// start.
// This method is DEPRECATED.
func (g *Glob) Split(start int) *Glob {
- if start >= len(g.elems) {
- return &Glob{elems: nil, recursive: g.recursive, restricted: g.restricted}
+ suffix := g
+ for i := start - 1; i >= 0; i-- {
+ suffix = suffix.Tail()
}
- return &Glob{elems: g.elems[start:], recursive: g.recursive, restricted: g.restricted}
+ return suffix
}
// MatchInitialSegment tries to match segment against the initial element of g.
@@ -268,6 +103,14 @@
return true, allExact, g
}
+// SplitFixedElements returns the part of the glob pattern that contains only
+// fixed elements, and the glob that follows it.
+// This method is DEPRECATED.
+func (g *Glob) SplitFixedElements() ([]string, *Glob) {
+ prefix, left := g.Glob.SplitFixedElements()
+ return prefix, &Glob{left}
+}
+
// SplitFixedPrefix returns the part of the glob pattern that contains only
// fixed elements, and the glob that follows it.
// This method is DEPRECATED.