From e3ef29e80dd3d2d50e805895347d32e3f2d3926a Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 14 Jan 2023 11:59:08 +0100 Subject: [PATCH] libc: Implement wcstombs() --- libc/include/stdlib.h | 3 +++ libc/src/stdlib.cpp | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 9b38de3d..1c31d81c 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -110,6 +110,9 @@ extern "C" /* Convert a multibyte character string to a wide character string. */ size_t mbstowcs(wchar_t* buf, const char* src, size_t max); + /* Convert a wide character string to a multibyte character string. */ + size_t wcstombs(char* buf, const wchar_t* src, size_t max); + #ifdef __cplusplus } #endif diff --git a/libc/src/stdlib.cpp b/libc/src/stdlib.cpp index 7d107125..9bd77c6f 100644 --- a/libc/src/stdlib.cpp +++ b/libc/src/stdlib.cpp @@ -115,6 +115,17 @@ extern "C" return decoder.decode(buf, max).value_or((size_t)-1); } + size_t wcstombs(char* buf, const wchar_t* src, size_t max) + { + if (max == 0) return 0; + + Utf8StringEncoder encoder(src); + + if (!buf) { return encoder.byte_length().value_or((size_t)-1); } + + return encoder.encode(buf, max).value_or((size_t)-1); + } + void* malloc(size_t size) { auto rc = malloc_impl(size);