[llvm] 703b0ed - [ADT] Add StringRef consume_front_lower and consume_back_lower
Martin Storsjö via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 22 02:57:21 PDT 2021
Author: Martin Storsjö
Date: 2021-06-22T12:38:08+03:00
New Revision: 703b0ed8e208e5e6cf001689ea0a92296552f032
URL: https://github.com/llvm/llvm-project/commit/703b0ed8e208e5e6cf001689ea0a92296552f032
DIFF: https://github.com/llvm/llvm-project/commit/703b0ed8e208e5e6cf001689ea0a92296552f032.diff
LOG: [ADT] Add StringRef consume_front_lower and consume_back_lower
These serve as a convenient combination of consume_front/back and
startswith_lower/endswith_lower, consistent with other existing
case insensitive methods named <operation>_lower.
Differential Revision: https://reviews.llvm.org/D104218
Added:
Modified:
llvm/include/llvm/ADT/StringRef.h
llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
llvm/tools/llvm-rc/llvm-rc.cpp
llvm/unittests/ADT/StringRefTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 2279071064c33..bb1ab530f66ab 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -685,6 +685,16 @@ namespace llvm {
return true;
}
+ /// Returns true if this StringRef has the given prefix, ignoring case,
+ /// and removes that prefix.
+ bool consume_front_lower(StringRef Prefix) {
+ if (!startswith_lower(Prefix))
+ return false;
+
+ *this = drop_front(Prefix.size());
+ return true;
+ }
+
/// Returns true if this StringRef has the given suffix and removes that
/// suffix.
bool consume_back(StringRef Suffix) {
@@ -695,6 +705,16 @@ namespace llvm {
return true;
}
+ /// Returns true if this StringRef has the given suffix, ignoring case,
+ /// and removes that suffix.
+ bool consume_back_lower(StringRef Suffix) {
+ if (!endswith_lower(Suffix))
+ return false;
+
+ *this = drop_back(Suffix.size());
+ return true;
+ }
+
/// Return a reference to the substring from [Start, End).
///
/// \param Start The index of the starting character in the substring; if
diff --git a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
index 65ef877a8de8b..56f6b5dff36d6 100644
--- a/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
+++ b/llvm/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp
@@ -93,23 +93,16 @@ MachineTypes getDefaultMachine() {
return getMachine(Triple(sys::getDefaultTargetTriple()));
}
-static bool consume_back_lower(StringRef &S, const char *Str) {
- if (!S.endswith_lower(Str))
- return false;
- S = S.drop_back(strlen(Str));
- return true;
-}
-
Optional<std::string> getPrefix(StringRef Argv0) {
StringRef ProgName = llvm::sys::path::stem(Argv0);
// x86_64-w64-mingw32-dlltool -> x86_64-w64-mingw32
// llvm-dlltool -> None
// aarch64-w64-mingw32-llvm-dlltool-10.exe -> aarch64-w64-mingw32
ProgName = ProgName.rtrim("0123456789.-");
- if (!consume_back_lower(ProgName, "dlltool"))
+ if (!ProgName.consume_back_lower("dlltool"))
return None;
- consume_back_lower(ProgName, "llvm-");
- consume_back_lower(ProgName, "-");
+ ProgName.consume_back_lower("llvm-");
+ ProgName.consume_back_lower("-");
return ProgName.str();
}
diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp
index 8a4ef95cfabd6..78cebd90b7845 100644
--- a/llvm/tools/llvm-rc/llvm-rc.cpp
+++ b/llvm/tools/llvm-rc/llvm-rc.cpp
@@ -256,23 +256,16 @@ bool preprocess(StringRef Src, StringRef Dst, const RcOptions &Opts,
return true;
}
-static bool consume_back_lower(StringRef &S, const char *Str) {
- if (!S.endswith_lower(Str))
- return false;
- S = S.drop_back(strlen(Str));
- return true;
-}
-
static std::pair<bool, std::string> isWindres(llvm::StringRef Argv0) {
StringRef ProgName = llvm::sys::path::stem(Argv0);
// x86_64-w64-mingw32-windres -> x86_64-w64-mingw32, windres
// llvm-rc -> "", llvm-rc
// aarch64-w64-mingw32-llvm-windres-10.exe -> aarch64-w64-mingw32, llvm-windres
ProgName = ProgName.rtrim("0123456789.-");
- if (!consume_back_lower(ProgName, "windres"))
+ if (!ProgName.consume_back_lower("windres"))
return std::make_pair<bool, std::string>(false, "");
- consume_back_lower(ProgName, "llvm-");
- consume_back_lower(ProgName, "-");
+ ProgName.consume_back_lower("llvm-");
+ ProgName.consume_back_lower("-");
return std::make_pair<bool, std::string>(true, ProgName.str());
}
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp
index e3f943bdbf418..ed11a2aae8a8d 100644
--- a/llvm/unittests/ADT/StringRefTest.cpp
+++ b/llvm/unittests/ADT/StringRefTest.cpp
@@ -392,6 +392,24 @@ TEST(StringRefTest, ConsumeFront) {
EXPECT_TRUE(Str.consume_front(""));
}
+TEST(StringRefTest, ConsumeFrontLower) {
+ StringRef Str("heLLo");
+ EXPECT_TRUE(Str.consume_front_lower(""));
+ EXPECT_EQ("heLLo", Str);
+ EXPECT_FALSE(Str.consume_front("HEl"));
+ EXPECT_EQ("heLLo", Str);
+ EXPECT_TRUE(Str.consume_front_lower("HEl"));
+ EXPECT_EQ("Lo", Str);
+ EXPECT_FALSE(Str.consume_front_lower("loworld"));
+ EXPECT_EQ("Lo", Str);
+ EXPECT_FALSE(Str.consume_front_lower("ol"));
+ EXPECT_EQ("Lo", Str);
+ EXPECT_TRUE(Str.consume_front_lower("lo"));
+ EXPECT_EQ("", Str);
+ EXPECT_FALSE(Str.consume_front_lower("o"));
+ EXPECT_TRUE(Str.consume_front_lower(""));
+}
+
TEST(StringRefTest, EndsWith) {
StringRef Str("hello");
EXPECT_TRUE(Str.endswith(""));
@@ -427,6 +445,24 @@ TEST(StringRefTest, ConsumeBack) {
EXPECT_TRUE(Str.consume_back(""));
}
+TEST(StringRefTest, ConsumeBackLower) {
+ StringRef Str("heLLo");
+ EXPECT_TRUE(Str.consume_back_lower(""));
+ EXPECT_EQ("heLLo", Str);
+ EXPECT_FALSE(Str.consume_back("lO"));
+ EXPECT_EQ("heLLo", Str);
+ EXPECT_TRUE(Str.consume_back_lower("lO"));
+ EXPECT_EQ("heL", Str);
+ EXPECT_FALSE(Str.consume_back_lower("helhel"));
+ EXPECT_EQ("heL", Str);
+ EXPECT_FALSE(Str.consume_back_lower("hle"));
+ EXPECT_EQ("heL", Str);
+ EXPECT_TRUE(Str.consume_back_lower("hEl"));
+ EXPECT_EQ("", Str);
+ EXPECT_FALSE(Str.consume_back_lower("h"));
+ EXPECT_TRUE(Str.consume_back_lower(""));
+}
+
TEST(StringRefTest, Find) {
StringRef Str("helloHELLO");
StringRef LongStr("hellx xello hell ello world foo bar hello HELLO");
More information about the llvm-commits
mailing list