diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index 04b8c93bcd4c8bdfa55b40cf1e19ca55334d7172..ef348830ee722f64d3bd4abd555ee7836a3b19c7 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 ecd259335b24cafe7848a32e85b2fe770029ef87..351d48df2391725e1bca7cd9bed8925dade23b30 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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/expected/string/strpbrk.stdout b/tests/expected/string/strpbrk.stdout new file mode 100644 index 0000000000000000000000000000000000000000..7a6bc8b0891622c15710c10ff02546f8d6431179 --- /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 0000000000000000000000000000000000000000..40cae665b1330a94bf5d899dd888f1f12529ff6a --- /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; +}