[PATCH] D156796: [ADT] [NFC] Introduce isLower and isUpper helpers.

Jacek Caban via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 12:26:39 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGfd5ca3741395: [ADT] [NFC] Introduce isLower and isUpper helpers. (authored by jacek).

Changed prior to commit:
  https://reviews.llvm.org/D156796?vs=546041&id=546185#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156796/new/

https://reviews.llvm.org/D156796

Files:
  llvm/include/llvm/ADT/StringExtras.h
  llvm/unittests/ADT/StringExtrasTest.cpp


Index: llvm/unittests/ADT/StringExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/StringExtrasTest.cpp
+++ llvm/unittests/ADT/StringExtrasTest.cpp
@@ -35,6 +35,30 @@
   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> "));
Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -98,10 +98,14 @@
 /// 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 @@
 
 /// 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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156796.546185.patch
Type: text/x-patch
Size: 2452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230801/cd5bc4df/attachment.bin>


More information about the llvm-commits mailing list