use crate::regular_route::Automata;
use crate::{
CaptureLocations, CaptureMatches, Captures, Input, Matches, Regex, RegexImpl, RegexInput,
Result, WrapEngine, vm,
};
use alloc::boxed::Box;
use regex_automata::{hybrid, meta, util::captures::Captures as RaCaptures};
pub(crate) enum Cache {
Plan,
Regular {
cache: Box<meta::Cache>,
captures: RaCaptures,
},
Rich {
scratch: vm::Scratch,
route: Option<RouteCache>,
},
}
pub(crate) struct RouteCache {
dfa: Box<hybrid::regex::Cache>,
meta: Option<(Box<meta::Cache>, RaCaptures)>,
}
impl core::fmt::Debug for Cache {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::Plan => "Cache::Plan",
Self::Regular { .. } => "Cache::Regular",
Self::Rich { .. } => "Cache::Rich",
})
}
}
impl Cache {
pub(crate) fn regular(&mut self) -> Option<(&mut meta::Cache, &mut RaCaptures)> {
match self {
Self::Regular { cache, captures } => Some((cache.as_mut(), captures)),
Self::Plan | Self::Rich { .. } => None,
}
}
pub(crate) fn route(&mut self, automata: &Automata) -> Option<&mut hybrid::regex::Cache> {
match self {
Self::Rich { route, .. } => Some(Self::route_cache(route, automata).dfa.as_mut()),
Self::Plan | Self::Regular { .. } => None,
}
}
pub(crate) fn route_meta(
&mut self,
automata: &Automata,
) -> Option<(&mut meta::Cache, &mut RaCaptures)> {
let Self::Rich { route, .. } = self else {
return None;
};
let regex = automata.regex()?;
let (cache, captures) = Self::route_cache(route, automata)
.meta
.get_or_insert_with(|| (Box::new(regex.create_cache()), regex.create_captures()));
Some((cache.as_mut(), captures))
}
fn route_cache<'c>(
route: &'c mut Option<RouteCache>,
automata: &Automata,
) -> &'c mut RouteCache {
route.get_or_insert_with(|| RouteCache {
dfa: Box::new(automata.create_cache()),
meta: None,
})
}
pub(crate) fn rich(&mut self) -> Option<&mut vm::Scratch> {
match self {
Self::Rich { scratch, .. } => Some(scratch),
Self::Plan | Self::Regular { .. } => None,
}
}
}
pub struct Searcher<'r> {
regex: &'r Regex,
cache: Cache,
}
impl core::fmt::Debug for Searcher<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Searcher")
.field("regex", &self.regex)
.finish_non_exhaustive()
}
}
impl<'r> Searcher<'r> {
pub(crate) fn new(regex: &'r Regex) -> Self {
let cache = match ®ex.inner {
RegexImpl::Wrap {
engine: WrapEngine::Meta(inner),
..
} => Cache::Regular {
cache: Box::new(inner.create_cache()),
captures: inner.create_captures(),
},
RegexImpl::Wrap {
engine: WrapEngine::Plan(_),
..
} => Cache::Plan,
RegexImpl::Fancy { .. } => Cache::Rich {
scratch: vm::new_scratch(),
route: None,
},
};
Self { regex, cache }
}
pub fn regex(&self) -> &'r Regex {
self.regex
}
pub fn is_match<S: Input + ?Sized>(&mut self, input: &S) -> Result<bool> {
match &self.regex.inner {
RegexImpl::Wrap { .. } => self.is_match_input(RegexInput::new(input)),
RegexImpl::Fancy { .. } => {
self.regex
.fancy_is_match(&RegexInput::new(input), true, Some(&mut self.cache))
}
}
}
pub fn is_match_input<S: Input + ?Sized>(&mut self, input: RegexInput<'_, S>) -> Result<bool> {
match &self.regex.inner {
RegexImpl::Wrap {
engine: WrapEngine::Plan(_),
..
} => self.regex.is_match_input(input),
RegexImpl::Wrap {
engine: WrapEngine::Meta(inner),
..
} => {
if input.is_done() {
return Ok(false);
}
if let Some((cache, _)) = self.cache.regular() {
return Ok(inner
.search_half_with(cache, &crate::ra_input(&input).earliest(true))
.is_some());
}
Ok(self
.regex
.find_input_raw_cached(&input, 0, Some(&mut self.cache))?
.is_some())
}
RegexImpl::Fancy { .. } => {
self.regex
.fancy_is_match(&input, false, Some(&mut self.cache))
}
}
}
pub fn find<'t, S: Input + ?Sized>(&mut self, input: &'t S) -> Result<Option<S::Match<'t>>> {
self.find_input(RegexInput::new(input))
}
pub fn find_input<'t, S: Input + ?Sized>(
&mut self,
input: RegexInput<'t, S>,
) -> Result<Option<S::Match<'t>>> {
Ok(self
.regex
.find_input_raw_cached(&input, 0, Some(&mut self.cache))?
.map(|(start, end)| input.haystack().make_match(start, end)))
}
pub fn captures<'t, S: Input + ?Sized>(
&mut self,
input: &'t S,
) -> Result<Option<Captures<'t, S>>> {
self.captures_input(RegexInput::new(input))
}
pub fn captures_input<'t, S: Input + ?Sized>(
&mut self,
input: RegexInput<'t, S>,
) -> Result<Option<Captures<'t, S>>> {
self.regex
.captures_input_cached(&input, 0, Some(&mut self.cache))
}
pub fn find_iter<'s, 't, S: Input + ?Sized>(
&'s mut self,
haystack: &'t S,
) -> Matches<'s, 't, S> {
self.find_iter_input(RegexInput::new(haystack))
}
pub fn find_iter_input<'s, 't, S: Input + ?Sized>(
&'s mut self,
input: RegexInput<'t, S>,
) -> Matches<'s, 't, S> {
Matches::new(self.regex, input, Some(&mut self.cache))
}
pub fn captures_iter<'s, 't, S: Input + ?Sized>(
&'s mut self,
haystack: &'t S,
) -> CaptureMatches<'s, 't, S> {
self.captures_iter_input(RegexInput::new(haystack))
}
pub fn captures_iter_input<'s, 't, S: Input + ?Sized>(
&'s mut self,
input: RegexInput<'t, S>,
) -> CaptureMatches<'s, 't, S> {
CaptureMatches(self.find_iter_input(input))
}
pub fn captures_read<'t, S: Input + ?Sized>(
&mut self,
locations: &mut CaptureLocations,
haystack: &'t S,
) -> Result<Option<S::Match<'t>>> {
self.captures_read_input(locations, RegexInput::new(haystack))
}
pub fn captures_read_input<'t, S: Input + ?Sized>(
&mut self,
locations: &mut CaptureLocations,
input: RegexInput<'t, S>,
) -> Result<Option<S::Match<'t>>> {
let found = self
.regex
.captures_read_cached(&input, locations, Some(&mut self.cache))?;
Ok(found
.then(|| locations.get(0))
.flatten()
.map(|(start, end)| input.haystack().make_match(start, end)))
}
}