| 1 |
//! Insert line breaks between written buffers when they would overflow the line length. |
| 2 |
use std::io; |
| 3 |
|
| 4 |
// The pnm standard says to insert line breaks after 70 characters. Assumes that no line breaks |
| 5 |
// are actually written. We have to be careful to fully commit buffers or not commit them at all, |
| 6 |
// otherwise we might insert a newline in the middle of a token. |
| 7 |
pub(crate) struct AutoBreak<W: io::Write> { |
| 8 |
wrapped: W, |
| 9 |
line_capacity: usize, |
| 10 |
line: Vec<u8>, |
| 11 |
has_newline: bool, |
| 12 |
panicked: bool, // see https://github.com/rust-lang/rust/issues/30888 |
| 13 |
} |
| 14 |
|
| 15 |
impl<W: io::Write> AutoBreak<W> { |
| 16 |
pub(crate) fn new(writer: W, line_capacity: usize) -> io::Result<Self> { |
| 17 |
let mut line = Vec::new(); |
| 18 |
line.try_reserve_exact(line_capacity + 1)?; |
| 19 |
Ok(AutoBreak { |
| 20 |
wrapped: writer, |
| 21 |
line_capacity, |
| 22 |
line, |
| 23 |
has_newline: false, |
| 24 |
panicked: false, |
| 25 |
}) |
| 26 |
} |
| 27 |
|
| 28 |
fn flush_buf(&mut self) -> io::Result<()> { |
| 29 |
// from BufWriter |
| 30 |
let mut written = 0; |
| 31 |
let len = self.line.len(); |
| 32 |
let mut ret = Ok(()); |
| 33 |
while written < len { |
| 34 |
self.panicked = true; |
| 35 |
let r = self.wrapped.write(&self.line[written..]); |
| 36 |
self.panicked = false; |
| 37 |
match r { |
| 38 |
Ok(0) => { |
| 39 |
ret = Err(io::Error::new( |
| 40 |
io::ErrorKind::WriteZero, |
| 41 |
"failed to write the buffered data", |
| 42 |
)); |
| 43 |
break; |
| 44 |
} |
| 45 |
Ok(n) => written += n, |
| 46 |
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 47 |
Err(e) => { |
| 48 |
ret = Err(e); |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
if written > 0 { |
| 54 |
self.line.drain(..written); |
| 55 |
} |
| 56 |
ret |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
impl<W: io::Write> io::Write for AutoBreak<W> { |
| 61 |
fn write(&mut self, buffer: &[u8]) -> io::Result<usize> { |
| 62 |
if self.has_newline { |
| 63 |
self.flush()?; |
| 64 |
self.has_newline = false; |
| 65 |
} |
| 66 |
|
| 67 |
if !self.line.is_empty() && self.line.len() + buffer.len() > self.line_capacity { |
| 68 |
self.line.push(b'\n'); |
| 69 |
self.has_newline = true; |
| 70 |
self.flush()?; |
| 71 |
self.has_newline = false; |
| 72 |
} |
| 73 |
|
| 74 |
self.line.extend_from_slice(buffer); |
| 75 |
Ok(buffer.len()) |
| 76 |
} |
| 77 |
|
| 78 |
fn flush(&mut self) -> io::Result<()> { |
| 79 |
self.flush_buf()?; |
| 80 |
self.wrapped.flush() |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
impl<W: io::Write> Drop for AutoBreak<W> { |
| 85 |
fn drop(&mut self) { |
| 86 |
if !self.panicked { |
| 87 |
let _r = self.flush_buf(); |
| 88 |
// internal writer flushed automatically by Drop |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#[cfg(test)] |
| 94 |
mod tests { |
| 95 |
use super::*; |
| 96 |
use std::io::Write; |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_aligned_writes() { |
| 100 |
let mut output = Vec::new(); |
| 101 |
|
| 102 |
{ |
| 103 |
let mut writer = AutoBreak::new(&mut output, 10).unwrap(); |
| 104 |
writer.write_all(b"0123456789").unwrap(); |
| 105 |
writer.write_all(b"0123456789").unwrap(); |
| 106 |
} |
| 107 |
|
| 108 |
assert_eq!(output.as_slice(), b"0123456789\n0123456789"); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_greater_writes() { |
| 113 |
let mut output = Vec::new(); |
| 114 |
|
| 115 |
{ |
| 116 |
let mut writer = AutoBreak::new(&mut output, 10).unwrap(); |
| 117 |
writer.write_all(b"012").unwrap(); |
| 118 |
writer.write_all(b"345").unwrap(); |
| 119 |
writer.write_all(b"0123456789").unwrap(); |
| 120 |
writer.write_all(b"012345678910").unwrap(); |
| 121 |
writer.write_all(b"_").unwrap(); |
| 122 |
} |
| 123 |
|
| 124 |
assert_eq!(output.as_slice(), b"012345\n0123456789\n012345678910\n_"); |
| 125 |
} |
| 126 |
} |