[libc] Add isspace, isprint, isxdigit implementations.
authorcgyurgyik <gyurgyikcp@gmail.com>
Wed, 5 Aug 2020 14:42:30 +0000 (10:42 -0400)
committercgyurgyik <gyurgyikcp@gmail.com>
Wed, 5 Aug 2020 14:51:43 +0000 (10:51 -0400)
Reviewed By: sivachandra

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

14 files changed:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/spec/stdc.td
libc/src/ctype/CMakeLists.txt
libc/src/ctype/isprint.cpp [new file with mode: 0644]
libc/src/ctype/isprint.h [new file with mode: 0644]
libc/src/ctype/isspace.cpp [new file with mode: 0644]
libc/src/ctype/isspace.h [new file with mode: 0644]
libc/src/ctype/isxdigit.cpp [new file with mode: 0644]
libc/src/ctype/isxdigit.h [new file with mode: 0644]
libc/test/src/ctype/CMakeLists.txt
libc/test/src/ctype/isprint_test.cpp [new file with mode: 0644]
libc/test/src/ctype/isspace_test.cpp [new file with mode: 0644]
libc/test/src/ctype/isxdigit_test.cpp [new file with mode: 0644]

index 565fbf7..909166c 100644 (file)
@@ -7,8 +7,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.isdigit
     libc.src.ctype.isgraph
     libc.src.ctype.islower
+    libc.src.ctype.isprint
     libc.src.ctype.ispunct
+    libc.src.ctype.isspace
     libc.src.ctype.isupper
+    libc.src.ctype.isxdigit
     
     # errno.h entrypoints
     libc.src.errno.__errno_location
index 3cc243e..37a9d71 100644 (file)
@@ -10,8 +10,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.isdigit
     libc.src.ctype.isgraph
     libc.src.ctype.islower
+    libc.src.ctype.isprint
     libc.src.ctype.ispunct
+    libc.src.ctype.isspace
     libc.src.ctype.isupper
+    libc.src.ctype.isxdigit
 
     # errno.h entrypoints
     libc.src.errno.__errno_location
index 1f14f76..892fbb2 100644 (file)
@@ -82,15 +82,30 @@ def StdC : StandardSpec<"stdc"> {
               [ArgSpec<IntType>]
           >,
           FunctionSpec<
+              "isprint",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
+          FunctionSpec<
               "ispunct",
               RetValSpec<IntType>,
               [ArgSpec<IntType>]
           >,
           FunctionSpec<
+              "isspace",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
+          FunctionSpec<
               "isupper",
               RetValSpec<IntType>,
               [ArgSpec<IntType>]
           >,
+          FunctionSpec<
+              "isxdigit",
+              RetValSpec<IntType>,
+              [ArgSpec<IntType>]
+          >,
       ]
   >;
      
index c554e6c..4356eab 100644 (file)
@@ -69,6 +69,14 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  isprint
+  SRCS
+    isprint.cpp
+  HDRS
+    isprint.h
+)
+
+add_entrypoint_object(
   ispunct
   SRCS
     ispunct.cpp
@@ -79,9 +87,27 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  isspace
+  SRCS
+    isspace.cpp
+  HDRS
+    isspace.h
+)
+
+add_entrypoint_object(
   isupper
   SRCS
     isupper.cpp
   HDRS
     isupper.h
 )
