[libc-commits] [libc] [libc] Implement mbsinit (PR #150654)

Uzair Nawaz via libc-commits libc-commits at lists.llvm.org
Fri Jul 25 10:21:45 PDT 2025


https://github.com/uzairnawaz updated https://github.com/llvm/llvm-project/pull/150654

>From 90deb817af747b54d526941fc27c393a4cfd4e9c Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Fri, 25 Jul 2025 17:05:32 +0000
Subject: [PATCH 1/3] mbsinit

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/include/wchar.yaml                  |  6 +++++
 libc/src/wchar/CMakeLists.txt            | 15 +++++++++++
 libc/src/wchar/mbsinit.cpp               | 29 ++++++++++++++++++++
 libc/src/wchar/mbsinit.h                 | 22 +++++++++++++++
 libc/test/src/wchar/CMakeLists.txt       | 14 ++++++++++
 libc/test/src/wchar/mbsinit_test.cpp     | 34 ++++++++++++++++++++++++
 7 files changed, 121 insertions(+)
 create mode 100644 libc/src/wchar/mbsinit.cpp
 create mode 100644 libc/src/wchar/mbsinit.h
 create mode 100644 libc/test/src/wchar/mbsinit_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5af11db427310..4315bb060f79a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1264,6 +1264,7 @@ if(LLVM_LIBC_FULL_BUILD)
     # wchar.h entrypoints
     libc.src.wchar.mblen
     libc.src.wchar.mbrlen
+    libc.src.wchar.mbsinit
     libc.src.wchar.mbrtowc
     libc.src.wchar.mbtowc
     libc.src.wchar.mbstowcs
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index fbcbf1ce06b80..8178091ab2202 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -80,6 +80,12 @@ functions:
       - type: wchar_t *__restrict
       - type: const char *__restrict
       - type: size_t
+  - name: mbsinit
+    standards:
+      - stdc
+    return_type: int
+    arguments:
+      - type: mbstate_t *
   - name: mblen
     standards:
       - stdc
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index aca6634d45e7f..5a0fe2a523839 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -136,6 +136,21 @@ add_entrypoint_object(
     libc.src.__support.libc_errno
 )
 
+add_entrypoint_object(
+  mbsinit
+  SRCS
+    mbsinit.cpp
+  HDRS
+    mbsinit.h
+  DEPENDS
+    libc.hdr.types.wchar_t
+    libc.hdr.types.mbstate_t
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.wchar.character_converter
+    libc.src.__support.wchar.mbstate
+)
+
 add_entrypoint_object(
   mbrtowc
   SRCS
diff --git a/libc/src/wchar/mbsinit.cpp b/libc/src/wchar/mbsinit.cpp
new file mode 100644
index 0000000000000..8ad6103ea318a
--- /dev/null
+++ b/libc/src/wchar/mbsinit.cpp
@@ -0,0 +1,29 @@
+//===-- Implementation of mbsinit -----------------------------------------===//
+//
+// 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/wchar/mbsinit.h"
+
+#include "hdr/types/mbstate_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/wchar/character_converter.h"
+#include "src/__support/wchar/mbstate.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, mbsinit, (mbstate_t * ps)) {
+  if (ps == nullptr)
+    return true;
+  internal::CharacterConverter cr(reinterpret_cast<internal::mbstate *>(ps));
+  if (cr.isValidState() && cr.isEmpty()) {
+    return true;
+  }
+  return false;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/mbsinit.h b/libc/src/wchar/mbsinit.h
new file mode 100644
index 0000000000000..fa6be0f596fd0
--- /dev/null
+++ b/libc/src/wchar/mbsinit.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for mbsinit ---------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_MBSINIT_H
+#define LLVM_LIBC_SRC_WCHAR_MBSINIT_H
+
+#include "hdr/types/mbstate_t.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int mbsinit(mbstate_t *ps);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_MBSINIT_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 48d9c48f2380c..4b3bc88f3eb00 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -132,6 +132,20 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
+add_libc_test(
+  mbsinit_test
+  SUITE
+    libc_wchar_unittests
+  SRCS
+    mbsinit_test.cpp
+  DEPENDS
+    libc.src.string.memset
+    libc.src.wchar.mbsinit
+    libc.src.wchar.mbrtowc
+    libc.hdr.types.mbstate_t
+    libc.hdr.types.wchar_t
+)
+
 add_libc_test(
   mbsnrtowcs_test
   SUITE
diff --git a/libc/test/src/wchar/mbsinit_test.cpp b/libc/test/src/wchar/mbsinit_test.cpp
new file mode 100644
index 0000000000000..d9fbdff033216
--- /dev/null
+++ b/libc/test/src/wchar/mbsinit_test.cpp
@@ -0,0 +1,34 @@
+//===-- Unittests for mbsinit
+//----------------------------------------------===//
+//
+// 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 "hdr/types/wchar_t.h"
+#include "src/string/memset.h"
+#include "src/wchar/mbrtowc.h"
+#include "src/wchar/mbsinit.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcMBSInitTest, EmptyState) {
+  mbstate_t ps;
+  LIBC_NAMESPACE::memset(&ps, 0, sizeof(mbstate_t));
+  ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0);
+  ASSERT_NE(LIBC_NAMESPACE::mbsinit(nullptr), 0);
+}
+
+TEST(LlvmLibcMBSInitTest, ConversionTest) {
+  const char *src = "\xf0\x9f\xa4\xa3"; // 4 byte emoji
+  wchar_t dest[2];
+  mbstate_t ps;
+  LIBC_NAMESPACE::memset(&ps, 0, sizeof(mbstate_t));
+
+  ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0);
+  LIBC_NAMESPACE::mbrtowc(dest, src, 2, &ps); // partial conversion
+  ASSERT_EQ(LIBC_NAMESPACE::mbsinit(&ps), 0);
+  LIBC_NAMESPACE::mbrtowc(dest, src + 2, 2, &ps); // complete conversion
+  ASSERT_NE(LIBC_NAMESPACE::mbsinit(&ps), 0); // state should be reset now
+}

