pattern

package
v0.0.0-...-d1dce75 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package pattern implements a language for specifying glob patterns for path names starting at some root. The language does not follow the specs from filepath.Match but differs in one major point: it allows for directory wildcards.

Patterns consist of normal characters, non-separator wildcards '*' and '?', separators '/' and directory wildcards '**'.

A somewhat formal grammer can be given as:

pattern -> term ('/' term)*
term    -> '**' // directory wildcard: matches any directory
term    -> name
name    -> (char | '*' | '?')+
char    -> <any character except '/', '*' or '?'>

Index

Constants

View Source
const (
	// Separator defines the path separator to use in patterns. This is always
	// a forward slash independently of the underlying's OS separator
	Separator = '/'
	// SingleWildcard defines the the single non-separator character wildcard
	// operator.
	SingleWildcard = '?'
	// AnyWildcard defines the the any number of non-separator characters
	// wildcard operator.
	AnyWildcard = '*'
	// Backslash escapes the next character's special meaning
	Backslash = '\\'
	// GroupStart starts a range
	GroupStart = '['
	// GroupEnd starts a range
	GroupEnd = ']'
	// GroupNegate when used as the first character of a group negates the group.
	GroupNegate = '^'
	// Range defines the range operator
	Range = '-'
)

Variables

View Source
var (
	// ErrBadPattern is returned when an invalid pattern is found. Make
	// sure you use errors.Is to compare errors to this sentinel value.
	ErrBadPattern = errors.New("bad pattern")
)

Functions

This section is empty.

Types

type Pattern

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

Pattern defines a glob pattern prepared ahead of time which can be used to match filenames. Pattern is safe to use concurrently.

func New

func New(pat string) (*Pattern, error)

New creates a new pattern from pat and returns it. It returns an error indicating any invalid pattern.

func (*Pattern) GlobFS

func (pat *Pattern) GlobFS(fsys fs.FS, root string) ([]string, error)

GlobFS applies pat to all files found in fsys under root and returns the matching path names as a string slice. It uses fs.WalkDir internally and all constraints given for that function apply to GlobFS.

func (*Pattern) Match

func (pat *Pattern) Match(f string) bool

Match matches a file's path name f to the compiled pattern and returns whether the path matches the pattern or not.

Jump to

Keyboard shortcuts

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