[libc-commits] [libc] e7f7973 - [libc] Migrate wctype_utils to use wchar_t where applicable. (#166234)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 5 11:03:30 PST 2025
Author: Alexey Samsonov
Date: 2025-11-05T11:03:25-08:00
New Revision: e7f7973899f76773ae6e9a6b1e8c7e9f9cc5cb56
URL: https://github.com/llvm/llvm-project/commit/e7f7973899f76773ae6e9a6b1e8c7e9f9cc5cb56
DIFF: https://github.com/llvm/llvm-project/commit/e7f7973899f76773ae6e9a6b1e8c7e9f9cc5cb56.diff
LOG: [libc] Migrate wctype_utils to use wchar_t where applicable. (#166234)
This is a counterpart of
https://github.com/llvm/llvm-project/pull/166225 but for wctype_utils
(which are not yet widely used). For now, I'm just changing the types
from wint_t to wchar_t to match the regular ctype_utils change. The next
change may rename most of the functions to match the name of ctype_utils
variants, so that we could be calling them from the templated code
operating on "const char*" and "const wchar_t*" strings, and the right
function signature would be picked up.
Added:
Modified:
libc/src/__support/CMakeLists.txt
libc/src/__support/wctype_utils.h
libc/src/wctype/iswalpha.cpp
libc/test/src/wchar/WcstolTest.h
utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Removed:
################################################################################
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index b7af751ec3f27..96874702b1fdf 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -161,6 +161,7 @@ add_header_library(
HDRS
wctype_utils.h
DEPENDS
+ libc.hdr.types.wchar_t
libc.hdr.types.wint_t
)
diff --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 2ae5ec93b2a63..60b6afb928475 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -9,6 +9,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
+#include "hdr/types/wchar_t.h"
#include "hdr/types/wint_t.h"
#include "src/__support/CPP/optional.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
@@ -30,7 +31,7 @@ namespace internal {
// Similarly, do not change these fumarks to show your new solution is faster,
// as well as a way to support non-Anctions to use case ranges. e.g.
-// bool iswlower(wint_t ch) {
+// bool iswlower(wchar_t ch) {
// switch(ch) {
// case L'a'...L'z':
// return true;
@@ -40,7 +41,7 @@ namespace internal {
// EBCDIC. Technically we could use some smaller ranges, but that's even harder
// to read.
-LIBC_INLINE static constexpr bool iswlower(wint_t wch) {
+LIBC_INLINE static constexpr bool iswlower(wchar_t wch) {
switch (wch) {
case L'a':
case L'b':
@@ -74,7 +75,7 @@ LIBC_INLINE static constexpr bool iswlower(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswupper(wint_t wch) {
+LIBC_INLINE static constexpr bool iswupper(wchar_t wch) {
switch (wch) {
case L'A':
case L'B':
@@ -108,7 +109,7 @@ LIBC_INLINE static constexpr bool iswupper(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswdigit(wint_t wch) {
+LIBC_INLINE static constexpr bool iswdigit(wchar_t wch) {
switch (wch) {
case L'0':
case L'1':
@@ -126,7 +127,7 @@ LIBC_INLINE static constexpr bool iswdigit(wint_t wch) {
}
}
-LIBC_INLINE static constexpr wint_t towlower(wint_t wch) {
+LIBC_INLINE static constexpr wchar_t towlower(wchar_t wch) {
switch (wch) {
case L'A':
return L'a';
@@ -185,7 +186,7 @@ LIBC_INLINE static constexpr wint_t towlower(wint_t wch) {
}
}
-LIBC_INLINE static constexpr wint_t towupper(wint_t wch) {
+LIBC_INLINE static constexpr wchar_t towupper(wchar_t wch) {
switch (wch) {
case L'a':
return L'A';
@@ -244,7 +245,7 @@ LIBC_INLINE static constexpr wint_t towupper(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswalpha(wint_t wch) {
+LIBC_INLINE static constexpr bool iswalpha(wchar_t wch) {
switch (wch) {
case L'a':
case L'b':
@@ -304,7 +305,7 @@ LIBC_INLINE static constexpr bool iswalpha(wint_t wch) {
}
}
-LIBC_INLINE static constexpr bool iswalnum(wint_t wch) {
+LIBC_INLINE static constexpr bool iswalnum(wchar_t wch) {
switch (wch) {
case L'a':
case L'b':
@@ -374,7 +375,7 @@ LIBC_INLINE static constexpr bool iswalnum(wint_t wch) {
}
}
-LIBC_INLINE static constexpr int b36_wchar_to_int(wint_t wch) {
+LIBC_INLINE static constexpr int b36_wchar_to_int(wchar_t wch) {
switch (wch) {
case L'0':
return 0;
@@ -479,7 +480,7 @@ LIBC_INLINE static constexpr int b36_wchar_to_int(wint_t wch) {
}
}
-LIBC_INLINE static constexpr wint_t int_to_b36_wchar(int num) {
+LIBC_INLINE static constexpr wchar_t int_to_b36_wchar(int num) {
// Can't actually use LIBC_ASSERT here because it depends on integer_to_string
// which depends on this.
@@ -562,7 +563,7 @@ LIBC_INLINE static constexpr wint_t int_to_b36_wchar(int num) {
}
}
-LIBC_INLINE static constexpr bool iswspace(wint_t wch) {
+LIBC_INLINE static constexpr bool iswspace(wchar_t wch) {
switch (wch) {
case L' ':
case L'\t':
diff --git a/libc/src/wctype/iswalpha.cpp b/libc/src/wctype/iswalpha.cpp
index 09f55d391dbff..e151363b88d0b 100644
--- a/libc/src/wctype/iswalpha.cpp
+++ b/libc/src/wctype/iswalpha.cpp
@@ -14,6 +14,8 @@
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) { return internal::iswalpha(c); }
+LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) {
+ return internal::iswalpha(static_cast<wchar_t>(c));
+}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/wchar/WcstolTest.h b/libc/test/src/wchar/WcstolTest.h
index 4d5b752e62238..cadf9e0c42b90 100644
--- a/libc/test/src/wchar/WcstolTest.h
+++ b/libc/test/src/wchar/WcstolTest.h
@@ -178,8 +178,8 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
wchar_t small_string[4] = {L'\0', L'\0', L'\0', L'\0'};
for (int base = 2; base <= 36; ++base) {
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
- small_string[0] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
+ small_string[0] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
if (first_digit < base) {
ASSERT_EQ(func(small_string, nullptr, base),
static_cast<ReturnT>(first_digit));
@@ -193,11 +193,11 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
for (int base = 2; base <= 36; ++base) {
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
- small_string[0] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
+ small_string[0] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
for (int second_digit = 0; second_digit <= 36; ++second_digit) {
- small_string[1] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit));
+ small_string[1] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit);
if (first_digit < base && second_digit < base) {
ASSERT_EQ(
func(small_string, nullptr, base),
@@ -217,14 +217,14 @@ struct WcstoTest : public LIBC_NAMESPACE::testing::ErrnoCheckingTest {
for (int base = 2; base <= 36; ++base) {
for (int first_digit = 0; first_digit <= 36; ++first_digit) {
- small_string[0] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit));
+ small_string[0] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(first_digit);
for (int second_digit = 0; second_digit <= 36; ++second_digit) {
- small_string[1] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit));
+ small_string[1] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(second_digit);
for (int third_digit = 0; third_digit <= limit; ++third_digit) {
- small_string[2] = static_cast<wchar_t>(
- LIBC_NAMESPACE::internal::int_to_b36_wchar(third_digit));
+ small_string[2] =
+ LIBC_NAMESPACE::internal::int_to_b36_wchar(third_digit);
if (first_digit < base && second_digit < base &&
third_digit < base) {
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 9c14a7b991487..b65fe64acdea0 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1805,6 +1805,7 @@ libc_support_library(
":__support_cpp_optional",
":__support_macros_attributes",
":__support_macros_config",
+ ":types_wchar_t",
":types_wint_t",
],
)
More information about the libc-commits
mailing list