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

Uzair Nawaz via libc-commits libc-commits at lists.llvm.org
Wed Jun 18 13:59:17 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();
+  // Reading in bytes until we have a complete wc or error
+  for (; i < n && !wc.has_value(); ++i) {
+    int err = char_conv.push(static_cast<char8_t>(s[i]));
+    // Encoding error
+    if (err == -1)
+      return Error(-1);
+    wc = char_conv.pop_utf32();
----------------
uzairnawaz wrote:

Instead of relying on the error value of pop, it may be more readable to use the ```CharacterConverter::isComplete()``` method as the loop condition and only pop after the loop ends if there is a complete character

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


More information about the libc-commits mailing list