[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
Wed Jun 18 09:56:19 PDT 2025


================
@@ -0,0 +1,48 @@
+//===-- 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/mbstate_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_t *__restrict ps) {
+  CharacterConverter cr((internal::mbstate *)ps);
+
+  char buf[sizeof(wchar_t) / sizeof(char)];
+  if (s == nullptr) {
+    s = buf;
+    wc = L'\0';
+  }
+
+  int status = cr.push((char32_t)wc);
----------------
michaelrj-google wrote:

this should be a static_cast, not a c style cast. Also it isn't safe to assume that `wchar_t` is always `char32_t`, for now you can put a `static_assert` at the top of the function to check that the type is correct.

https://github.com/llvm/llvm-project/pull/144596


More information about the libc-commits mailing list