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

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 18:46:16 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Rahul Joshi (jurahul)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/105461.diff


2 Files Affected:

- (modified) llvm/include/llvm/ADT/StringExtras.h (+9) 
- (modified) llvm/unittests/ADT/StringExtrasTest.cpp (+13) 


``````````diff
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 20e6ad1f68f996..58980f4ee845c1 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -140,6 +140,15 @@ 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.
+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..3dbe793f61f937 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -10,6 +10,8 @@
 #include "llvm/Support/raw_ostream.h"
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
+#include <cctype>
+#include <limits>
 
 using namespace llvm;
 
@@ -380,3 +382,14 @@ TEST(StringExtrasTest, arrayToStringRef) {
   roundTripTestString("\0\n");
   roundTripTestString("\xFF\xFE");
 }
+
+TEST(StringExtrasTest, isPunct) {
+  // Loop over all valid char values and verify that llvm::isPunct matched
+  // std::ispunct(). Limit to first 256 characters in case sizeof(char) > 8.
+  int Count = 0;
+  for (char C = std::numeric_limits<char>::min(); Count < 256; ++C) {
+    EXPECT_EQ(static_cast<bool>(std::ispunct(C)), isPunct(C));
+    if (C == std::numeric_limits<char>::max())
+      break;
+  }
+}

``````````

</details>


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


More information about the llvm-commits mailing list