pattern

package
v0.0.0-...-fe9beca Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsLiteral

func IsLiteral(s string) bool

IsLiteral reports whether the pattern contains no regex metacharacters.

Types

type Config

type Config struct {
	// Pattern is the search string or regex.
	Pattern string
	// FixedStrings treats the pattern as a literal string.
	FixedStrings bool
	// IgnoreCase makes matching case-insensitive.
	IgnoreCase bool
	// SmartCase makes matching case-insensitive unless the pattern
	// contains uppercase characters.
	SmartCase bool
	// Pcre2 uses PCRE2-compatible regex engine.
	Pcre2 bool
	// Multiline enables matching across line boundaries.
	Multiline bool
	// WordRegexp matches only if the match is surrounded by word boundaries.
	WordRegexp bool
}

Config holds pattern matching options.

type LiteralMatcher

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

LiteralMatcher uses byte search for fixed-string matching. It is significantly faster than RegexMatcher for simple patterns. Users should generally create Matchers via New().

func (*LiteralMatcher) CaseFold

func (m *LiteralMatcher) CaseFold() bool

CaseFold returns true if this matcher performs case-insensitive matching.

func (*LiteralMatcher) Expand

func (m *LiteralMatcher) Expand(dst []byte, template []byte, line []byte, locs []int) []byte

Expand appends the replacement template to dst.

func (*LiteralMatcher) FindAll

func (m *LiteralMatcher) FindAll(data []byte, callback func(locs []int) bool)

FindAll searches for all matches in the data and calls the callback for each.

func (*LiteralMatcher) Literal

func (m *LiteralMatcher) Literal() []byte

Literal returns the fixed literal string if the matcher is purely literal and case-sensitive.

func (*LiteralMatcher) Literals

func (m *LiteralMatcher) Literals() [][]byte

Literals returns the literal as a single-element slice, or nil if case-insensitive (AC can't cheaply handle case-folded matching).

func (*LiteralMatcher) Match

func (m *LiteralMatcher) Match(line []byte) (locs []int, ok bool)

Match searches for the pattern in a single line.

func (*LiteralMatcher) MatchFile

func (m *LiteralMatcher) MatchFile(data []byte) bool

MatchFile searches the entire file contents for the pattern.

func (*LiteralMatcher) Name

func (m *LiteralMatcher) Name() string

Name returns "literal".

type Matcher

type Matcher interface {
	// Match searches for the pattern in a single line.
	// Returns the byte offsets of the match and all capture groups.
	// locs[0]:locs[1] is the full match, locs[2]:locs[3] is group 1, etc.
	// The caller MUST NOT retain the locs slice after the call.
	Match(line []byte) (locs []int, ok bool)
	// MatchFile searches the entire file contents for the pattern.
	MatchFile(data []byte) bool
	// Name returns the matcher implementation name ("literal", "regex", or "pcre").
	Name() string
	// Literal returns the fixed literal string if the matcher is purely literal,
	// or an empty slice if it's a regex. This allows for fast pre-filtering.
	Literal() []byte
	// Literals returns all mandatory literal strings for multi-pattern
	// pre-filtering (e.g., Aho-Corasick). Returns nil if extraction is not
	// possible (regex metacharacters, case-insensitive without a clean set, etc.).
	// When non-nil, every byte slice must appear in the data for a match to be
	// possible. Used by the search pre-filter to skip files early.
	Literals() [][]byte
	// FindAll searches for all matches in the data and calls the callback for each.
	// The callback returns false to stop searching.
	FindAll(data []byte, callback func(locs []int) bool)
	// Expand appends the replacement template to dst, replacing placeholders
	// like $1 or ${name} with captured groups from the match.
	Expand(dst []byte, template []byte, line []byte, locs []int) []byte
}

Matcher performs line-oriented matching.

func New

func New(cfg Config) (Matcher, error)

New compiles a Matcher from the given config.

type PCREMatcher

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

PCREMatcher wraps regexp2 for PCRE2-compatible pattern matching. Users should generally create Matchers via New().

func (*PCREMatcher) Expand

func (m *PCREMatcher) Expand(dst []byte, template []byte, line []byte, locs []int) []byte

Expand appends the replacement template to dst.

func (*PCREMatcher) FindAll

func (m *PCREMatcher) FindAll(data []byte, callback func(locs []int) bool)

FindAll searches for all matches in the data and calls the callback for each.

func (*PCREMatcher) Literal

func (m *PCREMatcher) Literal() []byte

func (*PCREMatcher) Literals

func (m *PCREMatcher) Literals() [][]byte

Literals extracts literals from simple alternation patterns.

func (*PCREMatcher) Match

func (m *PCREMatcher) Match(line []byte) (locs []int, ok bool)

Match searches for the pattern in a single line.

func (*PCREMatcher) MatchFile

func (m *PCREMatcher) MatchFile(data []byte) bool

MatchFile searches the entire file contents for the pattern.

func (*PCREMatcher) Name

func (m *PCREMatcher) Name() string

Name returns "pcre".

type RegexMatcher

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

RegexMatcher wraps stdlib regexp for pattern matching. Users should generally create Matchers via New().

func (*RegexMatcher) Expand

func (m *RegexMatcher) Expand(dst []byte, template []byte, line []byte, locs []int) []byte

Expand appends the replacement template to dst.

func (*RegexMatcher) FindAll

func (m *RegexMatcher) FindAll(data []byte, callback func(locs []int) bool)

FindAll searches for all matches in the data and calls the callback for each.

func (*RegexMatcher) Literal

func (m *RegexMatcher) Literal() []byte

func (*RegexMatcher) Literals

func (m *RegexMatcher) Literals() [][]byte

Literals extracts mandatory literals from simple alternation patterns like (foo|bar|baz). Returns nil for non-alternation regexes or case-insensitive patterns where Literal() already has a prefix.

func (*RegexMatcher) Match

func (m *RegexMatcher) Match(line []byte) (locs []int, ok bool)

Match searches for the pattern in a single line.

func (*RegexMatcher) MatchFile

func (m *RegexMatcher) MatchFile(data []byte) bool

MatchFile searches the entire file contents for the pattern.

func (*RegexMatcher) Name

func (m *RegexMatcher) Name() string

Name returns "regex".

Jump to

Keyboard shortcuts

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