use crate::{
buffer::{Buffer, GapBuffer, IdxChars},
exec::cached_stdin::{CachedStdin, CachedStdinIter},
};
pub trait IterBoundedChars {
fn iter_between(&self, from: usize, to: usize) -> CharIter<'_>;
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter<'_>;
}
impl IterBoundedChars for GapBuffer {
fn iter_between(&self, from: usize, to: usize) -> CharIter {
CharIter::Slice(self.slice(from, to).indexed_chars(from, false))
}
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter {
CharIter::Slice(self.slice(to, from).indexed_chars(to, true))
}
}
impl IterBoundedChars for Buffer {
fn iter_between(&self, from: usize, to: usize) -> CharIter {
CharIter::Slice(self.txt.slice(from, to).indexed_chars(from, false))
}
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter {
CharIter::Slice(self.txt.slice(to, from).indexed_chars(to, true))
}
}
pub enum CharIter<'a> {
Slice(IdxChars<'a>),
StdIn(CachedStdinIter<'a>),
}
impl<'a> Iterator for CharIter<'a> {
type Item = (usize, char);
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Slice(it) => it.next(),
Self::StdIn(it) => it.next(),
}
}
}
impl IterBoundedChars for CachedStdin {
fn iter_between(&self, from: usize, to: usize) -> CharIter {
CharIter::StdIn(CachedStdinIter {
inner: self,
from,
to,
})
}
fn rev_iter_between(&self, from: usize, to: usize) -> CharIter {
CharIter::StdIn(CachedStdinIter {
inner: self,
from,
to,
})
}
}