1111// Original implementation taken from rust-memchr
1212// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch
1313
14- use libc:: { c_void, c_int, size_t} ;
1514
1615
1716/// A safe interface to `memchr`.
@@ -27,25 +26,23 @@ use libc::{c_void, c_int, size_t};
2726///
2827/// This shows how to find the first position of a byte in a byte string.
2928///
30- /// ```rust
29+ /// ```rust,ignore
3130/// use memchr::memchr;
3231///
3332/// let haystack = b"the quick brown fox";
3433/// assert_eq!(memchr(b'k', haystack), Some(8));
3534/// ```
3635pub fn memchr ( needle : u8 , haystack : & [ u8 ] ) -> Option < usize > {
3736 // libc memchr
38- #[ cfg( any( not( target_os = "windows" ) ,
39- not( any( target_pointer_width = "32" ,
40- target_pointer_width = "64" ) ) ) ) ]
37+ #[ cfg( not( target_os = "windows" ) ) ]
4138 fn memchr_specific ( needle : u8 , haystack : & [ u8 ] ) -> Option < usize > {
42- use libc:: memchr as libc_memchr ;
39+ use libc;
4340
4441 let p = unsafe {
45- libc_memchr (
46- haystack. as_ptr ( ) as * const c_void ,
47- needle as c_int ,
48- haystack. len ( ) as size_t )
42+ libc :: memchr (
43+ haystack. as_ptr ( ) as * const libc :: c_void ,
44+ needle as libc :: c_int ,
45+ haystack. len ( ) as libc :: size_t )
4946 } ;
5047 if p. is_null ( ) {
5148 None
@@ -55,9 +52,7 @@ pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
5552 }
5653
5754 // use fallback on windows, since it's faster
58- #[ cfg( all( target_os = "windows" ,
59- any( target_pointer_width = "32" ,
60- target_pointer_width = "64" ) ) ) ]
55+ #[ cfg( target_os = "windows" ) ]
6156 fn memchr_specific ( needle : u8 , haystack : & [ u8 ] ) -> Option < usize > {
6257 fallback:: memchr ( needle, haystack)
6358 }
@@ -74,7 +69,7 @@ pub fn memchr(needle: u8, haystack: &[u8]) -> Option<usize> {
7469///
7570/// This shows how to find the last position of a byte in a byte string.
7671///
77- /// ```rust
72+ /// ```rust,ignore
7873/// use memchr::memrchr;
7974///
8075/// let haystack = b"the quick brown fox";
@@ -84,15 +79,15 @@ pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
8479
8580 #[ cfg( target_os = "linux" ) ]
8681 fn memrchr_specific ( needle : u8 , haystack : & [ u8 ] ) -> Option < usize > {
87- use libc:: memrchr as libc_memrchr ;
82+ use libc;
8883
8984 // GNU's memrchr() will - unlike memchr() - error if haystack is empty.
9085 if haystack. is_empty ( ) { return None }
9186 let p = unsafe {
92- libc_memrchr (
93- haystack. as_ptr ( ) as * const c_void ,
94- needle as c_int ,
95- haystack. len ( ) as size_t )
87+ libc :: memrchr (
88+ haystack. as_ptr ( ) as * const libc :: c_void ,
89+ needle as libc :: c_int ,
90+ haystack. len ( ) as libc :: size_t )
9691 } ;
9792 if p. is_null ( ) {
9893 None
@@ -101,16 +96,7 @@ pub fn memrchr(needle: u8, haystack: &[u8]) -> Option<usize> {
10196 }
10297 }
10398
104- #[ cfg( all( not( target_os = "linux" ) ,
105- any( target_pointer_width = "32" , target_pointer_width = "64" ) ) ) ]
106- fn memrchr_specific ( needle : u8 , haystack : & [ u8 ] ) -> Option < usize > {
107- fallback:: memrchr ( needle, haystack)
108- }
109-
110- // For the rare case of neither 32 bit nor 64-bit platform.
111- #[ cfg( all( not( target_os = "linux" ) ,
112- not( target_pointer_width = "32" ) ,
113- not( target_pointer_width = "64" ) ) ) ]
99+ #[ cfg( not( target_os = "linux" ) ) ]
114100 fn memrchr_specific ( needle : u8 , haystack : & [ u8 ] ) -> Option < usize > {
115101 haystack. iter ( ) . rposition ( |& b| b == needle)
116102 }
0 commit comments