[libc-commits] [libc] [libc] Added c32rtomb implementation (PR #236061)

Arya C S via libc-commits libc-commits at lists.llvm.org
Fri Nov 13 22:35:02 PST 2026


https://github.com/AryaCS111 updated https://github.com/llvm/llvm-project/pull/236061

>From 7eae86750c008ab68399800a5d609b0a19688179 Mon Sep 17 00:00:00 2001
From: "arya.cs" <arya.cs at blackfigtech.com>
Date: Tue, 27 Oct 2026 16:00:49 +0530
Subject: [PATCH 1/2] [libc] Added c32rtomb implementation

---
 libc/config/linux/riscv/entrypoints.txt       |   1 +
 libc/config/linux/x86_64/entrypoints.txt      |   1 +
 libc/include/CMakeLists.txt                   |   1 +
 libc/include/wchar.yaml                       |   9 ++
 libc/src/__support/wchar/CMakeLists.txt       |  16 ++
 libc/src/__support/wchar/c32rtomb.h           |  54 +++++++
 .../src/__support/wchar/character_converter.h |   5 +
 libc/src/wchar/CMakeLists.txt                 |  14 ++
 libc/src/wchar/c32rtomb.cpp                   |  46 ++++++
 libc/src/wchar/c32rtomb.h                     |  23 +++
 libc/test/src/wchar/CMakeLists.txt            |  16 ++
 libc/test/src/wchar/c32rtomb_test.cpp         | 145 ++++++++++++++++++
 12 files changed, 331 insertions(+)
 create mode 100644 libc/src/__support/wchar/c32rtomb.h
 create mode 100644 libc/src/wchar/c32rtomb.cpp
 create mode 100644 libc/src/wchar/c32rtomb.h
 create mode 100644 libc/test/src/wchar/c32rtomb_test.cpp

diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index b8082355d8c5af..080736567c5ce2 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1777,6 +1777,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.wchar.mbsrtowcs
     libc.src.wchar.mbsnrtowcs
     libc.src.wchar.wcrtomb
+    libc.src.wchar.c32rtomb
     libc.src.wchar.wcsrtombs
     libc.src.wchar.wcsnrtombs
     libc.src.wchar.fgetwc
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 59d5a4ab376732..d5a895b260e75b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1781,6 +1781,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.wchar.mbsrtowcs
     libc.src.wchar.mbsnrtowcs
     libc.src.wchar.wcrtomb
+    libc.src.wchar.c32rtomb
     libc.src.wchar.wcsrtombs
     libc.src.wchar.wcsnrtombs
     libc.src.wchar.fgetwc
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 7ace76cc9b56d3..6784d10637658d 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -1320,6 +1320,7 @@ add_header_macro(
     .llvm-libc-types.wchar_t
     .llvm-libc-types.FILE
     .llvm-libc-types.struct_tm
+    .llvm-libc-types.char32_t
 )
 
 add_header_macro(
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 2b321f6502ff16..ca047963978641 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -17,6 +17,7 @@ types:
   - type_name: wint_t
   - type_name: wchar_t
   - type_name: mbstate_t
+  - type_name: char32_t
 enums: []
 objects: []
 functions:
@@ -272,6 +273,14 @@ functions:
       - type: char *__restrict
       - type: wchar_t
       - type: mbstate_t *__restrict
+  - name: c32rtomb
+    standards:
+      - stdc
+    return_type: size_t
+    arguments:
+      - type: char *__restrict
+      - type: char32_t
+      - type: mbstate_t *__restrict
   - name: wcscpy
     standards:
       - stdc
diff --git a/libc/src/__support/wchar/CMakeLists.txt b/libc/src/__support/wchar/CMakeLists.txt
index bbb18f0fa57800..54614e77359786 100644
--- a/libc/src/__support/wchar/CMakeLists.txt
+++ b/libc/src/__support/wchar/CMakeLists.txt
@@ -40,6 +40,22 @@ add_header_library(
     .mbstate
 )
 
+add_header_library(
+  c32rtomb
+  HDRS
+    c32rtomb.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.char32_t
+    libc.hdr.types.size_t
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_assert
+    libc.src.__support.macros.null_check
+    .character_converter
+    .mbstate
+)
+
 add_header_library(
   wcrtomb
   HDRS
diff --git a/libc/src/__support/wchar/c32rtomb.h b/libc/src/__support/wchar/c32rtomb.h
new file mode 100644
index 00000000000000..9e8d34e13cd77c
--- /dev/null
+++ b/libc/src/__support/wchar/c32rtomb.h
@@ -0,0 +1,54 @@
+//===-- Implementation header for c32rtomb --------------------------------===//
+//
+// 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__SUPPORT_WCHAR_C32RTOMB_H
+#define LLVM_LIBC_SRC__SUPPORT_WCHAR_C32RTOMB_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/char32_t.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_assert.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/wchar/character_converter.h"
+#include "src/__support/wchar/mbstate.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+LIBC_INLINE ErrorOr<size_t> c32rtomb(char *__restrict dest_ptr, char32_t c32,
+                                     mbstate *__restrict ps) {
+  LIBC_CRASH_ON_NULLPTR(dest_ptr);
+  LIBC_CRASH_ON_NULLPTR(ps);
+
+  CharacterConverter cr(ps);
+
+  if (!cr.isValidState())
+    return Error(EINVAL);
+
+  int status = cr.push(c32);
+  if (status != 0)
+    return Error(status);
+
+  size_t count = 0;
+  while (!cr.isEmpty()) {
+    auto utf8 = cr.pop_utf8(); // can never fail as long as the push succeeded
+    LIBC_ASSERT(utf8.has_value());
+
+    *dest_ptr = utf8.value();
+    dest_ptr++;
+    count++;
+  }
+  return count;
+}
+
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC__SUPPORT_WCHAR_C32RTOMB_H
diff --git a/libc/src/__support/wchar/character_converter.h b/libc/src/__support/wchar/character_converter.h
index f754a602d2cce0..b518ea6eb24671 100644
--- a/libc/src/__support/wchar/character_converter.h
+++ b/libc/src/__support/wchar/character_converter.h
@@ -132,6 +132,11 @@ LIBC_INLINE int CharacterConverter::push(char32_t utf32) {
   if (!isEmpty())
     return -1;
 
+  if (utf32 >= 0xd800 && utf32 <= 0xdfff) {
+    clear();
+    return EILSEQ;
+  }
+
   state->partial = utf32;
 
   // determine number of utf-8 bytes needed to represent this utf32 value
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 3bbb9d483b0492..34576c83489c63 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -154,6 +154,20 @@ add_entrypoint_object(
     libc.src.__support.wchar.mbstate
 )
 
+add_entrypoint_object(
+  c32rtomb
+  SRCS
+    c32rtomb.cpp
+  HDRS
+    c32rtomb.h
+  DEPENDS
+    libc.hdr.types.char32_t
+    libc.hdr.types.mbstate_t
+    libc.src.__support.libc_errno
+    libc.src.__support.wchar.c32rtomb
+    libc.src.__support.wchar.mbstate
+)
+
 add_entrypoint_object(
   mbsinit
   SRCS
diff --git a/libc/src/wchar/c32rtomb.cpp b/libc/src/wchar/c32rtomb.cpp
new file mode 100644
index 00000000000000..b20554b3723dc3
--- /dev/null
+++ b/libc/src/wchar/c32rtomb.cpp
@@ -0,0 +1,46 @@
+//===-- Implementation of c32rtomb ----------------------------------------===//
+//
+// 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/c32rtomb.h"
+
+#include "hdr/types/char32_t.h"
+#include "hdr/types/mbstate_t.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/wchar/c32rtomb.h"
+#include "src/__support/wchar/mbstate.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, c32rtomb,
+                   (char *__restrict s, char32_t c32,
+                    mbstate_t *__restrict ps)) {
+  static internal::mbstate internal_mbstate;
+
+  // when s is nullptr, this is equivalent to c32rtomb(buf, U'\0', ps)
+  char buf[sizeof(char32_t) / sizeof(char)];
+  if (s == nullptr) {
+    s = buf;
+    c32 = 0;
+  }
+
+  auto result = internal::c32rtomb(
+      s, c32,
+      ps == nullptr ? &internal_mbstate
+                    : reinterpret_cast<internal::mbstate *>(ps));
+
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/c32rtomb.h b/libc/src/wchar/c32rtomb.h
new file mode 100644
index 00000000000000..42e289e4545d78
--- /dev/null
+++ b/libc/src/wchar/c32rtomb.h
@@ -0,0 +1,23 @@
+//===-- Implementation header for c32rtomb ----------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_C32RTOMB_H
+#define LLVM_LIBC_SRC_WCHAR_C32RTOMB_H
+
+#include "hdr/types/char32_t.h"
+#include "hdr/types/mbstate_t.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+size_t c32rtomb(char *__restrict s, char32_t c32, mbstate_t *__restrict ps);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_C32RTOMB_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 52b40eeafa56d3..2e9281a56e6030 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -150,6 +150,22 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
+add_libc_test(
+  c32rtomb_test
+  SUITE
+    libc-wchar-tests
+  SRCS
+    c32rtomb_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.src.wchar.c32rtomb
+    libc.src.string.memset
+    libc.hdr.types.char32_t
+    libc.hdr.types.mbstate_t
+    libc.src.__support.wchar.mbstate
+    libc.test.UnitTest.ErrnoCheckingTest
+)
+
 add_libc_test(
   wcsrtombs_test
   SUITE
diff --git a/libc/test/src/wchar/c32rtomb_test.cpp b/libc/test/src/wchar/c32rtomb_test.cpp
new file mode 100644
index 00000000000000..5f58614ab0d193
--- /dev/null
+++ b/libc/test/src/wchar/c32rtomb_test.cpp
@@ -0,0 +1,145 @@
+//===-- Unittests for c32rtomb --------------------------------------------===//
+//
+// 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/errno_macros.h"
+#include "hdr/types/char32_t.h"
+#include "hdr/types/mbstate_t.h"
+#include "src/__support/wchar/mbstate.h"
+#include "src/string/memset.h"
+#include "src/wchar/c32rtomb.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcC32RToMBTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcC32RToMBTest, OneByte) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  char32_t c32 = U'U';
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
+  ASSERT_EQ(cnt, static_cast<size_t>(1));
+  ASSERT_EQ(mb[0], 'U');
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcC32RToMBTest, TwoByte) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  // testing utf32: 0xff -> utf8: 0xc3 0xbf
+  char32_t c32 = 0xff;
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
+  ASSERT_EQ(cnt, static_cast<size_t>(2));
+  ASSERT_EQ(mb[0], static_cast<char>(0xc3));
+  ASSERT_EQ(mb[1], static_cast<char>(0xbf));
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcC32RToMBTest, ThreeByte) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  // testing utf32: 0xac15 -> utf8: 0xea 0xb0 0x95
+  char32_t c32 = 0xac15;
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
+  ASSERT_EQ(cnt, static_cast<size_t>(3));
+  ASSERT_EQ(mb[0], static_cast<char>(0xea));
+  ASSERT_EQ(mb[1], static_cast<char>(0xb0));
+  ASSERT_EQ(mb[2], static_cast<char>(0x95));
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcC32RToMBTest, FourByte) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  // testing utf32: 0x1f921 -> utf8: 0xf0 0x9f 0xa4 0xa1
+  char32_t c32 = 0x1f921;
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
+  ASSERT_EQ(cnt, static_cast<size_t>(4));
+  ASSERT_EQ(mb[0], static_cast<char>(0xf0));
+  ASSERT_EQ(mb[1], static_cast<char>(0x9f));
+  ASSERT_EQ(mb[2], static_cast<char>(0xa4));
+  ASSERT_EQ(mb[3], static_cast<char>(0xa1));
+  ASSERT_ERRNO_SUCCESS();
+}
+
+TEST_F(LlvmLibcC32RToMBTest, NullString) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  char32_t c32 = U'A';
+  char mb[4];
+
+  // should be equivalent to the call c32rtomb(buf, U'\0', state)
+  size_t cnt1 = LIBC_NAMESPACE::c32rtomb(nullptr, c32, &state);
+  ASSERT_ERRNO_SUCCESS();
+  size_t cnt2 = LIBC_NAMESPACE::c32rtomb(mb, 0, &state);
+  ASSERT_ERRNO_SUCCESS();
+
+  ASSERT_EQ(cnt1, cnt2);
+  ASSERT_EQ(cnt1, static_cast<size_t>(1));
+  ASSERT_EQ(mb[0], '\0');
+}
+
+TEST_F(LlvmLibcC32RToMBTest, NullState) {
+  char32_t c32 = U'A';
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, nullptr);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(cnt, static_cast<size_t>(1));
+  ASSERT_EQ(mb[0], 'A');
+}
+
+TEST_F(LlvmLibcC32RToMBTest, NullWideChar) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, 0, &state);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_EQ(cnt, static_cast<size_t>(1));
+  ASSERT_EQ(mb[0], '\0');
+}
+
+TEST_F(LlvmLibcC32RToMBTest, InvalidCharTooLarge) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  char32_t c32 = 0x12ffff;
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
+  ASSERT_EQ(cnt, static_cast<size_t>(-1));
+  ASSERT_ERRNO_EQ(EILSEQ);
+}
+
+TEST_F(LlvmLibcC32RToMBTest, InvalidSurrogate) {
+  mbstate_t state;
+  LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
+  char mb[4];
+
+  // Leading surrogate
+  size_t cnt1 = LIBC_NAMESPACE::c32rtomb(mb, 0xd800, &state);
+  ASSERT_EQ(cnt1, static_cast<size_t>(-1));
+  ASSERT_ERRNO_EQ(EILSEQ);
+
+  // Trailing surrogate
+  size_t cnt2 = LIBC_NAMESPACE::c32rtomb(mb, 0xdfff, &state);
+  ASSERT_EQ(cnt2, static_cast<size_t>(-1));
+  ASSERT_ERRNO_EQ(EILSEQ);
+}
+
+TEST_F(LlvmLibcC32RToMBTest, InvalidMBState) {
+  mbstate_t *state;
+  LIBC_NAMESPACE::internal::mbstate inv;
+  inv.total_bytes = 6;
+  state = reinterpret_cast<mbstate_t *>(&inv);
+  char32_t c32 = U'A';
+  char mb[4];
+  size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, state);
+  ASSERT_EQ(cnt, static_cast<size_t>(-1));
+  ASSERT_ERRNO_EQ(EINVAL);
+}

