From ec3488c7b0d36134ce67d79263e0882322cfd764 Mon Sep 17 00:00:00 2001
From: emturner <em.turner@tutanota.com>
Date: Fri, 22 Feb 2019 23:18:21 +0000
Subject: [PATCH] implements wcscspn from wchar.h

---
 src/header/wchar/mod.rs             | 17 +++++++++++++----
 tests/Makefile                      |  3 ++-
 tests/expected/wchar/wcscspn.stderr |  0
 tests/expected/wchar/wcscspn.stdout |  0
 tests/wchar/wcscspn.c               | 16 ++++++++++++++++
 5 files changed, 31 insertions(+), 5 deletions(-)
 create mode 100644 tests/expected/wchar/wcscspn.stderr
 create mode 100644 tests/expected/wchar/wcscspn.stdout
 create mode 100644 tests/wchar/wcscspn.c

diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs
index 9d1d245d2..19d8fbea6 100644
--- a/src/header/wchar/mod.rs
+++ b/src/header/wchar/mod.rs
@@ -295,7 +295,7 @@ pub unsafe extern "C" fn wcscat(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut
 }
 
 #[no_mangle]
-pub unsafe extern "C" fn wcschr(ws: *const wchar_t, wc: wchar_t) -> *mut c_int {
+pub unsafe extern "C" fn wcschr(ws: *const wchar_t, wc: wchar_t) -> *mut wchar_t {
     let mut i = 0;
     loop {
         if *ws.add(i) == wc {
@@ -331,9 +331,18 @@ pub unsafe extern "C" fn wcscpy(ws1: *mut wchar_t, ws2: *const wchar_t) -> *mut
     }
 }
 
-// #[no_mangle]
-pub extern "C" fn wcscspn(ws1: *const wchar_t, ws2: *const wchar_t) -> size_t {
-    unimplemented!();
+#[no_mangle]
+pub unsafe extern "C" fn wcscspn(ws1: *const wchar_t, ws2: *const wchar_t) -> size_t {
+    let mut i = 0;
+    loop {
+        let wc = *ws1.add(i);
+
+        if wc == 0 || wcschr(ws2, wc) != 0 as *mut wchar_t {
+            return i;
+        }
+
+        i += 1;
+    }
 }
 
 // #[no_mangle]
diff --git a/tests/Makefile b/tests/Makefile
index 30efeddae..0ae8aa786 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -86,7 +86,8 @@ EXPECT_BINS=\
 	wchar/mbrtowc \
 	wchar/mbsrtowcs \
 	wchar/putwchar \
-	wchar/wcrtomb
+	wchar/wcrtomb \
+	wchar/wcscspn
 
 # Binaries that may generate varied output
 BINS=\
diff --git a/tests/expected/wchar/wcscspn.stderr b/tests/expected/wchar/wcscspn.stderr
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/expected/wchar/wcscspn.stdout b/tests/expected/wchar/wcscspn.stdout
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/wchar/wcscspn.c b/tests/wchar/wcscspn.c
new file mode 100644
index 000000000..f61adae23
--- /dev/null
+++ b/tests/wchar/wcscspn.c
@@ -0,0 +1,16 @@
+#include <assert.h>
+#include <wchar.h>
+
+int main(void) {
+
+    assert(wcscspn(L"", L"") == 0);
+    assert(wcscspn(L"", L"h") == 0);
+    assert(wcscspn(L"a", L"a") == 0);
+
+    assert(wcscspn(L"ba", L"ab") == 0);
+    assert(wcscspn(L"ba", L"a") == 1);
+
+    assert(wcscspn(L"abcdefghijkl$\"£$%", L"zxqrst,./$w") == 12);
+
+    return 0;
+}
-- 
GitLab