diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -12,6 +12,8 @@ libc.src.ctype.isspace libc.src.ctype.isupper libc.src.ctype.isxdigit + libc.src.ctype.tolower + libc.src.ctype.toupper # errno.h entrypoints libc.src.errno.__errno_location diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -100,6 +100,8 @@ "isspace", "isupper", "isxdigit", + "tolower", + "toupper", ]; } diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -15,6 +15,8 @@ libc.src.ctype.isspace libc.src.ctype.isupper libc.src.ctype.isxdigit + libc.src.ctype.tolower + libc.src.ctype.toupper # errno.h entrypoints libc.src.errno.__errno_location diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td --- a/libc/spec/stdc.td +++ b/libc/spec/stdc.td @@ -106,6 +106,16 @@ RetValSpec, [ArgSpec] >, + FunctionSpec< + "tolower", + RetValSpec, + [ArgSpec] + >, + FunctionSpec< + "toupper", + RetValSpec, + [ArgSpec] + >, ] >; diff --git a/libc/src/ctype/CMakeLists.txt b/libc/src/ctype/CMakeLists.txt --- a/libc/src/ctype/CMakeLists.txt +++ b/libc/src/ctype/CMakeLists.txt @@ -66,6 +66,8 @@ islower.cpp HDRS islower.h + DEPENDS + .ctype_utils ) add_entrypoint_object( @@ -100,6 +102,8 @@ isupper.cpp HDRS isupper.h + DEPENDS + .ctype_utils ) add_entrypoint_object( @@ -111,3 +115,23 @@ DEPENDS .ctype_utils ) + +add_entrypoint_object( + tolower + SRCS + tolower.cpp + HDRS + tolower.h + DEPENDS + .ctype_utils +) + +add_entrypoint_object( + toupper + SRCS + toupper.cpp + HDRS + toupper.h + DEPENDS + .ctype_utils +) diff --git a/libc/src/ctype/ctype_utils.h b/libc/src/ctype/ctype_utils.h --- a/libc/src/ctype/ctype_utils.h +++ b/libc/src/ctype/ctype_utils.h @@ -18,14 +18,18 @@ // of a function call by inlining them. // ------------------------------------------------------ -static inline int isdigit(unsigned ch) { return (ch - '0') < 10; } - static inline int isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; } +static inline int isdigit(unsigned ch) { return (ch - '0') < 10; } + static inline int isalnum(unsigned ch) { return isalpha(ch) || isdigit(ch); } static inline int isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; } +static inline int islower(unsigned ch) { return (ch - 'a') < 26; } + +static inline int isupper(unsigned ch) { return (ch - 'A') < 26; } + } // namespace internal } // namespace __llvm_libc diff --git a/libc/src/ctype/islower.cpp b/libc/src/ctype/islower.cpp --- a/libc/src/ctype/islower.cpp +++ b/libc/src/ctype/islower.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/ctype/islower.h" +#include "src/ctype/ctype_utils.h" #include "src/__support/common.h" @@ -14,9 +15,6 @@ // TODO: Currently restricted to default locale. // These should be extended using locale information. -int LLVM_LIBC_ENTRYPOINT(islower)(int c) { - const unsigned ch = c; - return (ch - 'a') < 26; -} +int LLVM_LIBC_ENTRYPOINT(islower)(int c) { return internal::islower(c); } } // namespace __llvm_libc diff --git a/libc/src/ctype/isupper.cpp b/libc/src/ctype/isupper.cpp --- a/libc/src/ctype/isupper.cpp +++ b/libc/src/ctype/isupper.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/ctype/isupper.h" +#include "src/ctype/ctype_utils.h" #include "src/__support/common.h" @@ -14,9 +15,6 @@ // TODO: Currently restricted to default locale. // These should be extended using locale information. -int LLVM_LIBC_ENTRYPOINT(isupper)(int c) { - const unsigned ch = c; - return (ch - 'A') < 26; -} +int LLVM_LIBC_ENTRYPOINT(isupper)(int c) { return internal::isupper(c); } } // namespace __llvm_libc diff --git a/libc/src/ctype/tolower.h b/libc/src/ctype/tolower.h new file mode 100644 --- /dev/null +++ b/libc/src/ctype/tolower.h @@ -0,0 +1,18 @@ +//===-- Implementation header for tolower -------------------------*-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_TOLOWER_H +#define LLVM_LIBC_SRC_CTYPE_TOLOWER_H + +namespace __llvm_libc { + +int tolower(int c); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_CTYPE_TOLOWER_H diff --git a/libc/src/ctype/islower.cpp b/libc/src/ctype/tolower.cpp copy from libc/src/ctype/islower.cpp copy to libc/src/ctype/tolower.cpp --- a/libc/src/ctype/islower.cpp +++ b/libc/src/ctype/tolower.cpp @@ -1,4 +1,4 @@ -//===-- Implementation of islower------------------------------------------===// +//===-- Implementation of tolower------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "src/ctype/islower.h" +#include "src/ctype/tolower.h" +#include "src/ctype/ctype_utils.h" #include "src/__support/common.h" @@ -14,9 +15,10 @@ // TODO: Currently restricted to default locale. // These should be extended using locale information. -int LLVM_LIBC_ENTRYPOINT(islower)(int c) { - const unsigned ch = c; - return (ch - 'a') < 26; +int LLVM_LIBC_ENTRYPOINT(tolower)(int c) { + if (internal::isupper(c)) + return c + 'a' - 'A'; + return c; } } // namespace __llvm_libc diff --git a/libc/src/ctype/toupper.h b/libc/src/ctype/toupper.h new file mode 100644 --- /dev/null +++ b/libc/src/ctype/toupper.h @@ -0,0 +1,18 @@ +//===-- Implementation header for toupper -------------------------*-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_TOUPPER_H +#define LLVM_LIBC_SRC_CTYPE_TOUPPER_H + +namespace __llvm_libc { + +int toupper(int c); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_CTYPE_TOUPPER_H diff --git a/libc/src/ctype/islower.cpp b/libc/src/ctype/toupper.cpp copy from libc/src/ctype/islower.cpp copy to libc/src/ctype/toupper.cpp --- a/libc/src/ctype/islower.cpp +++ b/libc/src/ctype/toupper.cpp @@ -1,4 +1,4 @@ -//===-- Implementation of islower------------------------------------------===// +//===-- Implementation of toupper------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,7 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "src/ctype/islower.h" +#include "src/ctype/toupper.h" +#include "src/ctype/ctype_utils.h" #include "src/__support/common.h" @@ -14,9 +15,10 @@ // TODO: Currently restricted to default locale. // These should be extended using locale information. -int LLVM_LIBC_ENTRYPOINT(islower)(int c) { - const unsigned ch = c; - return (ch - 'a') < 26; +int LLVM_LIBC_ENTRYPOINT(toupper)(int c) { + if (internal::islower(c)) + return c + 'A' - 'a'; + return c; } } // namespace __llvm_libc diff --git a/libc/test/src/ctype/CMakeLists.txt b/libc/test/src/ctype/CMakeLists.txt --- a/libc/test/src/ctype/CMakeLists.txt +++ b/libc/test/src/ctype/CMakeLists.txt @@ -119,3 +119,23 @@ DEPENDS libc.src.ctype.isxdigit ) + +add_libc_unittest( + tolower + SUITE + libc_ctype_unittests + SRCS + tolower_test.cpp + DEPENDS + libc.src.ctype.tolower +) + +add_libc_unittest( + toupper + SUITE + libc_ctype_unittests + SRCS + toupper_test.cpp + DEPENDS + libc.src.ctype.toupper +) diff --git a/libc/test/src/ctype/tolower_test.cpp b/libc/test/src/ctype/tolower_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/ctype/tolower_test.cpp @@ -0,0 +1,20 @@ +//===-- Unittests for tolower----------------------------------------------===// +// +// 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/tolower.h" +#include "utils/UnitTest/Test.h" + +TEST(ToLower, DefaultLocale) { + for (int ch = 0; ch < 255; ++ch) { + // This follows pattern 'A' + 32 = 'a'. + if ('A' <= ch && ch <= 'Z') + EXPECT_EQ(__llvm_libc::tolower(ch), ch + 32); + else + EXPECT_EQ(__llvm_libc::tolower(ch), ch); + } +} diff --git a/libc/test/src/ctype/toupper_test.cpp b/libc/test/src/ctype/toupper_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/ctype/toupper_test.cpp @@ -0,0 +1,20 @@ +//===-- Unittests for toupper----------------------------------------------===// +// +// 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/toupper.h" +#include "utils/UnitTest/Test.h" + +TEST(ToUpper, DefaultLocale) { + for (int ch = 0; ch < 255; ++ch) { + // This follows pattern 'a' - 32 = 'A'. + if ('a' <= ch && ch <= 'z') + EXPECT_EQ(__llvm_libc::toupper(ch), ch - 32); + else + EXPECT_EQ(__llvm_libc::toupper(ch), ch); + } +}