Use memchr in libstd where possible, closes #30076

This commit is contained in:
Florian Hahn 2015-12-15 00:03:42 +01:00
parent ca52c56e34
commit de3e843d24
4 changed files with 8 additions and 4 deletions

View file

@ -19,6 +19,7 @@
use iter::Iterator;
use libc;
use mem;
use memchr;
use ops::Deref;
use option::Option::{self, Some, None};
use os::raw::c_char;
@ -188,7 +189,7 @@ pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError> {
}
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) }),
}

View file

@ -18,6 +18,7 @@
use error;
use fmt;
use io::{self, DEFAULT_BUF_SIZE, Error, ErrorKind, SeekFrom};
use memchr;
/// The `BufReader` struct adds buffering to any reader.
///
@ -746,7 +747,7 @@ pub fn into_inner(self) -> Result<W, IntoInnerError<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) }

View file

@ -254,6 +254,7 @@
use string::String;
use str;
use vec::Vec;
use memchr;
#[stable(feature = "rust1", since = "1.0.0")]
pub use self::buffered::{BufReader, BufWriter, LineWriter};
@ -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)

View file

@ -22,6 +22,7 @@
use iter;
use libc::{self, c_int, c_char, c_void};
use mem;
use memchr;
use path::{self, PathBuf};
use ptr;
use slice;
@ -406,7 +407,7 @@ fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
if input.is_empty() {
return None;
}
let pos = input[1..].iter().position(|&b| b == b'=').map(|p| p + 1);
let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
pos.map(|p| (
OsStringExt::from_vec(input[..p].to_vec()),
OsStringExt::from_vec(input[p+1..].to_vec()),