1use std::str::from_utf8_unchecked;
2
3pub struct Line<'a> {
4 pub bin: &'a [u8],
5 pub pre: usize,
6 pub cur: usize,
7}
8
9impl<'a> Line<'a> {
10 pub fn new(str: &'a str) -> Self {
11 Self {
12 bin: str.as_bytes(),
13 pre: 0,
14 cur: 0,
15 }
16 }
17}
18
19impl<'a> Iterator for Line<'a> {
20 type Item = &'a str;
21
22 fn next(&mut self) -> Option<Self::Item> {
23 let bin = self.bin;
24 let len = bin.len();
25 let mut cur = self.cur;
26 while cur < len {
27 self.cur += 1;
28 let i = bin[cur];
29 let is_break = if i == b'\r' {
30 let t = cur + 1;
31 if t < len && bin[t] == b'\n' {
32 self.cur += 1;
33 }
34 true
35 } else {
36 i == b'\n'
37 };
38 if is_break {
39 let r = &bin[self.pre..cur];
40 self.pre = self.cur;
41 return Some(unsafe { from_utf8_unchecked(r) });
42 }
43 cur = self.cur;
44 }
45 if self.cur != self.pre {
46 let r = Some(unsafe { from_utf8_unchecked(&bin[self.pre..]) });
47 self.pre = self.cur;
48 return r;
49 }
50 None
51 }
52}