Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use libc::memchr instead of iter().rposition(), closes #30076 #30151

Closed
wants to merge 1 commit into from
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
Member

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