diff --git a/src/string/src/lib.rs b/src/string/src/lib.rs index 5b5c15832b983c7fa1f02e777d8b6740a86b0655..3fd95d0014e64c2edf6369ee6dfe879a2e06381c 100644 --- a/src/string/src/lib.rs +++ b/src/string/src/lib.rs @@ -250,8 +250,13 @@ 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 p = s1.offset(strcspn(s1, s2) as isize); + if *p != 0 { + p as *mut c_char + } else { + ptr::null_mut() + } } #[no_mangle] @@ -299,8 +304,22 @@ pub unsafe extern "C" fn strspn(s1: *const c_char, s2: *const c_char) -> c_ulong } #[no_mangle] -pub extern "C" fn strstr(s1: *const c_char, s2: *const c_char) -> *mut c_char { - unimplemented!(); +pub unsafe extern "C" fn strstr(s1: *const c_char, s2: *const c_char) -> *mut c_char { + let mut i = 0; + while *s1.offset(i) != 0 { + let mut j = 0; + while *s2.offset(j) != 0 && *s1.offset(j + i) != 0 { + if *s2.offset(j) != *s1.offset(j + i) { + break; + } + j += 1; + if *s2.offset(j) == 0 { + return s1.offset(i) as *mut c_char; + } + } + i += 1; + } + ptr::null_mut() } #[no_mangle] diff --git a/tests/Makefile b/tests/Makefile index a2528896058cbcc1690c00a79b65746959f760c9..351d48df2391725e1bca7cd9bed8925dade23b30 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -27,6 +27,8 @@ EXPECT_BINS=\ string/strchr \ 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/expected/string/strstr.stderr b/tests/expected/string/strstr.stderr new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tests/expected/string/strstr.stdout b/tests/expected/string/strstr.stdout new file mode 100644 index 0000000000000000000000000000000000000000..e978edff7f142deb5af595baa3d297e01180e911 --- /dev/null +++ b/tests/expected/string/strstr.stdout @@ -0,0 +1,3 @@ +rust +libc we trust +NULL diff --git a/tests/string/strpbrk.c b/tests/string/strpbrk.c new file mode 100644 index 0000000000000000000000000000000000000000..dc0ebf7c534c1a92ea997a7aa212f8227c2cfc5d --- /dev/null +++ b/tests/string/strpbrk.c @@ -0,0 +1,20 @@ +#include <string.h> +#include <stdio.h> + +int main(int argc, char* argv[]) { + char* source = "The quick drawn fix jumps over the lazy bug"; + + // should be "The quick drawn fix jumps over the lazy bug" + char* res1 = strpbrk(source, "From The Very Beginning"); + printf("%s\n", (res1) ? res1 : "NULL"); + + // should be "lazy bug" + char* res2 = strpbrk(source, "lzbg"); + printf("%s\n", (res2) ? res2 : "NULL"); + + // should be "NULL" + char* res3 = strpbrk(source, "404"); + printf("%s\n", (res3) ? res3 : "NULL"); + + return 0; +} diff --git a/tests/string/strstr.c b/tests/string/strstr.c new file mode 100644 index 0000000000000000000000000000000000000000..6a074d18d83ca3c3fec9aa676866f171c84335de --- /dev/null +++ b/tests/string/strstr.c @@ -0,0 +1,18 @@ +#include <string.h> +#include <stdio.h> + +int main(int argc, char* argv[]) { + // should be "rust" + char* res1 = strstr("In relibc we trust", "rust"); + printf("%s\n", (res1) ? res1 : "NULL"); + + // should be "libc we trust" + char* res2 = strstr("In relibc we trust", "libc"); + printf("%s\n", (res2) ? res2 : "NULL"); + + // should be "NULL" + char* res3 = strstr("In relibc we trust", "bugs"); + printf("%s\n", (res3) ? res3 : "NULL"); + + return 0; +}