1 //===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===/
9 // This file implements the StringSwitch template, which mimics a switch()
10 // statement whose cases are string literals.
12 //===----------------------------------------------------------------------===/
13 #ifndef LLVM_ADT_STRINGSWITCH_H
14 #define LLVM_ADT_STRINGSWITCH_H
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Compiler.h"
23 /// \brief A switch()-like statement whose cases are string literals.
25 /// The StringSwitch class is a simple form of a switch() statement that
26 /// determines whether the given string matches one of the given string
27 /// literals. The template type parameter \p T is the type of the value that
28 /// will be returned from the string-switch expression. For example,
29 /// the following code switches on the name of a color in \c argv[i]:
32 /// Color color = StringSwitch<Color>(argv[i])
34 /// .Case("orange", Orange)
35 /// .Case("yellow", Yellow)
36 /// .Case("green", Green)
37 /// .Case("blue", Blue)
38 /// .Case("indigo", Indigo)
39 /// .Cases("violet", "purple", Violet)
40 /// .Default(UnknownColor);
42 template<typename T, typename R = T>
44 /// \brief The string we are matching.
47 /// \brief The pointer to the result of this switch statement, once known,
52 LLVM_ATTRIBUTE_ALWAYS_INLINE
53 explicit StringSwitch(StringRef S)
54 : Str(S), Result(nullptr) { }
57 LLVM_ATTRIBUTE_ALWAYS_INLINE
58 StringSwitch& Case(const char (&S)[N], const T& Value) {
59 if (!Result && N-1 == Str.size() &&
60 (std::memcmp(S, Str.data(), N-1) == 0)) {
68 LLVM_ATTRIBUTE_ALWAYS_INLINE
69 StringSwitch& EndsWith(const char (&S)[N], const T &Value) {
70 if (!Result && Str.size() >= N-1 &&
71 std::memcmp(S, Str.data() + Str.size() + 1 - N, N-1) == 0) {
79 LLVM_ATTRIBUTE_ALWAYS_INLINE
80 StringSwitch& StartsWith(const char (&S)[N], const T &Value) {
81 if (!Result && Str.size() >= N-1 &&
82 std::memcmp(S, Str.data(), N-1) == 0) {
89 template<unsigned N0, unsigned N1>
90 LLVM_ATTRIBUTE_ALWAYS_INLINE
91 StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1],
94 (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) ||
95 (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0))) {
102 template<unsigned N0, unsigned N1, unsigned N2>
103 LLVM_ATTRIBUTE_ALWAYS_INLINE
104 StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1],
105 const char (&S2)[N2], const T& Value) {
107 (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) ||
108 (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0) ||
109 (N2-1 == Str.size() && std::memcmp(S2, Str.data(), N2-1) == 0))) {
116 template<unsigned N0, unsigned N1, unsigned N2, unsigned N3>
117 LLVM_ATTRIBUTE_ALWAYS_INLINE
118 StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1],
119 const char (&S2)[N2], const char (&S3)[N3],
122 (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) ||
123 (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0) ||
124 (N2-1 == Str.size() && std::memcmp(S2, Str.data(), N2-1) == 0) ||
125 (N3-1 == Str.size() && std::memcmp(S3, Str.data(), N3-1) == 0))) {
132 template<unsigned N0, unsigned N1, unsigned N2, unsigned N3, unsigned N4>
133 LLVM_ATTRIBUTE_ALWAYS_INLINE
134 StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1],
135 const char (&S2)[N2], const char (&S3)[N3],
136 const char (&S4)[N4], const T& Value) {
138 (N0-1 == Str.size() && std::memcmp(S0, Str.data(), N0-1) == 0) ||
139 (N1-1 == Str.size() && std::memcmp(S1, Str.data(), N1-1) == 0) ||
140 (N2-1 == Str.size() && std::memcmp(S2, Str.data(), N2-1) == 0) ||
141 (N3-1 == Str.size() && std::memcmp(S3, Str.data(), N3-1) == 0) ||
142 (N4-1 == Str.size() && std::memcmp(S4, Str.data(), N4-1) == 0))) {
149 LLVM_ATTRIBUTE_ALWAYS_INLINE
150 R Default(const T& Value) const {
157 LLVM_ATTRIBUTE_ALWAYS_INLINE
159 assert(Result && "Fell off the end of a string-switch");
164 } // end namespace llvm
166 #endif // LLVM_ADT_STRINGSWITCH_H