Documentation
¶
Overview ¶
Package ahocorasick implements the Aho-Corasick algorithm for efficient multi-pattern string matching.
The Aho-Corasick algorithm allows searching for multiple patterns simultaneously in O(n + m + z) time, where n is the input length, m is the total pattern length, and z is the number of matches.
This package is under active development. See README for status.
Index ¶
- Constants
- type Automaton
- func (a *Automaton) Count(haystack []byte) int
- func (a *Automaton) Find(haystack []byte, start int) *Match
- func (a *Automaton) FindAll(haystack []byte, n int) []Match
- func (a *Automaton) FindAllOverlapping(haystack []byte) []Match
- func (a *Automaton) FindAt(haystack []byte, start int) *Match
- func (a *Automaton) IsMatch(haystack []byte) bool
- func (a *Automaton) MatchKind() MatchKind
- func (a *Automaton) Pattern(id int) []byte
- func (a *Automaton) PatternCount() int
- func (a *Automaton) StateCount() int
- type Builder
- func (b *Builder) AddPattern(pattern []byte) *Builder
- func (b *Builder) AddPatterns(patterns [][]byte) *Builder
- func (b *Builder) AddStrings(patterns []string) *Builder
- func (b *Builder) Build() (*Automaton, error)
- func (b *Builder) SetASCII(ascii bool) *Builder
- func (b *Builder) SetByteClasses(enabled bool) *Builder
- func (b *Builder) SetMatchKind(kind MatchKind) *Builder
- func (b *Builder) SetPrefilter(prefilter bool) *Builder
- type ByteClasses
- type Match
- type MatchKind
- type OptimizedNFA
- type PatternID
- type StateID
Constants ¶
const Version = "0.1.0-dev"
Version is the current library version.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Automaton ¶
type Automaton struct {
// contains filtered or unexported fields
}
Automaton is the compiled Aho-Corasick multi-pattern matcher. Uses optimized dense array transitions for high performance.
func (*Automaton) Find ¶
Find returns the first match in haystack starting at or after position start. Returns nil if no match is found.
func (*Automaton) FindAll ¶
FindAll returns all non-overlapping matches in the haystack. If n >= 0, at most n matches are returned.
func (*Automaton) FindAllOverlapping ¶
FindAllOverlapping returns all overlapping matches in the haystack. This may return multiple matches at the same position.
func (*Automaton) FindAt ¶
FindAt returns the first match starting exactly at position start. This is useful for anchored matching. Returns nil if no match starts at the given position.
func (*Automaton) IsMatch ¶
IsMatch returns true if any pattern matches anywhere in the haystack. Optimized: inlined nextState for maximum performance.
func (*Automaton) PatternCount ¶
PatternCount returns the number of patterns in the automaton.
func (*Automaton) StateCount ¶
StateCount returns the number of states in the underlying automaton. This is useful for debugging and performance analysis.
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder configures and builds an Aho-Corasick automaton.
func NewBuilder ¶
func NewBuilder() *Builder
NewBuilder creates a new Builder with default configuration. Default: LeftmostFirst semantics, byte classes enabled.
func (*Builder) AddPattern ¶
AddPattern adds a single pattern to the automaton.
func (*Builder) AddPatterns ¶
AddPatterns adds multiple patterns to the automaton.
func (*Builder) AddStrings ¶
AddStrings adds multiple string patterns to the automaton.
func (*Builder) Build ¶
Build constructs the Aho-Corasick automaton from the configured patterns. Returns an error if no patterns were added or if construction fails. Uses optimized dense array transitions for high performance (1+ GB/s).
func (*Builder) SetASCII ¶
SetASCII enables ASCII-only optimization. When true, patterns are assumed to contain only ASCII bytes.
func (*Builder) SetByteClasses ¶
SetByteClasses enables or disables byte class compression. When enabled, reduces memory usage by grouping equivalent bytes.
func (*Builder) SetMatchKind ¶
SetMatchKind sets the match semantics. Default is LeftmostFirst (Perl-compatible).
func (*Builder) SetPrefilter ¶
SetPrefilter enables or disables prefilter optimization. When enabled, a prefilter may be used to accelerate search.
type ByteClasses ¶
type ByteClasses struct {
// contains filtered or unexported fields
}
ByteClasses compresses the 256-byte alphabet into equivalence classes. Bytes that always transition to the same states can share a class. This reduces memory usage and improves cache utilization.
func NewByteClasses ¶
func NewByteClasses(patterns [][]byte) *ByteClasses
NewByteClasses creates ByteClasses from a set of patterns. It computes the minimal set of equivalence classes.
func NewSingletonByteClasses ¶
func NewSingletonByteClasses() *ByteClasses
NewSingletonByteClasses creates ByteClasses where each byte is its own class. This is the identity mapping (no compression).
func (*ByteClasses) AlphabetSize ¶
func (bc *ByteClasses) AlphabetSize() int
AlphabetSize returns the number of equivalence classes. This is the effective alphabet size for the automaton.
func (*ByteClasses) Get ¶
func (bc *ByteClasses) Get(b byte) int
Get returns the equivalence class for the given byte.
func (*ByteClasses) IsSingleton ¶
func (bc *ByteClasses) IsSingleton() bool
IsSingleton returns true if every byte is its own class.
func (*ByteClasses) NumClasses ¶
func (bc *ByteClasses) NumClasses() int
NumClasses returns the number of equivalence classes.
type Match ¶
type Match struct {
// PatternID is the index of the pattern that matched.
PatternID int
// Start is the byte offset of the start of the match.
Start int
// End is the byte offset of the end of the match (exclusive).
End int
}
Match represents a match found in the haystack.
type MatchKind ¶
type MatchKind int
MatchKind specifies the semantics of matching.
const ( // LeftmostFirst implements leftmost-first semantics. // When multiple patterns can match at the same position, // the pattern that appears first in the pattern list wins. // This is Perl-compatible behavior and matches Go's regexp package. LeftmostFirst MatchKind = iota // LeftmostLongest implements leftmost-longest semantics. // When multiple patterns can match at the same position, // the longest pattern wins. This is POSIX-compatible behavior. LeftmostLongest )
type OptimizedNFA ¶
type OptimizedNFA struct {
// contains filtered or unexported fields
}
OptimizedNFA represents an optimized Aho-Corasick automaton. Key optimizations: 1. Dense transitions: []StateID instead of map[byte]StateID 2. Precomputed root transitions: no failure link following for root 3. ByteClasses: reduced alphabet size