[llvm] b03b170 - [ADT] Add `isPunct` to StringExtras (#105461)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 13:28:32 PDT 2024


Author: Rahul Joshi
Date: 2024-08-21T13:28:28-07:00
New Revision: b03b170dd39799b4fb25ffe70b81d0cf0c7d7346

URL: https://github.com/llvm/llvm-project/commit/b03b170dd39799b4fb25ffe70b81d0cf0c7d7346
DIFF: https://github.com/llvm/llvm-project/commit/b03b170dd39799b4fb25ffe70b81d0cf0c7d7346.diff

LOG: [ADT] Add `isPunct` to StringExtras (#105461)

- Add `isPunct` to StringExtras.h.
- Add unit test for `isPunct` to StringExtrasTest.

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 20e6ad1f68f996..1317d521d4c191 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -140,6 +140,17 @@ inline bool isPrint(char C) {
   return (0x20 <= UC) && (UC <= 0x7E);
 }
 
+/// Checks whether character \p C is a punctuation character.
+///
+/// Locale-independent version of the C standard library ispunct. The list of
+/// punctuation characters can be found in the documentation of std::ispunct:
+/// https://en.cppreference.com/w/cpp/string/byte/ispunct.
+inline bool isPunct(char C) {
+  static constexpr StringLiteral Punctuations =
+      R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)";
+  return Punctuations.contains(C);
+}
+
 /// Checks whether character \p C is whitespace in the "C" locale.
 ///
 /// Locale-independent version of the C standard library isspace.

diff  --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 1fb1fea6577911..51f7c3948a3146 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -59,6 +59,18 @@ TEST(StringExtrasTest, isUpper) {
   EXPECT_FALSE(isUpper('\?'));
 }
 
+TEST(StringExtrasTest, isPunct) {
+  EXPECT_FALSE(isPunct('a'));
+  EXPECT_FALSE(isPunct('b'));
+  EXPECT_FALSE(isPunct('z'));
+  EXPECT_TRUE(isPunct('-'));
+  EXPECT_TRUE(isPunct(';'));
+  EXPECT_TRUE(isPunct('@'));
+  EXPECT_FALSE(isPunct('0'));
+  EXPECT_FALSE(isPunct('1'));
+  EXPECT_FALSE(isPunct('x'));
+}
+
 template <class ContainerT> void testJoin() {
   ContainerT Items;
   EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));


        


More information about the llvm-commits mailing list