>From 49ddf652168ec4aa0ae5421f369afcf4290537f8 Mon Sep 17 00:00:00 2001
From: "arya.cs" <arya.cs at blackfigtech.com>
Date: Sat, 14 Nov 2026 11:59:13 +0530
Subject: [PATCH 2/2] [libc] - updated comments according to the review

---
 libc/src/__support/wchar/c32rtomb.h   | 15 ++++++++++-----
 libc/src/wchar/c32rtomb.cpp           |  9 +++++++--
 libc/src/wchar/c32rtomb.h             |  7 ++++++-
 libc/test/src/wchar/c32rtomb_test.cpp | 19 ++++++++++++-------
 4 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/libc/src/__support/wchar/c32rtomb.h b/libc/src/__support/wchar/c32rtomb.h
index 9e8d34e13cd77c..b7297bd5b44643 100644
--- a/libc/src/__support/wchar/c32rtomb.h
+++ b/libc/src/__support/wchar/c32rtomb.h
@@ -1,13 +1,18 @@
-//===-- Implementation header for c32rtomb --------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation of the internal c32rtomb function.
+///
+//===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_SRC__SUPPORT_WCHAR_C32RTOMB_H
-#define LLVM_LIBC_SRC__SUPPORT_WCHAR_C32RTOMB_H
+#ifndef LLVM_LIBC_SRC___SUPPORT_WCHAR_C32RTOMB_H
+#define LLVM_LIBC_SRC___SUPPORT_WCHAR_C32RTOMB_H
 
 #include "hdr/errno_macros.h"
 #include "hdr/types/char32_t.h"
