summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/wchar.cpp
diff options
context:
space:
mode:
authorDaniel Bertalan <dani@danielbertalan.dev>2021-10-04 16:59:13 +0200
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-10-15 21:50:19 -0700
commitc8367df74639a399a2195a2a07ff7ac74b88d426 (patch)
treedd9febcec72643bec3138f47e43f9a5f364fde4f /Userland/Libraries/LibC/wchar.cpp
parent9c29e6cde7615cdb0dc8037dd67df422ecf064a6 (diff)
downloadserenity-c8367df74639a399a2195a2a07ff7ac74b88d426.zip
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`.
Diffstat (limited to 'Userland/Libraries/LibC/wchar.cpp')
-rw-r--r--Userland/Libraries/LibC/wchar.cpp19
1 files changed, 16 insertions, 3 deletions
diff --git a/Userland/Libraries/LibC/wchar.cpp b/Userland/Libraries/LibC/wchar.cpp
index 37e6202b34..a0e1b361b6 100644
--- a/Userland/Libraries/LibC/wchar.cpp
+++ b/Userland/Libraries/LibC/wchar.cpp
@@ -6,6 +6,7 @@
#include <AK/Assertions.h>
#include <AK/Format.h>
+#include <AK/UnicodeUtils.h>
#include <errno.h>
#include <wchar.h>
@@ -292,10 +293,22 @@ size_t mbrlen(const char*, size_t, mbstate_t*)
TODO();
}
-size_t wcrtomb(char*, wchar_t, mbstate_t*)
+size_t wcrtomb(char* s, wchar_t wc, mbstate_t*)
{
- dbgln("FIXME: Implement wcrtomb()");
- TODO();
+ if (s == nullptr)
+ wc = L'\0';
+
+ auto nwritten = AK::UnicodeUtils::code_point_to_utf8(wc, [&s](char byte) {
+ if (s != nullptr)
+ *s++ = byte;
+ });
+
+ if (nwritten < 0) {
+ errno = EILSEQ;
+ return (size_t)-1;
+ } else {
+ return nwritten;
+ }
}
int wcscoll(const wchar_t* ws1, const wchar_t* ws2)