[ADT] Add a single-character version of the small vector split routine
[oota-llvm.git] / include / llvm / ADT / StringSwitch.h
index 7dd5647df6051b59c264009ede855b4c8aa504a3..0393a0c373efc5f2becc640cef6b01c4bf368184 100644 (file)
@@ -48,8 +48,8 @@ class StringSwitch {
   const T *Result;
 
 public:
-  explicit StringSwitch(StringRef Str)
-  : Str(Str), Result(0) { }
+  explicit StringSwitch(StringRef S)
+  : Str(S), Result(nullptr) { }
 
   template<unsigned N>
   StringSwitch& Case(const char (&S)[N], const T& Value) {
@@ -61,6 +61,26 @@ public:
     return *this;
   }
 
+  template<unsigned N>
+  StringSwitch& EndsWith(const char (&S)[N], const T &Value) {
+    if (!Result && Str.size() >= N-1 &&
+        std::memcmp(S, Str.data() + Str.size() + 1 - N, N-1) == 0) {
+      Result = &Value;
+    }
+
+    return *this;
+  }
+
+  template<unsigned N>
+  StringSwitch& StartsWith(const char (&S)[N], const T &Value) {
+    if (!Result && Str.size() >= N-1 &&
+        std::memcmp(S, Str.data(), N-1) == 0) {
+      Result = &Value;
+    }
+
+    return *this;
+  }
+
   template<unsigned N0, unsigned N1>
   StringSwitch& Cases(const char (&S0)[N0], const char (&S1)[N1],
                       const T& Value) {