pattern

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendRune

func AppendRune(b []byte, r rune, mode Mode) ([]byte, bool)

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

func DecodeRune(s string, mode Mode) (rune, int)

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

func HasMeta(pat string, mode Mode) bool

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 Match

func Match(pat, s string, mode Mode) bool

Match reports whether the whole of s matches pat.

func MatchHistIgnore

func MatchHistIgnore(list, line, previous string, mode Mode) bool

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

func Quote(s string) string

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

func QuoteChars(s string, mode Mode) string

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

func RuneCount(s string, mode Mode) int

RuneCount returns the number of characters of s, as mode reads them. A byte that starts no valid character counts as one.

func Unescape

func Unescape(pat string) string

Unescape returns pat with the backslash of each escape removed: the text that a pattern with no special part matches.

func UnescapeChars

func UnescapeChars(pat string, mode Mode) string

UnescapeChars is Unescape for the characters of mode. It keeps each character of two bytes as it is.

func ValidString

func ValidString(s string, mode Mode) bool

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

func Compile(pat string, mode Mode) *Pattern

Compile reads the pattern pat. It never fails: text that starts no valid form matches itself, as in bash.

func (*Pattern) Find

func (p *Pattern) Find(s string, start int) (int, int, bool)

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) Match

func (p *Pattern) Match(s string) bool

Match reports whether the whole of s matches the pattern.

func (*Pattern) MatchName

func (p *Pattern) MatchName(name string, period bool) bool

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

func (p *Pattern) MatchPath(path string) bool

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

func (p *Pattern) MatchPrefix(s string, longest bool) (int, bool)

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

func (p *Pattern) MatchSuffix(s string, longest bool) (int, bool)

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.

func (*Pattern) Next

func (p *Pattern) Next(s string, i int) int

Next returns the offset after the character at the offset i of s, as the pattern counts characters in s: one byte, or one UTF-8 sequence.

Jump to

Keyboard shortcuts

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