mod regex;
use std::{collections::HashMap, ops::Range, rc::Rc};
pub use regex::Regex;
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
#[derive(Debug)]
pub struct Match<'t, I> {
input: &'t [I],
start: usize,
end: usize,
}
impl<'t, I> Match<'t, I> {
pub fn start(&self) -> usize {
self.start
}
pub fn end(&self) -> usize {
self.end
}
pub fn range(&self) -> Range<usize> {
self.start..self.end
}
pub fn values(&self) -> &'t [I] {
&self.input[self.range()]
}
}
#[derive(Debug)]
struct CaptureLocation {
pub start: usize,
pub end: usize,
}
#[derive(Debug)]
pub struct Captures<'t, I> {
input: &'t [I],
capture_locations: Vec<CaptureLocation>,
named_capture_index: Rc<HashMap<String, usize>>,
}
impl<'t, I> Captures<'t, I> {
pub fn get(&self, index: usize) -> Option<Match<'t, I>> {
self.capture_locations.get(index).map(|location| Match {
input: self.input,
start: location.start,
end: location.end,
})
}
pub fn name(&self, name: &str) -> Option<Match<'t, I>> {
self.named_capture_index
.get(name)
.and_then(|idx| self.get(*idx))
}
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.capture_locations.len()
}
}
pub trait CompiledRegex<I> {
fn is_match(&self, input: &[I]) -> bool;
fn is_full_match(&self, input: &[I]) -> bool {
if let Some(found_match) = self.find(input) {
found_match.start() == 0 && found_match.end() == input.len()
} else {
false
}
}
fn find<'t>(&self, input: &'t [I]) -> Option<Match<'t, I>>;
fn captures<'t>(&self, input: &'t [I]) -> Option<Captures<'t, I>>;
}