pub mod backtracking;
pub mod linear;
use crate::captures::{Captures, Match};
use crate::errors::CompileError;
use crate::flags::Flags;
use crate::haystack::Haystack;
pub use backtracking::Matcher;
pub trait RegexEngine: Send + Sync + 'static {
type Regex: CompiledRegex;
fn compile(&self, pattern: &str, flags: Flags) -> Result<Self::Regex, CompileError>;
}
pub trait CompiledRegex: Send + Sync {
fn pattern(&self) -> &str;
fn flags(&self) -> &Flags;
fn is_match(&self, text: &str) -> bool;
fn find(&self, text: &str) -> Option<Match>;
fn find_all<'a>(&'a self, text: &'a str) -> Box<dyn Iterator<Item = Match> + 'a>;
fn captures(&self, text: &str) -> Option<Captures>;
fn captures_all<'a>(&'a self, text: &'a str) -> Box<dyn Iterator<Item = Captures> + 'a>;
fn replace(&self, text: &str, replacement: &str) -> String;
fn replace_all(&self, text: &str, replacement: &str) -> String;
}
impl CompiledRegex for Box<dyn CompiledRegex> {
fn pattern(&self) -> &str {
(**self).pattern()
}
fn flags(&self) -> &Flags {
(**self).flags()
}
fn is_match(&self, text: &str) -> bool {
(**self).is_match(text)
}
fn find(&self, text: &str) -> Option<Match> {
(**self).find(text)
}
fn find_all<'a>(&'a self, text: &'a str) -> Box<dyn Iterator<Item = Match> + 'a> {
(**self).find_all(text)
}
fn captures(&self, text: &str) -> Option<Captures> {
(**self).captures(text)
}
fn captures_all<'a>(&'a self, text: &'a str) -> Box<dyn Iterator<Item = Captures> + 'a> {
(**self).captures_all(text)
}
fn replace(&self, text: &str, replacement: &str) -> String {
(**self).replace(text, replacement)
}
fn replace_all(&self, text: &str, replacement: &str) -> String {
(**self).replace_all(text, replacement)
}
}
pub trait CompiledRegexHaystack: CompiledRegex {
fn is_match_from<H: Haystack>(&self, haystack: H) -> bool;
fn find_from<H: Haystack>(&self, haystack: H) -> Option<Match>;
fn find_from_at<H: Haystack>(&self, haystack: H, start: usize) -> Option<Match>;
fn find_all_from<'a, H: Haystack + 'a>(
&'a self,
haystack: H,
) -> Box<dyn Iterator<Item = Match> + 'a>;
}
pub struct AnyRegexEngine<E: RegexEngine>(pub E);
impl<E: RegexEngine> RegexEngine for AnyRegexEngine<E>
where
E::Regex: 'static,
{
type Regex = Box<dyn CompiledRegex>;
fn compile(&self, pattern: &str, flags: Flags) -> Result<Self::Regex, CompileError> {
let regex = self.0.compile(pattern, flags)?;
Ok(Box::new(regex))
}
}