+
+add_entrypoint_object(
+  isxdigit
+  SRCS
+    isxdigit.cpp
+  HDRS
+    isxdigit.h
+  DEPENDS
+    .ctype_utils
+)
diff --git a/libc/src/ctype/isprint.cpp b/libc/src/ctype/isprint.cpp
new file mode 100644 (file)
index 0000000..6d0ebbb
--- /dev/null
@@ -0,0 +1,22 @@
+//===-- Implementation of isprint------------------------------------------===//
+//
+// 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/isprint.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(isprint)(int c) {
+  const unsigned ch = c;
+  return (ch - ' ') < 95;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/ctype/isprint.h b/libc/src/ctype/isprint.h
new file mode 100644 (file)
index 0000000..17ed56e
--- /dev/null
@@ -0,0 +1,18 @@
+//===-- Implementation header for isprint -------------------------*-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_ISPRINT_H
+#define LLVM_LIBC_SRC_CTYPE_ISPRINT_H
+
+namespace __llvm_libc {
+
+int isprint(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_ISPRINT_H
diff --git a/libc/src/ctype/isspace.cpp b/libc/src/ctype/isspace.cpp
new file mode 100644 (file)
index 0000000..ed6e161
--- /dev/null
@@ -0,0 +1,22 @@
+//===-- Implementation of isspace------------------------------------------===//
+//
+// 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/isspace.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(isspace)(int c) {
+  const unsigned ch = c;
+  return ch == ' ' || (ch - '\t') < 5;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/ctype/isspace.h b/libc/src/ctype/isspace.h
new file mode 100644 (file)
index 0000000..d919e9e
--- /dev/null
@@ -0,0 +1,18 @@
+//===-- Implementation header for isspace -------------------------*-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_ISSPACE_H
+#define LLVM_LIBC_SRC_CTYPE_ISSPACE_H
+
+namespace __llvm_libc {
+
+int isspace(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_ISSPACE_H
diff --git a/libc/src/ctype/isxdigit.cpp b/libc/src/ctype/isxdigit.cpp
new file mode 100644 (file)
index 0000000..497cf46
--- /dev/null
@@ -0,0 +1,23 @@
+//===-- Implementation of isxdigit-----------------------------------------===//
+//
+// 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/isxdigit.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(isxdigit)(int c) {
+  const unsigned ch = c;
+  return internal::isdigit(ch) || (ch | 32) - 'a' < 6;
+}
+
+} // namespace __llvm_libc
diff --git a/libc/src/ctype/isxdigit.h b/libc/src/ctype/isxdigit.h
new file mode 100644 (file)
index 0000000..a332ecc
--- /dev/null
@@ -0,0 +1,18 @@
+//===-- Implementation header for isxdigit ------------------------*-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_ISXDIGIT_H
+#define LLVM_LIBC_SRC_CTYPE_ISXDIGIT_H
+
+namespace __llvm_libc {
+
+int isxdigit(int c);
+
+} // namespace __llvm_libc
+
+#endif //  LLVM_LIBC_SRC_CTYPE_ISXDIGIT_H
index 3adf573..0d77134 100644 (file)
@@ -71,6 +71,16 @@ add_libc_unittest(
 )
 
 add_libc_unittest(
+  isprint
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    isprint_test.cpp
+  DEPENDS
+    libc.src.ctype.isprint
+)
+
+add_libc_unittest(
   ispunct
   SUITE
     libc_ctype_unittests
@@ -81,6 +91,16 @@ add_libc_unittest(
 )
 
 add_libc_unittest(
+  isspace
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    isspace_test.cpp
+  DEPENDS
+    libc.src.ctype.isspace
+)
+
+add_libc_unittest(
   isupper
   SUITE
     libc_ctype_unittests
@@ -89,3 +109,13 @@ add_libc_unittest(
   DEPENDS
     libc.src.ctype.isupper
 )
+
+add_libc_unittest(
+  isxdigit
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    isxdigit_test.cpp
+  DEPENDS
+    libc.src.ctype.isxdigit
+)
diff --git a/libc/test/src/ctype/isprint_test.cpp b/libc/test/src/ctype/isprint_test.cpp
new file mode 100644 (file)
index 0000000..565453e
--- /dev/null
@@ -0,0 +1,19 @@
+//===-- Unittests for isprint----------------------------------------------===//
+//
+// 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/isprint.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(IsPrint, DefaultLocale) {
+  for (int ch = 0; ch < 255; ++ch) {
+    if (' ' <= ch && ch <= '~') // A-Z, a-z, 0-9, punctuation, space.
+      EXPECT_NE(__llvm_libc::isprint(ch), 0);
+    else
+      EXPECT_EQ(__llvm_libc::isprint(ch), 0);
+  }
+}
diff --git a/libc/test/src/ctype/isspace_test.cpp b/libc/test/src/ctype/isspace_test.cpp
new file mode 100644 (file)
index 0000000..e1caded
--- /dev/null
@@ -0,0 +1,28 @@
+//===-- Unittests for isspace----------------------------------------------===//
+//
+// 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/isspace.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(IsSpace, DefaultLocale) {
+  // Loops through all characters, verifying that space characters
+  // return true and everything else returns false.
+  // Hexadecimal | Symbol
+  // ---------------------------
+  //    0x09     |   horizontal tab
+  //    0x0a     |   line feed
+  //    0x0b     |   vertical tab
+  //    0x0d     |   carriage return
+  //    0x20     |   space
+  for (int ch = 0; ch < 255; ++ch) {
+    if (ch == 0x20 || (0x09 <= ch && ch <= 0x0d))
+      EXPECT_NE(__llvm_libc::isspace(ch), 0);
+    else
+      EXPECT_EQ(__llvm_libc::isspace(ch), 0);
+  }
+}
diff --git a/libc/test/src/ctype/isxdigit_test.cpp b/libc/test/src/ctype/isxdigit_test.cpp
new file mode 100644 (file)
index 0000000..570cb82
--- /dev/null
@@ -0,0 +1,20 @@
+//===-- Unittests for isxdigit---------------------------------------------===//
+//
+// 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/isxdigit.h"
+#include "utils/UnitTest/Test.h"
+
+TEST(IsXDigit, DefaultLocale) {
+  for (int ch = 0; ch < 255; ++ch) {
+    if (('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f') ||
+        ('A' <= ch && ch <= 'F'))
+      EXPECT_NE(__llvm_libc::isxdigit(ch), 0);
+    else
+      EXPECT_EQ(__llvm_libc::isxdigit(ch), 0);
+  }
+}