1
//! Version of `std::str::Lines` that tracks line numbers and has `remainder()`
2

            
3
/// Version of `std::str::Lines` that tracks line numbers and has `remainder()`
4
///
5
/// Implements `Iterator`, returning one `str` for each line, with the `'\n'` removed.
6
///
7
/// Missing final newline is silently tolerated.
8
#[derive(Debug, Clone)]
9
pub struct Lines<'s> {
10
    /// Line number at the start of `rest`
11
    lno: usize,
12
    /// The remaining part of the document
13
    rest: &'s str,
14
}
15

            
16
/// Extension trait adding a method to `str`
17
pub trait StrExt: AsRef<str> {
18
    /// Remove `count` bytes from the end of `self`
19
    ///
20
    /// # Panics
21
    ///
22
    /// Panics if `count > self.len()`.
23
8515
    fn strip_end_counted(&self, count: usize) -> &str {
24
8515
        let s = self.as_ref();
25
8515
        &s[0..s.len().checked_sub(count).expect("stripping too much")]
26
8515
    }
27
}
28
impl StrExt for str {}
29

            
30
/// Information about the next line we have peeked
31
///
32
/// To get the line as an actual string, pass this to `peeked_line`.
33
///
34
/// # Correctness
35
///
36
/// Each `Peeked` is only valid in conjunction with the `Lines` that returned it,
37
/// and becomes invalidated if the `Lines` is modified
38
/// (ie, it can be invalidated by calls that take `&mut Lines`).
39
///
40
/// Cloning a `Peeked` is hazrdous since using it twice would be wrong.
41
///
42
/// None of this is checked at compile- or run-time.
43
// We could perhaps use lifetimes somehow to enforce this,
44
// but `ItemStream` wants `Peeked` to be `'static` and `Clone`.
45
#[derive(Debug, Clone, amplify::Getters)]
46
pub struct Peeked {
47
    /// The length of the next line
48
    //
49
    // # Invariant
50
    //
51
    // `rest[line_len]` is a newline, or `line_len` is `rest.len()`.
52
    #[getter(as_copy)]
53
    line_len: usize,
54
}
55

            
56
impl<'s> Lines<'s> {
57
    /// Start reading lines from a document as a string
58
600
    pub fn new(s: &'s str) -> Self {
59
600
        Lines { lno: 1, rest: s }
60
600
    }
61

            
62
    /// Line number of the next line we'll read
63
83881
    pub fn peek_lno(&self) -> usize {
64
83881
        self.lno
65
83881
    }
66

            
67
    /// Peek the next line
68
132599
    pub fn peek(&self) -> Option<Peeked> {
69
132599
        if self.rest.is_empty() {
70
734
            None
71
131865
        } else if let Some(newline) = self.rest.find('\n') {
72
131847
            Some(Peeked { line_len: newline })
73
        } else {
74
18
            Some(Peeked {
75
18
                line_len: self.rest.len(),
76
18
            })
77
        }
78
132599
    }
79

            
80
    /// The rest of the file as a `str`
81
146534
    pub fn remaining(&self) -> &'s str {
82
146534
        self.rest
83
146534
    }
84

            
85
    /// After `peek`, advance to the next line, consuming the one that was peeked
86
    ///
87
    /// # Correctness
88
    ///
89
    /// See [`Peeked`].
90
    #[allow(clippy::needless_pass_by_value)] // Yes, we want to consume Peeked
91
133616
    pub fn consume_peeked(&mut self, peeked: Peeked) -> &'s str {
92
133616
        let line = self.peeked_line(&peeked);
93
133616
        self.rest = &self.rest[peeked.line_len..];
94
133616
        if !self.rest.is_empty() {
95
133598
            debug_assert!(self.rest.starts_with('\n'));
96
133598
            self.rest = &self.rest[1..];
97
18
        }
98
133616
        self.lno += 1;
99
133616
        line
100
133616
    }
101

            
102
    /// After `peek`, obtain the actual peeked line as a `str`
103
    ///
104
    /// As with [`<Lines as Iterator>::next`](Lines::next), does not include the newline.
105
    // Rustdoc doesn't support linking` fully qualified syntax.
106
    // https://github.com/rust-lang/rust/issues/74563
107
    ///
108
    /// # Correctness
109
    ///
110
    /// See [`Peeked`].
111
218579
    pub fn peeked_line(&self, peeked: &Peeked) -> &'s str {
112
218579
        &self.rest[0..peeked.line_len()]
113
218579
    }
114
}
115

            
116
impl<'s> Iterator for Lines<'s> {
117
    type Item = &'s str;
118

            
119
49749
    fn next(&mut self) -> Option<&'s str> {
120
49749
        let peeked = self.peek()?;
121
49749
        let line = self.consume_peeked(peeked);
122
49749
        Some(line)
123
49749
    }
124
}