>From 57e6bfe2cd2d7dbadc39eac9d316df0660bc0590 Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Fri, 25 Jul 2025 17:05:53 +0000
Subject: [PATCH 2/3] format

---
 libc/src/wchar/mbsinit.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/src/wchar/mbsinit.cpp b/libc/src/wchar/mbsinit.cpp
index 8ad6103ea318a..251a15741b65a 100644
--- a/libc/src/wchar/mbsinit.cpp
+++ b/libc/src/wchar/mbsinit.cpp
@@ -20,9 +20,8 @@ LLVM_LIBC_FUNCTION(int, mbsinit, (mbstate_t * ps)) {
   if (ps == nullptr)
     return true;
   internal::CharacterConverter cr(reinterpret_cast<internal::mbstate *>(ps));
-  if (cr.isValidState() && cr.isEmpty()) {
+  if (cr.isValidState() && cr.isEmpty())
     return true;
-  }
   return false;
 }
 

>From 39a7026e115f270898437a7949d0e76ba4380eda Mon Sep 17 00:00:00 2001
From: Uzair Nawaz <uzairnawaz at google.com>
Date: Fri, 25 Jul 2025 17:17:56 +0000
Subject: [PATCH 3/3] removed redundant branch

---
 libc/src/wchar/mbsinit.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/src/wchar/mbsinit.cpp b/libc/src/wchar/mbsinit.cpp
index 251a15741b65a..23ba542ceefa3 100644
--- a/libc/src/wchar/mbsinit.cpp
+++ b/libc/src/wchar/mbsinit.cpp
@@ -20,9 +20,7 @@ LLVM_LIBC_FUNCTION(int, mbsinit, (mbstate_t * ps)) {
   if (ps == nullptr)
     return true;
   internal::CharacterConverter cr(reinterpret_cast<internal::mbstate *>(ps));
-  if (cr.isValidState() && cr.isEmpty())
-    return true;
-  return false;
+  return cr.isValidState() && cr.isEmpty();
 }
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list