[libc-commits] [libc] 04edcc0 - [libc] add isascii and toascii implementations

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Jan 12 15:41:27 PST 2021


Author: Michael Jones
Date: 2021-01-12T23:41:20Z
New Revision: 04edcc02638bc70772baa50a74e582bb8e029872

URL: https://github.com/llvm/llvm-project/commit/04edcc02638bc70772baa50a74e582bb8e029872
DIFF: https://github.com/llvm/llvm-project/commit/04edcc02638bc70772baa50a74e582bb8e029872.diff

LOG: [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

Added: 
    libc/src/ctype/isascii.cpp
    libc/src/ctype/isascii.h
    libc/src/ctype/toascii.cpp
    libc/src/ctype/toascii.h
    libc/test/src/ctype/isascii_test.cpp
    libc/test/src/ctype/toascii_test.cpp

Modified: 
    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/test/src/ctype/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 0db8c4b39caa..2b70cafd6fbf 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -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
     

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a80a8b4f105b..7c5367a9d528 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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
 

diff  --git a/libc/spec/gnu_ext.td b/libc/spec/gnu_ext.td
index d85c562d9256..0b0b8ca38c40 100644
--- a/libc/spec/gnu_ext.td
+++ b/libc/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 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
       ]
   >;
 
+
   let Headers = [
-    Math, String,
+    CType,
+    Math, 
+    String,
   ];
 }

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index 1bf64f082c62..32f6ef5844b1 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -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,

diff  --git a/libc/src/ctype/CMakeLists.txt b/libc/src/ctype/CMakeLists.txt
index da8c4403d959..dd7ee24520f8 100644
--- a/libc/src/ctype/CMakeLists.txt
+++ b/libc/src/ctype/CMakeLists.txt
@@ -24,6 +24,14 @@ add_entrypoint_object(
     .ctype_utils
 )
 
+add_entrypoint_object(
+  isascii
+  SRCS
+    isascii.cpp
+  HDRS
+    isascii.h
+)
+
 add_entrypoint_object(
   isblank
   SRCS
@@ -126,6 +134,14 @@ add_entrypoint_object(
     .ctype_utils
 )
 
+add_entrypoint_object(
+  toascii
+  SRCS
+    toascii.cpp
+  HDRS
+    toascii.h
+)
+
 add_entrypoint_object(
   toupper
   SRCS

diff  --git a/libc/src/ctype/isascii.cpp b/libc/src/ctype/isascii.cpp
new file mode 100644
index 000000000000..c12915ecd6ee
--- /dev/null
+++ b/libc/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/libc/src/ctype/isascii.h b/libc/src/ctype/isascii.h
new file mode 100644
index 000000000000..8ec304489b37
--- /dev/null
+++ b/libc/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/libc/src/ctype/toascii.cpp b/libc/src/ctype/toascii.cpp
new file mode 100644
index 000000000000..19a97ae91791
--- /dev/null
+++ b/libc/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/libc/src/ctype/toascii.h b/libc/src/ctype/toascii.h
new file mode 100644
index 000000000000..c3f48a38d84c
--- /dev/null
+++ b/libc/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/libc/test/src/ctype/CMakeLists.txt b/libc/test/src/ctype/CMakeLists.txt
index 4141708fc2b4..83ae6be20206 100644
--- a/libc/test/src/ctype/CMakeLists.txt
+++ b/libc/test/src/ctype/CMakeLists.txt
@@ -20,6 +20,16 @@ add_libc_unittest(
     libc.src.ctype.isalpha
 )
 
+add_libc_unittest(
+  isascii
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    isascii_test.cpp
+  DEPENDS
+    libc.src.ctype.isascii
+)
+
 add_libc_unittest(
   isblank
   SUITE
@@ -120,6 +130,16 @@ add_libc_unittest(
     libc.src.ctype.isxdigit
 )
 
+add_libc_unittest(
+  toascii
+  SUITE
+    libc_ctype_unittests
+  SRCS
+    toascii_test.cpp
+  DEPENDS
+    libc.src.ctype.toascii
+)
+
 add_libc_unittest(
   tolower
   SUITE

diff  --git a/libc/test/src/ctype/isascii_test.cpp b/libc/test/src/ctype/isascii_test.cpp
new file mode 100644
index 000000000000..b5f66d7aef57
--- /dev/null
+++ b/libc/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/libc/test/src/ctype/toascii_test.cpp b/libc/test/src/ctype/toascii_test.cpp
new file mode 100644
index 000000000000..1a47ae5b45c3
--- /dev/null
+++ b/libc/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);
+  }
+}


        


More information about the libc-commits mailing list