use crate::{Captures, Error, Needle};
#[derive(Debug, Clone)]
pub struct Lookup {
buf: Vec<u8>,
}
impl Lookup {
pub fn new() -> Self {
Self { buf: Vec::new() }
}
pub fn on<N>(&mut self, buf: &[u8], eof: bool, pattern: N) -> Result<Option<Captures>, Error>
where
N: Needle,
{
self.buf.extend(buf);
check(&mut self.buf, pattern, eof)
}
pub fn clear(&mut self) {
self.buf.clear();
}
}
impl Default for Lookup {
fn default() -> Self {
Self::new()
}
}
fn check<N>(buf: &mut Vec<u8>, needle: N, eof: bool) -> Result<Option<Captures>, Error>
where
N: Needle,
{
let found = needle.check(buf, eof)?;
if found.is_empty() {
return Ok(None);
}
let end_index = Captures::right_most_index(&found);
let involved_bytes = buf[..end_index].to_vec();
let found = Captures::new(involved_bytes, found);
let _ = buf.drain(..end_index);
Ok(Some(found))
}