1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 05:26:58 +08:00

replace std::string::substr() with operations that do not allocate memory

Modify the original string instead of creating a new copy with substr() when it
is not used for anything else afterwards.
This commit is contained in:
Rolf Eike Beer
2020-03-21 12:51:46 +01:00
parent 77616f4681
commit ef778d77e0
11 changed files with 31 additions and 23 deletions

View File

@@ -103,7 +103,7 @@ int cmCPackNSISGenerator::PackageFiles()
componentName = fileN.substr(0, slash); componentName = fileN.substr(0, slash);
// Strip off the component part of the path. // Strip off the component part of the path.
fileN = fileN.substr(slash + 1); fileN.erase(0, slash + 1);
} }
} }
std::replace(fileN.begin(), fileN.end(), '/', '\\'); std::replace(fileN.begin(), fileN.end(), '/', '\\');

View File

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

View File

@@ -6,6 +6,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <utility>
#include <vector> #include <vector>
#include "cmsys/FStream.hxx" #include "cmsys/FStream.hxx"
@@ -193,7 +194,8 @@ bool cmCTestGIT::UpdateByFetchAndReset()
if (line.find("\tnot-for-merge\t") == std::string::npos) { if (line.find("\tnot-for-merge\t") == std::string::npos) {
std::string::size_type pos = line.find('\t'); std::string::size_type pos = line.find('\t');
if (pos != std::string::npos) { if (pos != std::string::npos) {
sha1 = line.substr(0, pos); sha1 = std::move(line);
sha1.resize(pos);
} }
} }
} }

View File

@@ -284,12 +284,14 @@ int cmCTestScriptHandler::ReadInScript(const std::string& total_script_arg)
// if the argument has a , in it then it needs to be broken into the fist // if the argument has a , in it then it needs to be broken into the fist
// argument (which is the script) and the second argument which will be // argument (which is the script) and the second argument which will be
// passed into the scripts as S_ARG // passed into the scripts as S_ARG
std::string script = total_script_arg; std::string script;
std::string script_arg; std::string script_arg;
const std::string::size_type comma_pos = total_script_arg.find(','); const std::string::size_type comma_pos = total_script_arg.find(',');
if (comma_pos != std::string::npos) { if (comma_pos != std::string::npos) {
script = total_script_arg.substr(0, comma_pos); script = total_script_arg.substr(0, comma_pos);
script_arg = total_script_arg.substr(comma_pos + 1); script_arg = total_script_arg.substr(comma_pos + 1);
} else {
script = total_script_arg;
} }
// make sure the file exists // make sure the file exists
if (!cmSystemTools::FileExists(script)) { if (!cmSystemTools::FileExists(script)) {

View File

@@ -1893,7 +1893,8 @@ void cmCTestTestHandler::ExpandTestsToRunInformationForRerunFailed()
continue; continue;
} }
int val = atoi(line.substr(0, pos).c_str()); line.erase(pos);
int val = atoi(line.c_str());
this->TestsToRun.push_back(val); this->TestsToRun.push_back(val);
} }
ifs.close(); ifs.close();
@@ -2114,7 +2115,7 @@ void cmCTestTestHandler::CleanTestOutput(std::string& output, size_t length)
++current; ++current;
} }
} }
output = output.substr(0, current - begin); output.erase(current - begin);
// Append truncation message. // Append truncation message.
std::ostringstream msg; std::ostringstream msg;

View File

@@ -113,7 +113,8 @@ bool cmParseMumpsCoverage::LoadPackages(std::string const& d)
glob.FindFiles(pat); glob.FindFiles(pat);
for (std::string& file : glob.GetFiles()) { for (std::string& file : glob.GetFiles()) {
std::string name = cmSystemTools::GetFilenameName(file); std::string name = cmSystemTools::GetFilenameName(file);
this->RoutineToDirectory[name.substr(0, name.size() - 2)] = file; name.erase(name.size() - 2);
this->RoutineToDirectory[name] = file;
// initialize each file, this is left out until CDash is fixed // initialize each file, this is left out until CDash is fixed
// to handle large numbers of files // to handle large numbers of files
this->InitializeMumpsFile(file); this->InitializeMumpsFile(file);

View File

@@ -352,14 +352,14 @@ bool DumpFileWithLlvmNm(std::string const& nmPath, const char* filename,
line.c_str()); line.c_str());
return false; return false;
} }
const std::string sym = line.substr(0, sym_end);
const char sym_type = line[sym_end + 1]; const char sym_type = line[sym_end + 1];
line.resize(sym_end);
switch (sym_type) { switch (sym_type) {
case 'D': case 'D':
dataSymbols.insert(sym); dataSymbols.insert(line);
break; break;
case 'T': case 'T':
symbols.insert(sym); symbols.insert(line);
break; break;
} }
} }

View File

