[libc-commits] [libc] [libc] mbrtowc implementation (PR #144760)

Uzair Nawaz via libc-commits libc-commits at lists.llvm.org
Wed Jun 18 13:59:16 PDT 2025


================
@@ -0,0 +1,50 @@
+//===-- Implementation for mbrtowc function ---------------------*- C++ -*-===//
+//
+// 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/mbrtowc.h"
+#include "hdr/types/mbstate_t.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/wchar/character_converter.h"
+#include "src/__support/wchar/mbstate.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
+                        size_t n, mbstate *__restrict ps) {
+  CharacterConverter char_conv(ps);
+  if (s == nullptr)
+    return 0;
+  size_t i = 0;
+  auto wc = char_conv.pop_utf32();
----------------
uzairnawaz wrote:

why do we pop_utf32 here? Aren't we guaranteed to not have a complete utf32 character in our mbstate at this point? If a previous call to mbrtowc had enough information to complete a conversion it would have already popped the utf32 out already. 


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


More information about the libc-commits mailing list