Browse Source

replace "substr(0, xx) ==" with cmHasPrefix()

Rolf Eike Beer 5 years ago
parent
commit
bfb69f9543

+ 2 - 8
Source/CPack/IFW/cmCPackIFWGenerator.cxx

@@ -544,10 +544,7 @@ std::string cmCPackIFWGenerator::GetGroupPackageName(
   if (group->ParentGroup) {
   if (group->ParentGroup) {
     cmCPackIFWPackage* package = this->GetGroupPackage(group->ParentGroup);
     cmCPackIFWPackage* package = this->GetGroupPackage(group->ParentGroup);
     bool dot = !this->ResolveDuplicateNames;
     bool dot = !this->ResolveDuplicateNames;
-    if (dot && name.substr(0, package->Name.size()) == package->Name) {
-      dot = false;
-    }
-    if (dot) {
+    if (dot && !cmHasPrefix(name, package->Name)) {
       name = package->Name + "." + name;
       name = package->Name + "." + name;
     }
     }
   }
   }
@@ -576,10 +573,7 @@ std::string cmCPackIFWGenerator::GetComponentPackageName(
       return package->Name;
       return package->Name;
     }
     }
     bool dot = !this->ResolveDuplicateNames;
     bool dot = !this->ResolveDuplicateNames;
-    if (dot && name.substr(0, package->Name.size()) == package->Name) {
-      dot = false;
-    }
-    if (dot) {
+    if (dot && !cmHasPrefix(name, package->Name)) {
       name = package->Name + "." + name;
       name = package->Name + "." + name;
     }
     }
   }
   }

+ 1 - 2
Source/CPack/WiX/cmCPackWIXGenerator.cxx

