llvm::StringSwitch

A switch()-like statement whose cases are string literals.

Synopsis

Declared in <llvm/ADT/StringSwitch.h>

template<
    typename T,
    typename R = T>
class StringSwitch;

Description

The StringSwitch class is a simple form of a switch() statement that determines whether the given string matches one of the given string literals. The template type parameter T is the type of the value that will be returned from the string-switch expression. For example, the following code switches on the name of a color in argv[i]


Color color = StringSwitch<Color>(argv[i])
  .Case("red", Red)
  .Case("orange", Orange)
  .Case("yellow", Yellow)
  .Case("green", Green)
  .Case("blue", Blue)
  .Case("indigo", Indigo)
  .Cases({"violet", "purple"}, Violet)
  .Default(UnknownColor);

When multiple matches are found, the value of the first match is returned.

Member Functions

NameDescription
StringSwitch [constructor]Constructors
operator= Assignment operators
Case Match if the subject equals S (case-sensitive) and bind Value.
CaseLower Match if the subject equals S (case-insensitive) and bind Value.
Cases Match if the subject equals any of CaseStrings and bind Value.
CasesLower Match if the subject equals any of CaseStrings case-insensitively.
Default Return the matched value, or Value if no case matched.
DefaultUnreachable Declare default as unreachable, making sure that all cases were handled.
EndsWith Match if the subject ends with S (case-sensitive) and bind Value.
EndsWithLower Match if the subject ends with S (case-insensitive) and bind Value.
Predicate Match if predicate Pred returns true for the subject and bind Value.
StartsWith Match if the subject starts with S (case-sensitive) and bind Value.
StartsWithLower Match if the subject starts with S (case-insensitive) and bind Value.
operator R Convert to R by returning the match, or abort if none matched.