pub enum Matcher {
Literal {
pattern: String,
},
Regex {
re: Regex,
prescreen: Option<Regex>,
},
}Expand description
Abstraction over literal and regex matching.
Variants§
Literal
Regex
A regex matcher — used for both explicit --regex patterns and as the
implementation backing case-insensitive literal matching (via
regex::escape + (?i)), which avoids byte-offset mismatches from
str::to_lowercase() on multi-byte Unicode characters.
Implementations§
Source§impl Matcher
impl Matcher
Sourcepub fn new(op: &Op) -> Result<Self, RipsedError>
pub fn new(op: &Op) -> Result<Self, RipsedError>
Create a new matcher from an operation.
Sourcepub fn prescreen(&self, text: &str) -> bool
pub fn prescreen(&self, text: &str) -> bool
Cheap whole-buffer check: false means no line of text can match
this pattern, so per-line processing can be skipped entirely.
true means “maybe” — false positives are fine, false negatives
are a correctness bug (locked by a proptest).
Sourcepub fn replace(&self, text: &str, replacement: &str) -> Option<String>
pub fn replace(&self, text: &str, replacement: &str) -> Option<String>
Replace all matches in the given text. Returns None if no matches.
Sourcepub fn replace_n(
&self,
text: &str,
replacement: &str,
limit: usize,
) -> Option<(String, usize)>
pub fn replace_n( &self, text: &str, replacement: &str, limit: usize, ) -> Option<(String, usize)>
Replace up to limit matches (0 = unlimited), left to right.
Returns the new text and how many occurrences were replaced, or
None if nothing matched. With limit == 0 this is exactly
Matcher::replace plus the occurrence count.
Sourcepub fn find_replacements(&self, text: &str, replacement: &str) -> Vec<MatchSpan>
pub fn find_replacements(&self, text: &str, replacement: &str) -> Vec<MatchSpan>
Find every non-overlapping match in text and compute its expanded
replacement, left to right.
Spans are returned in ascending order and never overlap, with the
same semantics as Matcher::replace (str::replace for literals,
Regex::replace_all for regexes) — splicing each span’s replacement
into the original text reproduces replace’s output exactly.