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
use memchr::memchr;
use std::io::{self, BufReader, Read};
use crate::buffer::ScratchBuffer;
use crate::utils::trim_trailing_crlf;
/// A zero-copy & optimized line reader.
///
/// This reader recognizes both `LF` & `CRLF` line terminators, but not single
/// `CR`.
pub struct LineReader<R> {
inner: ScratchBuffer<R>,
}
impl<R: Read> LineReader<R> {
/// Create a new reader with default using the provided reader implementing
/// [`std::io::Read`].
///
/// Avoid providing a buffered reader because buffering will be handled for
/// you by the [`LineReader`].
pub fn from_reader(inner: R) -> Self {
Self {
inner: ScratchBuffer::new(inner),
}
}
/// Create a new reader with provided buffer capacity and using the provided
/// reader implementing [`std::io::Read`].
///
/// Avoid providing a buffered reader because buffering will be handled for
/// you by the [`LineReader`].
pub fn with_capacity(capacity: usize, inner: R) -> Self {
Self {
inner: ScratchBuffer::with_capacity(capacity, inner),
}
}
/// Consume the reader to count the number of lines as fast as possible.
pub fn count_lines(&mut self) -> io::Result<u64> {
let mut count: u64 = 0;
let mut current_is_empty = true;
loop {
let input = self.inner.fill_buf()?;
let len = input.len();
if len == 0 {
if !current_is_empty {
count += 1;
}
return Ok(count);
}
match memchr(b'\n', input) {
None => {
self.inner.consume(len);
current_is_empty = false;
}
Some(pos) => {
count += 1;
self.inner.consume(pos + 1);
current_is_empty = true;
}
};
}
}
/// Attempt to read the next line from underlying reader.
///
/// Will return `None` if the end of stream was reached.
pub fn read_line(&mut self) -> io::Result<Option<&[u8]>> {
self.inner.reset();
loop {
let input = self.inner.fill_buf()?;
let len = input.len();
if len == 0 {
if self.inner.has_something_saved() {
return Ok(Some(trim_trailing_crlf(self.inner.saved())));
}
return Ok(None);
}
match memchr(b'\n', input) {
None => {
self.inner.save();
}
Some(pos) => {
let bytes = self.inner.flush(pos + 1);
return Ok(Some(trim_trailing_crlf(bytes)));
}
};
}
}
/// Return the current byte offset of the reader.
#[inline(always)]
pub fn position(&self) -> u64 {
self.inner.position()
}
/// Return the underlying [`BufReader`].
#[inline(always)]
pub fn into_bufreader(self) -> BufReader<R> {
self.inner.into_bufreader()
}
/// Return the underlying reader.
///
/// **BEWARE**: Already buffered data will be lost!
#[inline(always)]
pub fn into_inner(self) -> R {
self.inner.into_bufreader().into_inner()
}
}
#[cfg(test)]
mod tests {
use std::io::Cursor;
use super::*;
#[test]
fn test_read_line() -> io::Result<()> {
let tests: &[(&[u8], Vec<&[u8]>)] = &[
(b"", vec![]),
(b"test", vec![b"test"]),
(
b"hello\nwhatever\r\nbye!",
vec![b"hello", b"whatever", b"bye!"],
),
(
b"hello\nwhatever\nbye!\n",
vec![b"hello", b"whatever", b"bye!"],
),
(
b"hello\nwhatever\r\nbye!\n\n\r\n\n",
vec![b"hello", b"whatever", b"bye!", b"", b"", b""],
),
];
for (data, expected) in tests {
let mut reader = LineReader::from_reader(Cursor::new(data));
let mut lines = Vec::new();
while let Some(line) = reader.read_line()? {
lines.push(line.to_vec());
}
assert_eq!(lines, *expected);
let mut reader = LineReader::from_reader(Cursor::new(data));
assert_eq!(reader.count_lines()?, expected.len() as u64);
}
Ok(())
}
}