Skip to content

Commit

Permalink
Use memrchr bindings provided by libc
Browse files Browse the repository at this point in the history
  • Loading branch information
fhahn committed Dec 18, 2015
1 parent aa1f8fd commit a206e55
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions src/libstd/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
// Original implementation taken from rust-memchr
// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch

use libc::{c_void, c_int, size_t};


/// A safe interface to `memchr`.
Expand All @@ -27,25 +26,23 @@ use libc::{c_void, c_int, size_t};
///
/// This shows how to find the first position of a byte in a byte string.
///
/// ```rust
/// ```rust,ignore
/// use memchr::memchr;
///
/// let haystack = b"the quick brown fox";
/// assert_eq!(memchr(b'k', haystack), Some(8));
/// ```
pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
// libc memchr
#[cfg(any(not(target_os = "windows"),
not(any(target_pointer_width = "32",
target_pointer_width = "64"))))]
#[cfg(not(target_os = "windows"))]
fn memchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
use libc::memchr as libc_memchr;
use libc;

let p = unsafe {
libc_memchr(
haystack.as_ptr() as *const c_void,
needle as c_int,
haystack.len() as size_t)
libc::memchr(
haystack.as_ptr() as *const libc::c_void,
needle as libc::c_int,
haystack.len() as libc::size_t)
};
if p.is_null() {
None
Expand All @@ -55,9 +52,7 @@ pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
}

// use fallback on windows, since it's faster
#[cfg(all(target_os = "windows",
any(target_pointer_width = "32",
target_pointer_width = "64")))]
#[cfg(target_os = "windows")]
fn memchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
fallback::memchr(needle, haystack)
}
Expand All @@ -74,7 +69,7 @@ pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
///
/// This shows how to find the last position of a byte in a byte string.
///
/// ```rust
/// ```rust,ignore
/// use memchr::memrchr;
///
/// let haystack = b"the quick brown fox";
Expand All @@ -84,15 +79,15 @@ pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {

#[cfg(target_os = "linux")]
fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
use libc::memrchr as libc_memrchr;
use libc;

// GNU's memrchr() will - unlike memchr() - error if haystack is empty.
if haystack.is_empty() {return None}
let p = unsafe {
libc_memrchr(
haystack.as_ptr() as *const c_void,
needle as c_int,
haystack.len() as size_t)
libc::memrchr(
haystack.as_ptr() as *const libc::c_void,
needle as libc::c_int,
haystack.len() as libc::size_t)
};
if p.is_null() {
None
Expand All @@ -101,16 +96,7 @@ pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
}
}

#[cfg(all(not(target_os = "linux"),
any(target_pointer_width = "32", target_pointer_width = "64")))]
fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
fallback::memrchr(needle, haystack)
}

// For the rare case of neither 32 bit nor 64-bit platform.
#[cfg(all(not(target_os = "linux"),
not(target_pointer_width = "32"),
not(target_pointer_width = "64")))]
#[cfg(not(target_os = "linux"))]
fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option<usize> {
haystack.iter().rposition(|&b| b == needle)
}
Expand Down

0 comments on commit a206e55

Please sign in to comment.