mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 08:56:40 +00:00
LibC: Implement wmemchr
This commit is contained in:
parent
5ac2e84264
commit
0ca1df4dc6
|
@ -65,6 +65,38 @@ TEST_CASE(wcsstr)
|
|||
EXPECT_EQ(ret, nullptr);
|
||||
}
|
||||
|
||||
TEST_CASE(wmemchr)
|
||||
{
|
||||
const wchar_t* input = L"abcde";
|
||||
wchar_t* ret;
|
||||
|
||||
// Empty haystack returns nothing.
|
||||
ret = wmemchr(L"", L'c', 0);
|
||||
EXPECT_EQ(ret, nullptr);
|
||||
|
||||
// Not included character returns nothing.
|
||||
ret = wmemchr(input, L'z', 5);
|
||||
EXPECT_EQ(ret, nullptr);
|
||||
|
||||
// Match at string start.
|
||||
ret = wmemchr(input, L'a', 5);
|
||||
EXPECT_EQ(ret, input);
|
||||
|
||||
// Match at string end.
|
||||
ret = wmemchr(input, L'e', 5);
|
||||
EXPECT_EQ(ret, input + 4);
|
||||
|
||||
input = L"abcde\0fg";
|
||||
|
||||
// Handle finding null characters.
|
||||
ret = wmemchr(input, L'\0', 8);
|
||||
EXPECT_EQ(ret, input + 5);
|
||||
|
||||
// Don't stop at null characters.
|
||||
ret = wmemchr(input, L'f', 8);
|
||||
EXPECT_EQ(ret, input + 6);
|
||||
}
|
||||
|
||||
TEST_CASE(wcscoll)
|
||||
{
|
||||
// Check if wcscoll is sorting correctly. At the moment we are doing raw char comparisons,
|
||||
|
|
|
@ -375,4 +375,14 @@ wchar_t* wcsstr(const wchar_t* haystack, const wchar_t* needle)
|
|||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n)
|
||||
{
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (s[i] == c)
|
||||
return const_cast<wchar_t*>(&s[i]);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,5 +43,6 @@ int wctob(wint_t);
|
|||
int mbsinit(const mbstate_t*);
|
||||
wchar_t* wcspbrk(const wchar_t*, const wchar_t*);
|
||||
wchar_t* wcsstr(const wchar_t*, const wchar_t*);
|
||||
wchar_t* wmemchr(const wchar_t*, wchar_t, size_t);
|
||||
|
||||
__END_DECLS
|
||||
|
|
Loading…
Reference in a new issue