Skip to content

Commit 502d6b4

Browse files
cgyurgyikmemfrob
authored andcommitted
[libc] Add tolower, toupper implementation.
Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D85326
1 parent 60ff83c commit 502d6b4

File tree

15 files changed

+194
-10
lines changed

15 files changed

+194
-10
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ set(TARGET_LIBC_ENTRYPOINTS
1212
libc.src.ctype.isspace
1313
libc.src.ctype.isupper
1414
libc.src.ctype.isxdigit
15+
libc.src.ctype.tolower
16+
libc.src.ctype.toupper
1517

1618
# errno.h entrypoints
1719
libc.src.errno.__errno_location

libc/config/linux/api.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def CTypeAPI : PublicAPI<"ctype.h"> {
100100
"isspace",
101101
"isupper",
102102
"isxdigit",
103+
"tolower",
104+
"toupper",
103105
];
104106
}
105107

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ set(TARGET_LIBC_ENTRYPOINTS
1515
libc.src.ctype.isspace
1616
libc.src.ctype.isupper
1717
libc.src.ctype.isxdigit
18+
libc.src.ctype.tolower
19+
libc.src.ctype.toupper
1820

1921
# errno.h entrypoints
2022
libc.src.errno.__errno_location

libc/spec/stdc.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ def StdC : StandardSpec<"stdc"> {
106106
RetValSpec<IntType>,
107107
[ArgSpec<IntType>]
108108
>,
109+
FunctionSpec<
110+
"tolower",
111+
RetValSpec<IntType>,
112+
[ArgSpec<IntType>]
113+
>,
114+
FunctionSpec<
115+
"toupper",
116+
RetValSpec<IntType>,
117+
[ArgSpec<IntType>]
118+
>,
109119
]
110120
>;
111121

libc/src/ctype/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ add_entrypoint_object(
6666
islower.cpp
6767
HDRS
6868
islower.h
69+
DEPENDS
70+
.ctype_utils
6971
)
7072

7173
add_entrypoint_object(
@@ -100,6 +102,8 @@ add_entrypoint_object(
100102
isupper.cpp
101103
HDRS
102104
isupper.h
105+
DEPENDS
106+
.ctype_utils
103107
)
104108

105109
add_entrypoint_object(
@@ -111,3 +115,23 @@ add_entrypoint_object(
111115
DEPENDS
112116
.ctype_utils
113117
)
118+
119+
add_entrypoint_object(
120+
tolower
121+
SRCS
122+
tolower.cpp
123+
HDRS
124+
tolower.h
125+
DEPENDS
126+
.ctype_utils
127+
)
128+
129+
add_entrypoint_object(
130+
toupper
131+
SRCS
132+
toupper.cpp
133+
HDRS
134+
toupper.h
135+
DEPENDS
136+
.ctype_utils
137+
)

libc/src/ctype/ctype_utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@ namespace internal {
1818
// of a function call by inlining them.
1919
// ------------------------------------------------------
2020

21-
static inline int isdigit(unsigned ch) { return (ch - '0') < 10; }
22-
2321
static inline int isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; }
2422

23+
static inline int isdigit(unsigned ch) { return (ch - '0') < 10; }
24+
2525
static inline int isalnum(unsigned ch) { return isalpha(ch) || isdigit(ch); }
2626

2727
static inline int isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; }
2828

29+
static inline int islower(unsigned ch) { return (ch - 'a') < 26; }
30+
31+
static inline int isupper(unsigned ch) { return (ch - 'A') < 26; }
32+
2933
} // namespace internal
3034
} // namespace __llvm_libc
3135

libc/src/ctype/islower.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/ctype/islower.h"
10+
#include "src/ctype/ctype_utils.h"
1011

1112
#include "src/__support/common.h"
1213

1314
namespace __llvm_libc {
1415

1516
// TODO: Currently restricted to default locale.
1617
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(islower)(int c) {
18-
const unsigned ch = c;
19-
return (ch - 'a') < 26;
20-
}
18+
int LLVM_LIBC_ENTRYPOINT(islower)(int c) { return internal::islower(c); }
2119

2220
} // namespace __llvm_libc

libc/src/ctype/isupper.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/ctype/isupper.h"
10+
#include "src/ctype/ctype_utils.h"
1011

1112
#include "src/__support/common.h"
1213

1314
namespace __llvm_libc {
1415

1516
// TODO: Currently restricted to default locale.
1617
// These should be extended using locale information.
17-
int LLVM_LIBC_ENTRYPOINT(isupper)(int c) {
18-
const unsigned ch = c;
19-
return (ch - 'A') < 26;
20-
}
18+
int LLVM_LIBC_ENTRYPOINT(isupper)(int c) { return internal::isupper(c); }
2119

2220
} // namespace __llvm_libc

libc/src/ctype/tolower.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation of tolower------------------------------------------===//
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/tolower.h"
10+
#include "src/ctype/ctype_utils.h"
11+
12+
#include "src/__support/common.h"
13+
14+
namespace __llvm_libc {
15+
16+
// TODO: Currently restricted to default locale.
17+
// These should be extended using locale information.
18+
int LLVM_LIBC_ENTRYPOINT(tolower)(int c) {
19+
if (internal::isupper(c))
20+
return c + 'a' - 'A';
21+
return c;
22+
}
23+
24+
} // namespace __llvm_libc

libc/src/ctype/tolower.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for tolower -------------------------*-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_TOLOWER_H
10+
#define LLVM_LIBC_SRC_CTYPE_TOLOWER_H
11+
12+
namespace __llvm_libc {
13+
14+
int tolower(int c);
15+
16+
} // namespace __llvm_libc
17+
18+
#endif // LLVM_LIBC_SRC_CTYPE_TOLOWER_H

0 commit comments

Comments
 (0)