Skip to content

Commit

Permalink
LibC: Implement wmemcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi authored and bgianfo committed Oct 3, 2021
1 parent 0ca1df4 commit 485c0ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Tests/LibC/TestWchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <LibTest/TestCase.h>

#include <string.h>
#include <wchar.h>

TEST_CASE(wcspbrk)
Expand Down Expand Up @@ -97,6 +98,22 @@ TEST_CASE(wmemchr)
EXPECT_EQ(ret, input + 6);
}

TEST_CASE(wmemcpy)
{
const wchar_t* input = L"abc\0def";
auto buf = static_cast<wchar_t*>(malloc(8 * sizeof(wchar_t)));

if (!buf) {
FAIL("Could not allocate space for copy target");
return;
}

wchar_t* ret = wmemcpy(buf, input, 8);

EXPECT_EQ(ret, buf);
EXPECT_EQ(memcmp(buf, input, 8 * sizeof(wchar_t)), 0);
}

TEST_CASE(wcscoll)
{
// Check if wcscoll is sorting correctly. At the moment we are doing raw char comparisons,
Expand Down
8 changes: 8 additions & 0 deletions Userland/Libraries/LibC/wchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,12 @@ wchar_t* wmemchr(const wchar_t* s, wchar_t c, size_t n)

return nullptr;
}

wchar_t* wmemcpy(wchar_t* dest, const wchar_t* src, size_t n)
{
for (size_t i = 0; i < n; i++)
dest[i] = src[i];

return dest;
}
}
1 change: 1 addition & 0 deletions Userland/Libraries/LibC/wchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ 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);
wchar_t* wmemcpy(wchar_t*, const wchar_t*, size_t);

__END_DECLS

0 comments on commit 485c0ef

Please sign in to comment.