Skip to content

Commit

Permalink
Auto merge of #30381 - fhahn:memchr-in-std, r=alexcrichton
Browse files Browse the repository at this point in the history
This PR adds `memchr`and `memrchr` based on @BurntSushi 's rust-memchr crate to libstd (as discussed in #30151).

I've update some places in libstd to use memchr/memrchr, but I am not sure if there are other places where it could be used as well.

ref #30076
  • Loading branch information
bors committed Dec 19, 2015
2 parents 9e27895 + a206e55 commit 8ad12c3
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use io;
use iter::Iterator;
use libc;
use mem;
use memchr;
use ops::Deref;
use option::Option::{self, Some, None};
use os::raw::c_char;
Expand Down Expand Up @@ -188,7 +189,7 @@ impl CString {
}

fn _new(bytes: Vec<u8>) -> Result<CString, NulError> {
match bytes.iter().position(|x| *x == 0) {
match memchr::memchr(0, &bytes) {
Some(i) => Err(NulError(i, bytes)),
None => Ok(unsafe { CString::from_vec_unchecked(bytes) }),
}
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use cmp;
use error;
use fmt;
use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom};
use memchr;

/// The `BufReader` struct adds buffering to any reader.
///
Expand Down Expand Up @@ -746,7 +747,7 @@ 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') {
match memchr::memrchr(b'\n', buf) {
Some(i) => {
let n = try!(self.inner.write(&buf[..i + 1]));
if n != i + 1 { return Ok(n) }
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ use result;
use string::String;
use str;
use vec::Vec;
use memchr;

#[stable(feature = "rust1", since = "1.0.0")]
pub use self::buffered::{BufReader, BufWriter, LineWriter};
Expand Down Expand Up @@ -1194,7 +1195,7 @@ fn read_until<R: BufRead + ?Sized>(r: &mut R, delim: u8, buf: &mut Vec<u8>)
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e)
};
match available.iter().position(|x| *x == delim) {
match memchr::memchr(delim, available) {
Some(i) => {
buf.extend_from_slice(&available[..i + 1]);
(true, i + 1)
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
#![feature(link_args)]
#![feature(linkage)]
#![feature(macro_reexport)]
#![feature(num_bits_bytes)]
#![feature(on_unimplemented)]
#![feature(oom)]
#![feature(optin_builtin_traits)]
Expand Down Expand Up @@ -429,6 +430,7 @@ pub mod path;
pub mod process;
pub mod sync;
pub mod time;
mod memchr;

#[macro_use]
#[path = "sys/common/mod.rs"] mod sys_common;
Expand Down
Loading

0 comments on commit 8ad12c3

Please sign in to comment.