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

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 11:43:20 PDT 2024


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/105461

>From 3241472a31beb24882ae590c0a697712f044e8b4 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 20 Aug 2024 18:38:44 -0700
Subject: [PATCH] [ADT] Add `isPunct` to StringExtras

- Add `isPunct` to StringExtras.h.
- Add unit test for `isPunct` to StringExtrasTest.
---
 llvm/include/llvm/ADT/StringExtras.h    | 11 +++++++++++
 llvm/unittests/ADT/StringExtrasTest.cpp | 12 ++++++++++++
 2 files changed, 23 insertions(+)

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