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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use super::Result;
use std::io::{ErrorKind, Read, Seek, SeekFrom};

pub fn start_of_next_line<T: Seek + Read>(f: &mut T) -> Result<Option<u64>> {
    let mut buf = [0; 1];
    let mut pos = f.stream_position()?;

    loop {
        pos += 1;
        if let Err(e) = f.read_exact(&mut buf) {
            if e.kind() == ErrorKind::UnexpectedEof {
                return Ok(None);
            } else {
                return Err(e.into());
            }
        }

        if buf[0] == 0x0a {
            return Ok(Some(pos));
        }
    }
}

pub fn start_of_prev_line<T: Seek + Read>(f: &mut T) -> Result<Option<u64>> {
    start_of_current_line(f)?;

    let mut buf = [0; 1];
    let mut pos = f.stream_position()?;

    if pos == 0 {
        return Ok(None);
    }

    pos -= 1;
    f.seek(SeekFrom::Start(pos))?;

    loop {
        if pos == 0 {
            f.seek(SeekFrom::Start(0))?;
            return Ok(Some(0));
        }

        pos -= 1;
        f.seek(SeekFrom::Start(pos))?;
        f.read_exact(&mut buf)?;

        if buf[0] == 0x0a {
            return Ok(Some(pos + 1));
        }
    }
}

pub fn start_of_current_line<T: Seek + Read>(f: &mut T) -> Result<u64> {
    let mut buf = [0; 1];
    let mut pos = f.stream_position()?;

    if let Err(e) = f.read_exact(&mut buf) {
        // If we try to read past the end of the file, which is what
        // ErrorKind::UnexpectedEof represents, it's not really a problem. We
        // just quietly drop in to the loop below and start backtracking. If
        // not, we raise the error.
        if e.kind() != ErrorKind::UnexpectedEof {
            return Err(e.into());
        }
    }

    if buf[0] == 0x0a {
        if pos == 0 {
            f.seek(SeekFrom::Start(0))?;
            return Ok(0);
        }
        f.seek(SeekFrom::Start(pos - 1))?;
        pos -= 1;
    } else {
        f.seek(SeekFrom::Start(pos))?;
    }

    loop {
        // If we're at the start we are by definition at the start of the line,
        // so just rewind the single-byte read we just did and return a 0
        // position.
        if pos == 0 {
            f.seek(SeekFrom::Start(0))?;
            return Ok(pos);
        }

        if let Err(e) = f.read_exact(&mut buf) {
            if e.kind() != ErrorKind::UnexpectedEof {
                return Err(e.into());
            }
        } else {
            // If we've read a newline character (0x0a), we've reached the start
            // of the line and can return the position we just read.
            if buf[0] == 0x0a {
                return Ok(pos + 1);
            }
        }

        // We haven't reached the start of the line, so we go back a byte and
        // start the loop again.
        pos -= 1;
        f.seek(SeekFrom::Start(pos))?;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::{BufRead, Cursor, Seek, SeekFrom};
    use test_case::test_case;

    fn read_line(r: &mut impl BufRead) -> Result<String> {
        let mut buf = String::new();
        r.read_line(&mut buf)?;
        Ok(buf)
    }

    #[test_case("",                         0  => ""         ; "empty file")]
    #[test_case("line 1\nline 2\nline 3",   0  => "line 1\n" ; "start of first line")]
    #[test_case("line 1\nline 2\nline 3",   3  => "line 1\n" ; "middle of first line")]
    #[test_case("line 1\nline 2\nline 3",   6  => "line 1\n" ; "end of first line")]
    #[test_case("line 1\nline 2\nline 3",   7  => "line 2\n" ; "start of second line")]
    #[test_case("line 1\nline 2\nline 3",   12 => "line 2\n" ; "middle of second line")]
    #[test_case("line 1\nline 2\nline 3",   13 => "line 2\n" ; "end of second line")]
    #[test_case("line 1\nline 2\nline 3",   14 => "line 3"   ; "start of third line")]
    #[test_case("line 1\nline 2\nline 3",   15 => "line 3"   ; "middle of third line")]
    #[test_case("line 1\nline 2\nline 3",   19 => "line 3"   ; "end of third line")]
    #[test_case("line 1\nline 2\nline 3",   26 => "line 3"   ; "past eof")]
    #[test_case("line 1\nline 2\nline 3\n", 20 => "line 3\n" ; "last line when line ends with eof")]
    fn test_start_of_current_line(s: &str, pos: u64) -> String {
        let mut r = Cursor::new(s.as_bytes());
        r.seek(SeekFrom::Start(pos)).unwrap();
        start_of_current_line(&mut r).unwrap();
        read_line(&mut r).unwrap()
    }

    #[test_case("line 1\nline 2\nline 3", 0  => Some(7)  ; "start of first line")]
    #[test_case("line 1\nline 2\nline 3", 2  => Some(7)  ; "middle of first line")]
    #[test_case("line 1\nline 2\nline 3", 6  => Some(7)  ; "end of first line")]
    #[test_case("line 1\nline 2\nline 3", 7  => Some(14) ; "start of second line")]
    #[test_case("line 1\nline 2\nline 3", 9  => Some(14) ; "middle of second line")]
    #[test_case("line 1\nline 2\nline 3", 13 => Some(14) ; "end of second line")]
    #[test_case("line 1\nline 2\nline 3", 14 => None     ; "start of last line")]
    #[test_case("line 1\nline 2\nline 3", 16 => None     ; "middle of last line")]
    #[test_case("line 1\nline 2\nline 3", 19 => None     ; "end of last line")]
    fn test_start_of_next_line(s: &str, pos: u64) -> Option<u64> {
        let mut r = Cursor::new(s.as_bytes());
        r.seek(SeekFrom::Start(pos)).unwrap();
        start_of_next_line(&mut r).unwrap()
    }

    #[test_case("line 1\nline 2\nline 3", 0  => None     ; "start of first line")]
    #[test_case("line 1\nline 2\nline 3", 2  => None     ; "middle of first line")]
    #[test_case("line 1\nline 2\nline 3", 1  => None     ; "second letter of first line")]
    #[test_case("line 1\nline 2\nline 3", 6  => None     ; "end of first line")]
    #[test_case("line 1\nline 2\nline 3", 7  => Some(0)  ; "start of second line")]
    #[test_case("line 1\nline 2\nline 3", 9  => Some(0)  ; "middle of second line")]
    #[test_case("line 1\nline 2\nline 3", 13 => Some(0)  ; "end of second line")]
    #[test_case("line 1\nline 2\nline 3", 14 => Some(7)  ; "start of last line")]
    #[test_case("line 1\nline 2\nline 3", 16 => Some(7)  ; "middle of last line")]
    #[test_case("line 1\nline 2\nline 3", 19 => Some(7)  ; "end of last line")]
    fn test_start_of_prev_line(s: &str, pos: u64) -> Option<u64> {
        let mut r = Cursor::new(s.as_bytes());
        r.seek(SeekFrom::Start(pos)).unwrap();
        start_of_prev_line(&mut r).unwrap()
    }
}