1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#[derive(Copy, Clone)]
pub struct Cursor<'a> {
slice: &'a [char],
index: usize,
}
impl<'a> Cursor<'a> {
pub fn new(slice: &'a [char]) -> Cursor<'a> {
Cursor { slice, index: 0 }
}
#[inline]
pub fn is_empty(&self) -> bool {
self.index >= self.slice.len()
}
#[inline]
pub fn peek0(&self) -> Option<char> {
self.slice.get(self.index).copied()
}
#[inline]
pub fn peek_range(&self, n: usize) -> Option<&'a [char]> {
self.slice.get(self.index..n)
}
pub fn consume(&mut self, target: char) -> bool {
match self.peek0() {
Some(c) => c == target,
_ => false,
}
}
pub fn advance(&mut self) -> Option<char> {
let c = self.slice.get(self.index).copied();
if c.is_some() {
self.index += 1;
}
c
}
pub fn advance_n(&mut self, n: usize) -> Option<&'a [char]> {
let slice = self.slice.get(self.index..n);
if slice.is_some() {
self.index += n;
}
slice
}
pub fn advance_while(&mut self, mut pred: impl FnMut(char) -> bool) -> &'a [char] {
let begin = self.index;
while !self.is_empty() && pred(self.peek0().unwrap()) {
self.index += 1;
}
&self.slice[begin..self.index]
}
#[inline]
pub fn index(&self) -> usize {
self.index
}
#[inline]
pub fn iter<'c>(&'c self) -> Iter<'a, 'c> {
Iter(0, self)
}
}
pub struct Iter<'a, 'b>(usize, &'b Cursor<'a>);
impl<'a, 'b> Iterator for Iter<'a, 'b> {
type Item = char;
fn next(&mut self) -> Option<Self::Item> {
let c = self.1.slice.get(self.0 + self.1.index).copied();
self.0 += 1;
c
}
}