use std::iter::Iterator;
use super::Region;
#[derive(Debug)]
pub struct Captures<'t> {
text: &'t str,
region: Region
}
impl<'t> Captures<'t> {
pub fn new(text: &'t str, region: Region) -> Captures<'t> {
Captures {
text: text,
region: region
}
}
pub fn pos(&self, pos: usize) -> Option<(usize, usize)> {
self.region.pos(pos)
}
pub fn at(&self, pos: usize) -> Option<&'t str> {
self.pos(pos).map(|(beg, end)| &self.text[beg..end])
}
pub fn len(&self) -> usize {
self.region.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn iter(&'t self) -> SubCaptures<'t> {
SubCaptures { idx: 0, caps: self }
}
pub fn iter_pos(&'t self) -> SubCapturesPos<'t> {
SubCapturesPos { idx: 0, caps: self }
}
}
pub struct SubCaptures<'t> {
idx: usize,
caps: &'t Captures<'t>
}
impl<'t> Iterator for SubCaptures<'t> {
type Item = Option<&'t str>;
fn next(&mut self) -> Option<Option<&'t str>> {
if self.idx < self.caps.len() {
self.idx += 1;
Some(self.caps.at(self.idx - 1))
} else {
None
}
}
}
pub struct SubCapturesPos<'t> {
idx: usize,
caps: &'t Captures<'t>
}
impl<'t> Iterator for SubCapturesPos<'t> {
type Item = Option<(usize, usize)>;
fn next(&mut self) -> Option<Option<(usize, usize)>> {
if self.idx < self.caps.len() {
self.idx += 1;
Some(self.caps.pos(self.idx - 1))
} else {
None
}
}
}