瀏覽代碼

stringapi: Take strings in escaping functions

Ben Boeckel 11 年之前
父節點
當前提交
215b1addf0

+ 28 - 26
Source/cmLocalGenerator.cxx

@@ -2358,7 +2358,7 @@ void cmLocalGenerator::AppendFlags(std::string& flags,
 
 //----------------------------------------------------------------------------
 void cmLocalGenerator::AppendFlagEscape(std::string& flags,
-                                        const char* rawFlag)
+                                        const std::string& rawFlag)
 {
   this->AppendFlags(flags, this->EscapeForShell(rawFlag).c_str());
 }
@@ -3187,7 +3187,7 @@ cmLocalGenerator
 }
 
 //----------------------------------------------------------------------------
-std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
+std::string cmLocalGenerator::EscapeForShellOldStyle(const std::string& str)
 {
   std::string result;
 #if defined(_WIN32) && !defined(__CYGWIN__)
@@ -3203,7 +3203,7 @@ std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
     }
   return str;
 #else
-  for(const char* ch = str; *ch != '\0'; ++ch)
+  for(const char* ch = str.c_str(); *ch != '\0'; ++ch)
     {
     if(*ch == ' ')
       {
@@ -3216,28 +3216,30 @@ std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
 }
 
 //----------------------------------------------------------------------------
-static bool cmLocalGeneratorIsShellOperator(const char* str)
-{
-  if(strcmp(str, "<") == 0 ||
-     strcmp(str, ">") == 0 ||
-     strcmp(str, "<<") == 0 ||
-     strcmp(str, ">>") == 0 ||
-     strcmp(str, "|") == 0 ||
-     strcmp(str, "||") == 0 ||
-     strcmp(str, "&&") == 0 ||
-     strcmp(str, "&>") == 0 ||
-     strcmp(str, "1>") == 0 ||
-     strcmp(str, "2>") == 0 ||
-     strcmp(str, "2>&1") == 0 ||
-     strcmp(str, "1>&2") == 0)
+static bool cmLocalGeneratorIsShellOperator(const std::string& str)
+{
+  static std::set<std::string> shellOperators;
+  if(shellOperators.empty())
     {
-    return true;
+    shellOperators.insert("<");
+    shellOperators.insert(">");
+    shellOperators.insert("<<");
+    shellOperators.insert(">>");
+    shellOperators.insert("|");
+    shellOperators.insert("||");
+    shellOperators.insert("&&");
+    shellOperators.insert("&>");
+    shellOperators.insert("1>");
+    shellOperators.insert("2>");
+    shellOperators.insert("2>&1");
+    shellOperators.insert("1>&2");
     }
-  return false;
+  return shellOperators.count(str) > 0;
 }
 
 //----------------------------------------------------------------------------
-std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
+std::string cmLocalGenerator::EscapeForShell(const std::string& str,
+                                             bool makeVars,
                                              bool forEcho)
 {
   // Do not escape shell operators.
@@ -3279,28 +3281,28 @@ std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
 
   // Compute the buffer size needed.
   int size = (this->WindowsShell ?
-              cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
-              cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
+              cmsysSystem_Shell_GetArgumentSizeForWindows(str.c_str(), flags) :
+              cmsysSystem_Shell_GetArgumentSizeForUnix(str.c_str(), flags));
 
   // Compute the shell argument itself.
   std::vector<char> arg(size);
   if(this->WindowsShell)
     {
-    cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
+    cmsysSystem_Shell_GetArgumentForWindows(str.c_str(), &arg[0], flags);
     }
   else
     {
-    cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
+    cmsysSystem_Shell_GetArgumentForUnix(str.c_str(), &arg[0], flags);
     }
   return std::string(&arg[0]);
 }
 
 //----------------------------------------------------------------------------
-std::string cmLocalGenerator::EscapeForCMake(const char* str)
+std::string cmLocalGenerator::EscapeForCMake(const std::string& str)
 {
   // Always double-quote the argument to take care of most escapes.
   std::string result = "\"";
-  for(const char* c = str; *c; ++c)
+  for(const char* c = str.c_str(); *c; ++c)
     {
     if(*c == '"')
       {

+ 5 - 4
Source/cmLocalGenerator.h

@@ -150,7 +150,8 @@ public:
                               const char* config);
   ///! Append flags to a string.
   virtual void AppendFlags(std::string& flags, const char* newFlags);
-  virtual void AppendFlagEscape(std::string& flags, const char* rawFlag);
+  virtual void AppendFlagEscape(std::string& flags,
+                                const std::string& rawFlag);
   ///! Get the include flags for the current makefile and language
   std::string GetIncludeFlags(const std::vector<std::string> &includes,
                               cmGeneratorTarget* target,
@@ -285,14 +286,14 @@ public:
       system to replace make variable references.  Optionally adjust
       escapes for the special case of passing to the native echo
       command.  */
-  std::string EscapeForShell(const char* str, bool makeVars = false,
+  std::string EscapeForShell(const std::string& str, bool makeVars = false,
                              bool forEcho = false);
 
   /** Backwards-compatibility version of EscapeForShell.  */
-  std::string EscapeForShellOldStyle(const char* str);
+  std::string EscapeForShellOldStyle(const std::string& str);
 
   /** Escape the given string as an argument in a CMake script.  */
-  static std::string EscapeForCMake(const char* str);
+  static std::string EscapeForCMake(const std::string& str);
 
   enum FortranFormat
     {

+ 2 - 2
Source/cmLocalVisualStudio7Generator.cxx

@@ -2100,7 +2100,7 @@ void cmLocalVisualStudio7Generator::WriteVCProjFooter(std::ostream& fout,
        << "</VisualStudioProject>\n";
 }
 
-std::string cmLocalVisualStudio7GeneratorEscapeForXML(const char* s)
+std::string cmLocalVisualStudio7GeneratorEscapeForXML(const std::string& s)
 {
   std::string ret = s;
   cmSystemTools::ReplaceString(ret, "&", "&amp;");
@@ -2111,7 +2111,7 @@ std::string cmLocalVisualStudio7GeneratorEscapeForXML(const char* s)
   return ret;
 }
 
-std::string cmLocalVisualStudio7Generator::EscapeForXML(const char* s)
+std::string cmLocalVisualStudio7Generator::EscapeForXML(const std::string& s)
 {
   return cmLocalVisualStudio7GeneratorEscapeForXML(s);
 }

+ 1 - 1
Source/cmLocalVisualStudio7Generator.h

@@ -84,7 +84,7 @@ private:
   void WriteConfiguration(std::ostream& fout,
                           const char* configName,
                           const std::string& libName, cmTarget &tgt);
-  std::string EscapeForXML(const char* s);
+  std::string EscapeForXML(const std::string& s);
   std::string ConvertToXMLOutputPath(const char* path);
   std::string ConvertToXMLOutputPathSingle(const char* path);
   void OutputTargetRules(std::ostream& fout, const char* configName,

+ 1 - 1
Source/cmLocalXCodeGenerator.cxx

@@ -37,7 +37,7 @@ cmLocalXCodeGenerator::GetTargetDirectory(cmTarget const&) const
 
 //----------------------------------------------------------------------------
 void cmLocalXCodeGenerator::AppendFlagEscape(std::string& flags,
-                                             const char* rawFlag)
+                                             const std::string& rawFlag)
 {
   cmGlobalXCodeGenerator* gg =
     static_cast<cmGlobalXCodeGenerator*>(this->GlobalGenerator);

+ 2 - 1
Source/cmLocalXCodeGenerator.h

@@ -28,7 +28,8 @@ public:
 
   virtual ~cmLocalXCodeGenerator();
   virtual std::string GetTargetDirectory(cmTarget const& target) const;
-  virtual void AppendFlagEscape(std::string& flags, const char* rawFlag);
+  virtual void AppendFlagEscape(std::string& flags,
+                                const std::string& rawFlag);
   virtual void Generate();
   virtual void GenerateInstallRules();
 private:

+ 4 - 3
Source/cmSystemTools.cxx

@@ -179,10 +179,11 @@ void cmSystemTools::ExpandRegistryValues(std::string& source, KeyWOW64)
 }
 #endif
 
-std::string cmSystemTools::EscapeQuotes(const char* str)
+std::string cmSystemTools::EscapeQuotes(const std::string& str)
 {
-  std::string result = "";
-  for(const char* ch = str; *ch != '\0'; ++ch)
+  std::string result;
+  result.reserve(str.size());
+  for(const char* ch = str.c_str(); *ch != '\0'; ++ch)
     {
     if(*ch == '"')
       {

+ 1 - 1
Source/cmSystemTools.h

@@ -47,7 +47,7 @@ public:
                                    KeyWOW64 view = KeyWOW64_Default);
 
   ///! Escape quotes in a string.
-  static std::string EscapeQuotes(const char* str);
+  static std::string EscapeQuotes(const std::string& str);
 
   /** Map help document name to file name.  */
   static std::string HelpFileName(std::string);