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