Verified Commit baddbb98 authored by jD91mZM2's avatar jD91mZM2
Browse files

Don't reinvent the wheel in strings.h

parent 1acc2a1a
Pipeline #1437 failed with stages
in 8 minutes and 59 seconds
......@@ -87,7 +87,16 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: usize)
#[no_mangle]
pub unsafe extern "C" fn memcpy(s1: *mut c_void, s2: *const c_void, n: usize) -> *mut c_void {
platform::memcpy(s1, s2, n)
let mut i = 0;
while i + 7 < n {
*(s1.offset(i as isize) as *mut u64) = *(s2.offset(i as isize) as *const u64);
i += 8;
}
while i < n {
*(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize);
i += 1;
}
s1
}
#[no_mangle]
......
......@@ -20,38 +20,20 @@ pub unsafe extern "C" fn bcmp(first: *const c_void, second: *const c_void, n: si
#[no_mangle]
pub unsafe extern "C" fn bcopy(src: *const c_void, dst: *mut c_void, n: size_t) {
let src = src as *mut c_char;
let dst = dst as *mut c_char;
let mut tmp = Vec::with_capacity(n);
for i in 0..n as isize {
tmp.push(*src.offset(i));
}
for (i, val) in tmp.into_iter().enumerate() {
*dst.offset(i as isize) = val;
}
ptr::copy(src as *const u8, dst as *mut u8, n);
}
#[no_mangle]
pub unsafe extern "C" fn bzero(src: *mut c_void, n: size_t) {
let src = src as *mut c_char;
for i in 0..n as isize {
*src.offset(i) = 0;
}
pub unsafe extern "C" fn bzero(dst: *mut c_void, n: size_t) {
ptr::write_bytes(dst as *mut u8, 0, n);
}
#[no_mangle]
pub extern "C" fn ffs(mut i: c_int) -> c_int {
pub extern "C" fn ffs(i: c_int) -> c_int {
if i == 0 {
return 0;
}
let mut n = 1;
while i & 1 == 0 {
i >>= 1;
n += 1;
}
n
1 + i.trailing_zeros() as c_int
}
#[no_mangle]
......@@ -89,14 +71,7 @@ pub unsafe extern "C" fn strcasecmp(mut first: *const c_char, mut second: *const
let mut i = *first;
let mut j = *second;
if i >= b'A' as c_char && i <= b'Z' as c_char {
i += (b'a' - b'A') as c_char;
}
if j >= b'A' as c_char && j <= b'Z' as c_char {
j += (b'a' - b'A') as c_char;
}
if i != j {
if i & !32 != j & !32 {
return -1;
}
......@@ -120,14 +95,7 @@ pub unsafe extern "C" fn strncasecmp(
let mut i = *first;
let mut j = *second;
if i >= b'A' as c_char && i <= b'Z' as c_char {
i += (b'a' - b'A') as c_char;
}
if j >= b'A' as c_char && j <= b'Z' as c_char {
j += (b'a' - b'A') as c_char;
}
if i != j {
if i & !32 != j & !32 {
return -1;
}
......@@ -136,7 +104,7 @@ pub unsafe extern "C" fn strncasecmp(
n -= 1;
}
// Both strings didn't end with NUL bytes (unless we reached the limit)
if n != 0 && *first != *second {
if n > 0 && *first != *second {
return -1;
}
0
......
......@@ -43,20 +43,6 @@ pub static mut environ: *mut *mut c_char = ptr::null_mut();
#[allow(non_upper_case_globals)]
pub static mut inner_environ: Vec<*mut c_char> = Vec::new();
// NOTE: defined here rather than in string because memcpy() is useful in multiple crates
pub unsafe fn memcpy(s1: *mut c_void, s2: *const c_void, n: usize) -> *mut c_void {
let mut i = 0;
while i + 7 < n {
*(s1.offset(i as isize) as *mut u64) = *(s2.offset(i as isize) as *const u64);
i += 8;
}
while i < n {
*(s1 as *mut u8).offset(i as isize) = *(s2 as *const u8).offset(i as isize);
i += 1;
}
s1
}
pub trait WriteByte: fmt::Write {
fn write_u8(&mut self, byte: u8) -> fmt::Result;
}
......@@ -114,10 +100,10 @@ impl StringWriter {
pub unsafe fn write(&mut self, buf: &[u8]) {
if self.1 > 1 {
let copy_size = buf.len().min(self.1 - 1);
memcpy(
self.0 as *mut c_void,
buf.as_ptr() as *const c_void,
copy_size,
ptr::copy_nonoverlapping(
buf.as_ptr(),
self.0,
copy_size
);
self.1 -= copy_size;
......@@ -145,10 +131,10 @@ pub struct UnsafeStringWriter(pub *mut u8);
impl UnsafeStringWriter {
pub unsafe fn write(&mut self, buf: &[u8]) {
memcpy(
self.0 as *mut c_void,
buf.as_ptr() as *const c_void,
buf.len(),
ptr::copy_nonoverlapping(
buf.as_ptr(),
self.0,
buf.len()
);
*self.0.offset(buf.len() as isize) = b'\0';
self.0 = self.0.offset(buf.len() as isize);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment