Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,14 +746,23 @@ impl<W: Write> LineWriter<W> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<W: Write> Write for LineWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match buf.iter().rposition(|b| *b == b'\n') {
Some(i) => {
let n = try!(self.inner.write(&buf[..i + 1]));
if n != i + 1 { return Ok(n) }
try!(self.inner.flush());
self.inner.write(&buf[i + 1..]).map(|i| n + i)
}
None => self.inner.write(buf),
use libc;

let p = unsafe {
libc::memchr(
buf.as_ptr() as *const libc::c_void,
b'\n' as libc::c_int,
buf.len() as libc::size_t)
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best to have a safe wrapper function for memchr — and then the structure of fn write doesn't need to change at all either.


if p.is_null() {
self.inner.write(buf)
} else {
let i = p as usize - (buf.as_ptr() as usize);
let n = try!(self.inner.write(&buf[..i + 1]));
if n != i + 1 { return Ok(n) }
try!(self.inner.flush());
self.inner.write(&buf[i + 1..]).map(|i| n + i)
}
}

Expand Down