| 1 | //===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/ |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | //===----------------------------------------------------------------------===/ |
| 7 | /// |
| 8 | /// \file |
| 9 | /// This file implements the StringSwitch template, which mimics a switch() |
| 10 | /// statement whose cases are string literals. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===/ |
| 13 | #ifndef LLVM_ADT_STRINGSWITCH_H |
| 14 | #define LLVM_ADT_STRINGSWITCH_H |
| 15 | |
| 16 | #include "llvm/ADT/StringRef.h" |
| 17 | #include "llvm/Support/ErrorHandling.h" |
| 18 | #include <cassert> |
| 19 | #include <cstring> |
| 20 | #include <initializer_list> |
| 21 | #include <optional> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | /// A switch()-like statement whose cases are string literals. |
| 26 | /// |
| 27 | /// The StringSwitch class is a simple form of a switch() statement that |
| 28 | /// determines whether the given string matches one of the given string |
| 29 | /// literals. The template type parameter \p T is the type of the value that |
| 30 | /// will be returned from the string-switch expression. For example, |
| 31 | /// the following code switches on the name of a color in \c argv[i]: |
| 32 | /// |
| 33 | /// \code |
| 34 | /// Color color = StringSwitch<Color>(argv[i]) |
| 35 | /// .Case("red", Red) |
| 36 | /// .Case("orange", Orange) |
| 37 | /// .Case("yellow", Yellow) |
| 38 | /// .Case("green", Green) |
| 39 | /// .Case("blue", Blue) |
| 40 | /// .Case("indigo", Indigo) |
| 41 | /// .Cases({"violet", "purple"}, Violet) |
| 42 | /// .Default(UnknownColor); |
| 43 | /// \endcode |
| 44 | /// |
| 45 | /// When multiple matches are found, the value of the first match is returned. |
| 46 | template<typename T, typename R = T> |
| 47 | class StringSwitch { |
| 48 | /// The string we are matching. |
| 49 | const StringRef Str; |
| 50 | |
| 51 | /// The pointer to the result of this switch statement, once known, |
| 52 | /// null before that. |
| 53 | std::optional<T> Result; |
| 54 | |
| 55 | public: |
| 56 | explicit StringSwitch(StringRef S) |
| 57 | : Str(S), Result() { } |
| 58 | |
| 59 | StringSwitch(StringSwitch &&) = default; |
| 60 | |
| 61 | // StringSwitch is not copyable. |
| 62 | StringSwitch(const StringSwitch &) = delete; |
| 63 | |
| 64 | // StringSwitch is not assignable due to 'Str' being 'const'. |
| 65 | void operator=(const StringSwitch &) = delete; |
| 66 | void operator=(StringSwitch &&) = delete; |
| 67 | |
| 68 | // Case-sensitive case matchers. |
| 69 | StringSwitch &Case(StringLiteral S, T Value) { |
| 70 | CaseImpl(S, Value); |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | StringSwitch& EndsWith(StringLiteral S, T Value) { |
| 75 | if (!Result && Str.ends_with(Suffix: S)) { |
| 76 | Result = std::move(Value); |
| 77 | } |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | StringSwitch& StartsWith(StringLiteral S, T Value) { |
| 82 | if (!Result && Str.starts_with(Prefix: S)) { |
| 83 | Result = std::move(Value); |
| 84 | } |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | StringSwitch &Cases(std::initializer_list<StringLiteral> CaseStrings, |
| 89 | T Value) { |
| 90 | // Stop matching after the string is found. |
| 91 | for (StringLiteral S : CaseStrings) |
| 92 | if (CaseImpl(S, Value)) |
| 93 | break; |
| 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | // Case-insensitive case matchers. |
| 98 | StringSwitch &CaseLower(StringLiteral S, T Value) { |
| 99 | CaseLowerImpl(S, Value); |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | StringSwitch &EndsWithLower(StringLiteral S, T Value) { |
| 104 | if (!Result && Str.ends_with_insensitive(Suffix: S)) |
| 105 | Result = std::move(Value); |
| 106 | |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | StringSwitch &StartsWithLower(StringLiteral S, T Value) { |
| 111 | if (!Result && Str.starts_with_insensitive(Prefix: S)) |
| 112 | Result = std::move(Value); |
| 113 | |
| 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | StringSwitch &CasesLower(std::initializer_list<StringLiteral> CaseStrings, |
| 118 | T Value) { |
| 119 | // Stop matching after the string is found. |
| 120 | for (StringLiteral S : CaseStrings) |
| 121 | if (CaseLowerImpl(S, Value)) |
| 122 | break; |
| 123 | return *this; |
| 124 | } |
| 125 | |
| 126 | [[nodiscard]] R Default(T Value) { |
| 127 | if (Result) |
| 128 | return std::move(*Result); |
| 129 | return Value; |
| 130 | } |
| 131 | |
| 132 | /// Declare default as unreachable, making sure that all cases were handled. |
| 133 | [[nodiscard]] R DefaultUnreachable( |
| 134 | const char *Message = "Fell off the end of a string-switch" ) { |
| 135 | if (Result) |
| 136 | return std::move(*Result); |
| 137 | llvm_unreachable(Message); |
| 138 | } |
| 139 | |
| 140 | [[nodiscard]] operator R() { return DefaultUnreachable(); } |
| 141 | |
| 142 | private: |
| 143 | // Returns true when a match is found. If `Str` matches the `S` argument, |
| 144 | // stores the result. |
| 145 | bool CaseImpl(StringLiteral S, T &Value) { |
| 146 | if (Result) |
| 147 | return true; |
| 148 | |
| 149 | if (Str != S) |
| 150 | return false; |
| 151 | |
| 152 | Result = std::move(Value); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | // Returns true when a match is found. If `Str` matches the `S` argument |
| 157 | // (case-insensitive), stores the result. |
| 158 | bool CaseLowerImpl(StringLiteral S, T &Value) { |
| 159 | if (Result) |
| 160 | return true; |
| 161 | |
| 162 | if (!Str.equals_insensitive(RHS: S)) |
| 163 | return false; |
| 164 | |
| 165 | Result = std::move(Value); |
| 166 | return true; |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | } // end namespace llvm |
| 171 | |
| 172 | #endif // LLVM_ADT_STRINGSWITCH_H |
| 173 | |