#![doc = include_str!("../README.md")]
#[cfg(feature = "futures")]
pub mod futures;
pub mod io;
pub mod needle;
pub use crate::io::UntilNeedleRead;
pub use crate::needle::Needle;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Captures {
buf: Vec<u8>,
match_start: usize,
}
impl Captures {
fn new(buf: Vec<u8>, match_start: usize) -> Self {
Self { buf, match_start }
}
pub fn before(&self) -> &[u8] {
&self.buf[..self.match_start]
}
pub fn matched(&self) -> &[u8] {
&self.buf[self.match_start..]
}
pub fn into_bytes(self) -> Vec<u8> {
self.buf
}
pub fn split(self) -> (Vec<u8>, Vec<u8>) {
let mut buf = self.buf;
let matched = buf.split_off(self.match_start);
(buf, matched)
}
pub fn as_bytes(&self) -> &[u8] {
&self.buf
}
pub fn total_bytes_read(&self) -> usize {
self.buf.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn captures() {
let buf = b"hello world";
let match_start = "world".findin(buf).unwrap().start;
let captures = Captures::new(buf.to_vec(), match_start);
assert_eq!(captures.before(), b"hello ");
assert_eq!(captures.matched(), b"world");
assert_eq!(captures.as_bytes(), buf);
assert_eq!(captures.total_bytes_read(), buf.len());
let (before, matched) = captures.split();
assert_eq!(before, b"hello ");
assert_eq!(matched, b"world");
}
}