Skip to content

Commit

Permalink
Use libc::memchr instead of iter().rposition(), closes rust-lang#30076
Browse files Browse the repository at this point in the history
  • Loading branch information
fhahn committed Dec 1, 2015
1 parent 1a7db13 commit bb3bf3f
Showing 1 changed file with 17 additions and 8 deletions.
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)
};

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

0 comments on commit bb3bf3f

Please sign in to comment.