[libc-commits] [libc] [llvm] [libc] Remove the dependency of integer_to_string on wctype_utils (PR #236576)

Mike Lothian via libc-commits libc-commits at lists.llvm.org
Sat Nov 28 03:02:04 PST 2026


https://github.com/FireBurn updated https://github.com/llvm/llvm-project/pull/236576

>From 805a66788352af1ba97cfa63079ae8404dad27ab Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Wed, 28 Oct 2026 21:43:25 +0000
Subject: [PATCH 1/2] [libc] Remove the dependency of integer_to_string on
 wctype_utils

With LIBC_CONF_WCTYPE_MODE=LIBC_WCTYPE_MODE_UTF8 and the tests enabled, CMake fails at configure time with "Maximum recursion depth of 1000 exceeded" in get_object_files_for_test. wctype_utils pulls in the UTF8 tables, which reach libc_assert and CPP::string, and both of those depend on integer_to_string, which depended on wctype_utils for the wchar_t overloads of int_to_b36_char and toupper. The cycle is invisible to the compiler because of the include guards, but get_object_files_for_test follows the target dependencies without end

Digits are always ASCII, so form the digit as a char and widen it, which needs only ctype_utils, and drop the include and the dependency. The output is the same as before for both char and wchar_t

With the change the tests configure and build in UTF8 mode, and check-libc passes (2302 of 2302) with LIBC_CONF_WCTYPE_MODE=LIBC_WCTYPE_MODE_UTF8, the tests and the experimental entrypoints enabled. That configuration did not configure before. I built with LIBC_WNO_ERROR=ON, because perfect_hash_map.h has -Wshadow errors in that mode which are a separate problem and which I will send separately. The existing wchar_t cases in integer_to_string_test cover the changed function

The Bazel overlay listed the dependency on `__support_wctype_utils` for `__support_integer_to_string`, so I removed that line too. `snprintf`, which uses it, builds with Bazel

AI disclosure: this patch was written with Claude Code. I reviewed the change and ran the build and tests as described above

Assisted-by: Claude
---
 libc/src/__support/CMakeLists.txt             |  1 -
 libc/src/__support/integer_to_string.h        | 20 ++++++++++++++-----
 .../libc/src/__support/BUILD.bazel            |  1 -
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 9b1089fa48c8c3..b087d263284ef3 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -262,7 +262,6 @@ add_header_library(
     .big_int
     .common
     .ctype_utils
-    .wctype_utils
     libc.hdr.stdint_proxy
     libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index 4472294bb6e08a..be175c862c6ee5 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -70,7 +70,6 @@
 #include "src/__support/common.h"
 #include "src/__support/ctype_utils.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/wctype_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -363,11 +362,22 @@ class IntegerToString {
     using UNSIGNED_T = make_integral_or_big_int_unsigned_t<T>;
 
     LIBC_INLINE static CharT digit_char(uint8_t digit) {
-      const CharT result = internal::int_to_b36_char(digit);
-      if constexpr (Fmt::BASE <= 10) {
-        return result;
+      if constexpr (cpp::is_same_v<CharT, char>) {
+        // char may be EBCDIC, whose letters are not contiguous, so the table
+        // in ctype_utils is used.
+        const char result = internal::int_to_b36_char(digit);
+        if constexpr (Fmt::BASE <= 10) {
+          return result;
+        } else {
+          return Fmt::IS_UPPERCASE ? internal::toupper(result) : result;
+        }
       } else {
-        return Fmt::IS_UPPERCASE ? internal::toupper(result) : result;
+        // wchar_t is Unicode whatever char is, and its digits and letters are
+        // contiguous, so they are formed from wide literals.
+        if (digit < 10)
+          return static_cast<CharT>(L'0' + digit);
+        return static_cast<CharT>((Fmt::IS_UPPERCASE ? L'A' : L'a') + digit -
+                                  10);
       }
     }
 
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
index 491b4b67113277..d7a76ba7bdc103 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
@@ -242,7 +242,6 @@ libc_support_library(
         ":big_int",
         ":common",
         ":ctype_utils",
-        ":wctype_utils",
         "//libc/hdr:stdint_proxy",
         "//libc/src/__support/CPP:algorithm",
         "//libc/src/__support/CPP:array",

>From 8725b845fab0c80063a96806eea757da459ec264 Mon Sep 17 00:00:00 2001
From: Mike Lothian <mike at fireburn.co.uk>
Date: Sat, 28 Nov 2026 04:43:21 +0000
Subject: [PATCH 2/2] [libc] Move the switch/case wctype functions to
 wctype_basics.h

integer_to_string can then use them without pulling in the Unicode tables, which keeps character generation in __support

Assisted-by: Claude
---
 libc/src/__support/CMakeLists.txt             |  12 +
 libc/src/__support/integer_to_string.h        |  15 +-
 libc/src/__support/wctype_basics.h            | 540 ++++++++++++++++++
 libc/src/__support/wctype_utils.h             | 513 +----------------
 libc/test/src/__support/wctype_utils_test.cpp |   2 +
 .../libc/src/__support/BUILD.bazel            |  12 +
 6 files changed, 574 insertions(+), 520 deletions(-)
 create mode 100644 libc/src/__support/wctype_basics.h

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index b087d263284ef3..1fdc19891b0e36 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -211,11 +211,22 @@ if("${LIBC_CONF_WCTYPE_MODE}" STREQUAL "LIBC_WCTYPE_MODE_UTF8")
   )
 endif()
 
+add_header_library(
+  wctype_basics
+  HDRS
+    wctype_basics.h
+  DEPENDS
+    libc.hdr.types.wchar_t
+    libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   wctype_utils
   HDRS
     wctype_utils.h
   DEPENDS
+    .wctype_basics
     ${wctype_utils_deps}
 )
 
@@ -262,6 +273,7 @@ add_header_library(
     .big_int
     .common
     .ctype_utils
+    .wctype_basics
     libc.hdr.stdint_proxy
     libc.src.__support.CPP.algorithm
     libc.src.__support.CPP.limits
diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h
index be175c862c6ee5..8a7d6e0b62b072 100644
--- a/libc/src/__support/integer_to_string.h
+++ b/libc/src/__support/integer_to_string.h
@@ -70,6 +70,7 @@
 #include "src/__support/common.h"
 #include "src/__support/ctype_utils.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/wctype_basics.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
@@ -363,8 +364,6 @@ class IntegerToString {
 
     LIBC_INLINE static CharT digit_char(uint8_t digit) {
       if constexpr (cpp::is_same_v<CharT, char>) {
-        // char may be EBCDIC, whose letters are not contiguous, so the table
-        // in ctype_utils is used.
         const char result = internal::int_to_b36_char(digit);
         if constexpr (Fmt::BASE <= 10) {
           return result;
@@ -372,12 +371,12 @@ class IntegerToString {
           return Fmt::IS_UPPERCASE ? internal::toupper(result) : result;
         }
       } else {
-        // wchar_t is Unicode whatever char is, and its digits and letters are
-        // contiguous, so they are formed from wide literals.
-        if (digit < 10)
-          return static_cast<CharT>(L'0' + digit);
-        return static_cast<CharT>((Fmt::IS_UPPERCASE ? L'A' : L'a') + digit -
-                                  10);
+        const wchar_t result = internal::int_to_b36_wchar(digit);
+        if constexpr (Fmt::BASE <= 10) {
+          return result;
+        } else {
+          return Fmt::IS_UPPERCASE ? internal::ascii::toupper(result) : result;
+        }
       }
     }
 
diff --git a/libc/src/__support/wctype_basics.h b/libc/src/__support/wctype_basics.h
new file mode 100644
index 00000000000000..a3632007058625
--- /dev/null
+++ b/libc/src/__support/wctype_basics.h
@@ -0,0 +1,540 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Wide character functions written as switch/case statements, which need none
+/// of the Unicode tables that wctype_utils.h pulls in.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_WCTYPE_BASICS_H
+#define LLVM_LIBC_SRC___SUPPORT_WCTYPE_BASICS_H
+
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+// -----------------------------------------------------------------------------
+// ******************                 WARNING                 ******************
+// ****************** DO NOT TRY TO OPTIMIZE THESE FUNCTIONS! ******************
+// -----------------------------------------------------------------------------
+// This switch/case form is easier for the compiler to understand, and is
+// optimized into a form that is almost always the same as or better than
+// versions written by hand (see https://godbolt.org/z/qvrebqvvr). Also this
+// form makes these functions encoding independent. If you want to rewrite these
+// functions, make sure you have benchmarks to show your new solution is faster,
+// as well as a way to support non-ASCII character encodings.
+
+// 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 islower(wchar_t ch) {
+//    switch(ch) {
+//    case L'a'...L'z':
+//      return true;
+//    }
+//  }
+// This assumes the character ranges are contiguous, which they aren't in
+// EBCDIC. Technically we could use some smaller ranges, but that's even harder
+// to read.
+namespace ascii {
+LIBC_INLINE constexpr bool islower(wchar_t wch) {
+  switch (wch) {
+  case L'a':
+  case L'b':
+  case L'c':
+  case L'd':
+  case L'e':
+  case L'f':
+  case L'g':
+  case L'h':
+  case L'i':
+  case L'j':
+  case L'k':
+  case L'l':
+  case L'm':
+  case L'n':
+  case L'o':
+  case L'p':
+  case L'q':
+  case L'r':
+  case L's':
+  case L't':
+  case L'u':
+  case L'v':
+  case L'w':
+  case L'x':
+  case L'y':
+  case L'z':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isupper(wchar_t wch) {
+  switch (wch) {
+  case L'A':
+  case L'B':
+  case L'C':
+  case L'D':
+  case L'E':
+  case L'F':
+  case L'G':
+  case L'H':
+  case L'I':
+  case L'J':
+  case L'K':
+  case L'L':
+  case L'M':
+  case L'N':
+  case L'O':
+  case L'P':
+  case L'Q':
+  case L'R':
+  case L'S':
+  case L'T':
+  case L'U':
+  case L'V':
+  case L'W':
+  case L'X':
+  case L'Y':
+  case L'Z':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isdigit(wchar_t wch) {
+  switch (wch) {
+  case L'0':
+  case L'1':
+  case L'2':
+  case L'3':
+  case L'4':
+  case L'5':
+  case L'6':
+  case L'7':
+  case L'8':
+  case L'9':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isalpha(wchar_t wch) {
+  switch (wch) {
+  case L'a':
+  case L'b':
+  case L'c':
+  case L'd':
+  case L'e':
+  case L'f':
+  case L'g':
+  case L'h':
+  case L'i':
+  case L'j':
+  case L'k':
+  case L'l':
+  case L'm':
+  case L'n':
+  case L'o':
+  case L'p':
+  case L'q':
+  case L'r':
+  case L's':
+  case L't':
+  case L'u':
+  case L'v':
+  case L'w':
+  case L'x':
+  case L'y':
+  case L'z':
+  case L'A':
+  case L'B':
+  case L'C':
+  case L'D':
+  case L'E':
+  case L'F':
+  case L'G':
+  case L'H':
+  case L'I':
+  case L'J':
+  case L'K':
+  case L'L':
+  case L'M':
+  case L'N':
+  case L'O':
+  case L'P':
+  case L'Q':
+  case L'R':
+  case L'S':
+  case L'T':
+  case L'U':
+  case L'V':
+  case L'W':
+  case L'X':
+  case L'Y':
+  case L'Z':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isalnum(wchar_t wch) {
+  switch (wch) {
+  case L'a':
+  case L'b':
+  case L'c':
+  case L'd':
+  case L'e':
+  case L'f':
+  case L'g':
+  case L'h':
+  case L'i':
+  case L'j':
+  case L'k':
+  case L'l':
+  case L'm':
+  case L'n':
+  case L'o':
+  case L'p':
+  case L'q':
+  case L'r':
+  case L's':
+  case L't':
+  case L'u':
+  case L'v':
+  case L'w':
+  case L'x':
+  case L'y':
+  case L'z':
+  case L'A':
+  case L'B':
+  case L'C':
+  case L'D':
+  case L'E':
+  case L'F':
+  case L'G':
+  case L'H':
+  case L'I':
+  case L'J':
+  case L'K':
+  case L'L':
+  case L'M':
+  case L'N':
+  case L'O':
+  case L'P':
+  case L'Q':
+  case L'R':
+  case L'S':
+  case L'T':
+  case L'U':
+  case L'V':
+  case L'W':
+  case L'X':
+  case L'Y':
+  case L'Z':
+  case L'0':
+  case L'1':
+  case L'2':
+  case L'3':
+  case L'4':
+  case L'5':
+  case L'6':
+  case L'7':
+  case L'8':
+  case L'9':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isspace(wchar_t wch) {
+  switch (wch) {
+  case L' ':
+  case L'\t':
+  case L'\n':
+  case L'\v':
+  case L'\f':
+  case L'\r':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isblank(wchar_t wch) {
+  switch (wch) {
+  case L' ':
+  case L'\t':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool isgraph(wchar_t wch) {
+  return 0x20 < wch && wch < 0x7f;
+}
+
+LIBC_INLINE constexpr bool isprint(wchar_t wch) {
+  return (static_cast<unsigned>(wch) - ' ') < 95;
+}
+
+LIBC_INLINE constexpr bool isxdigit(wchar_t wch) {
+  switch (wch) {
+  case L'a':
+  case L'b':
+  case L'c':
+  case L'd':
+  case L'e':
+  case L'f':
+  case L'A':
+  case L'B':
+  case L'C':
+  case L'D':
+  case L'E':
+  case L'F':
+  case L'0':
+  case L'1':
+  case L'2':
+  case L'3':
+  case L'4':
+  case L'5':
+  case L'6':
+  case L'7':
+  case L'8':
+  case L'9':
+    return true;
+  default:
+    return false;
+  }
+}
+
+LIBC_INLINE constexpr bool iscntrl(wchar_t wch) {
+  return (wch < 0x20 || wch == 0x7f);
+}
+
+LIBC_INLINE constexpr bool ispunct(wchar_t wch) {
+  return !isalnum(wch) && isgraph(wch);
+}
+
+LIBC_INLINE constexpr wchar_t tolower(wchar_t wch) {
+  switch (wch) {
+  case L'A':
+    return L'a';
+  case L'B':
+    return L'b';
+  case L'C':
+    return L'c';
+  case L'D':
+    return L'd';
+  case L'E':
+    return L'e';
+  case L'F':
+    return L'f';
+  case L'G':
+    return L'g';
+  case L'H':
+    return L'h';
+  case L'I':
+    return L'i';
+  case L'J':
+    return L'j';
+  case L'K':
+    return L'k';
+  case L'L':
+    return L'l';
+  case L'M':
+    return L'm';
+  case L'N':
+    return L'n';
+  case L'O':
+    return L'o';
+  case L'P':
+    return L'p';
+  case L'Q':
+    return L'q';
+  case L'R':
+    return L'r';
+  case L'S':
+    return L's';
+  case L'T':
+    return L't';
+  case L'U':
+    return L'u';
+  case L'V':
+    return L'v';
+  case L'W':
+    return L'w';
+  case L'X':
+    return L'x';
+  case L'Y':
+    return L'y';
+  case L'Z':
+    return L'z';
+  default:
+    return wch;
+  }
+}
+
+LIBC_INLINE constexpr wchar_t toupper(wchar_t wch) {
+  switch (wch) {
+  case L'a':
+    return L'A';
+  case L'b':
+    return L'B';
+  case L'c':
+    return L'C';
+  case L'd':
+    return L'D';
+  case L'e':
+    return L'E';
+  case L'f':
+    return L'F';
+  case L'g':
+    return L'G';
+  case L'h':
+    return L'H';
+  case L'i':
+    return L'I';
+  case L'j':
+    return L'J';
+  case L'k':
+    return L'K';
+  case L'l':
+    return L'L';
+  case L'm':
+    return L'M';
+  case L'n':
+    return L'N';
+  case L'o':
+    return L'O';
+  case L'p':
+    return L'P';
+  case L'q':
+    return L'Q';
+  case L'r':
+    return L'R';
+  case L's':
+    return L'S';
+  case L't':
+    return L'T';
+  case L'u':
+    return L'U';
+  case L'v':
+    return L'V';
+  case L'w':
+    return L'W';
+  case L'x':
+    return L'X';
+  case L'y':
+    return L'Y';
+  case L'z':
+    return L'Z';
+  default:
+    return wch;
+  }
+}
+
+} // namespace ascii
+
+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.
+
+  // LIBC_ASSERT(num < 36);
+  switch (num) {
+  case 0:
+    return L'0';
+  case 1:
+    return L'1';
+  case 2:
+    return L'2';
+  case 3:
+    return L'3';
+  case 4:
+    return L'4';
+  case 5:
+    return L'5';
+  case 6:
+    return L'6';
+  case 7:
+    return L'7';
+  case 8:
+    return L'8';
+  case 9:
+    return L'9';
+  case 10:
+    return L'a';
+  case 11:
+    return L'b';
+  case 12:
+    return L'c';
+  case 13:
+    return L'd';
+  case 14:
+    return L'e';
+  case 15:
+    return L'f';
+  case 16:
+    return L'g';
+  case 17:
+    return L'h';
+  case 18:
+    return L'i';
+  case 19:
+    return L'j';
+  case 20:
+    return L'k';
+  case 21:
+    return L'l';
+  case 22:
+    return L'm';
+  case 23:
+    return L'n';
+  case 24:
+    return L'o';
+  case 25:
+    return L'p';
+  case 26:
+    return L'q';
+  case 27:
+    return L'r';
+  case 28:
+    return L's';
+  case 29:
+    return L't';
+  case 30:
+    return L'u';
+  case 31:
+    return L'v';
+  case 32:
+    return L'w';
+  case 33:
+    return L'x';
+  case 34:
+    return L'y';
+  case 35:
+    return L'z';
+  default:
+    return L'!';
+  }
+}
+
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_WCTYPE_BASICS_H
diff --git a/libc/src/__support/wctype_utils.h b/libc/src/__support/wctype_utils.h
index 633c9139f7369a..a0a2935cfc52d8 100644
--- a/libc/src/__support/wctype_utils.h
+++ b/libc/src/__support/wctype_utils.h
@@ -13,6 +13,7 @@
 #include "src/__support/CPP/bit.h"
 #include "src/__support/macros/attributes.h" // LIBC_INLINE
 #include "src/__support/macros/config.h"
+#include "src/__support/wctype_basics.h"
 
 #define LIBC_WCTYPE_MODE_ASCII 0
 #define LIBC_WCTYPE_MODE_UTF8 1
@@ -29,435 +30,6 @@
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
 
-// -----------------------------------------------------------------------------
-// ******************                 WARNING                 ******************
-// ****************** DO NOT TRY TO OPTIMIZE THESE FUNCTIONS! ******************
-// -----------------------------------------------------------------------------
-// This switch/case form is easier for the compiler to understand, and is
-// optimized into a form that is almost always the same as or better than
-// versions written by hand (see https://godbolt.org/z/qvrebqvvr). Also this
-// form makes these functions encoding independent. If you want to rewrite these
-// functions, make sure you have benchmarks to show your new solution is faster,
-// as well as a way to support non-ASCII character encodings.
-
-// 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 islower(wchar_t ch) {
-//    switch(ch) {
-//    case L'a'...L'z':
-//      return true;
-//    }
-//  }
-// This assumes the character ranges are contiguous, which they aren't in
-// EBCDIC. Technically we could use some smaller ranges, but that's even harder
-// to read.
-namespace ascii {
-LIBC_INLINE constexpr bool islower(wchar_t wch) {
-  switch (wch) {
-  case L'a':
-  case L'b':
-  case L'c':
-  case L'd':
-  case L'e':
-  case L'f':
-  case L'g':
-  case L'h':
-  case L'i':
-  case L'j':
-  case L'k':
-  case L'l':
-  case L'm':
-  case L'n':
-  case L'o':
-  case L'p':
-  case L'q':
-  case L'r':
-  case L's':
-  case L't':
-  case L'u':
-  case L'v':
-  case L'w':
-  case L'x':
-  case L'y':
-  case L'z':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isupper(wchar_t wch) {
-  switch (wch) {
-  case L'A':
-  case L'B':
-  case L'C':
-  case L'D':
-  case L'E':
-  case L'F':
-  case L'G':
-  case L'H':
-  case L'I':
-  case L'J':
-  case L'K':
-  case L'L':
-  case L'M':
-  case L'N':
-  case L'O':
-  case L'P':
-  case L'Q':
-  case L'R':
-  case L'S':
-  case L'T':
-  case L'U':
-  case L'V':
-  case L'W':
-  case L'X':
-  case L'Y':
-  case L'Z':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isdigit(wchar_t wch) {
-  switch (wch) {
-  case L'0':
-  case L'1':
-  case L'2':
-  case L'3':
-  case L'4':
-  case L'5':
-  case L'6':
-  case L'7':
-  case L'8':
-  case L'9':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isalpha(wchar_t wch) {
-  switch (wch) {
-  case L'a':
-  case L'b':
-  case L'c':
-  case L'd':
-  case L'e':
-  case L'f':
-  case L'g':
-  case L'h':
-  case L'i':
-  case L'j':
-  case L'k':
-  case L'l':
-  case L'm':
-  case L'n':
-  case L'o':
-  case L'p':
-  case L'q':
-  case L'r':
-  case L's':
-  case L't':
-  case L'u':
-  case L'v':
-  case L'w':
-  case L'x':
-  case L'y':
-  case L'z':
-  case L'A':
-  case L'B':
-  case L'C':
-  case L'D':
-  case L'E':
-  case L'F':
-  case L'G':
-  case L'H':
-  case L'I':
-  case L'J':
-  case L'K':
-  case L'L':
-  case L'M':
-  case L'N':
-  case L'O':
-  case L'P':
-  case L'Q':
-  case L'R':
-  case L'S':
-  case L'T':
-  case L'U':
-  case L'V':
-  case L'W':
-  case L'X':
-  case L'Y':
-  case L'Z':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isalnum(wchar_t wch) {
-  switch (wch) {
-  case L'a':
-  case L'b':
-  case L'c':
-  case L'd':
-  case L'e':
-  case L'f':
-  case L'g':
-  case L'h':
-  case L'i':
-  case L'j':
-  case L'k':
-  case L'l':
-  case L'm':
-  case L'n':
-  case L'o':
-  case L'p':
-  case L'q':
-  case L'r':
-  case L's':
-  case L't':
-  case L'u':
-  case L'v':
-  case L'w':
-  case L'x':
-  case L'y':
-  case L'z':
-  case L'A':
-  case L'B':
-  case L'C':
-  case L'D':
-  case L'E':
-  case L'F':
-  case L'G':
-  case L'H':
-  case L'I':
-  case L'J':
-  case L'K':
-  case L'L':
-  case L'M':
-  case L'N':
-  case L'O':
-  case L'P':
-  case L'Q':
-  case L'R':
-  case L'S':
-  case L'T':
-  case L'U':
-  case L'V':
-  case L'W':
-  case L'X':
-  case L'Y':
-  case L'Z':
-  case L'0':
-  case L'1':
-  case L'2':
-  case L'3':
-  case L'4':
-  case L'5':
-  case L'6':
-  case L'7':
-  case L'8':
-  case L'9':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isspace(wchar_t wch) {
-  switch (wch) {
-  case L' ':
-  case L'\t':
-  case L'\n':
-  case L'\v':
-  case L'\f':
-  case L'\r':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isblank(wchar_t wch) {
-  switch (wch) {
-  case L' ':
-  case L'\t':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool isgraph(wchar_t wch) {
-  return 0x20 < wch && wch < 0x7f;
-}
-
-LIBC_INLINE constexpr bool isprint(wchar_t wch) {
-  return (static_cast<unsigned>(wch) - ' ') < 95;
-}
-
-LIBC_INLINE constexpr bool isxdigit(wchar_t wch) {
-  switch (wch) {
-  case L'a':
-  case L'b':
-  case L'c':
-  case L'd':
-  case L'e':
-  case L'f':
-  case L'A':
-  case L'B':
-  case L'C':
-  case L'D':
-  case L'E':
-  case L'F':
-  case L'0':
-  case L'1':
-  case L'2':
-  case L'3':
-  case L'4':
-  case L'5':
-  case L'6':
-  case L'7':
-  case L'8':
-  case L'9':
-    return true;
-  default:
-    return false;
-  }
-}
-
-LIBC_INLINE constexpr bool iscntrl(wchar_t wch) {
-  return (wch < 0x20 || wch == 0x7f);
-}
-
-LIBC_INLINE constexpr bool ispunct(wchar_t wch) {
-  return !isalnum(wch) && isgraph(wch);
-}
-
-LIBC_INLINE constexpr wchar_t tolower(wchar_t wch) {
-  switch (wch) {
-  case L'A':
-    return L'a';
-  case L'B':
-    return L'b';
-  case L'C':
-    return L'c';
-  case L'D':
-    return L'd';
-  case L'E':
-    return L'e';
-  case L'F':
-    return L'f';
-  case L'G':
-    return L'g';
-  case L'H':
-    return L'h';
-  case L'I':
-    return L'i';
-  case L'J':
-    return L'j';
-  case L'K':
-    return L'k';
-  case L'L':
-    return L'l';
-  case L'M':
-    return L'm';
-  case L'N':
-    return L'n';
-  case L'O':
-    return L'o';
-  case L'P':
-    return L'p';
-  case L'Q':
-    return L'q';
-  case L'R':
-    return L'r';
-  case L'S':
-    return L's';
-  case L'T':
-    return L't';
-  case L'U':
-    return L'u';
-  case L'V':
-    return L'v';
-  case L'W':
-    return L'w';
-  case L'X':
-    return L'x';
-  case L'Y':
-    return L'y';
-  case L'Z':
-    return L'z';
-  default:
-    return wch;
-  }
-}
-
-LIBC_INLINE constexpr wchar_t toupper(wchar_t wch) {
-  switch (wch) {
-  case L'a':
-    return L'A';
-  case L'b':
-    return L'B';
-  case L'c':
-    return L'C';
-  case L'd':
-    return L'D';
-  case L'e':
-    return L'E';
-  case L'f':
-    return L'F';
-  case L'g':
-    return L'G';
-  case L'h':
-    return L'H';
-  case L'i':
-    return L'I';
-  case L'j':
-    return L'J';
-  case L'k':
-    return L'K';
-  case L'l':
-    return L'L';
-  case L'm':
-    return L'M';
-  case L'n':
-    return L'N';
-  case L'o':
-    return L'O';
-  case L'p':
-    return L'P';
-  case L'q':
-    return L'Q';
-  case L'r':
-    return L'R';
-  case L's':
-    return L'S';
-  case L't':
-    return L'T';
-  case L'u':
-    return L'U';
-  case L'v':
-    return L'V';
-  case L'w':
-    return L'W';
-  case L'x':
-    return L'X';
-  case L'y':
-    return L'Y';
-  case L'z':
-    return L'Z';
-  default:
-    return wch;
-  }
-}
-
-} // namespace ascii
-
 LIBC_INLINE constexpr bool islower(wchar_t wch) {
 #if LIBC_CONF_WCTYPE_MODE != LIBC_WCTYPE_MODE_UTF8
   return ascii::islower(wch);
@@ -707,89 +279,6 @@ LIBC_INLINE constexpr int b36_char_to_int(wchar_t wch) {
   }
 }
 
-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.
-
-  // LIBC_ASSERT(num < 36);
-  switch (num) {
-  case 0:
-    return L'0';
-  case 1:
-    return L'1';
-  case 2:
-    return L'2';
-  case 3:
-    return L'3';
-  case 4:
-    return L'4';
-  case 5:
-    return L'5';
-  case 6:
-    return L'6';
-  case 7:
-    return L'7';
-  case 8:
-    return L'8';
-  case 9:
-    return L'9';
-  case 10:
-    return L'a';
-  case 11:
-    return L'b';
-  case 12:
-    return L'c';
-  case 13:
-    return L'd';
-  case 14:
-    return L'e';
-  case 15:
-    return L'f';
-  case 16:
-    return L'g';
-  case 17:
-    return L'h';
-  case 18:
-    return L'i';
-  case 19:
-    return L'j';
-  case 20:
-    return L'k';
-  case 21:
-    return L'l';
-  case 22:
-    return L'm';
-  case 23:
-    return L'n';
-  case 24:
-    return L'o';
-  case 25:
-    return L'p';
-  case 26:
-    return L'q';
-  case 27:
-    return L'r';
-  case 28:
-    return L's';
-  case 29:
-    return L't';
-  case 30:
-    return L'u';
-  case 31:
-    return L'v';
-  case 32:
-    return L'w';
-  case 33:
-    return L'x';
-  case 34:
-    return L'y';
-  case 35:
-    return L'z';
-  default:
-    return L'!';
-  }
-}
-
 // An overload which provides a way to compare input with specific character
 // values, when input can be of a regular or a wide character type.
 LIBC_INLINE static constexpr bool
diff --git a/libc/test/src/__support/wctype_utils_test.cpp b/libc/test/src/__support/wctype_utils_test.cpp
index ae4770a7765a22..499f4f30b461f8 100644
--- a/libc/test/src/__support/wctype_utils_test.cpp
+++ b/libc/test/src/__support/wctype_utils_test.cpp
@@ -17,6 +17,7 @@ namespace ascii_mode {
 #undef LIBC_CONF_WCTYPE_MODE
 #define LIBC_CONF_WCTYPE_MODE LIBC_WCTYPE_MODE_ASCII
 
+#undef LLVM_LIBC_SRC___SUPPORT_WCTYPE_BASICS_H
 #undef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
 
 namespace LIBC_NAMESPACE_DECL {
@@ -40,6 +41,7 @@ using ::LIBC_NAMESPACE::wctype_internal::toupper;
 namespace cpp = ::LIBC_NAMESPACE::cpp;
 } // namespace LIBC_NAMESPACE_DECL
 
+#undef LLVM_LIBC_SRC___SUPPORT_WCTYPE_BASICS_H
 #undef LLVM_LIBC_SRC___SUPPORT_WCTYPE_UTILS_H
 
 #include "src/__support/wctype_utils.h"
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
index d7a76ba7bdc103..5a4395c4c9c508 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
@@ -242,6 +242,7 @@ libc_support_library(
         ":big_int",
         ":common",
         ":ctype_utils",
+        ":wctype_basics",
         "//libc/hdr:stdint_proxy",
         "//libc/src/__support/CPP:algorithm",
         "//libc/src/__support/CPP:array",
@@ -393,10 +394,21 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "wctype_basics",
+    hdrs = ["wctype_basics.h"],
+    deps = [
+        "//libc/hdr/types:wchar_t",
+        "//libc/src/__support/macros:attributes",
+        "//libc/src/__support/macros:config",
+    ],
+)
+
 libc_support_library(
     name = "wctype_utils",
     hdrs = ["wctype_utils.h"],
     deps = [
+        ":wctype_basics",
         "//libc/hdr/types:wchar_t",
         "//libc/hdr/types:wctype_t",
         "//libc/src/__support/CPP:array",



More information about the libc-commits mailing list