[llvm] [ADT] Add `isPunct` to StringExtras (PR #105461)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 20 18:54:31 PDT 2024
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/105461
>From f9db70cd527547b5c3f095d87a576ab8fd68af9c 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 1/2] [ADT] Add `isPunct` to StringExtras
- Add `isPunct` to StringExtras.h.
- Add unit test for `isPunct` to StringExtrasTest.
---
llvm/include/llvm/ADT/StringExtras.h | 9 +++++++++
llvm/unittests/ADT/StringExtrasTest.cpp | 13 +++++++++++++
2 files changed, 22 insertions(+)
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;
+ }
+}
>From 5f335408cd5ee35cb761039672cb694337af946d Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 20 Aug 2024 18:54:24 -0700
Subject: [PATCH 2/2] Update llvm/unittests/ADT/StringExtrasTest.cpp
Co-authored-by: Yanzuo Liu <zwuis at outlook.com>
---
llvm/unittests/ADT/StringExtrasTest.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp
index 3dbe793f61f937..ceda98ba63a2e2 100644
--- a/llvm/unittests/ADT/StringExtrasTest.cpp
+++ b/llvm/unittests/ADT/StringExtrasTest.cpp
@@ -385,7 +385,7 @@ TEST(StringExtrasTest, arrayToStringRef) {
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.
+ // std::ispunct(). Limit to first 256 characters in case CHAR_BIT > 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));
More information about the llvm-commits
mailing list