From 485c0ef691822873df8a6961ec099779ff19412c Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 22 Sep 2021 10:21:06 +0000 Subject: [PATCH] LibC: Implement wmemcpy --- Tests/LibC/TestWchar.cpp | 17 +++++++++++++++++ Userland/Libraries/LibC/wchar.cpp | 8 ++++++++ Userland/Libraries/LibC/wchar.h | 1 + 3 files changed, 26 insertions(+) diff --git a/Tests/LibC/TestWchar.cpp b/Tests/LibC/TestWchar.cpp index 82e658366a..776f776386 100644 --- a/Tests/LibC/TestWchar.cpp +++ b/Tests/LibC/TestWchar.cpp @@ -6,6 +6,7 @@ #include +#include #include TEST_CASE(wcspbrk) @@ -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(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, diff --git a/Userland/Libraries/LibC/wchar.cpp b/Userland/Libraries/LibC/wchar.cpp index a3927111d3..052ab8b1cc 100644 --- a/Userland/Libraries/LibC/wchar.cpp +++ b/Userland/Libraries/LibC/wchar.cpp @@ -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; +} } diff --git a/Userland/Libraries/LibC/wchar.h b/Userland/Libraries/LibC/wchar.h index 5ec7f9e253..a622906218 100644 --- a/Userland/Libraries/LibC/wchar.h +++ b/Userland/Libraries/LibC/wchar.h @@ -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