Expand description
Compile-once, match-many matcher API.
This module exposes a first-class boundary between pattern validation/compilation and repeated matching:
MatcherOptionsis the explicit, serialisable construction configuration.MatcherBuildervalidates patterns up front and rejects invalid (empty, blank unlessMatcherOptions::allow_blank_patternsis set, or too-short) and duplicate patterns.CompiledMatcheris an immutable,Send + Syncmatcher that can be reused across many queries and threads without rebuilding the automaton.
Compiled internals are intentionally not serialisable; persist the
source patterns plus MatcherOptions instead and rebuild on load.
With default options, results are identical to
crate::matcher::find_matches semantics (leftmost-longest, whole-word
matches only, ASCII case-insensitive, minimum pattern length enforced).
MatcherOptions::overlap and MatcherOptions::word_boundaries opt in
to reporting every (possibly overlapping) occurrence and to matching
inside words, for callers such as anchor re-location that need a plain
“find all occurrences” search.
§Positions without per-match allocation
CompiledMatcher::find_matches returns owned Matched records, which
clone the pattern string and its NormalizedTerm for every match.
Callers that only need where each match is can use
CompiledMatcher::find_positions (a borrowing, allocation-free
iterator) or CompiledMatcher::push_positions (appends to a reusable
buffer, sorted for MatchOverlap::All). Both yield the Copy
PositionedMatch; look the pattern and term up on demand with
CompiledMatcher::pattern and CompiledMatcher::term.
use terraphim_automata::{MatchOverlap, MatcherBuilder, MatcherOptions};
use terraphim_types::{NormalizedTerm, NormalizedTermValue};
let mut builder = MatcherBuilder::new(MatcherOptions::default());
for word in ["rust", "rust async"] {
builder.insert(
word.to_string(),
NormalizedTerm::with_auto_id(NormalizedTermValue::from(word)),
)?;
}
let matcher = builder.build()?;
let text = "rust async and rust";
let found: Vec<(&str, usize, usize)> = matcher
.find_positions(text)
.map(|m| (matcher.pattern(m.pattern_index).unwrap(), m.start, m.end))
.collect();
assert_eq!(found, [("rust async", 0, 10), ("rust", 15, 19)]);Structs§
- Compiled
Matcher - An immutable compiled matcher: build once, query many times.
- Matcher
Builder - Validates patterns and compiles them into an immutable
CompiledMatcher. - Matcher
Options - Explicit construction configuration for a
CompiledMatcher. - Positioned
Match - Where one match lies in the searched text: byte offsets plus the index of the pattern that matched.
Enums§
- Match
Overlap - How a
CompiledMatchertreats occurrences that overlap one another.
Constants§
- DEFAULT_
MIN_ PATTERN_ LENGTH - Default minimum pattern length, mirroring
find_matches.