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, bool)
- 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, bool)
- 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 DFA
- type Match
- type MatchKind
- type OptimizedNFA
- type PatternID
- type StateID
Constants ¶
const Version = "0.3.1"
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 a fully compiled DFA with premultiplied state IDs for maximum throughput.
func (*Automaton) Find ¶
Find returns the first match in haystack starting at or after position start. Uses the flagged transition table for inline match detection. Prefilter: skips ahead using bytes.IndexByte when no start byte is nearby.
Zero allocations — the Match is returned by value.
func (*Automaton) FindAll ¶
FindAll returns all non-overlapping matches in the haystack. If n >= 0, at most n matches are returned. Uses an inline DFA loop to avoid per-match heap allocations.
func (*Automaton) FindAllOverlapping ¶
FindAllOverlapping returns all overlapping matches in the haystack.
func (*Automaton) FindAt ¶
FindAt returns the first match starting exactly at position start.
Zero allocations — the Match is returned by value.
func (*Automaton) IsMatch ¶
IsMatch returns true if any pattern matches anywhere in the haystack. This is the most optimized search path — zero allocations, minimal branching.
Uses a two-level prefilter strategy:
- Skip-ahead: use SIMD bytes.IndexByte to jump directly to positions where a match could start, skipping all non-pattern bytes in bulk.
- DFA scan: run the automaton from the skip position to verify the match.
If the automaton returns to start state during scanning, it re-engages the prefilter to skip ahead again. This is the same strategy as BurntSushi's Rust implementation.
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.
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 DFA ¶ added in v0.2.0
type DFA struct {
// contains filtered or unexported fields
}
DFA represents a fully compiled deterministic finite automaton.
Key properties:
- All failure transitions are pre-computed into the transition table
- Single flat []uint32 array for all transitions (cache-friendly)
- Premultiplied state IDs: sid = stateIndex << stride2
- Match flag embedded in high bit of each transition value
- Match check in IsMatch: bitmap[stateIndex/64] & (1 << (stateIndex%64))
- Lookup: trans[sid + byteClass] — one addition, one load per byte
func (*DFA) MemoryUsage ¶ added in v0.2.1
MemoryUsage returns the approximate heap memory used by this DFA in bytes.
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