@@ -350,8 +350,7 @@ void cmCPackWIXGenerator::CreateWiXPropertiesIncludeFile()
   std::vector<std::string> options = GetOptions();
   std::vector<std::string> options = GetOptions();
 
 
   for (std::string const& name : options) {
   for (std::string const& name : options) {
-    if (name.length() > prefix.length() &&
-        name.substr(0, prefix.length()) == prefix) {
+    if (cmHasPrefix(name, prefix)) {
       std::string id = name.substr(prefix.length());
       std::string id = name.substr(prefix.length());
       std::string value = GetOption(name.c_str());
       std::string value = GetOption(name.c_str());
 
 

+ 4 - 3
Source/CTest/cmCTestCoverageHandler.cxx

@@ -680,8 +680,9 @@ void cmCTestCoverageHandler::PopulateCustomVectors(cmMakefile* mf)
 //
 //
 #ifdef _WIN32
 #ifdef _WIN32
 #  define fnc(s) cmSystemTools::LowerCase(s)
 #  define fnc(s) cmSystemTools::LowerCase(s)
+#  define fnc_prefix(s, t) fnc(s.substr(0, t.size())) == fnc(t)
 #else
 #else
-#  define fnc(s) s
+#  define fnc_prefix(s, t) cmHasPrefix(s, t)
 #endif
 #endif
 
 
 bool IsFileInDir(const std::string& infile, const std::string& indir)
 bool IsFileInDir(const std::string& infile, const std::string& indir)
@@ -689,8 +690,8 @@ bool IsFileInDir(const std::string& infile, const std::string& indir)
   std::string file = cmSystemTools::CollapseFullPath(infile);
   std::string file = cmSystemTools::CollapseFullPath(infile);
   std::string dir = cmSystemTools::CollapseFullPath(indir);
   std::string dir = cmSystemTools::CollapseFullPath(indir);
 
 
-  return file.size() > dir.size() &&
-    fnc(file.substr(0, dir.size())) == fnc(dir) && file[dir.size()] == '/';
+  return file.size() > dir.size() && fnc_prefix(file, dir) &&
+    file[dir.size()] == '/';
 }
 }
 
 
 int cmCTestCoverageHandler::HandlePHPCoverage(
 int cmCTestCoverageHandler::HandlePHPCoverage(

+ 1 - 1
Source/CTest/cmParseCacheCoverage.cxx

@@ -155,7 +155,7 @@ bool cmParseCacheCoverage::ReadCMCovFile(const char* file)
     // if we have a routine name, check for end of routine
     // if we have a routine name, check for end of routine
     else {
     else {
       // Totals in arg 0 marks the end of a routine
       // Totals in arg 0 marks the end of a routine
-      if (separateLine[0].substr(0, 6) == "Totals") {
+      if (cmHasLiteralPrefix(separateLine[0], "Totals")) {
         routine.clear(); // at the end of this routine
         routine.clear(); // at the end of this routine
         filepath.clear();
         filepath.clear();
         continue; // move to next line
         continue; // move to next line

+ 2 - 2
Source/cmConditionEvaluator.cxx

@@ -494,12 +494,12 @@ bool cmConditionEvaluator::HandleLevel1(cmArgumentList& newArgs, std::string&,
       if (this->IsKeyword(keyDEFINED, *arg) && argP1 != newArgs.end()) {
       if (this->IsKeyword(keyDEFINED, *arg) && argP1 != newArgs.end()) {
         size_t argP1len = argP1->GetValue().size();
         size_t argP1len = argP1->GetValue().size();
         bool bdef = false;
         bool bdef = false;
-        if (argP1len > 4 && argP1->GetValue().substr(0, 4) == "ENV{" &&
+        if (argP1len > 4 && cmHasLiteralPrefix(argP1->GetValue(), "ENV{") &&
             argP1->GetValue().operator[](argP1len - 1) == '}') {
             argP1->GetValue().operator[](argP1len - 1) == '}') {
           std::string env = argP1->GetValue().substr(4, argP1len - 5);
           std::string env = argP1->GetValue().substr(4, argP1len - 5);
           bdef = cmSystemTools::HasEnv(env);
           bdef = cmSystemTools::HasEnv(env);
         } else if (argP1len > 6 &&
         } else if (argP1len > 6 &&
-                   argP1->GetValue().substr(0, 6) == "CACHE{" &&
+                   cmHasLiteralPrefix(argP1->GetValue(), "CACHE{") &&
                    argP1->GetValue().operator[](argP1len - 1) == '}') {
                    argP1->GetValue().operator[](argP1len - 1) == '}') {
           std::string cache = argP1->GetValue().substr(6, argP1len - 7);
           std::string cache = argP1->GetValue().substr(6, argP1len - 7);
           bdef =
           bdef =

+ 2 - 2
Source/cmExportBuildAndroidMKGenerator.cxx

@@ -118,13 +118,13 @@ void cmExportBuildAndroidMKGenerator::GenerateInterfaceProperties(
           } else {
           } else {
             bool relpath = false;
             bool relpath = false;
             if (type == cmExportBuildAndroidMKGenerator::INSTALL) {
             if (type == cmExportBuildAndroidMKGenerator::INSTALL) {
-              relpath = lib.substr(0, 3) == "../";
+              relpath = cmHasLiteralPrefix(lib, "../");
             }
             }
             // check for full path or if it already has a -l, or
             // check for full path or if it already has a -l, or
             // in the case of an install check for relative paths
             // in the case of an install check for relative paths
             // if it is full or a link library then use string directly
             // if it is full or a link library then use string directly
             if (cmSystemTools::FileIsFullPath(lib) ||
             if (cmSystemTools::FileIsFullPath(lib) ||
-                lib.substr(0, 2) == "-l" || relpath) {
+                cmHasLiteralPrefix(lib, "-l") || relpath) {
               ldlibs += " " + lib;
               ldlibs += " " + lib;
               // if it is not a path and does not have a -l then add -l
               // if it is not a path and does not have a -l then add -l
             } else if (!lib.empty()) {
             } else if (!lib.empty()) {

+ 1 - 1
Source/cmRST.cxx

@@ -102,7 +102,7 @@ void cmRST::ProcessModule(std::istream& is)
           this->ProcessLine("");
           this->ProcessLine("");
           continue;
           continue;
         }
         }
-        if (line.substr(0, 2) == "# ") {
+        if (cmHasLiteralPrefix(line, "# ")) {
           this->ProcessLine(line.substr(2));
           this->ProcessLine(line.substr(2));
           continue;
           continue;
         }
         }

+ 1 - 2
Source/cmSystemTools.cxx

@@ -1055,8 +1055,7 @@ bool cmSystemTools::SimpleGlob(const std::string& glob,
         if (type < 0 && !cmSystemTools::FileIsDirectory(fname)) {
         if (type < 0 && !cmSystemTools::FileIsDirectory(fname)) {
           continue;
           continue;
         }
         }
-        if (sfname.size() >= ppath.size() &&
-            sfname.substr(0, ppath.size()) == ppath) {
+        if (cmHasPrefix(sfname, ppath)) {
           files.push_back(fname);
           files.push_back(fname);
           res = true;
           res = true;
         }
         }

+ 2 - 3
Source/cmcmd.cxx

@@ -1054,8 +1054,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args)
         homeOutDir = args[5];
         homeOutDir = args[5];
         startOutDir = args[6];
         startOutDir = args[6];
         depInfo = args[7];
         depInfo = args[7];
-        if (args.size() >= 9 && args[8].length() >= 8 &&
-            args[8].substr(0, 8) == "--color=") {
+        if (args.size() >= 9 && cmHasLiteralPrefix(args[8], "--color=")) {
           // Enable or disable color based on the switch value.
           // Enable or disable color based on the switch value.
           color = (args[8].size() == 8 || cmIsOn(args[8].substr(8)));
           color = (args[8].size() == 8 || cmIsOn(args[8].substr(8)));
         }
         }
@@ -1304,7 +1303,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args)
         } else if (arg == "--debug") {
         } else if (arg == "--debug") {
           pipe.clear();
           pipe.clear();
           isDebug = true;
           isDebug = true;
-        } else if (arg.substr(0, pipePrefix.size()) == pipePrefix) {
+        } else if (cmHasPrefix(arg, pipePrefix)) {
           isDebug = false;
           isDebug = false;
           pipe = arg.substr(pipePrefix.size());
           pipe = arg.substr(pipePrefix.size());
           if (pipe.empty()) {
           if (pipe.empty()) {