[libc] Implements isdigit and isalnum. Adds a utility header to inline
functions to avoid overhead of function calls.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D84893

GitOrigin-RevId: e263dcc3efeae419b691730da5e37977e1a681f6
diff --git a/config/linux/aarch64/entrypoints.txt b/config/linux/aarch64/entrypoints.txt
index 7e3589b..8d2cc16 100644
--- a/config/linux/aarch64/entrypoints.txt
+++ b/config/linux/aarch64/entrypoints.txt
@@ -1,6 +1,8 @@
 set(TARGET_LIBC_ENTRYPOINTS
     # ctype.h entrypoints
+    libc.src.ctype.isalnum
     libc.src.ctype.isalpha
+    libc.src.ctype.isdigit
     
     # errno.h entrypoints
     libc.src.errno.__errno_location
diff --git a/config/linux/api.td b/config/linux/api.td
index aea3fed..01e6eab 100644
--- a/config/linux/api.td
+++ b/config/linux/api.td
@@ -88,7 +88,9 @@
 
 def CTypeAPI : PublicAPI<"ctype.h"> {
   let Functions = [
+    "isalnum",
     "isalpha",
+    "isdigit",
   ];
 }
 
diff --git a/config/linux/x86_64/entrypoints.txt b/config/linux/x86_64/entrypoints.txt
index c361fce..4484f6c 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -3,7 +3,9 @@
     libc.src.assert.__assert_fail
     
     # ctype.h entrypoints
+    libc.src.ctype.isalnum
     libc.src.ctype.isalpha
+    libc.src.ctype.isdigit
 
     # errno.h entrypoints
     libc.src.errno.__errno_location
diff --git a/spec/stdc.td b/spec/stdc.td
index 1b93960..fed24cb 100644
--- a/spec/stdc.td
+++ b/spec/stdc.td
@@ -47,10 +47,20 @@
       [], // Enumerations
       [
           FunctionSpec<
+              "isalnum",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
+          FunctionSpec<
               "isalpha",
               RetValSpec<IntType>,
               [ArgSpec<IntType>]
           >,
+          FunctionSpec<
+              "isdigit",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
       ]
   >;
      
diff --git a/src/ctype/CMakeLists.txt b/src/ctype/CMakeLists.txt
index 686ec78..53161b8 100644
--- a/src/ctype/CMakeLists.txt
+++ b/src/ctype/CMakeLists.txt
@@ -1,7 +1,35 @@
+add_header_library(
+  ctype_utils
+  HDRS
+    ctype_utils.h
+)
+
+add_entrypoint_object(
+  isalnum
+  SRCS
+    isalnum.cpp
+  HDRS
+    isalnum.h
+  DEPENDS
+    .ctype_utils
+)
+
 add_entrypoint_object(
   isalpha
   SRCS
     isalpha.cpp
   HDRS
     isalpha.h
+  DEPENDS
+    .ctype_utils
+)
+
+add_entrypoint_object(
+  isdigit
+  SRCS
+    isdigit.cpp
+  HDRS
+    isdigit.h
+  DEPENDS
+    .ctype_utils
 )
diff --git a/src/ctype/ctype_utils.h b/src/ctype/ctype_utils.h
new file mode 100644
index 0000000..4e8d396
--- /dev/null
+++ b/src/ctype/ctype_utils.h
@@ -0,0 +1,34 @@
+//===-- Collection of utils for implementing ctype functions-------*-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_CTYPE_CTYPE_UTILS_H
+#define LLVM_LIBC_SRC_CTYPE_CTYPE_UTILS_H
+
+namespace __llvm_libc {
+namespace internal {
+
+// ------------------------------------------------------
+// Rationale: Since these classification functions are
+// called in other functions, we will avoid the overhead
+// of a function call by inlining them.
+// ------------------------------------------------------
+
+static inline int isdigit(int c) {
+  const unsigned ch = c;
+  return (ch - '0') < 10;
+}
+
+static inline int isalpha(int c) {
+  const unsigned ch = c;
+  return (ch | 32) - 'a' < 26;
+}
+
+} // namespace internal
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_CTYPE_UTILS_H
diff --git a/src/ctype/isalnum.cpp b/src/ctype/isalnum.cpp
new file mode 100644
index 0000000..08b6520
--- /dev/null
+++ b/src/ctype/isalnum.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of isalnum------------------------------------------===//
+//
+// 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/ctype/isalnum.h"
+#include "src/ctype/ctype_utils.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+// TODO: Currently restricted to default locale.
+// These should be extended using locale information.
+int LLVM_LIBC_ENTRYPOINT(isalnum)(int c) {
+  return internal::isalpha(c) || internal::isdigit(c);
+}
+
+} // namespace __llvm_libc
diff --git a/src/ctype/isalnum.h b/src/ctype/isalnum.h
new file mode 100644
index 0000000..beb202a
--- /dev/null
+++ b/src/ctype/isalnum.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for isalnum -------------------------*-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_CTYPE_ISALNUM_H
+#define LLVM_LIBC_SRC_CTYPE_ISALNUM_H
+
+namespace __llvm_libc {
+
+int isalnum(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_ISALNUM_H
diff --git a/src/ctype/isalpha.cpp b/src/ctype/isalpha.cpp
index e945ca9..4b37254 100644
--- a/src/ctype/isalpha.cpp
+++ b/src/ctype/isalpha.cpp
@@ -9,14 +9,12 @@
 #include "src/ctype/isalpha.h"
 
 #include "src/__support/common.h"
+#include "src/ctype/ctype_utils.h"
 
 namespace __llvm_libc {
 
 // TODO: Currently restricted to default locale.
 // These should be extended using locale information.
-int LLVM_LIBC_ENTRYPOINT(isalpha)(int c) {
-  const unsigned ch = c;
-  return (ch | 32) - 'a' < 26;
-}
+int LLVM_LIBC_ENTRYPOINT(isalpha)(int c) { return internal::isalpha(c); }
 
 } // namespace __llvm_libc
diff --git a/src/ctype/isdigit.cpp b/src/ctype/isdigit.cpp
new file mode 100644
index 0000000..94ec42a
--- /dev/null
+++ b/src/ctype/isdigit.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of isdigit------------------------------------------===//
+//
+// 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/ctype/isdigit.h"
+#include "src/__support/common.h"
+#include "src/ctype/ctype_utils.h"
+
+namespace __llvm_libc {
+
+// TODO: Currently restricted to default locale.
+// These should be extended using locale information.
+int LLVM_LIBC_ENTRYPOINT(isdigit)(int c) { return internal::isdigit(c); }
+
+} // namespace __llvm_libc
diff --git a/src/ctype/isdigit.h b/src/ctype/isdigit.h
new file mode 100644
index 0000000..32a7623
--- /dev/null
+++ b/src/ctype/isdigit.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for isdigit -------------------------*-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_CTYPE_ISDIGIT_H
+#define LLVM_LIBC_SRC_CTYPE_ISDIGIT_H
+
+namespace __llvm_libc {
+
+int isdigit(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_ISDIGIT_H
diff --git a/test/src/ctype/CMakeLists.txt b/test/src/ctype/CMakeLists.txt
index b7572d3..7834746 100644
--- a/test/src/ctype/CMakeLists.txt
+++ b/test/src/ctype/CMakeLists.txt
@@ -1,6 +1,16 @@
 add_libc_testsuite(libc_ctype_unittests)
 
 add_libc_unittest(
+  isalnum
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    isalnum_test.cpp
+  DEPENDS
+    libc.src.ctype.isalnum
+)
+
+add_libc_unittest(
   isalpha
   SUITE
     libc_ctype_unittests
@@ -9,3 +19,13 @@
   DEPENDS
     libc.src.ctype.isalpha
 )
+
+add_libc_unittest(
+  isdigit
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    isdigit_test.cpp
+  DEPENDS
+    libc.src.ctype.isdigit
+)
diff --git a/test/src/ctype/isalnum_test.cpp b/test/src/ctype/isalnum_test.cpp
new file mode 100644
index 0000000..1c4ad7d
--- /dev/null
+++ b/test/src/ctype/isalnum_test.cpp
@@ -0,0 +1,27 @@
+//===-- Unittests for isalnum----------------------------------------------===//
+//
+// 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/ctype/isalnum.h"
+#include "utils/UnitTest/Test.h"
+
+// Helper function that makes a call to isalnum a bit cleaner
+// for use with testing utilities, since it explicitly requires
+// a boolean value for EXPECT_TRUE and EXPECT_FALSE.
+bool call_isalnum(int c) { return __llvm_libc::isalnum(c); }
+
+TEST(IsAlNum, DefaultLocale) {
+  // Loops through all characters, verifying that numbers and letters
+  // return true and everything else returns false.
+  for (int c = 0; c < 255; ++c) {
+    if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') ||
+        ('0' <= c && c <= '9'))
+      EXPECT_TRUE(call_isalnum(c));
+    else
+      EXPECT_FALSE(call_isalnum(c));
+  }
+}
diff --git a/test/src/ctype/isdigit_test.cpp b/test/src/ctype/isdigit_test.cpp
new file mode 100644
index 0000000..6fea956
--- /dev/null
+++ b/test/src/ctype/isdigit_test.cpp
@@ -0,0 +1,26 @@
+//===-- Unittests for isdigit----------------------------------------------===//
+//
+// 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/ctype/isdigit.h"
+#include "utils/UnitTest/Test.h"
+
+// Helper function that makes a call to isdigit a bit cleaner
+// for use with testing utilities, since it explicitly requires
+// a boolean value for EXPECT_TRUE and EXPECT_FALSE.
+bool call_isdigit(int c) { return __llvm_libc::isdigit(c); }
+
+TEST(IsDigit, DefaultLocale) {
+  // Loops through all characters, verifying that numbers return true
+  // and everything else returns false.
+  for (int ch = 0; ch < 255; ++ch) {
+    if ('0' <= ch && ch <= '9')
+      EXPECT_TRUE(call_isdigit(ch));
+    else
+      EXPECT_FALSE(call_isdigit(ch));
+  }
+}