Index: include/llvm/ADT/StringSwitch.h =================================================================== --- include/llvm/ADT/StringSwitch.h +++ include/llvm/ADT/StringSwitch.h @@ -15,6 +15,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/Compiler.h" +#include "llvm/Support/Error.h" #include #include @@ -48,6 +49,10 @@ /// null before that. const T *Result; + template struct Ignore { + template Ignore(const U &) {} + }; + public: LLVM_ATTRIBUTE_ALWAYS_INLINE explicit StringSwitch(StringRef S) @@ -68,6 +73,7 @@ ~StringSwitch() = default; + // Case-sensitive case matchers template LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch& Case(const char (&S)[N], const T& Value) { @@ -101,66 +107,66 @@ return *this; } - template - LLVM_ATTRIBUTE_ALWAYS_INLINE - StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1], - const T& Value) { - if (!Result && ( - (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) || - (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0))) { - Result = &Value; - } + template + LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &Cases(const char (&S)[N], + const T &Value) { + return Case(S, Value); + } - return *this; + template + LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & + Cases(const char (&S1)[N1], const char (&S2)[N2], const T3 &A3, + const Ts &... As) { + const T &Value = [](Ignore..., const T &V) -> const T & { + return V; + }(A3, As...); + return Case(S1, Value).Cases(S2, A3, As...); } - template - LLVM_ATTRIBUTE_ALWAYS_INLINE - StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1], - const char (&S2)[N2], const T& Value) { - if (!Result && ( - (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) || - (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0) || - (N2-1 == Str.size() && std::memcmp(S2, Str.data(), N2-1) == 0))) { + // Case-insensitive case matchers. + template + 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 - LLVM_ATTRIBUTE_ALWAYS_INLINE - StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1], - const char (&S2)[N2], const char (&S3)[N3], - const T& Value) { - if (!Result && ( - (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) || - (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0) || - (N2-1 == Str.size() && std::memcmp(S2, Str.data(), N2-1) == 0) || - (N3-1 == Str.size() && std::memcmp(S3, Str.data(), N3-1) == 0))) { + template + 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 - LLVM_ATTRIBUTE_ALWAYS_INLINE - StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1], - const char (&S2)[N2], const char (&S3)[N3], - const char (&S4)[N4], const T& Value) { - if (!Result && ( - (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) || - (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0) || - (N2-1 == Str.size() && std::memcmp(S2, Str.data(), N2-1) == 0) || - (N3-1 == Str.size() && std::memcmp(S3, Str.data(), N3-1) == 0) || - (N4-1 == Str.size() && std::memcmp(S4, Str.data(), N4-1) == 0))) { + template + 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 + LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch &CasesLower(const char (&S)[N], + const T &Value) { + return CaseLower(S, Value); + } + + template + LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & + CasesLower(const char (&S1)[N1], const char (&S2)[N2], const T3 &A3, + const Ts &... As) { + const T &Value = [](Ignore..., const T &V) -> const T & { + return V; + }(A3, As...); + return CaseLower(S1, Value).CasesLower(S2, A3, As...); + } + LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T& Value) const { if (Result) @@ -170,6 +176,14 @@ } LLVM_ATTRIBUTE_ALWAYS_INLINE + Expected Expect() const { + if (Result) + return *Result; + return make_error("Value did not occur in StringSwitch", + inconvertibleErrorCode()); + } + + LLVM_ATTRIBUTE_ALWAYS_INLINE operator R() const { assert(Result && "Fell off the end of a string-switch"); return *Result; Index: unittests/ADT/CMakeLists.txt =================================================================== --- unittests/ADT/CMakeLists.txt +++ unittests/ADT/CMakeLists.txt @@ -55,6 +55,7 @@ SparseSetTest.cpp StringMapTest.cpp StringRefTest.cpp + StringSwitchTest.cpp TinyPtrVectorTest.cpp TripleTest.cpp TwineTest.cpp Index: unittests/ADT/StringSwitchTest.cpp =================================================================== --- /dev/null +++ unittests/ADT/StringSwitchTest.cpp @@ -0,0 +1,247 @@ +//===- llvm/unittest/ADT/StringSwitchTest.cpp - StringSwitch unit tests ---===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/StringSwitch.h" +#include "gtest/gtest.h" + +using namespace llvm; + +#define EXPECT_EXPECTED(Expected, Actual) \ + { \ + auto Result = (Actual); \ + auto E = Result.takeError(); \ + EXPECT_FALSE(static_cast(E)); \ + if (E) { \ + consumeError(std::move(E)); \ + } else { \ + EXPECT_EQ(Expected, *Result); \ + } \ + } + +#define EXPECT_UNEXPECTED(Exp) \ + { \ + auto E = Exp.takeError(); \ + EXPECT_TRUE(static_cast(E)); \ + if (E) { \ + consumeError(std::move(E)); \ + } \ + } + +TEST(StringSwitchTest, CaseSensitiveCase) { + auto Translate = [](StringRef S) { + return llvm::StringSwitch(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) + .Case("A", 10) + .Case("B", 11) + .Case("C", 12) + .Case("D", 13) + .Case("E", 14) + .Case("F", 15) + .Default(-1); + }; + EXPECT_EQ(1, Translate("1")); + EXPECT_EQ(2, Translate("2")); + EXPECT_EQ(11, Translate("B")); + EXPECT_EQ(-1, Translate("b")); + EXPECT_EQ(-1, Translate("")); + EXPECT_EQ(-1, Translate("Test")); +} + +TEST(StringSwitchTest, CaseInsensitiveCase) { + auto Translate = [](StringRef S) { + return llvm::StringSwitch(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, CaseSensitiveStartsWith) { + auto Translate = [](StringRef S) { + return llvm::StringSwitch>(S) + .StartsWith("add", [](int X, int Y) { return X + Y; }) + .StartsWith("sub", [](int X, int Y) { return X - Y; }) + .StartsWith("mul", [](int X, int Y) { return X * Y; }) + .StartsWith("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(0, Translate("nothing")(10, 5)); + EXPECT_EQ(0, Translate("ADDER")(10, 5)); +} + +TEST(StringSwitchTest, CaseInensitiveStartsWith) { + auto Translate = [](StringRef S) { + return llvm::StringSwitch>(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, CaseSensitiveEndsWith) { + enum class Suffix { Possible, PastTense, Process, InProgressAction, Unknown }; + + auto Translate = [](StringRef S) { + return llvm::StringSwitch(S) + .EndsWith("able", Suffix::Possible) + .EndsWith("ed", Suffix::PastTense) + .EndsWith("ation", Suffix::Process) + .EndsWith("ing", Suffix::InProgressAction) + .Default(Suffix::Unknown); + }; + + 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")); + EXPECT_EQ(Suffix::Unknown, Translate("OPTIMIZABLE")); +} + +TEST(StringSwitchTest, CaseInsensitiveEndsWith) { + enum class Suffix { Possible, PastTense, Process, InProgressAction, Unknown }; + + auto Translate = [](StringRef S) { + return llvm::StringSwitch(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, CaseSensitiveCases) { + enum class OSType { Windows, Linux, Unknown }; + + auto Translate = [](StringRef S) { + return llvm::StringSwitch(S) + .Cases("windows", "win32", "winnt", OSType::Windows) + .Cases("linux", "unix", "*nix", "posix", OSType::Linux) + .Default(OSType::Unknown); + }; + + EXPECT_EQ(OSType::Windows, Translate("windows")); + 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::Unknown, Translate("Windows")); + EXPECT_EQ(OSType::Unknown, Translate("")); +} + +TEST(StringSwitchTest, CaseInensitiveCases) { + enum class OSType { Windows, Linux, Unknown }; + + auto Translate = [](StringRef S) { + return llvm::StringSwitch(S) + .CasesLower("windows", "win32", "winnt", OSType::Windows) + .CasesLower("linux", "unix", "*nix", "posix", OSType::Linux) + .Default(OSType::Unknown); + }; + + EXPECT_EQ(OSType::Windows, Translate("windows")); + 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("Windows")); + EXPECT_EQ(OSType::Linux, Translate("Linux")); + + EXPECT_EQ(OSType::Unknown, Translate("")); +} + +TEST(StringSwitchTest, Expected) { + auto Translate = [](StringRef S) { + return llvm::StringSwitch(S) + .Cases("true", "on", "yes", "1", "totally!", true) + .Cases("false", "off", "no", "0", "wut?", false) + .Expect(); + }; + + EXPECT_EXPECTED(true, Translate("true")); + EXPECT_EXPECTED(true, Translate("on")); + EXPECT_EXPECTED(true, Translate("yes")); + EXPECT_EXPECTED(true, Translate("1")); + EXPECT_EXPECTED(true, Translate("totally!")); + + EXPECT_EXPECTED(false, Translate("false")); + EXPECT_EXPECTED(false, Translate("off")); + EXPECT_EXPECTED(false, Translate("no")); + EXPECT_EXPECTED(false, Translate("0")); + EXPECT_EXPECTED(false, Translate("wut?")); + + EXPECT_UNEXPECTED(Translate("")); + EXPECT_UNEXPECTED(Translate("invalid")); +}