mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 03:07:36 +00:00
LibC: Implement wcrtomb
This function converts a single wide character into its multibyte representation (UTF-8 in our case). It is called from libc++'s `std::basic_ostream<wchar_t>::flush`, which gets called at program exit from a global destructor in order to flush `std::wcout`.
This commit is contained in:
parent
9c29e6cde7
commit
c8367df746
3 changed files with 72 additions and 3 deletions
|
@ -285,3 +285,34 @@ TEST_CASE(mbrtowc)
|
|||
EXPECT_EQ(ret, -1ul);
|
||||
EXPECT_EQ(errno, EILSEQ);
|
||||
}
|
||||
|
||||
TEST_CASE(wcrtomb)
|
||||
{
|
||||
char buf[MB_LEN_MAX];
|
||||
size_t ret = 0;
|
||||
|
||||
// Ensure that `wc` is ignored when buf is a nullptr.
|
||||
ret = wcrtomb(nullptr, L'a', nullptr);
|
||||
EXPECT_EQ(ret, 1ul);
|
||||
|
||||
ret = wcrtomb(nullptr, L'\U0001F41E', nullptr);
|
||||
EXPECT_EQ(ret, 1ul);
|
||||
|
||||
// When the buffer is non-null, the multibyte representation is written into it.
|
||||
ret = wcrtomb(buf, L'a', nullptr);
|
||||
EXPECT_EQ(ret, 1ul);
|
||||
EXPECT_EQ(memcmp(buf, "a", ret), 0);
|
||||
|
||||
ret = wcrtomb(buf, L'\U0001F41E', nullptr);
|
||||
EXPECT_EQ(ret, 4ul);
|
||||
EXPECT_EQ(memcmp(buf, "\xf0\x9f\x90\x9e", ret), 0);
|
||||
|
||||
// When the wide character is invalid, -1 is returned and errno is set to EILSEQ.
|
||||
ret = wcrtomb(buf, 0x110000, nullptr);
|
||||
EXPECT_EQ(ret, (size_t)-1);
|
||||
EXPECT_EQ(errno, EILSEQ);
|
||||
|
||||
// Replacement characters and conversion errors are not confused.
|
||||
ret = wcrtomb(buf, L'\uFFFD', nullptr);
|
||||
EXPECT_NE(ret, (size_t)-1);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue