-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinput.rs
175 lines (154 loc) · 5.03 KB
/
input.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
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
168
169
170
171
172
173
174
175
use std::collections::VecDeque;
use std::io::BufRead;
use std::{io, iter};
use content_inspector::ContentType;
use miette::{MietteSpanContents, SourceCode, SourceSpan, SpanContents};
const LINES_BEFORE: usize = 1;
const LINES_AFTER: usize = 3;
pub struct InputReader<R = Box<dyn BufRead>> {
reader: R,
buffer: Vec<u8>,
/// The absolute positions of each line in the buffer.
line_sizes: VecDeque<usize>,
/// The absolute line number of the current line.
line_count: usize,
/// The absolute position of the current line.
offset: usize,
/// The position of the current line relative to the buffer beginning.
rel_offset: usize,
}
impl<R: BufRead> InputReader<R> {
pub fn new(reader: R) -> Self {
Self {
reader,
buffer: Vec::new(),
line_sizes: VecDeque::new(),
line_count: 0,
offset: 0,
rel_offset: 0,
}
}
pub fn next_line(&mut self) -> io::Result<Option<InputLine>> {
if let Some(n) = self.line_sizes.pop_front() {
// Shift to the second line
debug_assert!(self.line_sizes.len() >= LINES_BEFORE);
self.buffer.drain(..n);
self.rel_offset -= n;
// Peek one line ahead
self.read_line()?;
} else {
// In the initial call, pad precedent empty lines,
self.line_sizes.reserve(LINES_BEFORE + 1 + LINES_AFTER);
self.line_sizes.extend(iter::repeat(0).take(LINES_BEFORE));
// and then peek subsequent context lines
for _ in 0..=LINES_AFTER {
self.read_line()?;
}
}
let source;
if let Some(&size) = self.line_sizes.get(LINES_BEFORE) {
source = Some(InputLine {
buffer: &self.buffer,
line_sizes: &self.line_sizes,
line_count: self.line_count,
offset: self.offset,
rel_offset: self.rel_offset,
size,
});
self.line_count += 1;
self.offset += size;
self.rel_offset += size;
} else {
// EOF reached
self.line_count = usize::MAX;
source = None;
}
Ok(source)
}
fn read_line(&mut self) -> io::Result<usize> {
// TODO: limit line size
let size = self.reader.read_until(b'\n', &mut self.buffer)?;
if size != 0 {
self.line_sizes.push_back(size);
}
Ok(size)
}
}
#[derive(Debug)]
pub struct InputLine<'a> {
buffer: &'a [u8],
line_sizes: &'a VecDeque<usize>,
line_count: usize,
offset: usize,
rel_offset: usize,
size: usize,
}
impl<'a> InputLine<'a> {
/// Returns the content of this line.
pub fn contents(&self) -> &'a [u8] {
&self.buffer[self.rel_offset..self.rel_offset + self.size]
}
/// Returns the absolute offset of a byte index relative to the line start.
pub fn offset_of(&self, i: usize) -> usize {
self.offset + i
}
pub fn content_type(&self) -> ContentType {
content_inspector::inspect(self.buffer)
}
}
impl SourceCode for InputLine<'_> {
fn read_span<'a>(
&'a self,
span: &SourceSpan,
lines_before: usize,
lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, miette::MietteError> {
debug_assert!((self.offset..self.offset + self.size).contains(&span.offset()));
let start;
let offset;
let line;
let column;
if lines_before == 0 {
offset = span.offset();
column = offset - self.offset;
start = self.rel_offset + column;
line = self.line_count;
} else {
// count precedent lines and bytes
let (lines, bytes) = self
.line_sizes
.range(0..LINES_BEFORE)
.copied()
.rev()
.take(lines_before)
.take_while(|&n| n > 0)
.fold((0, 0), |(lines, bytes), n| (lines + 1, bytes + n));
offset = self.offset - bytes;
column = 0;
start = self.rel_offset - bytes;
line = self.line_count - lines;
}
let end;
let line_count;
if lines_after == 0 {
end = start + span.len();
line_count = self.line_count;
} else {
// count subsequent lines and bytes
let (lines, bytes) = self
.line_sizes
.range(LINES_BEFORE..)
.copied()
.take(lines_before + 1)
.take_while(|&n| n > 0)
.fold((0, 0), |(lines, bytes), n| (lines + 1, bytes + n));
end = self.rel_offset + bytes;
line_count = self.line_count + lines;
}
let data = &self.buffer[start..end];
let span = SourceSpan::from((offset, end - start));
Ok(Box::new(MietteSpanContents::new(
data, span, line, column, line_count,
)))
}
}