[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();
+  }
+  if (wc.has_value()) {
+    *pwc = wc.value();
+    // null terminator -> return 0
+    if (wc.value() == L'\0')
+      return 0;
+    return i;
+  }
+  // Incomplete but potentially valid
+  return Error(-2);
----------------
uzairnawaz wrote:

Should this be an error case? It may be better to just return -2 here, that way in the public libc function you can update errno if there is an error, without having to check specifically for this case. 

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


More information about the libc-commits mailing list