From 1b078f87b77e65afac3a998af8223bbebe729df0 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 22 Sep 2021 08:52:27 +0000 Subject: [PATCH] LibC: Implement wcspbrk --- Tests/LibC/TestWchar.cpp | 27 +++++++++++++++++++++++++++ Userland/Libraries/LibC/wchar.cpp | 11 +++++++++++ Userland/Libraries/LibC/wchar.h | 1 + 3 files changed, 39 insertions(+) diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp index d4e84725d37..ed248531a3f 100644 --- a/Tests/LibC/TestWchar.cpp +++ b/Tests/LibC/TestWchar.cpp @@ -8,6 +8,33 @@ #include +TEST_CASE(wcspbrk) +{ + const wchar_t* input; + wchar_t* ret; + + // Test empty haystack. + ret = wcspbrk(L"", L"ab"); + EXPECT_EQ(ret, nullptr); + + // Test empty needle. + ret = wcspbrk(L"ab", L""); + EXPECT_EQ(ret, nullptr); + + // Test search for a single character. + input = L"abcd"; + ret = wcspbrk(input, L"a"); + EXPECT_EQ(ret, input); + + // Test search for multiple characters, none matches. + ret = wcspbrk(input, L"zxy"); + EXPECT_EQ(ret, nullptr); + + // Test search for multiple characters, last matches. + ret = wcspbrk(input, L"zxyc"); + EXPECT_EQ(ret, input + 2); +} + TEST_CASE(wcscoll) { // Check if wcscoll is sorting correctly. At the moment we are doing raw char comparisons, diff --git a/Userland/Libraries/LibC/wchar.cpp b/Userland/Libraries/LibC/wchar.cpp index ab7de3e653f..39eb3e85792 100644 --- a/Userland/Libraries/LibC/wchar.cpp +++ b/Userland/Libraries/LibC/wchar.cpp @@ -344,4 +344,15 @@ int mbsinit(const mbstate_t* state) return 1; } + +wchar_t* wcspbrk(const wchar_t* wcs, const wchar_t* accept) +{ + for (const wchar_t* cur = accept; *cur; cur++) { + wchar_t* res = wcschr(wcs, *cur); + if (res) + return res; + } + + return nullptr; +} } diff --git a/Userland/Libraries/LibC/wchar.h b/Userland/Libraries/LibC/wchar.h index 2db7dc4da65..98c68249459 100644 --- a/Userland/Libraries/LibC/wchar.h +++ b/Userland/Libraries/LibC/wchar.h @@ -41,5 +41,6 @@ size_t wcrtomb(char*, wchar_t, mbstate_t*); int wcscoll(const wchar_t*, const wchar_t*); int wctob(wint_t); int mbsinit(const mbstate_t*); +wchar_t* wcspbrk(const wchar_t*, const wchar_t*); __END_DECLS