#[derive(Debug)]
pub struct Stream<'a, T> {
pub idx: usize,
data: &'a [T],
}
impl<T: Copy> Stream<'_, T> {
#[inline]
pub fn current_cpy(&self) -> Option<T> {
self.data.get(self.idx).copied()
}
}
impl<T: Eq + Copy> Stream<'_, T> {
#[inline]
pub fn expect_and_skip(&mut self, expect: T) -> Option<T> {
let c = self.current_cpy()?;
if c == expect {
self.advance();
Some(c)
} else {
None
}
}
pub fn expect_oneof_and_skip(&mut self, expect: &[T]) -> Option<T> {
let c = self.current_cpy()?;
if expect.contains(&c) {
self.advance();
return Some(c);
}
None
}
#[inline]
pub fn expect_and_skip_cond(&mut self, expect: T) -> bool {
self.expect_and_skip(expect).is_some()
}
}
impl<'a, T> Stream<'a, T> {
#[inline]
pub const fn new(data: &'a [T]) -> Self {
Self { data, idx: 0 }
}
#[inline]
pub const fn advance(&mut self) {
self.idx += 1;
}
#[inline]
pub fn current(&self) -> Option<&T> {
self.data.get(self.idx)
}
#[inline]
pub const fn is_eof(&self) -> bool {
self.idx >= self.data.len()
}
#[inline]
pub fn slice(&self, from: usize, to: usize) -> &'a [T] {
&self.data[from..to]
}
}