From 826cf0c61c2e0712914f986e756d1fdef56f9c5c Mon Sep 17 00:00:00 2001 From: Justin Raymond Date: Sat, 10 Mar 2018 11:10:19 -0500 Subject: [PATCH 1/3] implement strrchr --- src/string/src/lib.rs | 13 +++++++++++-- tests/Makefile | 1 + tests/string/strrchr.c | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/string/strrchr.c diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index a3d454fe7e..bbb1a53c69 100644 --- a/src/string/src/lib.rs +++ b/src/string/src/lib.rs @@ -223,8 +223,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 *s.offset(i) != 0 && i >= 0 { + if *s.offset(i) == c { + return s.offset(i) as *mut c_char; + } + i -= 1; + } + ptr::null_mut() } #[no_mangle] diff --git a/tests/Makefile b/tests/Makefile index 0726ba101b..8ca12a9a8d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -25,6 +25,7 @@ BINS=\ sprintf \ stdlib/strtol \ string/strncmp \ + string/strrchr \ unlink \ write diff --git a/tests/string/strrchr.c b/tests/string/strrchr.c new file mode 100644 index 0000000000..7edc98040b --- /dev/null +++ b/tests/string/strrchr.c @@ -0,0 +1,22 @@ +#include +#include + + +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; +} From 23a409204f3619499546b7f9f72433eddc0ac21f Mon Sep 17 00:00:00 2001 From: Justin Raymond Date: Sat, 10 Mar 2018 12:36:42 -0500 Subject: [PATCH 2/3] Update .gitignore --- tests/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/.gitignore b/tests/.gitignore index 929fcc62d7..c5a743e74f 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -32,6 +32,7 @@ /string/strncmp /string/strcspn /string/strchr +/string/strrchr /string/strspn /unlink /write From 63ee4de1627e3cbbc87bddc9b631217da9cb15ef Mon Sep 17 00:00:00 2001 From: Justin Raymond Date: Sat, 10 Mar 2018 12:39:30 -0500 Subject: [PATCH 3/3] Update lib.rs --- src/string/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index 81e15e819f..962c8f761d 100644 --- a/src/string/src/lib.rs +++ b/src/string/src/lib.rs @@ -259,7 +259,7 @@ 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 *s.offset(i) != 0 && i >= 0 { + while i >= 0 { if *s.offset(i) == c { return s.offset(i) as *mut c_char; }