From 1e969afd43b83477a2251e72400970d690d058b8 Mon Sep 17 00:00:00 2001 From: Andrii Zymohliad <azymohliad@protonmail.com> Date: Mon, 12 Mar 2018 18:01:12 +0800 Subject: [PATCH] Implement strpbrk(), add strpbrk test --- src/string/src/lib.rs | 11 +++++++++-- tests/Makefile | 1 + tests/expected/string/strpbrk.stderr | 0 tests/expected/string/strpbrk.stdout | 3 +++ tests/string/strpbrk.c | 21 +++++++++++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/expected/string/strpbrk.stderr create mode 100644 tests/expected/string/strpbrk.stdout create mode 100644 tests/string/strpbrk.c diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index 04b8c93bc..ef348830e 100644 --- a/src/string/src/lib.rs +++ b/src/string/src/lib.rs @@ -250,8 +250,15 @@ pub unsafe extern "C" fn strncpy(s1: *mut c_char, s2: *const c_char, n: usize) - } #[no_mangle] -pub extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char { - unimplemented!(); +pub unsafe extern "C" fn strpbrk(s1: *const c_char, s2: *const c_char) -> *mut c_char { + let mut i = 0; + while *s1.offset(i) != 0 { + if !strchr(s2, *s1.offset(i) as i32).is_null() { + return s1.offset(i) as *mut c_char; + } + i += 1; + } + ptr::null_mut() } #[no_mangle] diff --git a/tests/Makefile b/tests/Makefile index ecd259335..351d48df2 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -28,6 +28,7 @@ EXPECT_BINS=\ string/strrchr \ string/strspn \ string/strstr \ + string/strpbrk \ unlink \ write diff --git a/tests/expected/string/strpbrk.stderr b/tests/expected/string/strpbrk.stderr new file mode 100644 index 000000000..e69de29bb diff --git a/tests/expected/string/strpbrk.stdout b/tests/expected/string/strpbrk.stdout new file mode 100644 index 000000000..7a6bc8b08 --- /dev/null +++ b/tests/expected/string/strpbrk.stdout @@ -0,0 +1,3 @@ +The quick drawn fix jumps over the lazy bug +lazy bug +NULL diff --git a/tests/string/strpbrk.c b/tests/string/strpbrk.c new file mode 100644 index 000000000..40cae665b --- /dev/null +++ b/tests/string/strpbrk.c @@ -0,0 +1,21 @@ +#include <string.h> +#include <stdio.h> + +int main(int argc, char* argv[]) { + char* str = "The quick drawn fix jumps over the lazy bug"; + + // should be "The quick drawn fix jumps over the lazy bug" + char* str1 = strpbrk(str, "From The Very Beginning"); + printf("%s\n", (str1) ? str1 : "NULL"); + + // should be "lazy bug" + char* str2 = strpbrk(str, "lzbg"); + printf("%s\n", (str2) ? str2 : "NULL"); + + + // should be "NULL" + char* str3 = strpbrk(str, "404"); + printf("%s\n", (str3) ? str3 : "NULL"); + + return 0; +} -- GitLab