speedup the common case where nothing needs to be quoted
authorChris Lattner <sabre@nondot.org>
Thu, 10 Nov 2005 21:47:01 +0000 (21:47 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 10 Nov 2005 21:47:01 +0000 (21:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24294 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/Mangler.cpp

index 44fe1003140c5ac3d8779bf6542efba3500f8337..264b84c6fe34bff1d8c7a9bbafc735354de3149c 100644 (file)
@@ -65,16 +65,29 @@ std::string Mangler::makeNameProper(const std::string &X, const char *Prefix) {
     if (*I >= '0' && *I <= '9')
       NeedsQuotes = true;
     
-    for (std::string::const_iterator E = X.end(); I != E; ++I)
+    // Do an initial scan of the string, checking to see if we need quotes or
+    // to escape a '"' or not.
+    if (!NeedsQuotes)
+      for (std::string::const_iterator E = X.end(); I != E; ++I)
+        if (!isCharAcceptable(*I)) {
+          NeedsQuotes = true;
+          break;
+        }
+    
+    // In the common case, we don't need quotes.  Handle this quickly.
+    if (!NeedsQuotes)
+      return Result + X;
+    
+    // Otherwise, construct the string the expensive way.
+    I = X.begin();
+    if (*I == 1) ++I;   // Skip the marker if present.
+    for (std::string::const_iterator E = X.end(); I != E; ++I) {
       if (*I == '"')
         Result += "_QQ_";
-      else {
-        if (!isCharAcceptable(*I))
-          NeedsQuotes = true;
+      else
         Result += *I;
-      }
-    if (NeedsQuotes)
-      Result = '"' + Result + '"';
+    }
+    Result = '"' + Result + '"';
   }
   return Result;
 }