Browse Source

Implement strpbrk(), add strpbrk test

Andrii Zymohliad 7 years ago
parent
commit
1e969afd43

+ 9 - 2
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]

+ 1 - 0
tests/Makefile

@@ -28,6 +28,7 @@ EXPECT_BINS=\
 	string/strrchr \
 	string/strspn \
 	string/strstr \
+	string/strpbrk \
 	unlink \
 	write
 

+ 0 - 0
tests/expected/string/strpbrk.stderr


+ 3 - 0
tests/expected/string/strpbrk.stdout

@@ -0,0 +1,3 @@
+The quick drawn fix jumps over the lazy bug
+lazy bug
+NULL

+ 21 - 0
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;
+}