Index: include/llvm/ADT/StringSwitch.h =================================================================== --- include/llvm/ADT/StringSwitch.h +++ include/llvm/ADT/StringSwitch.h @@ -13,6 +13,7 @@ #ifndef LLVM_ADT_STRINGSWITCH_H #define LLVM_ADT_STRINGSWITCH_H +#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Compiler.h" #include @@ -68,6 +69,7 @@ ~StringSwitch() = default; + // Case-sensitive case matchers template LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch& Case(const char (&S)[N], const T& Value) { @@ -182,14 +184,75 @@ return Case(S0, Value).Cases(S1, S2, S3, S4, S5, S6, S7, S8, S9, Value); } + // 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 &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 &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 (&S0)[N0], const char (&S1)[N1], const T &Value) { + return CaseLower(S0, Value).CaseLower(S1, Value); + } + + template + 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 + 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 + 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; } LLVM_ATTRIBUTE_ALWAYS_INLINE + Optional OrNone() const { + if (Result) + return *Result; + return None; + } + + 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 @@ -56,6 +56,7 @@ StringExtrasTest.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,226 @@ +//===- 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; + +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) + .OrNone(); + }; + + EXPECT_EQ(true, Translate("true")); + EXPECT_EQ(true, Translate("on")); + EXPECT_EQ(true, Translate("yes")); + EXPECT_EQ(true, Translate("1")); + EXPECT_EQ(true, Translate("totally!")); + + EXPECT_EQ(false, Translate("false")); + EXPECT_EQ(false, Translate("off")); + EXPECT_EQ(false, Translate("no")); + EXPECT_EQ(false, Translate("0")); + EXPECT_EQ(false, Translate("wut?")); + + EXPECT_EQ(None, Translate("")); + EXPECT_EQ(None, Translate("invalid")); +}