glob

package module
v0.0.0-...-1eb79d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2016 License: BSD-3-Clause Imports: 5 Imported by: 60

README

glob

A configurable globbing and matching algorithm for go.

The package is based on the globbing and matching code from package path/filepath.

Documentation at http://godoc.org/github.com/mb0/glob

Documentation

Overview

Package glob provides configurable globbing and matching algorithms. It can be used to for glob-style matching on string slices.

Index

Constants

This section is empty.

Variables

View Source
var ErrBadPattern = errors.New("syntax error in pattern")

ErrBadPattern indicates a globbing pattern was malformed.

Functions

func GlobStrings

func GlobStrings(list []string, pattern string) (matches []string, err error)

GlobStrings returns all expanded paths from list matching pattern or nil if there are no matches. This function calls the corresponding method of a Globber with default Config and is equivalent to:

glob.New(glob.Default()).GlobStrings(list, pattern)

func Match

func Match(pattern, name string) (bool, error)

Match returns true if name matches the shell file name pattern. This function calls the corresponding method of a Globber with default Config and is equivalent to:

glob.New(glob.Default()).Match(pattern, name)

Types

type Config

type Config struct {
	Separator byte // Separator separates path element
	Star      byte // Star wildcard matching zero or more characters
	Quest     byte // Quest wildcard matching one character
	Range     byte // Range start matches ranges one character from a specified range
	RangeEnd  byte // RangeEnd ends a Range. Ranges of characters can be specified with '-' e.g. [0-9]
	RangeNeg  byte // RangeNeg negates the Range e.g. [^a-z]
	GlobStar  bool // GlobStar enables multiple Stars to match across directory boundaries
}

Config represents Globber options. It can be used to change the supported pattern control characters and Separator as well as enable GlobStar behaviour.

func Default

func Default() Config

Default returns the Config used by the package-level default Globber. The values are:

Config{
        Separator: '/',
        Star:      '*',
        Quest:     '?',
        Range:     '[',
        RangeEnd:  ']',
        RangeNeg:  '^',
        GlobStar:  true,
}

type Globber

type Globber struct {
	// contains filtered or unexported fields
}

Globber provides configurable globbing and matching algorithms.

func New

func New(c Config) (*Globber, error)

New returns a new Globber with the given Config c.

func (*Globber) GlobStrings

func (g *Globber) GlobStrings(list []string, pattern string) (matches []string, err error)

GlobStrings returns all expanded paths from list matching pattern or nil if there are no matches. The syntax of pattern is the same as in Match. The control characters of the pattern and GlobStar matching behaviour can be configured in Config. The given list must be sorted in ascending order. New matches are added in lexicographical order.

func (*Globber) Match

func (g *Globber) Match(pattern, name string) (bool, error)

Match returns true if name matches the shell file name pattern. The control characters can be changed with glob.Config The pattern syntax is:

pattern:
        { term }
term:
        Star         matches any sequence of non-Separator characters
        Star+        matches across directory Separators if GlobStar is enabled
        Quest        matches any single non-Separator character
        Range [ RangeNeg ] { character-range } RangeEnd
                    character class (must be non-empty)
        c           matches character c (c != '*', '?', '\\', '[')
        '\\' c      matches character c

character-range:
        c           matches character c (c != '\\', '-', RangeEnd)
        '\\' c      matches character c
        lo '-' hi   matches character c for lo <= c <= hi

Match requires pattern to match all of name, not just a substring. The only possible returned error is ErrBadPattern, when pattern is malformed.

On Windows, escaping is disabled. Instead, '\\' is treated as path separator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL