Use the memchr crate

https://github.com/BurntSushi/rust-memchr is supposed to be a whole
lot faster :)
This commit is contained in:
jD91mZM2
2019-04-21 19:09:37 +02:00
parent fa94f1b6d5
commit 3a3fd3da39
5 changed files with 20 additions and 72 deletions
+7 -41
View File
@@ -1,8 +1,6 @@
//! string implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/string.h.html
use core::mem;
use core::ptr;
use core::usize;
use core::{mem, ptr, slice, usize};
use cbitset::BitSet256;
@@ -30,45 +28,13 @@ pub unsafe extern "C" fn memccpy(
}
#[no_mangle]
pub unsafe extern "C" fn memchr(s: *const c_void, c: c_int, n: size_t) -> *mut c_void {
let mut s = s as *const u8;
let c = c as u8;
let mut n = n;
// read one byte at a time until s is aligned
while s as usize % mem::size_of::<usize>() != 0 {
if n == 0 {
return ptr::null_mut();
}
if *s == c {
return s as *mut c_void;
}
n -= 1;
s = s.offset(1);
pub unsafe extern "C" fn memchr(haystack: *const c_void, needle: c_int, len: size_t) -> *mut c_void {
let haystack = slice::from_raw_parts(haystack as *const u8, len as usize);
match memchr::memchr(needle as u8, haystack) {
Some(index) => haystack[index..].as_ptr() as *mut c_void,
None => ptr::null_mut()
}
let mut s = s as *const usize;
let lowbits = !0 as usize / 255;
let highbits = lowbits * 0x80;
let repeated_c = lowbits * c as usize;
while n >= mem::size_of::<usize>() {
// read multiple bytes at a time
// turn the requested byte into 8 zero bits
let m = *s ^ repeated_c;
// subtracting one from zero flips high bit from 0 to 1
if (m.wrapping_sub(lowbits) & !m & highbits) != 0 {
break;
}
n -= mem::size_of::<usize>();
s = s.offset(1);
}
let mut s = s as *const u8;
while n > 0 {
if *s == c {
return s as *mut c_void;
}
n -= 1;
s = s.offset(1);
}
ptr::null_mut()
}
#[no_mangle]
+4 -31
View File
@@ -2,20 +2,12 @@
use core::ptr;
use header::ctype;
use header::{ctype, string};
use platform::types::*;
#[no_mangle]
pub unsafe extern "C" fn bcmp(first: *const c_void, second: *const c_void, n: size_t) -> c_int {
let first = first as *const c_char;
let second = second as *const c_char;
for i in 0..n as isize {
if *first.offset(i) != *second.offset(i) {
return -1;
}
}
0
string::memcmp(first, second, n)
}
#[no_mangle]
@@ -38,31 +30,12 @@ pub extern "C" fn ffs(i: c_int) -> c_int {
#[no_mangle]
pub unsafe extern "C" fn index(mut s: *const c_char, c: c_int) -> *mut c_char {
while *s != 0 {
if *s == c as c_char {
// Input is const but output is mutable. WHY C, WHY DO THIS?
return s as *mut c_char;
}
s = s.offset(1);
}
ptr::null_mut()
string::strchr(s, c)
}
#[no_mangle]
pub unsafe extern "C" fn rindex(mut s: *const c_char, c: c_int) -> *mut c_char {
let original = s;
while *s != 0 {
s = s.offset(1);
}
while s != original {
s = s.offset(-1);
if *s == c as c_char {
// Input is const but output is mutable. WHY C, WHY DO THIS?
return s as *mut c_char;
}
}
ptr::null_mut()
string::strrchr(s, c)
}
#[no_mangle]
+1
View File
@@ -30,6 +30,7 @@ extern crate core_io;
extern crate goblin;
#[macro_use]
extern crate lazy_static;
extern crate memchr;
#[macro_use]
extern crate memoffset;
extern crate posix_regex;