Переглянути джерело

Modernize: Prefer .substr in place of .c_str() + int

A lot of temporary/local strings were created out of C-strings
substr can utilize current string size, so in theory be a little
more efficient.
Artur Ryt 6 роки тому
батько
коміт
2d66567dca

+ 1 - 1
Source/CPack/cpack.cxx

@@ -82,7 +82,7 @@ int cpackDefinitionArgument(const char* argument, const char* cValue,
     return 0;
   }
   std::string key = value.substr(0, pos);
-  value = value.c_str() + pos + 1;
+  value = value.substr(pos + 1);
   def->Map[key] = value;
   cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG,
               "Set CPack variable: " << key << " to \"" << value << "\""

+ 2 - 2
Source/CTest/cmCTestBuildHandler.cxx

@@ -387,7 +387,7 @@ int cmCTestBuildHandler::ProcessHandler()
     std::string srcdirrep;
     for (cc = srcdir.size() - 2; cc > 0; cc--) {
       if (srcdir[cc] == '/') {
-        srcdirrep = srcdir.c_str() + cc;
+        srcdirrep = srcdir.substr(cc);
         srcdirrep = "/..." + srcdirrep;
         srcdir = srcdir.substr(0, cc + 1);
         break;
@@ -401,7 +401,7 @@ int cmCTestBuildHandler::ProcessHandler()
     std::string bindirrep;
     for (cc = bindir.size() - 2; cc > 0; cc--) {
       if (bindir[cc] == '/') {
-        bindirrep = bindir.c_str() + cc;
+        bindirrep = bindir.substr(cc);
         bindirrep = "/..." + bindirrep;
         bindir = bindir.substr(0, cc + 1);
         break;

+ 1 - 1
Source/CTest/cmCTestGIT.cxx

@@ -547,7 +547,7 @@ private:
   {
     // Look for header fields that we need.
     if (cmHasLiteralPrefix(this->Line, "commit ")) {
-      this->Rev.Rev = this->Line.c_str() + 7;
+      this->Rev.Rev = this->Line.substr(7);
     } else if (cmHasLiteralPrefix(this->Line, "author ")) {
       Person author;
       this->ParsePerson(this->Line.c_str() + 7, author);

+ 1 - 1
Source/CTest/cmCTestLaunch.cxx

@@ -308,7 +308,7 @@ void cmCTestLaunch::LoadLabels()
     if (line[0] == ' ') {
       // Label lines appear indented by one space.
       if (inTarget || inSource) {
-        this->Labels.insert(line.c_str() + 1);
+        this->Labels.insert(line.substr(1));
       }
     } else if (!this->OptionSource.empty() && !inSource) {
       // Non-indented lines specify a source file name.  The first one

+ 2 - 2
Source/CTest/cmCTestSVN.cxx

@@ -515,7 +515,7 @@ private:
     if (path.size() > this->SVN->SourceDirectory.size() &&
         strncmp(path.c_str(), this->SVN->SourceDirectory.c_str(),
                 this->SVN->SourceDirectory.size()) == 0) {
-      local_path = path.c_str() + this->SVN->SourceDirectory.size() + 1;
+      local_path = path.substr(this->SVN->SourceDirectory.size() + 1);
     } else {
       local_path = path;
     }
@@ -554,7 +554,7 @@ std::string cmCTestSVN::SVNInfo::BuildLocalPath(std::string const& path) const
   // Add path with base prefix removed
   if (path.size() > this->Base.size() &&
       strncmp(path.c_str(), this->Base.c_str(), this->Base.size()) == 0) {
-    local_path += (path.c_str() + this->Base.size());
+    local_path += path.substr(this->Base.size());
   } else {
     local_path += path;
   }

+ 2 - 2
Source/CTest/cmCTestTestHandler.cxx

@@ -2265,8 +2265,8 @@ bool cmCTestTestHandler::SetTestsProperties(
             size_t pos = val.find_first_of('=');
             if (pos != std::string::npos) {
               std::string mKey = val.substr(0, pos);
-              const char* mVal = val.c_str() + pos + 1;
-              rt.Measurements[mKey] = mVal;
+              std::string mVal = val.substr(pos + 1);
+              rt.Measurements[mKey] = std::move(mVal);
             } else {
               rt.Measurements[val] = "1";
             }

+ 1 - 1
Source/cmDependsFortran.cxx

@@ -295,7 +295,7 @@ void cmDependsFortran::MatchRemoteModules(std::istream& fin,
           // They do not include the ".mod" extension.
           mod += ".mod";
         }
-        this->ConsiderModule(mod.c_str() + 1, stampDir);
+        this->ConsiderModule(mod.substr(1), stampDir);
       }
     } else if (line == "provides") {
       doing_provides = true;

+ 5 - 8
Source/cmDependsJavaParserHelper.cxx

@@ -5,6 +5,7 @@
 #include "cmDependsJavaLexer.h"
 #include "cmSystemTools.h"
 
+#include "cm_string_view.hxx"
 #include "cmsys/FStream.hxx"
 #include <iostream>
 #include <stdio.h>
@@ -298,14 +299,10 @@ void cmDependsJavaParserHelper::Error(const char* str)
   unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
   fprintf(stderr, "JPError: %s (%lu / Line: %d)\n", str, pos,
           this->CurrentLine);
-  int cc;
-  std::cerr << "String: [";
-  for (cc = 0;
-       cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
-       cc++) {
-    std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
-  }
-  std::cerr << "]" << std::endl;
+  std::cerr << "String: ["
+            << cm::string_view{ this->InputBuffer }.substr(
+                 this->InputBufferPos, 30)
+            << "]" << std::endl;
 }
 
 void cmDependsJavaParserHelper::UpdateCombine(const char* str1,

+ 1 - 1
Source/cmFileInstaller.cxx

@@ -319,7 +319,7 @@ bool cmFileInstaller::HandleInstallDestination()
         return false;
       }
     }
-    destination = sdestdir + (destination.c_str() + skip);
+    destination = sdestdir + destination.substr(skip);
     this->DestDirLength = int(sdestdir.size());
   }
 

+ 1 - 1
Source/cmLocalGenerator.cxx

@@ -2266,7 +2266,7 @@ void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
       def += define.substr(0, eq);
       if (eq != std::string::npos) {
         def += "=";
-        def += this->EscapeForShell(define.c_str() + eq + 1, true);
+        def += this->EscapeForShell(define.substr(eq + 1), true);
       }
     }
     definesString += itemSeparator;

+ 1 - 1
Source/cmTargetPropertyComputer.h

@@ -81,7 +81,7 @@ private:
                                           context)) {
           return nullptr;
         }
-        const char* configName = prop.c_str() + 9;
+        std::string configName = prop.substr(9);
         return ComputeLocation(tgt, configName);
       }
 

+ 1 - 1
Source/cmcmd.cxx

@@ -620,7 +620,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
         std::string const& a = *ai;
         if (cmHasLiteralPrefix(a, "--unset=")) {
           // Unset environment variable.
-          cmSystemTools::UnPutEnv(a.c_str() + 8);
+          cmSystemTools::UnPutEnv(a.substr(8));
         } else if (!a.empty() && a[0] == '-') {
           // Environment variable and command names cannot start in '-',
           // so this must be an unknown option.