LLVM 23.0.0git
StringSwitch.h
Go to the documentation of this file.
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"
18#include <cassert>
19#include <cstring>
20#include <initializer_list>
21#include <optional>
22
23namespace 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.
46template<typename T, typename R = T>
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
55public:
57 : Str(S), Result() { }
58
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.
70 CaseImpl(S, Value);
71 return *this;
72 }
73
75 if (!Result && Str.ends_with(S)) {
76 Result = std::move(Value);
77 }
78 return *this;
79 }
80
82 if (!Result && Str.starts_with(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.
99 CaseLowerImpl(S, Value);
100 return *this;
101 }
102
104 if (!Result && Str.ends_with_insensitive(S))
105 Result = std::move(Value);
106
107 return *this;
108 }
109
111 if (!Result && Str.starts_with_insensitive(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 // A StringSwitch case that is selected if the specified predicate
127 // returns true for the subject string.
129 if (!Result && Pred(Str))
130 Result = std::move(Value);
131 return *this;
132 }
133
134 [[nodiscard]] R Default(T Value) {
135 if (Result)
136 return std::move(*Result);
137 return Value;
138 }
139
140 /// Declare default as unreachable, making sure that all cases were handled.
141 [[nodiscard]] R DefaultUnreachable(
142 const char *Message = "Fell off the end of a string-switch") {
143 if (Result)
144 return std::move(*Result);
145 llvm_unreachable(Message);
146 }
147
148 [[nodiscard]] operator R() { return DefaultUnreachable(); }
149
150private:
151 // Returns true when a match is found. If `Str` matches the `S` argument,
152 // stores the result.
153 bool CaseImpl(StringLiteral S, T &Value) {
154 if (Result)
155 return true;
156
157 if (Str != S)
158 return false;
159
160 Result = std::move(Value);
161 return true;
162 }
163
164 // Returns true when a match is found. If `Str` matches the `S` argument
165 // (case-insensitive), stores the result.
166 bool CaseLowerImpl(StringLiteral S, T &Value) {
167 if (Result)
168 return true;
169
170 if (!Str.equals_insensitive(S))
171 return false;
172
173 Result = std::move(Value);
174 return true;
175 }
176};
177
178} // end namespace llvm
179
180#endif // LLVM_ADT_STRINGSWITCH_H
#define T
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:882
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
StringSwitch & EndsWithLower(StringLiteral S, T Value)
StringSwitch & StartsWithLower(StringLiteral S, T Value)
StringSwitch & CaseLower(StringLiteral S, T Value)
StringSwitch & Case(StringLiteral S, T Value)
void operator=(StringSwitch &&)=delete
void operator=(const StringSwitch &)=delete
R DefaultUnreachable(const char *Message="Fell off the end of a string-switch")
Declare default as unreachable, making sure that all cases were handled.
StringSwitch & Predicate(function_ref< bool(StringRef)> Pred, T Value)
StringSwitch & CasesLower(std::initializer_list< StringLiteral > CaseStrings, T Value)
StringSwitch & StartsWith(StringLiteral S, T Value)
StringSwitch(const StringSwitch &)=delete
StringSwitch & EndsWith(StringLiteral S, T Value)
StringSwitch(StringSwitch &&)=default
StringSwitch & Cases(std::initializer_list< StringLiteral > CaseStrings, T Value)
StringSwitch(StringRef S)
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.