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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use super::{super::results::elapsed, conv::ConvertSlice, error::ErrWriter, read::Reader};
use opts::number::units;
use results::{Error, Result, Success};
use std::{
cmp::Ordering,
io::{BufRead, Read, Write},
time::SystemTime,
};
const BYTE_THRESHOLD_FOR_REPORT: usize = 5 * units::MB;
const BLOCK_THRESHOLD_FOR_REPORT: usize = 25;
const LINE_THRESHOLD_FOR_REPORT: usize = 20;
pub fn standard<R, W, C, E>(mut r: Reader<R>, w: &mut W, c: C, mut e: ErrWriter<E>) -> Result<Success>
where
R: Read,
W: Write,
C: ConvertSlice<u8>,
E: Write,
{
let start = SystemTime::now();
let mut bytes = 0;
loop {
let converted = match e.handle(r.fill_buf())? {
Some(ref buf) if buf.len() == 0 => return Ok(Success::Bytes { bytes, start }),
None => continue,
Some(buf) => c.convert_copy(buf),
};
e.handle(w.write_all(&converted))?;
r.consume(converted.len());
bytes += converted.len();
e.report_status_standard(bytes, &start)?
}
}
pub fn bytes<R, W, C, E>(mut r: R, w: &mut W, c: &C, mut e: ErrWriter<E>, max_bytes: usize) -> Result<Success>
where
R: BufRead,
W: Write,
C: ConvertSlice<u8>,
E: Write,
{
let start = SystemTime::now();
let mut bytes = 0;
loop {
let converted = match e.handle(r.fill_buf())? {
None => continue,
Some(buf) => match usize::min(max_bytes.saturating_sub(bytes), buf.len()) {
0 => return Ok(Success::Bytes { bytes, start }),
n => c.convert_copy(&buf[..n]),
},
};
e.handle(w.write_all(&converted))?;
r.consume(converted.len());
bytes += converted.len();
}
}
pub fn block<R, W, C, E>(r: R, w: &mut W, c: C, mut e: ErrWriter<E>, block_size: usize) -> Result<Success>
where
R: BufRead,
W: Write,
C: ConvertSlice<u8>,
E: Write,
{
let (mut lines, mut padded, mut truncated): (usize, usize, usize) = (0, 0, 0);
let (mut buf, mut bytes): (Vec<u8>, std::io::Bytes<_>) = (Vec::with_capacity(block_size), r.bytes());
let start = SystemTime::now();
while {
if let Some(b) = skip_trailing_while_match(&mut bytes, &b'\n')? {
buf.push(b)
};
e.handle(fill_buf_until_match(&mut bytes, &mut buf, b'\n'))?;
buf.len() != 0
} {
lines += 1;
match buf.len().cmp(&block_size) {
Ordering::Greater => truncated += 1,
Ordering::Less => padded += 1,
Ordering::Equal => {},
}
c.convert_slice(&mut buf);
buf.resize(block_size, b' ');
w.write_all(&buf)?;
e.report_status_block(lines, &start);
buf.clear();
}
Ok(Success::Block {
start,
lines,
truncated,
padded,
block_size,
})
}
pub fn unblock<R, W, C, E>(r: R, w: &mut W, c: C, mut e: ErrWriter<E>, block_size: usize) -> Result<Success>
where
R: BufRead,
W: Write,
C: ConvertSlice<u8>,
E: Write,
{
let start = SystemTime::now();
let mut buf: Vec<u8> = Vec::with_capacity(block_size + 1); let mut bytes = r.bytes();
let mut blocks = 0;
while {
if let Some(Some(b)) = e.handle(skip_trailing_while_match(&mut bytes, &b' '))? {
buf.push(b)
};
fill_buf_to(&mut bytes, &mut buf, block_size)?;
buf.len() != 0
} {
buf.push(b'\n');
c.convert_slice(&mut buf);
e.handle(w.write_all(&mut buf))?;
blocks += 1;
e.report_status_unblock(blocks, &start);
buf.clear();
}
Ok(Success::Unblock {
blocks,
block_size,
start,
})
}
#[inline]
fn skip_trailing_while_match<I, T>(it: &mut I, unwanted: &T) -> std::io::Result<Option<T>>
where
I: Iterator<Item = ::std::io::Result<T>>,
T: PartialEq,
{
loop {
match it.next() {
Some(Ok(ref t)) if t == unwanted => {},
Some(t) => return Ok(Some(t?)),
None => return Ok(None),
}
}
}
#[inline]
pub fn fill_buf_until_match<I, T>(it: &mut I, buf: &mut Vec<T>, want: T) -> std::io::Result<()>
where
I: Iterator<Item = std::io::Result<T>>,
T: PartialEq,
{
loop {
match it.next() {
Some(Ok(ref t)) if t == &want => return Ok(()),
Some(Ok(t)) => buf.push(t),
Some(Err(err)) => return Err(err),
None => return Ok(()),
}
}
}
#[inline]
pub fn fill_buf_to<I, T>(it: &mut I, buf: &mut Vec<T>, cap: usize) -> std::io::Result<()>
where
I: Iterator<Item = std::io::Result<T>>,
{
for _ in buf.len()..cap {
match it.next() {
Some(Ok(t)) => buf.push(t),
Some(Err(err)) => return Err(err),
None => break,
};
}
Ok(())
}