[libc-commits] [libc] [libc][wchar] Implement POSIX wcwidth function (PR #241728)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 11 23:53:19 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Alexey Samsonov (vonosmas)

<details>
<summary>Changes</summary>

Current implementation behavior (consistent with iswprint):
- Returns 0 for L'\0'.
- In default LIBC_WCTYPE_MODE_ASCII: returns 1 for printable ASCII (0x20..0x7e) and -1 for all other characters.
- In LIBC_WCTYPE_MODE_UTF8: returns 1 for all characters with PropertyFlag::PRINT and -1 otherwise (wctype_utils does not yet track 0-width combining marks or 2-width East Asian/emoji characters).

Comparison with other libc implementations:
- Matches glibc identically in the default "C" locale (the only locale LLVM-libc currently supports).
- Differs from glibc in UTF-8 locales and from musl (which always treats wchar_t as Unicode regardless of locale): in ASCII mode non-ASCII codepoints return -1, and in UTF-8 mode combining marks and wide characters return 1 instead of 0 or 2.

---
Full diff: https://github.com/llvm/llvm-project/pull/241728.diff


9 Files Affected:

- (modified) libc/config/linux/aarch64/entrypoints.txt (+1) 
- (modified) libc/config/linux/riscv/entrypoints.txt (+1) 
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1) 
- (modified) libc/include/wchar.yaml (+6) 
- (modified) libc/src/wchar/CMakeLists.txt (+13) 
- (added) libc/src/wchar/wcwidth.cpp (+32) 
- (added) libc/src/wchar/wcwidth.h (+26) 
- (modified) libc/test/src/wchar/CMakeLists.txt (+12) 
- (added) libc/test/src/wchar/wcwidth_test.cpp (+48) 


``````````diff
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index a9941aab496cf..48d1a53895dc7 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -537,6 +537,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wcsncasecmp
     libc.src.wchar.wcslen
     libc.src.wchar.wctob
+    libc.src.wchar.wcwidth
 
     # wctype.h entrypoints
     libc.src.wctype.iswalpha
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 1f52d96704a42..12f744b29b496 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -585,6 +585,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wcslen
     libc.src.wchar.wcsnlen
     libc.src.wchar.wctob
+    libc.src.wchar.wcwidth
     libc.src.wchar.wmemmove
     libc.src.wchar.wmemset
     libc.src.wchar.wcschr
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index f30f73548e326..1c3b3705125ed 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -585,6 +585,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wcslen
     libc.src.wchar.wcsnlen
     libc.src.wchar.wctob
+    libc.src.wchar.wcwidth
     libc.src.wchar.wmemmove
     libc.src.wchar.wmemset
     libc.src.wchar.wcschr
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 2b321f6502ff1..862df3e16fb8c 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -453,3 +453,9 @@ functions:
       - type: size_t
       - type: const wchar_t *__restrict
       - type: '...'
+  - name: wcwidth
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: wchar_t
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 3bbb9d483b049..a8763c57a0f3e 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -767,3 +767,16 @@ add_entrypoint_object(
     libc.src.__support.macros.config
     libc.src.errno.errno
 )
+
+add_entrypoint_object(
+  wcwidth
+  SRCS
+    wcwidth.cpp
+  HDRS
+    wcwidth.h
+  DEPENDS
+    libc.hdr.types.wchar_t
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.wctype_utils
+)
diff --git a/libc/src/wchar/wcwidth.cpp b/libc/src/wchar/wcwidth.cpp
new file mode 100644
index 0000000000000..01597269fb018
--- /dev/null
+++ b/libc/src/wchar/wcwidth.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 wcwidth function implementation.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/wchar/wcwidth.h"
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/wctype_utils.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, wcwidth, (wchar_t wc)) {
+  if (wc == L'\0')
+    return 0;
+  // In LIBC_WCTYPE_MODE_UTF8, wctype_utils does not yet distinguish zero-width
+  // combining marks or double-width characters, so all printable characters
+  // report a column width of 1.
+  return internal::isprint(wc) ? 1 : -1;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcwidth.h b/libc/src/wchar/wcwidth.h
new file mode 100644
index 0000000000000..e1b5a146d4c32
--- /dev/null
+++ b/libc/src/wchar/wcwidth.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 wcwidth function declaration.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_WCWIDTH_H
+#define LLVM_LIBC_SRC_WCHAR_WCWIDTH_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int wcwidth(wchar_t wc);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WCWIDTH_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 52b40eeafa56d..77e801974f4d2 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -753,3 +753,15 @@ add_libc_test(
     libc.src.wchar.swprintf
     libc.test.UnitTest.ErrnoCheckingTest
 )
+
+add_libc_test(
+  wcwidth_test
+  SUITE
+    libc-wchar-tests
+  SRCS
+    wcwidth_test.cpp
+  DEPENDS
+    libc.hdr.types.wchar_t
+    libc.src.__support.wctype_utils
+    libc.src.wchar.wcwidth
+)
diff --git a/libc/test/src/wchar/wcwidth_test.cpp b/libc/test/src/wchar/wcwidth_test.cpp
new file mode 100644
index 0000000000000..9bb8e2272cd4c
--- /dev/null
+++ b/libc/test/src/wchar/wcwidth_test.cpp
@@ -0,0 +1,48 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 wcwidth.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/wctype_utils.h"
+#include "src/wchar/wcwidth.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWcwidthTest, NullCharacter) {
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\0'), 0);
+}
+
+TEST(LlvmLibcWcwidthTest, PrintableAscii) {
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L' '), 1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'a'), 1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'5'), 1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'~'), 1);
+}
+
+TEST(LlvmLibcWcwidthTest, NonPrintableAsciiAndInvalid) {
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\a'), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\t'), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\n'), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\r'), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(static_cast<wchar_t>(0x7f)), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(static_cast<wchar_t>(0x80)), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(static_cast<wchar_t>(-1)), -1);
+}
+
+TEST(LlvmLibcWcwidthTest, NonAsciiCharacters) {
+#if LIBC_CONF_WCTYPE_MODE == LIBC_WCTYPE_MODE_UTF8
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\u00e1'), 1); // 'á'
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\u03b2'), 1); // 'β'
+#else
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\u00e1'), -1);
+  EXPECT_EQ(LIBC_NAMESPACE::wcwidth(L'\u03b2'), -1);
+#endif // LIBC_CONF_WCTYPE_MODE == LIBC_WCTYPE_MODE_UTF8
+}

``````````

</details>


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


More information about the libc-commits mailing list