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
use errors::Result;
use std::io::Read;

const NL: u8 = '\n' as u8;

/// Find the line corresponding to the given position.
pub fn find_line<'a, R: AsMut<Read + 'a>>(
    mut reader: R,
    span: (usize, usize),
) -> Result<(String, usize, (usize, usize))> {
    let r = reader.as_mut();

    let mut line = 0usize;
    let mut current = 0usize;
    let mut buffer: Vec<u8> = Vec::new();

    let start = span.0;
    let end = span.1;

    let mut it = r.bytes().peekable();
    let mut read = 0usize;

    while let Some(b) = it.next() {
        let b = b?;
        read += 1;

        match b {
            NL => {}
            _ => {
                buffer.push(b);
                continue;
            }
        }

        let start_of_line = current;
        current += read;

        if current > start {
            let buffer = String::from_utf8(buffer)?;
            let end = ::std::cmp::min(end, current);
            let range = (start - start_of_line, end - start_of_line);
            return Ok((buffer, line, range));
        }

        read = 0usize;
        line += 1;
        buffer.clear();
    }

    Err("bad file position".into())
}

/// Find the range corresponding to the given position.
pub fn find_range<'a, R: AsMut<Read + 'a>>(
    mut reader: R,
    span: (usize, usize),
) -> Result<(usize, usize, usize, usize)> {
    let r = reader.as_mut();

    let mut line_start = 0usize;
    let mut line_end = 0usize;

    let mut col_start = 0usize;
    let mut col_end = 0usize;

    let mut line = 0usize;
    let mut col = 0usize;

    let mut it = r.bytes().enumerate();

    while let Some((c, b)) = it.next() {
        let b = b?;

        let mut new_line = b == NL;

        if new_line {
            line += 1;
            col = 0;
        }

        if c == span.0 {
            line_start = line;
            col_start = col;
        }

        if c == span.1 {
            line_end = line;
            col_end = col;
            break;
        }

        if !new_line {
            col += 1;
        }
    }

    Ok((line_start, line_end, col_start, col_end))
}