aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcgyurgyik <gyurgyikcp@gmail.com>2020-08-05 10:42:30 -0400
committercgyurgyik <gyurgyikcp@gmail.com>2020-08-05 10:51:43 -0400
commit1fdab96130fc86f2635d26b617adf10608b8e63b (patch)
treeaa0d4dd1e5e1636f2794e7452b26f740b1f32c9e
parent6a06c7a0a7688ce142865e92d879b8bece79de7a (diff)
downloadllvm-project-1fdab96130fc86f2635d26b617adf10608b8e63b.tar.gz
[libc] Add isspace, isprint, isxdigit implementations.
Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D85270
-rw-r--r--libc/config/linux/aarch64/entrypoints.txt3
-rw-r--r--libc/config/linux/x86_64/entrypoints.txt3
-rw-r--r--libc/spec/stdc.td15
-rw-r--r--libc/src/ctype/CMakeLists.txt26
-rw-r--r--libc/src/ctype/isprint.cpp22
-rw-r--r--libc/src/ctype/isprint.h18
-rw-r--r--libc/src/ctype/isspace.cpp22
-rw-r--r--libc/src/ctype/isspace.h18
-rw-r--r--libc/src/ctype/isxdigit.cpp23
-rw-r--r--libc/src/ctype/isxdigit.h18
-rw-r--r--libc/test/src/ctype/CMakeLists.txt30
-rw-r--r--libc/test/src/ctype/isprint_test.cpp19
-rw-r--r--libc/test/src/ctype/isspace_test.cpp28
-rw-r--r--libc/test/src/ctype/isxdigit_test.cpp20
14 files changed, 265 insertions, 0 deletions
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 565fbf78fcb9..909166c289b1 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -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
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 3cc243e426c2..37a9d71c1dc7 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 1f14f7655359..892fbb2d3076 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -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>]
+ >,
]
>;
diff --git a/libc/src/ctype/CMakeLists.txt b/libc/src/ctype/CMakeLists.txt
index c554e6cb7851..4356eabf23fe 100644
--- a/libc/src/ctype/CMakeLists.txt
+++ b/libc/src/ctype/CMakeLists.txt
@@ -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
index 000000000000..6d0ebbb2b95c
--- /dev/null
+++ b/libc/src/ctype/isprint.cpp
@@ -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
index 000000000000..17ed56ef2534
--- /dev/null
+++ b/libc/src/ctype/isprint.h
@@ -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
index 000000000000..ed6e1610bd63
--- /dev/null
+++ b/libc/src/ctype/isspace.cpp
@@ -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
index 000000000000..d919e9e7d972
--- /dev/null
+++ b/libc/src/ctype/isspace.h
@@ -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
index 000000000000..497cf469a360
--- /dev/null
+++ b/libc/src/ctype/isxdigit.cpp
@@ -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
index 000000000000..a332ecc1018f
--- /dev/null
+++ b/libc/src/ctype/isxdigit.h
@@ -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
diff --git a/libc/test/src/ctype/CMakeLists.txt b/libc/test/src/ctype/CMakeLists.txt
index 3adf5739d72a..0d77134b95eb 100644
--- a/libc/test/src/ctype/CMakeLists.txt
+++ b/libc/test/src/ctype/CMakeLists.txt
@@ -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
index 000000000000..565453e0013c
--- /dev/null
+++ b/libc/test/src/ctype/isprint_test.cpp
@@ -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
index 000000000000..e1caded67452
--- /dev/null
+++ b/libc/test/src/ctype/isspace_test.cpp
@@ -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
index 000000000000..570cb82b3c78
--- /dev/null
+++ b/libc/test/src/ctype/isxdigit_test.cpp
@@ -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);
+ }
+}