mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-24 11:59:56 +00:00
[Support] Add case-insensitive versions of StringSwitch members.
This adds support for CaseLower, CasesLower, StartsWithLower, and EndsWithLower. Differential revision: https://reviews.llvm.org/D24686 llvm-svn: 283244
This commit is contained in:
parent
aba6f48815
commit
cc36037bff
@ -68,6 +68,7 @@ public:
|
||||
|
||||
~StringSwitch() = default;
|
||||
|
||||
// Case-sensitive case matchers
|
||||
template<unsigned N>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
||||
StringSwitch& Case(const char (&S)[N], const T& Value) {
|
||||
@ -182,8 +183,62 @@ public:
|
||||
return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value);
|
||||
}
|
||||
|
||||
// Case-insensitive case matchers.
|
||||
template <unsigned N>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &CaseLower(const char (&S)[N],
|
||||
const T &Value) {
|
||||
if (!Result && Str.equals_lower(StringRef(S, N - 1)))
|
||||
Result = &Value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <unsigned N>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &EndsWithLower(const char (&S)[N],
|
||||
const T &Value) {
|
||||
if (!Result && Str.endswith_lower(StringRef(S, N - 1)))
|
||||
Result = &Value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <unsigned N>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &StartsWithLower(const char (&S)[N],
|
||||
const T &Value) {
|
||||
if (!Result && Str.startswith_lower(StringRef(S, N - 1)))
|
||||
Result = &Value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
template <unsigned N0, unsigned N1>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &
|
||||
CasesLower(const char (&S0)[N0], const char (&S1)[N1], const T &Value) {
|
||||
return CaseLower(S0, Value).CaseLower(S1, Value);
|
||||
}
|
||||
|
||||
template <unsigned N0, unsigned N1, unsigned N2>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &
|
||||
CasesLower(const char (&S0)[N0], const char (&S1)[N1], const char (&S2)[N2],
|
||||
const T &Value) {
|
||||
return CaseLower(S0, Value).CasesLower(S1, S2, Value);
|
||||
}
|
||||
|
||||
template <unsigned N0, unsigned N1, unsigned N2, unsigned N3>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &
|
||||
CasesLower(const char (&S0)[N0], const char (&S1)[N1], const char (&S2)[N2],
|
||||
const char (&S3)[N3], const T &Value) {
|
||||
return CaseLower(S0, Value).CasesLower(S1, S2, S3, Value);
|
||||
}
|
||||
|
||||
template <unsigned N0, unsigned N1, unsigned N2, unsigned N3, unsigned N4>
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &
|
||||
CasesLower(const char (&S0)[N0], const char (&S1)[N1], const char (&S2)[N2],
|
||||
const char (&S3)[N3], const char (&S4)[N4], const T &Value) {
|
||||
return CaseLower(S0, Value).CasesLower(S1, S2, S3, S4, Value);
|
||||
}
|
||||
|
||||
LLVM_ATTRIBUTE_ALWAYS_INLINE
|
||||
R Default(const T& Value) const {
|
||||
R Default(const T &Value) const {
|
||||
if (Result)
|
||||
return *Result;
|
||||
return Value;
|
||||
|
@ -41,6 +41,36 @@ TEST(StringSwitchTest, Case) {
|
||||
EXPECT_EQ(-1, Translate("Test"));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, CaseLower) {
|
||||
auto Translate = [](StringRef S) {
|
||||
return llvm::StringSwitch<int>(S)
|
||||
.Case("0", 0)
|
||||
.Case("1", 1)
|
||||
.Case("2", 2)
|
||||
.Case("3", 3)
|
||||
.Case("4", 4)
|
||||
.Case("5", 5)
|
||||
.Case("6", 6)
|
||||
.Case("7", 7)
|
||||
.Case("8", 8)
|
||||
.Case("9", 9)
|
||||
.CaseLower("A", 10)
|
||||
.CaseLower("B", 11)
|
||||
.CaseLower("C", 12)
|
||||
.CaseLower("D", 13)
|
||||
.CaseLower("E", 14)
|
||||
.CaseLower("F", 15)
|
||||
.Default(-1);
|
||||
};
|
||||
EXPECT_EQ(1, Translate("1"));
|
||||
EXPECT_EQ(2, Translate("2"));
|
||||
EXPECT_EQ(11, Translate("B"));
|
||||
EXPECT_EQ(11, Translate("b"));
|
||||
|
||||
EXPECT_EQ(-1, Translate(""));
|
||||
EXPECT_EQ(-1, Translate("Test"));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, StartsWith) {
|
||||
auto Translate = [](StringRef S) {
|
||||
return llvm::StringSwitch<std::function<int(int, int)>>(S)
|
||||
@ -60,6 +90,29 @@ TEST(StringSwitchTest, StartsWith) {
|
||||
EXPECT_EQ(0, Translate("ADDER")(10, 5));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, StartsWithLower) {
|
||||
auto Translate = [](StringRef S) {
|
||||
return llvm::StringSwitch<std::function<int(int, int)>>(S)
|
||||
.StartsWithLower("add", [](int X, int Y) { return X + Y; })
|
||||
.StartsWithLower("sub", [](int X, int Y) { return X - Y; })
|
||||
.StartsWithLower("mul", [](int X, int Y) { return X * Y; })
|
||||
.StartsWithLower("div", [](int X, int Y) { return X / Y; })
|
||||
.Default([](int X, int Y) { return 0; });
|
||||
};
|
||||
|
||||
EXPECT_EQ(15, Translate("adder")(10, 5));
|
||||
EXPECT_EQ(5, Translate("subtracter")(10, 5));
|
||||
EXPECT_EQ(50, Translate("multiplier")(10, 5));
|
||||
EXPECT_EQ(2, Translate("divider")(10, 5));
|
||||
|
||||
EXPECT_EQ(15, Translate("AdDeR")(10, 5));
|
||||
EXPECT_EQ(5, Translate("SuBtRaCtEr")(10, 5));
|
||||
EXPECT_EQ(50, Translate("MuLtIpLiEr")(10, 5));
|
||||
EXPECT_EQ(2, Translate("DiViDeR")(10, 5));
|
||||
|
||||
EXPECT_EQ(0, Translate("nothing")(10, 5));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, EndsWith) {
|
||||
enum class Suffix { Possible, PastTense, Process, InProgressAction, Unknown };
|
||||
|
||||
@ -80,6 +133,26 @@ TEST(StringSwitchTest, EndsWith) {
|
||||
EXPECT_EQ(Suffix::Unknown, Translate("OPTIMIZABLE"));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, EndsWithLower) {
|
||||
enum class Suffix { Possible, PastTense, Process, InProgressAction, Unknown };
|
||||
|
||||
auto Translate = [](StringRef S) {
|
||||
return llvm::StringSwitch<Suffix>(S)
|
||||
.EndsWithLower("able", Suffix::Possible)
|
||||
.EndsWithLower("ed", Suffix::PastTense)
|
||||
.EndsWithLower("ation", Suffix::Process)
|
||||
.EndsWithLower("ing", Suffix::InProgressAction)
|
||||
.Default(Suffix::Unknown);
|
||||
};
|
||||
|
||||
EXPECT_EQ(Suffix::Possible, Translate("optimizable"));
|
||||
EXPECT_EQ(Suffix::Possible, Translate("OPTIMIZABLE"));
|
||||
EXPECT_EQ(Suffix::PastTense, Translate("optimized"));
|
||||
EXPECT_EQ(Suffix::Process, Translate("optimization"));
|
||||
EXPECT_EQ(Suffix::InProgressAction, Translate("optimizing"));
|
||||
EXPECT_EQ(Suffix::Unknown, Translate("optimizer"));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, Cases) {
|
||||
enum class OSType { Windows, Linux, Unknown };
|
||||
|
||||
@ -105,3 +178,29 @@ TEST(StringSwitchTest, Cases) {
|
||||
EXPECT_EQ(OSType::Unknown, Translate("Windows"));
|
||||
EXPECT_EQ(OSType::Unknown, Translate(""));
|
||||
}
|
||||
|
||||
TEST(StringSwitchTest, CasesLower) {
|
||||
enum class OSType { Windows, Linux, Unknown };
|
||||
|
||||
auto Translate = [](StringRef S) {
|
||||
return llvm::StringSwitch<OSType>(S)
|
||||
.CasesLower("wind\0ws", "win32", "winnt", OSType::Windows)
|
||||
.CasesLower("linux", "unix", "*nix", "posix", OSType::Linux)
|
||||
.Default(OSType::Unknown);
|
||||
};
|
||||
|
||||
EXPECT_EQ(OSType::Windows, Translate(llvm::StringRef("WIND\0WS", 7)));
|
||||
EXPECT_EQ(OSType::Windows, Translate("WIN32"));
|
||||
EXPECT_EQ(OSType::Windows, Translate("WINNT"));
|
||||
|
||||
EXPECT_EQ(OSType::Linux, Translate("LINUX"));
|
||||
EXPECT_EQ(OSType::Linux, Translate("UNIX"));
|
||||
EXPECT_EQ(OSType::Linux, Translate("*NIX"));
|
||||
EXPECT_EQ(OSType::Linux, Translate("POSIX"));
|
||||
|
||||
EXPECT_EQ(OSType::Windows, Translate(llvm::StringRef("wind\0ws", 7)));
|
||||
EXPECT_EQ(OSType::Linux, Translate("linux"));
|
||||
|
||||
EXPECT_EQ(OSType::Unknown, Translate("wind"));
|
||||
EXPECT_EQ(OSType::Unknown, Translate(""));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user