Skip to main content

Module compiled

Module compiled 

Source
Expand description

Compile-once, match-many matcher API.

This module exposes a first-class boundary between pattern validation/compilation and repeated matching:

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§

CompiledMatcher
An immutable compiled matcher: build once, query many times.
MatcherBuilder
Validates patterns and compiles them into an immutable CompiledMatcher.
MatcherOptions
Explicit construction configuration for a CompiledMatcher.
PositionedMatch
Where one match lies in the searched text: byte offsets plus the index of the pattern that matched.

Enums§

MatchOverlap
How a CompiledMatcher treats occurrences that overlap one another.

Constants§

DEFAULT_MIN_PATTERN_LENGTH
Default minimum pattern length, mirroring find_matches.