1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[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
This commit is contained in:
Martin Storsjö 2021-06-14 14:43:46 +03:00
parent 7b3baaea7f
commit a307928fe5
4 changed files with 62 additions and 20 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -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());
}

View File

@ -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");