[libc-commits] [libc] [libc] fix iswalpha signiture and test (PR #152343)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Aug 6 10:24:26 PDT 2025


https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/152343

>From d5c1dfecdd22fe0fe516474785848aacffe68b12 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 6 Aug 2025 10:12:44 -0700
Subject: [PATCH] [libc] fix iswalpha signiture and test

The iswalpha function is only working for characters in the ascii range
right now, which isn't ideal. Also it was returning a bool when the
function is supposed to return an int.
---
 libc/src/wctype/iswalpha.cpp           |  2 +-
 libc/src/wctype/iswalpha.h             |  2 +-
 libc/test/src/wctype/iswalpha_test.cpp | 78 +++++++++++++-------------
 3 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/libc/src/wctype/iswalpha.cpp b/libc/src/wctype/iswalpha.cpp
index e18f29370fbd0..09f55d391dbff 100644
--- a/libc/src/wctype/iswalpha.cpp
+++ b/libc/src/wctype/iswalpha.cpp
@@ -14,6 +14,6 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-LLVM_LIBC_FUNCTION(bool, iswalpha, (wint_t c)) { return internal::iswalpha(c); }
+LLVM_LIBC_FUNCTION(int, iswalpha, (wint_t c)) { return internal::iswalpha(c); }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wctype/iswalpha.h b/libc/src/wctype/iswalpha.h
index 681fc6ba79a54..0353388607b60 100644
--- a/libc/src/wctype/iswalpha.h
+++ b/libc/src/wctype/iswalpha.h
@@ -14,7 +14,7 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-bool iswalpha(wint_t c);
+int iswalpha(wint_t c);
 
 } // namespace LIBC_NAMESPACE_DECL
 
diff --git a/libc/test/src/wctype/iswalpha_test.cpp b/libc/test/src/wctype/iswalpha_test.cpp
index f3f75f4dc7aa5..a82c0057c1807 100644
--- a/libc/test/src/wctype/iswalpha_test.cpp
+++ b/libc/test/src/wctype/iswalpha_test.cpp
@@ -9,46 +9,48 @@
 #include "src/__support/CPP/span.h"
 #include "src/wctype/iswalpha.h"
 
-#include "test/UnitTest/LibcTest.h"
 #include "test/UnitTest/Test.h"
 
-namespace {
-
-// TODO: Merge the wctype tests using this framework.
-constexpr char WALPHA_ARRAY[] = {
-    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
-    'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
-    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
-    'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-};
-
-bool in_span(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) {
-  for (size_t i = 0; i < arr.size(); ++i)
-    if (static_cast<int>(arr[i]) == ch)
-      return true;
-  return false;
-}
-
-} // namespace
-
 TEST(LlvmLibciswalpha, SimpleTest) {
-  EXPECT_TRUE(LIBC_NAMESPACE::iswalpha('a'));
-  EXPECT_TRUE(LIBC_NAMESPACE::iswalpha('B'));
-
-  EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('3'));
-  EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(' '));
-  EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('?'));
-  EXPECT_FALSE(LIBC_NAMESPACE::iswalpha('\0'));
-  EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(-1));
+  EXPECT_NE(LIBC_NAMESPACE::iswalpha('a'), 0);
+  EXPECT_NE(LIBC_NAMESPACE::iswalpha('B'), 0);
+
+  EXPECT_EQ(LIBC_NAMESPACE::iswalpha('3'), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::iswalpha(' '), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::iswalpha('?'), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::iswalpha('\0'), 0);
+  EXPECT_EQ(LIBC_NAMESPACE::iswalpha(-1), 0);
 }
 
-TEST(LlvmLibciswalpha, DefaultLocale) {
-  // Loops through all characters, verifying that letters return
-  // true and everything else returns false.
-  for (int ch = -255; ch < 255; ++ch) {
-    if (in_span(ch, WALPHA_ARRAY))
-      EXPECT_TRUE(LIBC_NAMESPACE::iswalpha(ch));
-    else
-      EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(ch));
-  }
-}
+// TODO: once iswalpha supports more than just ascii-range characters add a
+// proper test.
+
+// namespace {
+
+// // TODO: Merge the wctype tests using this framework.
+// constexpr char WALPHA_ARRAY[] = {
+//     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+//     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+//     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+//     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+// };
+
+// bool in_span(int ch, LIBC_NAMESPACE::cpp::span<const char> arr) {
+//   for (size_t i = 0; i < arr.size(); ++i)
+//     if (static_cast<int>(arr[i]) == ch)
+//       return true;
+//   return false;
+// }
+
+// } // namespace
+
+// TEST(LlvmLibciswalpha, DefaultLocale) {
+//   // Loops through all characters, verifying that letters return
+//   // true and everything else returns false.
+//   for (int ch = -255; ch < 255; ++ch) {
+//     if (in_span(ch, WALPHA_ARRAY))
+//       EXPECT_TRUE(LIBC_NAMESPACE::iswalpha(ch));
+//     else
+//       EXPECT_FALSE(LIBC_NAMESPACE::iswalpha(ch));
+//   }
+// }



More information about the libc-commits mailing list