[llvm] 1b97645 - [ADT] Introduce StringRef::{starts,ends}_width{,_insensitive}
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 15 15:06:47 PDT 2022
Author: Kazu Hirata
Date: 2022-10-15T15:06:37-07:00
New Revision: 1b97645e56bf321b06d1353024339958b64fd242
URL: https://github.com/llvm/llvm-project/commit/1b97645e56bf321b06d1353024339958b64fd242
DIFF: https://github.com/llvm/llvm-project/commit/1b97645e56bf321b06d1353024339958b64fd242.diff
LOG: [ADT] Introduce StringRef::{starts,ends}_width{,_insensitive}
This patch introduces:
StringRef::starts_with
StringRef::starts_with_insensitive
StringRef::ends_with
StringRef::ends_with_insensitive
to be more compatible with std::string and std::string_view.
I'm planning to deprecate the existing functions in favor of the new
ones.
Differential Revision: https://reviews.llvm.org/D136030
Added:
Modified:
llvm/include/llvm/ADT/StringRef.h
llvm/lib/Support/StringRef.cpp
llvm/unittests/ADT/StringRefTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 02159eb078661..032f42a51ec0b 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -253,22 +253,35 @@ namespace llvm {
/// @{
/// Check if this string starts with the given \p Prefix.
- [[nodiscard]] bool startswith(StringRef Prefix) const {
+ [[nodiscard]] bool starts_with(StringRef Prefix) const {
return Length >= Prefix.Length &&
compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
}
+ [[nodiscard]] bool startswith(StringRef Prefix) const {
+ return starts_with(Prefix);
+ }
/// Check if this string starts with the given \p Prefix, ignoring case.
- [[nodiscard]] bool startswith_insensitive(StringRef Prefix) const;
+ [[nodiscard]] bool starts_with_insensitive(StringRef Prefix) const;
+ [[nodiscard]] bool startswith_insensitive(StringRef Prefix) const {
+ return starts_with_insensitive(Prefix);
+ }
/// Check if this string ends with the given \p Suffix.
- [[nodiscard]] bool endswith(StringRef Suffix) const {
+ [[nodiscard]] bool ends_with(StringRef Suffix) const {
return Length >= Suffix.Length &&
- compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
+ compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) ==
+ 0;
+ }
+ [[nodiscard]] bool endswith(StringRef Suffix) const {
+ return ends_with(Suffix);
}
/// Check if this string ends with the given \p Suffix, ignoring case.
- [[nodiscard]] bool endswith_insensitive(StringRef Suffix) const;
+ [[nodiscard]] bool ends_with_insensitive(StringRef Suffix) const;
+ [[nodiscard]] bool endswith_insensitive(StringRef Suffix) const {
+ return ends_with_insensitive(Suffix);
+ }
/// @}
/// @name String Searching
@@ -612,7 +625,7 @@ namespace llvm {
/// Returns true if this StringRef has the given prefix and removes that
/// prefix.
bool consume_front(StringRef Prefix) {
- if (!startswith(Prefix))
+ if (!starts_with(Prefix))
return false;
*this = drop_front(Prefix.size());
@@ -632,7 +645,7 @@ namespace llvm {
/// Returns true if this StringRef has the given suffix and removes that
/// suffix.
bool consume_back(StringRef Suffix) {
- if (!endswith(Suffix))
+ if (!ends_with(Suffix))
return false;
*this = drop_back(Suffix.size());
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index 7e19a79812305..dd54d53861429 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -42,12 +42,12 @@ int StringRef::compare_insensitive(StringRef RHS) const {
return Length < RHS.Length ? -1 : 1;
}
-bool StringRef::startswith_insensitive(StringRef Prefix) const {
+bool StringRef::starts_with_insensitive(StringRef Prefix) const {
return Length >= Prefix.Length &&
ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0;
}
-bool StringRef::endswith_insensitive(StringRef Suffix) const {
+bool StringRef::ends_with_insensitive(StringRef Suffix) const {
return Length >= Suffix.Length &&
ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
}
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp
index 719f7c6d760e7..3827ebeb23980 100644
--- a/llvm/unittests/ADT/StringRefTest.cpp
+++ b/llvm/unittests/ADT/StringRefTest.cpp
@@ -356,20 +356,20 @@ TEST(StringRefTest, Trim) {
TEST(StringRefTest, StartsWith) {
StringRef Str("hello");
- EXPECT_TRUE(Str.startswith(""));
- EXPECT_TRUE(Str.startswith("he"));
- EXPECT_FALSE(Str.startswith("helloworld"));
- EXPECT_FALSE(Str.startswith("hi"));
+ EXPECT_TRUE(Str.starts_with(""));
+ EXPECT_TRUE(Str.starts_with("he"));
+ EXPECT_FALSE(Str.starts_with("helloworld"));
+ EXPECT_FALSE(Str.starts_with("hi"));
}
TEST(StringRefTest, StartsWithInsensitive) {
StringRef Str("heLLo");
- EXPECT_TRUE(Str.startswith_insensitive(""));
- EXPECT_TRUE(Str.startswith_insensitive("he"));
- EXPECT_TRUE(Str.startswith_insensitive("hell"));
- EXPECT_TRUE(Str.startswith_insensitive("HELlo"));
- EXPECT_FALSE(Str.startswith_insensitive("helloworld"));
- EXPECT_FALSE(Str.startswith_insensitive("hi"));
+ EXPECT_TRUE(Str.starts_with_insensitive(""));
+ EXPECT_TRUE(Str.starts_with_insensitive("he"));
+ EXPECT_TRUE(Str.starts_with_insensitive("hell"));
+ EXPECT_TRUE(Str.starts_with_insensitive("HELlo"));
+ EXPECT_FALSE(Str.starts_with_insensitive("helloworld"));
+ EXPECT_FALSE(Str.starts_with_insensitive("hi"));
}
TEST(StringRefTest, ConsumeFront) {
@@ -408,21 +408,21 @@ TEST(StringRefTest, ConsumeFrontInsensitive) {
TEST(StringRefTest, EndsWith) {
StringRef Str("hello");
- EXPECT_TRUE(Str.endswith(""));
- EXPECT_TRUE(Str.endswith("lo"));
- EXPECT_FALSE(Str.endswith("helloworld"));
- EXPECT_FALSE(Str.endswith("worldhello"));
- EXPECT_FALSE(Str.endswith("so"));
+ EXPECT_TRUE(Str.ends_with(""));
+ EXPECT_TRUE(Str.ends_with("lo"));
+ EXPECT_FALSE(Str.ends_with("helloworld"));
+ EXPECT_FALSE(Str.ends_with("worldhello"));
+ EXPECT_FALSE(Str.ends_with("so"));
}
TEST(StringRefTest, EndsWithInsensitive) {
StringRef Str("heLLo");
- EXPECT_TRUE(Str.endswith_insensitive(""));
- EXPECT_TRUE(Str.endswith_insensitive("lo"));
- EXPECT_TRUE(Str.endswith_insensitive("LO"));
- EXPECT_TRUE(Str.endswith_insensitive("ELlo"));
- EXPECT_FALSE(Str.endswith_insensitive("helloworld"));
- EXPECT_FALSE(Str.endswith_insensitive("hi"));
+ EXPECT_TRUE(Str.ends_with_insensitive(""));
+ EXPECT_TRUE(Str.ends_with_insensitive("lo"));
+ EXPECT_TRUE(Str.ends_with_insensitive("LO"));
+ EXPECT_TRUE(Str.ends_with_insensitive("ELlo"));
+ EXPECT_FALSE(Str.ends_with_insensitive("helloworld"));
+ EXPECT_FALSE(Str.ends_with_insensitive("hi"));
}
TEST(StringRefTest, ConsumeBack) {
More information about the llvm-commits
mailing list