forked from rust-lang/annotate-snippets-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor_line.rs
40 lines (36 loc) · 1.07 KB
/
cursor_line.rs
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
use super::end_line::EndLine;
pub(crate) struct CursorLines<'a>(&'a str);
impl CursorLines<'_> {
pub(crate) fn new(src: &str) -> CursorLines<'_> {
CursorLines(src)
}
}
impl<'a> Iterator for CursorLines<'a> {
type Item = (&'a str, EndLine);
fn next(&mut self) -> Option<Self::Item> {
if self.0.is_empty() {
None
} else {
self.0
.find('\n')
.map(|x| {
let ret = if 0 < x {
if self.0.as_bytes()[x - 1] == b'\r' {
(&self.0[..x - 1], EndLine::Crlf)
} else {
(&self.0[..x], EndLine::Lf)
}
} else {
("", EndLine::Lf)
};
self.0 = &self.0[x + 1..];
ret
})
.or_else(|| {
let ret = Some((self.0, EndLine::Eof));
self.0 = "";
ret
})
}
}
}