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
use unicode_width::UnicodeWidthChar;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextPos {
pub col: usize,
pub line: usize,
pub byte: usize,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Span {
pub start: TextPos,
pub end: TextPos,
}
impl TextPos {
pub fn start() -> TextPos {
TextPos {
col: 0,
line: 0,
byte: 0,
}
}
pub fn next(self, src: &str) -> Option<(char, TextPos)> {
let mut iter = src[self.byte..].char_indices();
let (i, c) = match iter.next() {
Some((_, c)) => {
match iter.next() {
Some((i, _)) => (self.byte + i, c),
None => (src.len(), c),
}
},
None => return None,
};
let pos = if c == '\n' {
TextPos {
col: 0,
line: self.line + 1,
byte: i,
}
}
else {
TextPos {
col: self.col + c.width().unwrap_or(0),
line: self.line,
byte: i,
}
};
Some((c, pos))
}
}