[llvm] fd5ca37 - [ADT] [NFC] Introduce isLower and isUpper helpers.
Jacek Caban via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 1 12:26:26 PDT 2023
Author: Jacek Caban
Date: 2023-08-01T21:24:32+02:00
New Revision: fd5ca37413957cc36352ff3e7c02399288c74af2
URL: https://github.com/llvm/llvm-project/commit/fd5ca37413957cc36352ff3e7c02399288c74af2
DIFF: https://github.com/llvm/llvm-project/commit/fd5ca37413957cc36352ff3e7c02399288c74af2.diff
LOG: [ADT] [NFC] Introduce isLower and isUpper helpers.
Reviewed By: MaskRay, jhenderson
Differential Revision: https://reviews.llvm.org/D156796
Added:
Modified:
llvm/include/llvm/ADT/StringExtras.h
llvm/unittests/ADT/StringExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index eec010e893228d..30397b23ab03b3 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -98,10 +98,14 @@ inline bool isDigit(char C) { return C >= '0' && C <= '9'; }
/// Checks if character \p C is a hexadecimal numeric character.
inline bool isHexDigit(char C) { return hexDigitValue(C) != ~0U; }
+/// Checks if character \p C is a lowercase letter as classified by "C" locale.
+inline bool isLower(char C) { return 'a' <= C && C <= 'z'; }
+
+/// Checks if character \p C is a uppercase letter as classified by "C" locale.
+inline bool isUpper(char C) { return 'A' <= C && C <= 'Z'; }
+
/// Checks if character \p C is a valid letter as classified by "C" locale.
-inline bool isAlpha(char C) {
- return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z');
-}
+inline bool isAlpha(char C) { return isLower(C) || isUpper(C); }
/// Checks whether character \p C is either a decimal digit or an uppercase or
/// lowercase letter as classified by "C" locale.
@@ -137,14 +141,14 @@ inline bool isSpace(char C) {
/// Returns the corresponding lowercase character if \p x is uppercase.
inline char toLower(char x) {
- if (x >= 'A' && x <= 'Z')
+ if (isUpper(x))
return x - 'A' + 'a';
return x;
}
/// Returns the corresponding uppercase character if \p x is lowercase.
inline char toUpper(char x) {
- if (x >= 'a' && x <= 'z')
+ if (isLower(x))
return x - 'a' + 'A';
return x;
}
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 2557dcedbaf019..971560b8eb8c4e 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -35,6 +35,30 @@ TEST(StringExtrasTest, isSpace) {
EXPECT_FALSE(isSpace('_'));
}
+TEST(StringExtrasTest, isLower) {
+ EXPECT_TRUE(isLower('a'));
+ EXPECT_TRUE(isLower('b'));
+ EXPECT_TRUE(isLower('z'));
+ EXPECT_FALSE(isLower('A'));
+ EXPECT_FALSE(isLower('B'));
+ EXPECT_FALSE(isLower('Z'));
+ EXPECT_FALSE(isLower('\0'));
+ EXPECT_FALSE(isLower('\t'));
+ EXPECT_FALSE(isLower('\?'));
+}
+
+TEST(StringExtrasTest, isUpper) {
+ EXPECT_FALSE(isUpper('a'));
+ EXPECT_FALSE(isUpper('b'));
+ EXPECT_FALSE(isUpper('z'));
+ EXPECT_TRUE(isUpper('A'));
+ EXPECT_TRUE(isUpper('B'));
+ EXPECT_TRUE(isUpper('Z'));
+ EXPECT_FALSE(isUpper('\0'));
+ EXPECT_FALSE(isUpper('\t'));
+ EXPECT_FALSE(isUpper('\?'));
+}
+
TEST(StringExtrasTest, Join) {
std::vector<std::string> Items;
EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
More information about the llvm-commits
mailing list