pub mod bytes;
use std::iter::FusedIterator;
use regex::{Matches, Regex};
pub trait RegexSplit {
fn split_inclusive<'r, 't>(&'r self, text: &'t str) -> SplitInclusive<'r, 't>;
fn split_inclusive_left<'r, 't>(&'r self, text: &'t str) -> SplitInclusiveLeft<'r, 't>;
}
#[derive(Debug)]
pub struct SplitInclusive<'r, 't> {
finder: Matches<'r, 't>,
last: usize,
text: &'t str,
}
impl<'r, 't> Iterator for SplitInclusive<'r, 't> {
type Item = &'t str;
fn next(&mut self) -> Option<Self::Item> {
match self.finder.next() {
None => {
if self.last > self.text.len() {
None
} else {
let s = &self.text[self.last..];
self.last = self.text.len() + 1; Some(s)
}
}
Some(m) => {
let matched = &self.text[self.last..m.end()];
self.last = m.end();
Some(matched)
}
}
}
}
impl<'r, 't> FusedIterator for SplitInclusive<'r, 't> {}
#[derive(Debug)]
pub struct SplitInclusiveLeft<'r, 't> {
finder: Matches<'r, 't>,
last: usize,
text: &'t str,
}
impl<'r, 't> Iterator for SplitInclusiveLeft<'r, 't> {
type Item = &'t str;
fn next(&mut self) -> Option<Self::Item> {
match self.finder.next() {
None => {
if self.last > self.text.len() {
None
} else {
let s = &self.text[self.last..];
self.last = self.text.len() + 1; Some(s)
}
}
Some(m) => {
let matched = &self.text[self.last..m.start()];
self.last = m.start();
Some(matched)
}
}
}
}
impl<'r, 't> FusedIterator for SplitInclusiveLeft<'r, 't> {}
impl RegexSplit for Regex {
fn split_inclusive<'r, 't>(&'r self, text: &'t str) -> SplitInclusive<'r, 't> {
SplitInclusive {
finder: self.find_iter(text),
last: 0,
text,
}
}
fn split_inclusive_left<'r, 't>(&'r self, text: &'t str) -> SplitInclusiveLeft<'r, 't> {
SplitInclusiveLeft {
finder: self.find_iter(text),
last: 0,
text,
}
}
}