[libc] add isascii and toascii implementations
authorMichael Jones <michaelrj@google.com>
Tue, 12 Jan 2021 22:37:56 +0000 (22:37 +0000)
committerMichael Jones <michaelrj@google.com>
Tue, 12 Jan 2021 23:41:20 +0000 (23:41 +0000)
adding both at once since these are trivial functions.

Reviewed By: sivachandra

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

12 files changed:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/spec/gnu_ext.td
libc/spec/posix.td
libc/src/ctype/CMakeLists.txt
libc/src/ctype/isascii.cpp [new file with mode: 0644]
libc/src/ctype/isascii.h [new file with mode: 0644]
libc/src/ctype/toascii.cpp [new file with mode: 0644]
libc/src/ctype/toascii.h [new file with mode: 0644]
libc/test/src/ctype/CMakeLists.txt
libc/test/src/ctype/isascii_test.cpp [new file with mode: 0644]
libc/test/src/ctype/toascii_test.cpp [new file with mode: 0644]

index 0db8c4b..2b70caf 100644 (file)
@@ -2,6 +2,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     # 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 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.isspace
     libc.src.ctype.isupper
     libc.src.ctype.isxdigit
+    libc.src.ctype.toascii
     libc.src.ctype.tolower
     libc.src.ctype.toupper
     
index a80a8b4..7c5367a 100644 (file)
@@ -5,6 +5,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     # 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 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.isspace
     libc.src.ctype.isupper
     libc.src.ctype.isxdigit
+    libc.src.ctype.toascii
     libc.src.ctype.tolower
     libc.src.ctype.toupper
 
index d85c562..0b0b8ca 100644 (file)
@@ -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 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
       ]
   >;
 
+
   let Headers = [
-    Math, String,
+    CType,
+    Math, 
+    String,
   ];
 }
index 1bf64f0..32f6ef5 100644 (file)
@@ -235,7 +235,22 @@ def POSIX : StandardSpec<"POSIX"> {
     ]
   >;
 
+  HeaderSpec CType = HeaderSpec<
+    "ctype.h",
+    [], // Macros
+    [], // Types
+    [], // Enumerations
+    [
+        FunctionSpec<
+            "isascii",
+            RetValSpec<IntType>,
+            [ArgSpec<IntType>]
+        >,
+    ]
+  >;
+
   let Headers = [
+    CType,
     Errno,
     SysMMan,
     Signal,
index da8c440..dd7ee24 100644 (file)
@@ -25,6 +25,14 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  isascii
+  SRCS
+    isascii.cpp
+  HDRS
+    isascii.h
+)
+
+add_entrypoint_object(
   isblank
   SRCS
     isblank.cpp
@@ -127,6 +135,14 @@ add_entrypoint_object(
 )
 
 add_entrypoint_object(
+  toascii
+  SRCS
+    toascii.cpp
+  HDRS
+    toascii.h
+)
+
+add_entrypoint_object(
   toupper
   SRCS
     toupper.cpp
diff --git a/libc/src/ctype/isascii.cpp b/libc/src/ctype/isascii.cpp
new file mode 100644 (file)
index 0000000..c12915e
--- /dev/null
@@ -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/libc/src/ctype/isascii.h b/libc/src/ctype/isascii.h
new file mode 100644 (file)
index 0000000..8ec3044
--- /dev/null
@@ -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/libc/src/ctype/toascii.cpp b/libc/src/ctype/toascii.cpp
new file mode 100644 (file)
index 0000000..19a97ae
--- /dev/null
@@ -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/libc/src/ctype/toascii.h b/libc/src/ctype/toascii.h
new file mode 100644 (file)
index 0000000..c3f48a3
--- /dev/null
@@ -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
index 4141708..83ae6be 100644 (file)
@@ -21,6 +21,16 @@ add_libc_unittest(
 )
 
 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(
 )
 
 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/libc/test/src/ctype/isascii_test.cpp b/libc/test/src/ctype/isascii_test.cpp
new file mode 100644 (file)
index 0000000..b5f66d7
--- /dev/null
@@ -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/libc/test/src/ctype/toascii_test.cpp b/libc/test/src/ctype/toascii_test.cpp
new file mode 100644 (file)
index 0000000..1a47ae5
--- /dev/null
@@ -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);
+  }
+}