From 07e4e764a0e4b4dcdeca6a11045220ffb5904f7c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 30 Jul 2025 00:28:17 +0300 Subject: [PATCH] LibC: Implement mbtowc --- userspace/libraries/LibC/stdlib.cpp | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/userspace/libraries/LibC/stdlib.cpp b/userspace/libraries/LibC/stdlib.cpp index 0cb34180..bf35b090 100644 --- a/userspace/libraries/LibC/stdlib.cpp +++ b/userspace/libraries/LibC/stdlib.cpp @@ -570,6 +570,45 @@ int mblen(const char* s, size_t n) ASSERT_NOT_REACHED(); } +int mbtowc(wchar_t* __restrict pwc, const char* __restrict s, size_t n) +{ + // no state-dependent encodings + if (s == nullptr) + return 0; + + switch (__getlocale(LC_CTYPE)) + { + case LOCALE_INVALID: + ASSERT_NOT_REACHED(); + case LOCALE_POSIX: + if (pwc != nullptr) + *pwc = *s; + return *s ? 1 : 0; + case LOCALE_UTF8: + const auto* us = reinterpret_cast(s); + + const uint32_t length = BAN::UTF8::byte_length(*us); + if (length == BAN::UTF8::invalid || n < length) + { + errno = EILSEQ; + return -1; + } + + const auto wch = BAN::UTF8::to_codepoint(us); + if (wch == BAN::UTF8::invalid) + { + errno = EILSEQ; + return -1; + } + + if (pwc) + *pwc = wch; + + return wch ? length : 0; + } + ASSERT_NOT_REACHED(); +} + size_t mbstowcs(wchar_t* __restrict pwcs, const char* __restrict s, size_t n) { size_t written = 0;