Skip to content
Snippets Groups Projects
Unverified Commit 089b41da authored by Jeremy Soller's avatar Jeremy Soller Committed by GitHub
Browse files

Merge pull request #62 from jrraymond/strrchr

implement strrchr
parents c2a76870 63ee4de1
No related branches found
No related tags found
No related merge requests found
......@@ -255,8 +255,17 @@ pub extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char {
}
#[no_mangle]
pub extern "C" fn strrchr(s: *const c_char, c: c_int) -> *mut c_char {
unimplemented!();
pub unsafe extern "C" fn strrchr(s: *const c_char, c: c_int) -> *mut c_char {
let len = strlen(s) as isize;
let c = c as i8;
let mut i = len - 1;
while i >= 0 {
if *s.offset(i) == c {
return s.offset(i) as *mut c_char;
}
i -= 1;
}
ptr::null_mut()
}
#[no_mangle]
......
......@@ -32,6 +32,7 @@
/string/strncmp
/string/strcspn
/string/strchr
/string/strrchr
/string/strspn
/unlink
/write
......@@ -27,6 +27,7 @@ BINS=\
string/strncmp \
string/strcspn \
string/strchr \
string/strrchr \
string/strspn \
unlink \
write
......
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char s0[] = "hello, world";
char* ptr = strrchr(s0, 'l');
if (ptr != &s0[10]) {
printf("%p != %p\n", ptr, &s0[10]);
printf("strrchr FAIL , exit with status code %d\n", 1);
return 1;
}
char s1[] = "";
ptr = strrchr(s1, 'a');
if (ptr != NULL) {
printf("%p != 0\n", ptr);
printf("strrchr FAIL, exit with status code %d\n", 1);
return 1;
}
printf("strrch PASS, exiting with status code %d\n", 0);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment