1use std::str::Chars;
23/// Peekable iterator over a char sequence.
4///
5/// Next characters can be peeked via `first` method,
6/// and position can be shifted forward via `bump` method.
7pub struct Cursor<'a> {
8 len_remaining: usize,
9/// Iterator over chars. Slightly faster than a &str.
10chars: Chars<'a>,
11#[cfg(debug_assertions)]
12prev: char,
13}
1415pub(crate) const EOF_CHAR: char = '\0';
1617impl<'a> Cursor<'a> {
18pub fn new(input: &'a str) -> Cursor<'a> {
19Cursor {
20 len_remaining: input.len(),
21 chars: input.chars(),
22#[cfg(debug_assertions)]
23prev: EOF_CHAR,
24 }
25 }
2627pub fn as_str(&self) -> &'a str {
28self.chars.as_str()
29 }
3031/// Returns the last eaten symbol (or `'\0'` in release builds).
32 /// (For debug assertions only.)
33pub(crate) fn prev(&self) -> char {
34#[cfg(debug_assertions)]
35{
36self.prev
37 }
3839#[cfg(not(debug_assertions))]
40{
41 EOF_CHAR
42 }
43 }
4445/// Peeks the next symbol from the input stream without consuming it.
46 /// If requested position doesn't exist, `EOF_CHAR` is returned.
47 /// However, getting `EOF_CHAR` doesn't always mean actual end of file,
48 /// it should be checked with `is_eof` method.
49pub fn first(&self) -> char {
50// `.next()` optimizes better than `.nth(0)`
51self.chars.clone().next().unwrap_or(EOF_CHAR)
52 }
5354/// Peeks the second symbol from the input stream without consuming it.
55pub(crate) fn second(&self) -> char {
56// `.next()` optimizes better than `.nth(1)`
57let mut iter = self.chars.clone();
58iter.next();
59iter.next().unwrap_or(EOF_CHAR)
60 }
6162/// Peeks the third symbol from the input stream without consuming it.
63pub fn third(&self) -> char {
64// `.next()` optimizes better than `.nth(1)`
65let mut iter = self.chars.clone();
66iter.next();
67iter.next();
68iter.next().unwrap_or(EOF_CHAR)
69 }
7071/// Checks if there is nothing more to consume.
72pub(crate) fn is_eof(&self) -> bool {
73self.chars.as_str().is_empty()
74 }
7576/// Returns amount of already consumed symbols.
77pub(crate) fn pos_within_token(&self) -> u32 {
78 (self.len_remaining - self.chars.as_str().len()) as u3279 }
8081/// Resets the number of bytes consumed to 0.
82pub(crate) fn reset_pos_within_token(&mut self) {
83self.len_remaining = self.chars.as_str().len();
84 }
8586/// Moves to the next character.
87pub(crate) fn bump(&mut self) -> Option<char> {
88let c = self.chars.next()?;
8990#[cfg(debug_assertions)]
91{
92self.prev = c;
93 }
9495Some(c)
96 }
9798/// Eats symbols while predicate returns true or until the end of file is reached.
99pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
100// It was tried making optimized version of this for eg. line comments, but
101 // LLVM can inline all of this and compile it down to fast iteration over bytes.
102while predicate(self.first()) && !self.is_eof() {
103self.bump();
104 }
105 }
106107pub(crate) fn eat_until(&mut self, byte: u8) {
108self.chars = match memchr::memchr(byte, self.as_str().as_bytes()) {
109Some(index) => self.as_str()[index..].chars(),
110None => "".chars(),
111 }
112 }
113}