RBIT Instruction only available for ARMv6t2 and above.
[oota-llvm.git] / lib / Support / Regex.cpp
index 3e112df6c77be6f082467f865e44d948344b247c..e8344ef74d9c9916ab097363a8ac8781b79073c4 100644 (file)
@@ -14,8 +14,8 @@
 #include "llvm/Support/Regex.h"
 #include "regex_impl.h"
 #include "llvm/ADT/SmallVector.h"
-#include "llvm/Support/ErrorHandling.h"
-#include "llvm/Support/raw_ostream.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
 #include <string>
 using namespace llvm;
 
@@ -33,15 +33,17 @@ Regex::Regex(StringRef regex, unsigned Flags) {
 }
 
 Regex::~Regex() {
-  llvm_regfree(preg);
-  delete preg;
+  if (preg) {
+    llvm_regfree(preg);
+    delete preg;
+  }
 }
 
 bool Regex::isValid(std::string &Error) {
   if (!error)
     return true;
   
-  size_t len = llvm_regerror(error, preg, NULL, 0);
+  size_t len = llvm_regerror(error, preg, nullptr, 0);
   
   Error.resize(len - 1);
   llvm_regerror(error, preg, &Error[0], len);
@@ -157,7 +159,7 @@ std::string Regex::sub(StringRef Repl, StringRef String,
           RefValue < Matches.size())
         Res += Matches[RefValue];
       else if (Error && Error->empty())
-        *Error = "invalid backreference string '" + Ref.str() + "'";
+        *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
       break;
     }
     }
@@ -169,37 +171,22 @@ std::string Regex::sub(StringRef Repl, StringRef String,
   return Res;
 }
 
+// These are the special characters matched in functions like "p_ere_exp".
+static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
+
 bool Regex::isLiteralERE(StringRef Str) {
   // Check for regex metacharacters.  This list was derived from our regex
   // implementation in regcomp.c and double checked against the POSIX extended
   // regular expression specification.
-  return Str.find_first_of("()^$|*+?.[]\\{}") == StringRef::npos;
+  return Str.find_first_of(RegexMetachars) == StringRef::npos;
 }
 
 std::string Regex::escape(StringRef String) {
   std::string RegexStr;
-
   for (unsigned i = 0, e = String.size(); i != e; ++i) {
-    switch (String[i]) {
-    // These are the special characters matched in "p_ere_exp".
-    case '(':
-    case ')':
-    case '^':
-    case '$':
-    case '|':
-    case '*':
-    case '+':
-    case '?':
-    case '.':
-    case '[':
-    case '\\':
-    case '{':
+    if (strchr(RegexMetachars, String[i]))
       RegexStr += '\\';
-      // FALL THROUGH.
-    default:
-      RegexStr += String[i];
-      break;
-    }
+    RegexStr += String[i];
   }
 
   return RegexStr;