Documentation
¶
Overview ¶
Package pattern matches the patterns of the shell, and expands a pattern into the names of files.
A pattern is the text of a case item, of the right side of == in [[ ]], of the pattern in ${x#pattern} and its kin, and of a word that names files, such as *.go. In a pattern, "*" matches any text, "?" matches one character, and a bracket expression such as [a-z] or [[:digit:]] matches one character of a set. A backslash makes the character after it match only itself. With ExtGlob, the forms ?(a|b), *(a|b), +(a|b), @(a|b) and !(a|b) match zero or one, zero or more, one or more, exactly one, or none of the patterns in the parentheses.
The package ports the patterns of brush-parser and brush-core, in pattern.rs and patterns.rs. brush turns a pattern into a regular expression. This package does not, as docs/DESIGN.md says: it matches a pattern by hand, on bytes, as bash does in the C locale. With UTF8, it matches characters of UTF-8, as bash does in a UTF-8 locale, unless the pattern or the text is not valid UTF-8. With Big5, it matches characters of Big5, as bash does in a locale such as zh_TW.big5, and DecodeRune, AppendRune and their kin read and write them. A pattern never fails to compile. Text that starts no valid form, such as a "[" with no "]", matches itself, as in bash.
The matcher keeps the set of the offsets in the text that the pattern so far can reach, and takes each part of the pattern in turn. So the time is bounded by the length of the pattern times the square of the length of the text, and no pattern takes exponential time.
With PathName or Period, Match follows strmatch of bash with FNM_PATHNAME or FNM_PERIOD. Those rules depend on the order in which bash tries the parts of a pattern, so a port of the steps of bash matches such a pattern. It keeps the result of each step once a match takes many steps, and so its time is bounded too.
MatchHistIgnore matches a line against the patterns of HISTIGNORE, as bash does before it adds a line to the history.
The package is a module of its own, which needs only the standard library, so that a program other than hail, such as the line editor rline, can match the patterns of bash with it (D40). Package glob of hail expands a pattern into the names of files.
Index ¶
- func AppendRune(b []byte, r rune, mode Mode) ([]byte, bool)
- func DecodeRune(s string, mode Mode) (rune, int)
- func HasMeta(pat string, mode Mode) bool
- func Match(pat, s string, mode Mode) bool
- func MatchHistIgnore(list, line, previous string, mode Mode) bool
- func Quote(s string) string
- func QuoteChars(s string, mode Mode) string
- func RuneCount(s string, mode Mode) int
- func Unescape(pat string) string
- func UnescapeChars(pat string, mode Mode) string
- func ValidString(s string, mode Mode) bool
- type Mode
- type Pattern
- func (p *Pattern) Find(s string, start int) (int, int, bool)
- func (p *Pattern) Match(s string) bool
- func (p *Pattern) MatchName(name string, period bool) bool
- func (p *Pattern) MatchPath(path string) bool
- func (p *Pattern) MatchPrefix(s string, longest bool) (int, bool)
- func (p *Pattern) MatchSuffix(s string, longest bool) (int, bool)
- func (p *Pattern) Next(s string, i int) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendRune ¶
AppendRune appends the character r to b as mode writes it, and reports whether mode has a character for r. As bytes, only ASCII characters can be written, as in the C locale of glibc.
func DecodeRune ¶
DecodeRune returns the first character of s and its length in bytes, as the characters of mode read it: UTF-8 with UTF8, Big5 with Big5, and one byte otherwise, which is its own code. A byte that starts no valid character gives utf8.RuneError and 1, as does an empty s. A character of Big5 gives its Unicode code point.
func HasMeta ¶
HasMeta reports whether pat holds a special character of a pattern, as unquoted_glob_pattern_p of bash does: a "*" or a "?" with no backslash before it, a "[" with a "]" after it and no "/" between them, or with ExtGlob a form of extglob. A word that holds none names itself, and needs no filename expansion.
func MatchHistIgnore ¶
MatchHistIgnore reports whether line matches a pattern of list, the value of HISTIGNORE, as bash decides whether to keep a line out of the history. The colons of list separate the patterns, except a colon that a backslash escapes, and one inside quotes or a bracket expression, or inside a form of extglob when mode has ExtGlob (splitColons). The backslash stays, so that "\:" matches a colon. An empty pattern matches nothing. The pattern "&" matches when line is previous, the line before it in the history. Any other pattern must match the whole line, in the mode mode.
func Quote ¶
Quote returns s with a backslash before each byte that is special in a pattern, so that the pattern matches s and nothing else.
func QuoteChars ¶
QuoteChars is Quote for the characters of mode: it puts a backslash before each special character, and keeps each character of two bytes as it is. In Big5, the trail byte of a character can be a special byte, such as "\", that Quote would escape.
func RuneCount ¶
RuneCount returns the number of characters of s, as mode reads them. A byte that starts no valid character counts as one.
func Unescape ¶
Unescape returns pat with the backslash of each escape removed: the text that a pattern with no special part matches.
func UnescapeChars ¶
UnescapeChars is Unescape for the characters of mode. It keeps each character of two bytes as it is.
func ValidString ¶
ValidString reports whether s is a sequence of valid characters of mode. Any s is valid as bytes.
Types ¶
type Mode ¶
type Mode uint
Mode selects how a pattern is read and matched.
const ( // ExtGlob reads the forms ?(...), *(...), +(...), @(...) and !(...), // as shopt -s extglob does. ExtGlob Mode = 1 << iota // NoCase matches a letter in either case, as shopt -s nocasematch and // nocaseglob do. NoCase // UTF8 matches characters of UTF-8, as bash does in a UTF-8 locale: // "?" matches one character, and a bracket expression holds // characters. When the pattern or the text is not valid UTF-8, the // match is on bytes, as in bash. UTF8 // PathName matches as strmatch of bash does with FNM_PATHNAME: "*", // "?" and a bracket expression do not match a "/", and a bracket // expression that holds a "/" is not one, so that its "[" matches // only a "[". The rules follow the order in which bash tries the // parts of a pattern, as in lib/glob/sm_loop.c. So "*" at the end // of a pattern matches a "/", and "*@(/)" never matches one. It // changes Match only. PathName // Period matches as strmatch of bash does with FNM_PERIOD: a "." at // the start of the text, or after a "/" with PathName, matches only // a "." of the pattern. It changes Match only. Period // Big5 matches characters of Big5, as bash does in a Big5 locale such // as zh_TW.big5, as UTF8 does for UTF-8. A character of Big5 is // matched as its Unicode code point, as the C library of bash sees // it. UTF8 wins when both are set. Big5 )
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
Pattern is a compiled pattern.
func Compile ¶
Compile reads the pattern pat. It never fails: text that starts no valid form matches itself, as in bash.
func (*Pattern) Find ¶
Find returns the first match of the pattern in s at or after the offset start: the match that starts first, and of those the longest, as bash finds the text that ${x/pattern/text} replaces. The bool is false when the pattern matches nowhere.
func (*Pattern) MatchName ¶
MatchName reports whether the name of a file matches the pattern, as bash matches a name that it reads from a directory. With period, a "." at the start of the name matches only a "." of the pattern. Without it, the names "." and ".." match only a "." of the pattern.
func (*Pattern) MatchPath ¶
MatchPath reports whether path matches the pattern as a pattern of GLOBIGNORE matches, which is Match with PathName: "*", "?" and a bracket expression do not match a "/". A "*" at the end of the pattern still matches any text, as in bash.
func (*Pattern) MatchPrefix ¶
MatchPrefix returns the length of the shortest prefix of s that the pattern matches, or of the longest one when longest is true. The bool is false when no prefix matches.
func (*Pattern) MatchSuffix ¶
MatchSuffix returns the offset of the shortest suffix of s that the pattern matches, or of the longest one when longest is true. The bool is false when no suffix matches. A suffix starts at a character.