[libc] add isascii and toascii implementations
adding both at once since these are trivial functions.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D94558
GitOrigin-RevId: 04edcc02638bc70772baa50a74e582bb8e029872
diff --git a/config/linux/aarch64/entrypoints.txt b/config/linux/aarch64/entrypoints.txt
index 0db8c4b..2b70caf 100644
--- a/config/linux/aarch64/entrypoints.txt
+++ b/config/linux/aarch64/entrypoints.txt
@@ -2,6 +2,7 @@
# ctype.h entrypoints
libc.src.ctype.isalnum
libc.src.ctype.isalpha
+ libc.src.ctype.isascii
libc.src.ctype.isblank
libc.src.ctype.iscntrl
libc.src.ctype.isdigit
@@ -12,6 +13,7 @@
libc.src.ctype.isspace
libc.src.ctype.isupper
libc.src.ctype.isxdigit
+ libc.src.ctype.toascii
libc.src.ctype.tolower
libc.src.ctype.toupper
diff --git a/config/linux/x86_64/entrypoints.txt b/config/linux/x86_64/entrypoints.txt
index a80a8b4..7c5367a 100644
--- a/config/linux/x86_64/entrypoints.txt
+++ b/config/linux/x86_64/entrypoints.txt
@@ -5,6 +5,7 @@
# ctype.h entrypoints
libc.src.ctype.isalnum
libc.src.ctype.isalpha
+ libc.src.ctype.isascii
libc.src.ctype.isblank
libc.src.ctype.iscntrl
libc.src.ctype.isdigit
@@ -15,6 +16,7 @@
libc.src.ctype.isspace
libc.src.ctype.isupper
libc.src.ctype.isxdigit
+ libc.src.ctype.toascii
libc.src.ctype.tolower
libc.src.ctype.toupper
diff --git a/spec/gnu_ext.td b/spec/gnu_ext.td
index d85c562..0b0b8ca 100644
--- a/spec/gnu_ext.td
+++ b/spec/gnu_ext.td
@@ -1,4 +1,18 @@
def GnuExtensions : StandardSpec<"GNUExtensions"> {
+ HeaderSpec CType = HeaderSpec<
+ "ctype.h",
+ [], // Macros
+ [], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "toascii",
+ RetValSpec<IntType>,
+ [ArgSpec<IntType>]
+ >,
+ ]
+ >;
+
HeaderSpec Math = HeaderSpec<
"math.h",
[], // Macros
@@ -27,7 +41,10 @@
]
>;
+
let Headers = [
- Math, String,
+ CType,
+ Math,
+ String,
];
}
diff --git a/spec/posix.td b/spec/posix.td
index 1bf64f0..32f6ef5 100644
--- a/spec/posix.td
+++ b/spec/posix.td
@@ -235,7 +235,22 @@
]
>;
+ HeaderSpec CType = HeaderSpec<
+ "ctype.h",
+ [], // Macros
+ [], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "isascii",
+ RetValSpec<IntType>,
+ [ArgSpec<IntType>]
+ >,
+ ]
+ >;
+
let Headers = [
+ CType,
Errno,
SysMMan,
Signal,
diff --git a/src/ctype/CMakeLists.txt b/src/ctype/CMakeLists.txt
index da8c440..dd7ee24 100644
--- a/src/ctype/CMakeLists.txt
+++ b/src/ctype/CMakeLists.txt
@@ -25,6 +25,14 @@
)
add_entrypoint_object(
+ isascii
+ SRCS
+ isascii.cpp
+ HDRS
+ isascii.h
+)
+
+add_entrypoint_object(
isblank
SRCS
isblank.cpp
@@ -127,6 +135,14 @@
)
add_entrypoint_object(
+ toascii
+ SRCS
+ toascii.cpp
+ HDRS
+ toascii.h
+)
+
+add_entrypoint_object(
toupper
SRCS
toupper.cpp
diff --git a/src/ctype/isascii.cpp b/src/ctype/isascii.cpp
new file mode 100644
index 0000000..c12915e
--- /dev/null
+++ b/src/ctype/isascii.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of isascii------------------------------------------===//
+//
+// 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/isascii.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, isascii, (int c)) { return (c & (~0x7f)) == 0; }
+
+} // namespace __llvm_libc
diff --git a/src/ctype/isascii.h b/src/ctype/isascii.h
new file mode 100644
index 0000000..8ec3044
--- /dev/null
+++ b/src/ctype/isascii.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for isascii -------------------------*-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_ISASCII_H
+#define LLVM_LIBC_SRC_CTYPE_ISASCII_H
+
+namespace __llvm_libc {
+
+int isascii(int c);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_CTYPE_ISASCII_H
diff --git a/src/ctype/toascii.cpp b/src/ctype/toascii.cpp
new file mode 100644
index 0000000..19a97ae
--- /dev/null
+++ b/src/ctype/toascii.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of toascii------------------------------------------===//
+//
+// 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/toascii.h"
+
+#include "src/__support/common.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, toascii, (int c)) { return (c & 0x7f); }
+
+} // namespace __llvm_libc
diff --git a/src/ctype/toascii.h b/src/ctype/toascii.h
new file mode 100644
index 0000000..c3f48a3
--- /dev/null
+++ b/src/ctype/toascii.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for toascii -------------------------*-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_TOASCII_H
+#define LLVM_LIBC_SRC_CTYPE_TOASCII_H
+
+namespace __llvm_libc {
+
+int toascii(int c);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_CTYPE_TOASCII_H
diff --git a/test/src/ctype/CMakeLists.txt b/test/src/ctype/CMakeLists.txt
index 4141708..83ae6be 100644
--- a/test/src/ctype/CMakeLists.txt
+++ b/test/src/ctype/CMakeLists.txt
@@ -21,6 +21,16 @@
)
add_libc_unittest(
+ isascii
+ SUITE
+ libc_ctype_unittests
+ SRCS
+ isascii_test.cpp
+ DEPENDS
+ libc.src.ctype.isascii
+)
+
+add_libc_unittest(
isblank
SUITE
libc_ctype_unittests
@@ -121,6 +131,16 @@
)
add_libc_unittest(
+ toascii
+ SUITE
+ libc_ctype_unittests
+ SRCS
+ toascii_test.cpp
+ DEPENDS
+ libc.src.ctype.toascii
+)
+
+add_libc_unittest(
tolower
SUITE
libc_ctype_unittests
diff --git a/test/src/ctype/isascii_test.cpp b/test/src/ctype/isascii_test.cpp
new file mode 100644
index 0000000..b5f66d7
--- /dev/null
+++ b/test/src/ctype/isascii_test.cpp
@@ -0,0 +1,23 @@
+//===-- Unittests for isascii----------------------------------------------===//
+//
+// 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/isascii.h"
+
+#include "utils/UnitTest/Test.h"
+
+TEST(IsAscii, DefaultLocale) {
+ // Loops through all characters, verifying that ascii characters
+ // (which are all 7 bit unsigned integers)
+ // return a non-zero integer and everything else returns zero.
+ for (int ch = 0; ch < 255; ++ch) {
+ if (ch <= 0x7f)
+ EXPECT_NE(__llvm_libc::isascii(ch), 0);
+ else
+ EXPECT_EQ(__llvm_libc::isascii(ch), 0);
+ }
+}
diff --git a/test/src/ctype/toascii_test.cpp b/test/src/ctype/toascii_test.cpp
new file mode 100644
index 0000000..1a47ae5
--- /dev/null
+++ b/test/src/ctype/toascii_test.cpp
@@ -0,0 +1,24 @@
+//===-- Unittests for toascii----------------------------------------------===//
+//
+// 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/toascii.h"
+
+#include "utils/UnitTest/Test.h"
+
+TEST(ToAscii, DefaultLocale) {
+ // Loops through all characters, verifying that ascii characters
+ // (which are all 7 bit unsigned integers)
+ // return themself, and that all other characters return themself
+ // mod 128 (which is equivalent to & 0x7f)
+ for (int ch = 0; ch < 255; ++ch) {
+ if (ch <= 0x7f)
+ EXPECT_EQ(__llvm_libc::toascii(ch), ch);
+ else
+ EXPECT_EQ(__llvm_libc::toascii(ch), ch & 0x7f);
+ }
+}