@@ -725,7 +725,7 @@ bool cmCTest::UpdateCTestConfiguration()
continue; continue;
} }
while (fin && (line.back() == '\\')) { while (fin && (line.back() == '\\')) {
line = line.substr(0, line.size() - 1); line.resize(line.size() - 1);
buffer[0] = 0; buffer[0] = 0;
fin.getline(buffer, 1023); fin.getline(buffer, 1023);
buffer[1023] = 0; buffer[1023] = 0;
@@ -2668,7 +2668,7 @@ std::string cmCTest::GetShortPathToFile(const char* cfname)
path = "./" + *res; path = "./" + *res;
if (path.back() == '/') { if (path.back() == '/') {
path = path.substr(0, path.size() - 1); path.resize(path.size() - 1);
} }
} }

View File

@@ -1798,10 +1798,10 @@ void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
if (use_build_rpath) { if (use_build_rpath) {
std::string d = ri; std::string d = ri;
if (!rootPath.empty() && cmHasPrefix(d, rootPath)) { if (!rootPath.empty() && cmHasPrefix(d, rootPath)) {
d = d.substr(rootPath.size()); d.erase(0, rootPath.size());
} else if (stagePath && *stagePath && cmHasPrefix(d, stagePath)) { } else if (stagePath && *stagePath && cmHasPrefix(d, stagePath)) {
std::string suffix = d.substr(strlen(stagePath)); d.erase(0, strlen(stagePath));
d = cmStrCat(installPrefix, '/', suffix); d = cmStrCat(installPrefix, '/', d);
cmSystemTools::ConvertToUnixSlashes(d); cmSystemTools::ConvertToUnixSlashes(d);
} else if (use_relative_build_rpath) { } else if (use_relative_build_rpath) {
// If expansion of the $ORIGIN token is supported and permitted per // If expansion of the $ORIGIN token is supported and permitted per
@@ -1829,10 +1829,10 @@ void cmComputeLinkInformation::GetRPath(std::vector<std::string>& runtimeDirs,
!cmSystemTools::IsSubDirectory(ri, topBinaryDir)) { !cmSystemTools::IsSubDirectory(ri, topBinaryDir)) {
std::string d = ri; std::string d = ri;
if (!rootPath.empty() && cmHasPrefix(d, rootPath)) { if (!rootPath.empty() && cmHasPrefix(d, rootPath)) {
d = d.substr(rootPath.size()); d.erase(0, rootPath.size());
} else if (stagePath && *stagePath && cmHasPrefix(d, stagePath)) { } else if (stagePath && *stagePath && cmHasPrefix(d, stagePath)) {
std::string suffix = d.substr(strlen(stagePath)); d.erase(0, strlen(stagePath));
d = cmStrCat(installPrefix, '/', suffix); d = cmStrCat(installPrefix, '/', d);
cmSystemTools::ConvertToUnixSlashes(d); cmSystemTools::ConvertToUnixSlashes(d);
} }
if (emitted.insert(d).second) { if (emitted.insert(d).second) {

View File

@@ -89,7 +89,8 @@ void cmRST::ProcessModule(std::istream& is)
this->ProcessLine(line); this->ProcessLine(line);
} else { } else {
if (line[0] != '#') { if (line[0] != '#') {
this->ProcessLine(line.substr(0, pos)); line.resize(pos);
this->ProcessLine(line);
} }
rst.clear(); rst.clear();
this->Reset(); this->Reset();
@@ -103,7 +104,8 @@ void cmRST::ProcessModule(std::istream& is)
continue; continue;
} }
if (cmHasLiteralPrefix(line, "# ")) { if (cmHasLiteralPrefix(line, "# ")) {
this->ProcessLine(line.substr(2)); line.erase(0, 2);
this->ProcessLine(line);
continue; continue;
} }
rst.clear(); rst.clear();

View File

@@ -32,14 +32,14 @@ void compare(const char* refName, const char* testName)
// trailing null to the string that we need to strip before testing for a // trailing null to the string that we need to strip before testing for a
// trailing space. // trailing space.
if (refLine.size() && refLine[refLine.size() - 1] == 0) { if (refLine.size() && refLine[refLine.size() - 1] == 0) {
refLine = refLine.substr(0, refLine.size() - 1); refLine.resize(refLine.size() - 1);
} }
if (testLine.size() && testLine[testLine.size() - 1] == 0) { if (testLine.size() && testLine[testLine.size() - 1] == 0) {
testLine = testLine.substr(0, testLine.size() - 1); testLine.resize(testLine.size() - 1);
} }
// The reference files never have trailing spaces: // The reference files never have trailing spaces:
if (testLine.size() && testLine[testLine.size() - 1] == ' ') { if (testLine.size() && testLine[testLine.size() - 1] == ' ') {
testLine = testLine.substr(0, testLine.size() - 1); testLine.resize(testLine.size() - 1);
} }
if (refLine != testLine) { if (refLine != testLine) {
std::cout << "Ref and test are not the same:\n Ref: \"" << refLine std::cout << "Ref and test are not the same:\n Ref: \"" << refLine