StringSwitch.h 6.4 KB

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