[libc-commits] [libc] [libc] Implemented wcrtomb internal function and public libc function (PR #144596)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Fri Jun 20 09:56:56 PDT 2025
================
@@ -0,0 +1,50 @@
+//===-- Implementation of wcrtomb -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/wchar/wcrtomb.h"
+#include "src/__support/error_or.h"
+#include "src/__support/wchar/character_converter.h"
+#include "src/__support/wchar/mbstate.h"
+
+#include "hdr/types/char32_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc,
+ mbstate *__restrict ps) {
+ static_assert(sizeof(wchar_t) == 4);
+
+ CharacterConverter cr(ps);
+
+ // when s is nullptr, this is equivalent to wcrtomb(buf, L'\0', ps)
+ char buf[sizeof(wchar_t) / sizeof(char)];
+ if (s == nullptr) {
+ s = buf;
+ wc = L'\0';
+ }
----------------
michaelrj-google wrote:
does it make sense to handle this buffering behavior in the internal function or in the public function? Maybe if `s == nullptr` in the internal function you could return an error.
https://github.com/llvm/llvm-project/pull/144596
More information about the libc-commits
mailing list