Skip to content

Commit 3241472

Browse files
committed
[ADT] Add isPunct to StringExtras
- Add `isPunct` to StringExtras.h. - Add unit test for `isPunct` to StringExtrasTest.
1 parent 6816a13 commit 3241472

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

llvm/include/llvm/ADT/StringExtras.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ inline bool isPrint(char C) {
140140
return (0x20 <= UC) && (UC <= 0x7E);
141141
}
142142

143+
/// Checks whether character \p C is a punctuation character.
144+
///
145+
/// Locale-independent version of the C standard library ispunct. The list of
146+
/// punctuation characters can be found in the documentation of std::ispunct:
147+
/// https://en.cppreference.com/w/cpp/string/byte/ispunct.
148+
inline bool isPunct(char C) {
149+
static constexpr StringLiteral Punctuations =
150+
R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)";
151+
return Punctuations.contains(C);
152+
}
153+
143154
/// Checks whether character \p C is whitespace in the "C" locale.
144155
///
145156
/// Locale-independent version of the C standard library isspace.

llvm/unittests/ADT/StringExtrasTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ TEST(StringExtrasTest, isUpper) {
5959
EXPECT_FALSE(isUpper('\?'));
6060
}
6161

62+
TEST(StringExtrasTest, isPunct) {
63+
EXPECT_FALSE(isPunct('a'));
64+
EXPECT_FALSE(isPunct('b'));
65+
EXPECT_FALSE(isPunct('z'));
66+
EXPECT_TRUE(isPunct('-'));
67+
EXPECT_TRUE(isPunct(';'));
68+
EXPECT_TRUE(isPunct('@'));
69+
EXPECT_FALSE(isPunct('0'));
70+
EXPECT_FALSE(isPunct('1'));
71+
EXPECT_FALSE(isPunct('x'));
72+
}
73+
6274
template <class ContainerT> void testJoin() {
6375
ContainerT Items;
6476
EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));

0 commit comments

Comments
 (0)