use std::str::Chars;
pub(crate) struct Cursor<'a> {
chars: Chars<'a>,
len_remaining: usize,
}
pub(crate) const EOF_CHAR: char = '\0';
impl<'a> Cursor<'a> {
pub(crate) fn new(input: &'a str) -> Cursor<'a> {
Cursor {
len_remaining: input.len(),
chars: input.chars(),
}
}
pub(crate) fn first(&self) -> char {
self.chars.clone().next().unwrap_or(EOF_CHAR)
}
pub(crate) fn is_eof(&self) -> bool {
self.chars.as_str().is_empty()
}
pub(crate) fn pos_within_token(&self) -> u32 {
(self.len_remaining - self.chars.as_str().len()) as u32
}
pub(crate) fn reset_pos_within_token(&mut self) {
self.len_remaining = self.chars.as_str().len();
}
pub(crate) fn bump(&mut self) -> Option<char> {
let c = self.chars.next()?;
Some(c)
}
pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
while predicate(self.first()) && !self.is_eof() {
self.bump();
}
}
}