@@ -38,7 +43,7 @@ LIBC_INLINE ErrorOr<size_t> c32rtomb(char *__restrict dest_ptr, char32_t c32,
 
   size_t count = 0;
   while (!cr.isEmpty()) {
-    auto utf8 = cr.pop_utf8(); // can never fail as long as the push succeeded
+    auto utf8 = cr.pop_utf8(); // Can never fail as long as the push succeeded.
     LIBC_ASSERT(utf8.has_value());
 
     *dest_ptr = utf8.value();
@@ -51,4 +56,4 @@ LIBC_INLINE ErrorOr<size_t> c32rtomb(char *__restrict dest_ptr, char32_t c32,
 } // namespace internal
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif // LLVM_LIBC_SRC__SUPPORT_WCHAR_C32RTOMB_H
+#endif // LLVM_LIBC_SRC___SUPPORT_WCHAR_C32RTOMB_H
diff --git a/libc/src/wchar/c32rtomb.cpp b/libc/src/wchar/c32rtomb.cpp
index b20554b3723dc3..1e2da52cbae8a3 100644
--- a/libc/src/wchar/c32rtomb.cpp
+++ b/libc/src/wchar/c32rtomb.cpp
@@ -1,10 +1,15 @@
-//===-- Implementation of c32rtomb ----------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the c32rtomb function implementation.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/wchar/c32rtomb.h"
 
@@ -23,7 +28,7 @@ LLVM_LIBC_FUNCTION(size_t, c32rtomb,
                     mbstate_t *__restrict ps)) {
   static internal::mbstate internal_mbstate;
 
-  // when s is nullptr, this is equivalent to c32rtomb(buf, U'\0', ps)
+  // When s is nullptr, this is equivalent to c32rtomb(buf, U'\0', ps).
   char buf[sizeof(char32_t) / sizeof(char)];
   if (s == nullptr) {
     s = buf;
diff --git a/libc/src/wchar/c32rtomb.h b/libc/src/wchar/c32rtomb.h
index 42e289e4545d78..aefc77aba1011f 100644
--- a/libc/src/wchar/c32rtomb.h
+++ b/libc/src/wchar/c32rtomb.h
@@ -1,10 +1,15 @@
-//===-- Implementation header for c32rtomb ----------------------*- 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains the c32rtomb function declaration.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC_WCHAR_C32RTOMB_H
 #define LLVM_LIBC_SRC_WCHAR_C32RTOMB_H
diff --git a/libc/test/src/wchar/c32rtomb_test.cpp b/libc/test/src/wchar/c32rtomb_test.cpp
index 5f58614ab0d193..79e98ded3ffa46 100644
--- a/libc/test/src/wchar/c32rtomb_test.cpp
+++ b/libc/test/src/wchar/c32rtomb_test.cpp
@@ -1,10 +1,15 @@
-//===-- Unittests for c32rtomb --------------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
 //
 //===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file contains unit tests for c32rtomb.
+///
+//===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
 #include "hdr/types/char32_t.h"
@@ -31,7 +36,7 @@ TEST_F(LlvmLibcC32RToMBTest, OneByte) {
 TEST_F(LlvmLibcC32RToMBTest, TwoByte) {
   mbstate_t state;
   LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
-  // testing utf32: 0xff -> utf8: 0xc3 0xbf
+  // Testing UTF-32: 0xff -> UTF-8: 0xc3 0xbf.
   char32_t c32 = 0xff;
   char mb[4];
   size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
@@ -44,7 +49,7 @@ TEST_F(LlvmLibcC32RToMBTest, TwoByte) {
 TEST_F(LlvmLibcC32RToMBTest, ThreeByte) {
   mbstate_t state;
   LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
-  // testing utf32: 0xac15 -> utf8: 0xea 0xb0 0x95
+  // Testing UTF-32: 0xac15 -> UTF-8: 0xea 0xb0 0x95.
   char32_t c32 = 0xac15;
   char mb[4];
   size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
@@ -58,7 +63,7 @@ TEST_F(LlvmLibcC32RToMBTest, ThreeByte) {
 TEST_F(LlvmLibcC32RToMBTest, FourByte) {
   mbstate_t state;
   LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
-  // testing utf32: 0x1f921 -> utf8: 0xf0 0x9f 0xa4 0xa1
+  // Testing UTF-32: 0x1f921 -> UTF-8: 0xf0 0x9f 0xa4 0xa1.
   char32_t c32 = 0x1f921;
   char mb[4];
   size_t cnt = LIBC_NAMESPACE::c32rtomb(mb, c32, &state);
@@ -76,7 +81,7 @@ TEST_F(LlvmLibcC32RToMBTest, NullString) {
   char32_t c32 = U'A';
   char mb[4];
 
-  // should be equivalent to the call c32rtomb(buf, U'\0', state)
+  // Should be equivalent to the call c32rtomb(buf, U'\0', state).
   size_t cnt1 = LIBC_NAMESPACE::c32rtomb(nullptr, c32, &state);
   ASSERT_ERRNO_SUCCESS();
   size_t cnt2 = LIBC_NAMESPACE::c32rtomb(mb, 0, &state);
@@ -121,12 +126,12 @@ TEST_F(LlvmLibcC32RToMBTest, InvalidSurrogate) {
   LIBC_NAMESPACE::memset(&state, 0, sizeof(mbstate_t));
   char mb[4];
 
-  // Leading surrogate
+  // Leading surrogate.
   size_t cnt1 = LIBC_NAMESPACE::c32rtomb(mb, 0xd800, &state);
   ASSERT_EQ(cnt1, static_cast<size_t>(-1));
   ASSERT_ERRNO_EQ(EILSEQ);
 
-  // Trailing surrogate
+  // Trailing surrogate.
   size_t cnt2 = LIBC_NAMESPACE::c32rtomb(mb, 0xdfff, &state);
   ASSERT_EQ(cnt2, static_cast<size_t>(-1));
   ASSERT_ERRNO_EQ(EILSEQ);



More information about the libc-commits mailing list