From a307928fe5bb84c56867b21c71b65975fe2d76b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 14 Jun 2021 14:43:46 +0300 Subject: [PATCH] [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 _lower. Differential Revision: https://reviews.llvm.org/D104218 --- include/llvm/ADT/StringRef.h | 20 +++++++++++ .../llvm-dlltool/DlltoolDriver.cpp | 13 ++----- tools/llvm-rc/llvm-rc.cpp | 13 ++----- unittests/ADT/StringRefTest.cpp | 36 +++++++++++++++++++ 4 files changed, 62 insertions(+), 20 deletions(-) diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 2279071064c..bb1ab530f66 100644 --- a/include/llvm/ADT/StringRef.h +++ b/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/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp b/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp index 65ef877a8de..56f6b5dff36 100644 --- a/lib/ToolDrivers/llvm-dlltool/DlltoolDriver.cpp +++ b/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 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/tools/llvm-rc/llvm-rc.cpp b/tools/llvm-rc/llvm-rc.cpp index 8a4ef95cfab..78cebd90b78 100644 --- a/tools/llvm-rc/llvm-rc.cpp +++ b/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 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(false, ""); - consume_back_lower(ProgName, "llvm-"); - consume_back_lower(ProgName, "-"); + ProgName.consume_back_lower("llvm-"); + ProgName.consume_back_lower("-"); return std::make_pair(true, ProgName.str()); } diff --git a/unittests/ADT/StringRefTest.cpp b/unittests/ADT/StringRefTest.cpp index e3f943bdbf4..ed11a2aae8a 100644 --- a/unittests/ADT/StringRefTest.cpp +++ b/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");