LibC: Implement wmemmove

This commit is contained in:
Tim Schumacher 2021-09-22 10:24:12 +00:00 committed by Brian Gianforcaro
parent fa1208edfd
commit 05b283f552
Notes: sideshowbarker 2024-07-18 05:37:06 +09:00
3 changed files with 42 additions and 0 deletions

View File

@ -137,6 +137,32 @@ TEST_CASE(wmemset)
free(buf);
}
TEST_CASE(wmemmove)
{
wchar_t* ret;
const wchar_t* string = L"abc\0def";
auto buf = static_cast<wchar_t*>(calloc(32, sizeof(wchar_t)));
if (!buf) {
FAIL("Could not allocate memory for target buffer");
return;
}
// Test moving to smaller addresses.
wmemcpy(buf + 3, string, 8);
ret = wmemmove(buf + 1, buf + 3, 8);
EXPECT_EQ(ret, buf + 1);
EXPECT_EQ(memcmp(string, buf + 1, 8 * sizeof(wchar_t)), 0);
// Test moving to larger addresses.
wmemcpy(buf + 16, string, 8);
ret = wmemmove(buf + 18, buf + 16, 8);
EXPECT_EQ(ret, buf + 18);
EXPECT_EQ(memcmp(string, buf + 18, 8 * sizeof(wchar_t)), 0);
free(buf);
}
TEST_CASE(wcscoll)
{
// Check if wcscoll is sorting correctly. At the moment we are doing raw char comparisons,

View File

@ -402,4 +402,19 @@ wchar_t* wmemset(wchar_t* wcs, wchar_t wc, size_t n)
return wcs;
}
wchar_t* wmemmove(wchar_t* dest, const wchar_t* src, size_t n)
{
if (dest > src) {
for (size_t i = 1; i <= n; i++) {
dest[n - i] = src[n - i];
}
} else if (dest < src) {
for (size_t i = 0; i < n; i++) {
dest[i] = src[i];
}
}
return dest;
}
}

View File

@ -46,5 +46,6 @@ 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);
wchar_t* wmemset(wchar_t*, wchar_t, size_t);
wchar_t* wmemmove(wchar_t*, const wchar_t*, size_t);
__END_DECLS