Skip to content

Commit 04edcc0

Browse files
[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
1 parent 314e29e commit 04edcc0

File tree

12 files changed

+190
-1
lines changed

12 files changed

+190
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(TARGET_LIBC_ENTRYPOINTS
22
# ctype.h entrypoints
33
libc.src.ctype.isalnum
44
libc.src.ctype.isalpha
5+
libc.src.ctype.isascii
56
libc.src.ctype.isblank
67
libc.src.ctype.iscntrl
78
libc.src.ctype.isdigit
@@ -12,6 +13,7 @@ set(TARGET_LIBC_ENTRYPOINTS
1213
libc.src.ctype.isspace
1314
libc.src.ctype.isupper
1415
libc.src.ctype.isxdigit
16+
libc.src.ctype.toascii
1517
libc.src.ctype.tolower
1618
libc.src.ctype.toupper
1719

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(TARGET_LIBC_ENTRYPOINTS
55
# ctype.h entrypoints
66
libc.src.ctype.isalnum
77
libc.src.ctype.isalpha
8+
libc.src.ctype.isascii
89
libc.src.ctype.isblank
910
libc.src.ctype.iscntrl
1011
libc.src.ctype.isdigit
@@ -15,6 +16,7 @@ set(TARGET_LIBC_ENTRYPOINTS
1516
libc.src.ctype.isspace
1617
libc.src.ctype.isupper
1718
libc.src.ctype.isxdigit
19+
libc.src.ctype.toascii
1820
libc.src.ctype.tolower
1921
libc.src.ctype.toupper
2022

libc/spec/gnu_ext.td

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
def GnuExtensions : StandardSpec<"GNUExtensions"> {
2+
HeaderSpec CType = HeaderSpec<
3+
"ctype.h",
4+
[], // Macros
5+
[], // Types
6+
[], // Enumerations
7+
[
8+
FunctionSpec<
9+
"toascii",
10+
RetValSpec<IntType>,
11+
[ArgSpec<IntType>]
12+
>,
13+
]
14+
>;
15+
216
HeaderSpec Math = HeaderSpec<
317
"math.h",
418
[], // Macros
@@ -27,7 +41,10 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
2741
]
2842
>;
2943

44+
3045
let Headers = [
31-
Math, String,
46+
CType,
47+
Math,
48+
String,
3249
];
3350
}

libc/spec/posix.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,22 @@ def POSIX : StandardSpec<"POSIX"> {
235235
]
236236
>;
237237

238+
HeaderSpec CType = HeaderSpec<
239+
"ctype.h",
240+
[], // Macros
241+
[], // Types
242+
[], // Enumerations
243+
[
244+
FunctionSpec<
245+
"isascii",
246+
RetValSpec<IntType>,
247+
[ArgSpec<IntType>]
248+
>,
249+
]
250+
>;
251+
238252
let Headers = [
253+
CType,
239254
Errno,
240255
SysMMan,
241256
Signal,

libc/src/ctype/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ add_entrypoint_object(
2424
.ctype_utils
2525
)
2626

27+
add_entrypoint_object(
28+
isascii
29+
SRCS
30+
isascii.cpp
31+
HDRS
32+
isascii.h
33+
)
34+
2735
add_entrypoint_object(
2836
isblank
2937
SRCS
@@ -126,6 +134,14 @@ add_entrypoint_object(
126134
.ctype_utils
127135
)
128136

137+
add_entrypoint_object(
138+
toascii
139+
SRCS
140+
toascii.cpp
141+
HDRS
142+
toascii.h
143+
)
144+
129145
add_entrypoint_object(
130146
toupper
131147
SRCS

libc/src/ctype/isascii.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of isascii------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/ctype/isascii.h"
10+
11+
#include "src/__support/common.h"
12+
13+
namespace __llvm_libc {
14+
15+
LLVM_LIBC_FUNCTION(int, isascii, (int c)) { return (c & (~0x7f)) == 0; }
16+
17+
} // namespace __llvm_libc

libc/src/ctype/isascii.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for isascii -------------------------*-C++-*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_CTYPE_ISASCII_H
10+
#define LLVM_LIBC_SRC_CTYPE_ISASCII_H
11+
12+
namespace __llvm_libc {
13+
14+
int isascii(int c);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_CTYPE_ISASCII_H

libc/src/ctype/toascii.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- Implementation of toascii------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/ctype/toascii.h"
10+
11+
#include "src/__support/common.h"
12+
13+
namespace __llvm_libc {
14+
15+
LLVM_LIBC_FUNCTION(int, toascii, (int c)) { return (c & 0x7f); }
16+
17+
} // namespace __llvm_libc

libc/src/ctype/toascii.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for toascii -------------------------*-C++-*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_CTYPE_TOASCII_H
10+
#define LLVM_LIBC_SRC_CTYPE_TOASCII_H
11+
12+
namespace __llvm_libc {
13+
14+
int toascii(int c);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_CTYPE_TOASCII_H

libc/test/src/ctype/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ add_libc_unittest(
2020
libc.src.ctype.isalpha
2121
)
2222

23+
add_libc_unittest(
24+
isascii
25+
SUITE
26+
libc_ctype_unittests
27+
SRCS
28+
isascii_test.cpp
29+
DEPENDS
30+
libc.src.ctype.isascii
31+
)
32+
2333
add_libc_unittest(
2434
isblank
2535
SUITE
@@ -120,6 +130,16 @@ add_libc_unittest(
120130
libc.src.ctype.isxdigit
121131
)
122132

133+
add_libc_unittest(
134+
toascii
135+
SUITE
136+
libc_ctype_unittests
137+
SRCS
138+
toascii_test.cpp
139+
DEPENDS
140+
libc.src.ctype.toascii
141+
)
142+
123143
add_libc_unittest(
124144
tolower
125145
SUITE

libc/test/src/ctype/isascii_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Unittests for isascii----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/ctype/isascii.h"
10+
11+
#include "utils/UnitTest/Test.h"
12+
13+
TEST(IsAscii, DefaultLocale) {
14+
// Loops through all characters, verifying that ascii characters
15+
// (which are all 7 bit unsigned integers)
16+
// return a non-zero integer and everything else returns zero.
17+
for (int ch = 0; ch < 255; ++ch) {
18+
if (ch <= 0x7f)
19+
EXPECT_NE(__llvm_libc::isascii(ch), 0);
20+
else
21+
EXPECT_EQ(__llvm_libc::isascii(ch), 0);
22+
}
23+
}

libc/test/src/ctype/toascii_test.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Unittests for toascii----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/ctype/toascii.h"
10+
11+
#include "utils/UnitTest/Test.h"
12+
13+
TEST(ToAscii, DefaultLocale) {
14+
// Loops through all characters, verifying that ascii characters
15+
// (which are all 7 bit unsigned integers)
16+
// return themself, and that all other characters return themself
17+
// mod 128 (which is equivalent to & 0x7f)
18+
for (int ch = 0; ch < 255; ++ch) {
19+
if (ch <= 0x7f)
20+
EXPECT_EQ(__llvm_libc::toascii(ch), ch);
21+
else
22+
EXPECT_EQ(__llvm_libc::toascii(ch), ch & 0x7f);
23+
}
24+
}

0 commit comments

Comments
 (0)