From 5784747d1b0404a0c1cb0223b15b823476023fba Mon Sep 17 00:00:00 2001 From: Daniel Pfeifer Date: Tue, 24 May 2016 21:24:10 +0200 Subject: Improve string find: prefer character overloads. Apply fix-its from clang-tidy's performance-faster-string-find checker. Ignore findings in kwsys. --- Source/CPack/IFW/cmCPackIFWPackage.cxx | 6 +++--- Source/CPack/cpack.cxx | 2 +- Source/CTest/cmCTestBuildHandler.cxx | 2 +- Source/CTest/cmCTestCoverageHandler.cxx | 4 ++-- Source/CTest/cmCTestSubmitHandler.cxx | 4 ++-- Source/CTest/cmCTestTestHandler.cxx | 2 +- Source/CTest/cmParseDelphiCoverage.cxx | 2 +- Source/CursesDialog/cmCursesMainForm.cxx | 2 +- Source/cmCTest.cxx | 4 ++-- Source/cmDependsFortran.cxx | 2 +- Source/cmExportFileGenerator.cxx | 8 ++++---- Source/cmGeneratorExpression.cxx | 4 ++-- Source/cmGeneratorTarget.cxx | 2 +- Source/cmLocalGenerator.cxx | 8 ++++---- Source/cmLocalNinjaGenerator.cxx | 2 +- Source/cmLocalUnixMakefileGenerator3.cxx | 10 +++++----- Source/cmMakefileTargetGenerator.cxx | 6 +++--- Source/cmSystemTools.cxx | 2 +- Source/cmcmd.cxx | 2 +- 19 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Source/CPack/IFW/cmCPackIFWPackage.cxx b/Source/CPack/IFW/cmCPackIFWPackage.cxx index df612e5..ad153d1 100644 --- a/Source/CPack/IFW/cmCPackIFWPackage.cxx +++ b/Source/CPack/IFW/cmCPackIFWPackage.cxx @@ -56,13 +56,13 @@ cmCPackIFWPackage::DependenceStruct::DependenceStruct( } else if ((pos = dependence.find(">=")) != std::string::npos) { Compare.Type = CompareGreaterOrEqual; Compare.Value = dependence.substr(pos + 2); - } else if ((pos = dependence.find("<")) != std::string::npos) { + } else if ((pos = dependence.find('<')) != std::string::npos) { Compare.Type = CompareLess; Compare.Value = dependence.substr(pos + 1); - } else if ((pos = dependence.find("=")) != std::string::npos) { + } else if ((pos = dependence.find('=')) != std::string::npos) { Compare.Type = CompareEqual; Compare.Value = dependence.substr(pos + 1); - } else if ((pos = dependence.find(">")) != std::string::npos) { + } else if ((pos = dependence.find('>')) != std::string::npos) { Compare.Type = CompareGreater; Compare.Value = dependence.substr(pos + 1); } diff --git a/Source/CPack/cpack.cxx b/Source/CPack/cpack.cxx index e54be2c..10f60b2 100644 --- a/Source/CPack/cpack.cxx +++ b/Source/CPack/cpack.cxx @@ -68,7 +68,7 @@ int cpackDefinitionArgument(const char* argument, const char* cValue, (void)argument; cpackDefinitions* def = static_cast(call_data); std::string value = cValue; - size_t pos = value.find_first_of("="); + size_t pos = value.find_first_of('='); if (pos == std::string::npos) { cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR, "Please specify CPack definitions as: KEY=VALUE" << std::endl); diff --git a/Source/CTest/cmCTestBuildHandler.cxx b/Source/CTest/cmCTestBuildHandler.cxx index 20b0e9c..7141daf 100644 --- a/Source/CTest/cmCTestBuildHandler.cxx +++ b/Source/CTest/cmCTestBuildHandler.cxx @@ -617,7 +617,7 @@ void cmCTestBuildHandler::GenerateXMLLogScraped(cmXMLWriter& xml) cmSystemTools::ConvertToUnixSlashes(cm->SourceFile); if (cm->SourceFile.find("/.../") != cm->SourceFile.npos) { cmSystemTools::ReplaceString(cm->SourceFile, "/.../", ""); - std::string::size_type p = cm->SourceFile.find("/"); + std::string::size_type p = cm->SourceFile.find('/'); if (p != cm->SourceFile.npos) { cm->SourceFile = cm->SourceFile.substr(p + 1, cm->SourceFile.size() - p); diff --git a/Source/CTest/cmCTestCoverageHandler.cxx b/Source/CTest/cmCTestCoverageHandler.cxx index 550a774..51456ed 100644 --- a/Source/CTest/cmCTestCoverageHandler.cxx +++ b/Source/CTest/cmCTestCoverageHandler.cxx @@ -1238,7 +1238,7 @@ int cmCTestCoverageHandler::HandleGCovCoverage( // Initially all entries are -1 (not used). If we get coverage // information, increment it to 0 first. if (vec[lineIdx] < 0) { - if (cov > 0 || prefix.find("#") != prefix.npos) { + if (cov > 0 || prefix.find('#') != prefix.npos) { vec[lineIdx] = 0; } } @@ -1543,7 +1543,7 @@ int cmCTestCoverageHandler::HandleLCovCoverage( // Initially all entries are -1 (not used). If we get coverage // information, increment it to 0 first. if (vec[lineIdx] < 0) { - if (cov > 0 || prefix.find("#") != prefix.npos) { + if (cov > 0 || prefix.find('#') != prefix.npos) { vec[lineIdx] = 0; } } diff --git a/Source/CTest/cmCTestSubmitHandler.cxx b/Source/CTest/cmCTestSubmitHandler.cxx index bbc6b37..f373348 100644 --- a/Source/CTest/cmCTestSubmitHandler.cxx +++ b/Source/CTest/cmCTestSubmitHandler.cxx @@ -417,7 +417,7 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix, } } std::string upload_as = url + - ((url.find("?", 0) == std::string::npos) ? "?" : "&") + "FileName=" + + ((url.find('?') == std::string::npos) ? '?' : '&') + "FileName=" + ofile; upload_as += "&MD5="; @@ -706,7 +706,7 @@ bool cmCTestSubmitHandler::TriggerUsingHTTP(const std::set& files, } } std::string turl = url + - ((url.find("?", 0) == std::string::npos) ? "?" : "&") + "xmlfile=" + + ((url.find('?') == std::string::npos) ? '?' : '&') + "xmlfile=" + ofile; *this->LogFile << "Trigger url: " << turl << std::endl; cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx index 4935f11..20ef693 100644 --- a/Source/CTest/cmCTestTestHandler.cxx +++ b/Source/CTest/cmCTestTestHandler.cxx @@ -1869,7 +1869,7 @@ bool cmCTestTestHandler::SetTestsProperties( cmSystemTools::ExpandListArgument(val, rtit->Labels); } if (key == "MEASUREMENT") { - size_t pos = val.find_first_of("="); + size_t pos = val.find_first_of('='); if (pos != val.npos) { std::string mKey = val.substr(0, pos); const char* mVal = val.c_str() + pos + 1; diff --git a/Source/CTest/cmParseDelphiCoverage.cxx b/Source/CTest/cmParseDelphiCoverage.cxx index d20b16a..2e06078 100644 --- a/Source/CTest/cmParseDelphiCoverage.cxx +++ b/Source/CTest/cmParseDelphiCoverage.cxx @@ -171,7 +171,7 @@ public: } lastoffset = line.find("class="); - endcovpos = line.find(">", lastoffset); + endcovpos = line.find('>', lastoffset); lineresult = line.substr(lastoffset + 7, (endcovpos - 8) - lastoffset); if (lineresult == "covered") { diff --git a/Source/CursesDialog/cmCursesMainForm.cxx b/Source/CursesDialog/cmCursesMainForm.cxx index 7368560..b740eb0 100644 --- a/Source/CursesDialog/cmCursesMainForm.cxx +++ b/Source/CursesDialog/cmCursesMainForm.cxx @@ -729,7 +729,7 @@ void cmCursesMainForm::FillCacheManagerFromUI() void cmCursesMainForm::FixValue(cmState::CacheEntryType type, const std::string& in, std::string& out) const { - out = in.substr(0, in.find_last_not_of(" ") + 1); + out = in.substr(0, in.find_last_not_of(' ') + 1); if (type == cmState::PATH || type == cmState::FILEPATH) { cmSystemTools::ConvertToUnixSlashes(out); } diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index 7e5d966..64e8078 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -721,7 +721,7 @@ bool cmCTest::UpdateCTestConfiguration() if (line[0] == '#') { continue; } - std::string::size_type cpos = line.find_first_of(":"); + std::string::size_type cpos = line.find_first_of(':'); if (cpos == line.npos) { continue; } @@ -2480,7 +2480,7 @@ void cmCTest::AddSubmitFile(Part part, const char* name) void cmCTest::AddCTestConfigurationOverwrite(const std::string& overStr) { - size_t epos = overStr.find("="); + size_t epos = overStr.find('='); if (epos == overStr.npos) { cmCTestLog(this, ERROR_MESSAGE, "CTest configuration overwrite specified in the wrong format." diff --git a/Source/cmDependsFortran.cxx b/Source/cmDependsFortran.cxx index b8af178..c595ada 100644 --- a/Source/cmDependsFortran.cxx +++ b/Source/cmDependsFortran.cxx @@ -77,7 +77,7 @@ cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg) for (std::vector::const_iterator it = definitions.begin(); it != definitions.end(); ++it) { std::string def = *it; - std::string::size_type assignment = def.find("="); + std::string::size_type assignment = def.find('='); if (assignment != std::string::npos) { def = it->substr(0, assignment); } diff --git a/Source/cmExportFileGenerator.cxx b/Source/cmExportFileGenerator.cxx index 2d645da..736c7da 100644 --- a/Source/cmExportFileGenerator.cxx +++ b/Source/cmExportFileGenerator.cxx @@ -608,8 +608,8 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( while ((pos = input.find("$", nameStartPos); - std::string::size_type commaPos = input.find(",", nameStartPos); + std::string::size_type closePos = input.find('>', nameStartPos); + std::string::size_type commaPos = input.find(',', nameStartPos); std::string::size_type nextOpenPos = input.find("$<", nameStartPos); if (commaPos == input.npos // Implied 'this' target || closePos == input.npos // Imcomplete expression. @@ -634,7 +634,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( lastPos = pos; while ((pos = input.find("$", nameStartPos); + std::string::size_type endPos = input.find('>', nameStartPos); if (endPos == input.npos) { errorString = "$ expression incomplete"; break; @@ -659,7 +659,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression( while (errorString.empty() && (pos = input.find("$", nameStartPos); + std::string::size_type endPos = input.find('>', nameStartPos); if (endPos == input.npos) { errorString = "$ expression incomplete"; break; diff --git a/Source/cmGeneratorExpression.cxx b/Source/cmGeneratorExpression.cxx index 002f9ec..53243b8 100644 --- a/Source/cmGeneratorExpression.cxx +++ b/Source/cmGeneratorExpression.cxx @@ -300,7 +300,7 @@ void cmGeneratorExpression::Split(const std::string& input, std::string part = input.substr(lastPos, pos - lastPos); std::string preGenex; if (!part.empty()) { - std::string::size_type startPos = input.rfind(";", pos); + std::string::size_type startPos = input.rfind(';', pos); if (startPos == std::string::npos) { preGenex = part; part = ""; @@ -364,7 +364,7 @@ std::string::size_type cmGeneratorExpression::Find(const std::string& input) { const std::string::size_type openpos = input.find("$<"); if (openpos != std::string::npos && - input.find(">", openpos) != std::string::npos) { + input.find('>', openpos) != std::string::npos) { return openpos; } return std::string::npos; diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 31fff9f..f30bb8b 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -145,7 +145,7 @@ struct DoAccept // where the user supplied the file name and Visual Studio // appended the suffix. std::string resx = f->GetFullPath(); - std::string hFileName = resx.substr(0, resx.find_last_of(".")) + ".h"; + std::string hFileName = resx.substr(0, resx.find_last_of('.')) + ".h"; data.ExpectedResxHeaders.insert(hFileName); data.ResxSources.push_back(f); } diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 4a52f51..2648bf4 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -640,7 +640,7 @@ std::string cmLocalGenerator::ExpandRuleVariable( if (variable == "TARGET_BASE") { // Strip the last extension off the target name. std::string targetBase = replaceValues.Target; - std::string::size_type pos = targetBase.rfind("."); + std::string::size_type pos = targetBase.rfind('.'); if (pos != targetBase.npos) { return targetBase.substr(0, pos); } else { @@ -2306,7 +2306,7 @@ std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName( std::string ssin = sin; // Avoid full paths by removing leading slashes. - ssin.erase(0, ssin.find_first_not_of("/")); + ssin.erase(0, ssin.find_first_not_of('/')); // Avoid full paths by removing colons. std::replace(ssin.begin(), ssin.end(), ':', '_'); @@ -2464,7 +2464,7 @@ std::string cmLocalGenerator::GetObjectFileNameWithoutTarget( // Remove the source extension if it is to be replaced. if (replaceExt) { keptSourceExtension = false; - std::string::size_type dot_pos = objectName.rfind("."); + std::string::size_type dot_pos = objectName.rfind('.'); if (dot_pos != std::string::npos) { objectName = objectName.substr(0, dot_pos); } @@ -2603,7 +2603,7 @@ bool cmLocalGenerator::CheckDefinition(std::string const& define) const } // Many compilers do not support # in the value so we disable it. - if (define.find_first_of("#") != define.npos) { + if (define.find_first_of('#') != define.npos) { std::ostringstream e; /* clang-format off */ e << "WARNING: Preprocessor definitions containing '#' may not be " diff --git a/Source/cmLocalNinjaGenerator.cxx b/Source/cmLocalNinjaGenerator.cxx index c7e1a90f8..d0b667f 100644 --- a/Source/cmLocalNinjaGenerator.cxx +++ b/Source/cmLocalNinjaGenerator.cxx @@ -205,7 +205,7 @@ void cmLocalNinjaGenerator::WritePools(std::ostream& os) cmSystemTools::ExpandListArgument(jobpools, pools); for (size_t i = 0; i < pools.size(); ++i) { const std::string pool = pools[i]; - const std::string::size_type eq = pool.find("="); + const std::string::size_type eq = pool.find('='); unsigned int jobs; if (eq != std::string::npos && sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) { diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index defac95..46891b7 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -71,7 +71,7 @@ private: static std::string cmSplitExtension(std::string const& in, std::string& base) { std::string ext; - std::string::size_type dot_pos = in.rfind("."); + std::string::size_type dot_pos = in.rfind('.'); if (dot_pos != std::string::npos) { // Remove the extension first in case &base == &in. ext = in.substr(dot_pos, std::string::npos); @@ -949,11 +949,11 @@ void cmLocalUnixMakefileGenerator3::AppendCustomCommand( cmSystemTools::ReplaceString(cmd, "/./", "/"); // Convert the command to a relative path only if the current // working directory will be the start-output directory. - bool had_slash = cmd.find("/") != cmd.npos; + bool had_slash = cmd.find('/') != cmd.npos; if (workingDir.empty()) { cmd = this->Convert(cmd, START_OUTPUT); } - bool has_slash = cmd.find("/") != cmd.npos; + bool has_slash = cmd.find('/') != cmd.npos; if (had_slash && !has_slash) { // This command was specified as a path to a file in the // current directory. Add a leading "./" so it can run @@ -975,9 +975,9 @@ void cmLocalUnixMakefileGenerator3::AppendCustomCommand( // must be written {{} instead of just {. Otherwise some // curly braces are removed. The hack can be skipped if the // first curly brace is the last character. - std::string::size_type lcurly = cmd.find("{"); + std::string::size_type lcurly = cmd.find('{'); if (lcurly != cmd.npos && lcurly < (cmd.size() - 1)) { - std::string::size_type rcurly = cmd.find("}"); + std::string::size_type rcurly = cmd.find('}'); if (rcurly == cmd.npos || rcurly > lcurly) { // The first curly is a left curly. Use the hack. std::string hack_cmd = cmd.substr(0, lcurly); diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index e480140..c67170d 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -661,9 +661,9 @@ void cmMakefileTargetGenerator::WriteObjectBuildFile( if (do_preprocess_rules || do_assembly_rules) { std::vector force_depends; force_depends.push_back("cmake_force"); - std::string::size_type dot_pos = relativeObj.rfind("."); + std::string::size_type dot_pos = relativeObj.rfind('.'); std::string relativeObjBase = relativeObj.substr(0, dot_pos); - dot_pos = obj.rfind("."); + dot_pos = obj.rfind('.'); std::string objBase = obj.substr(0, dot_pos); if (do_preprocess_rules) { @@ -1479,7 +1479,7 @@ void cmMakefileTargetGenerator::CreateLinkLibs( useResponseFile, useWatcomQuote); linkLibs = frameworkPath + linkPath + linkLibs; - if (useResponseFile && linkLibs.find_first_not_of(" ") != linkLibs.npos) { + if (useResponseFile && linkLibs.find_first_not_of(' ') != linkLibs.npos) { // Lookup the response file reference flag. std::string responseFlagVar = "CMAKE_"; responseFlagVar += diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index ed83069..52278d0 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -1298,7 +1298,7 @@ cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment() eit != currentEnv.end(); ++eit) { std::string var(*eit); - std::string::size_type pos = var.find("="); + std::string::size_type pos = var.find('='); if (pos != std::string::npos) { var = var.substr(0, pos); } diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 891b291..a7718d2 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -386,7 +386,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector& args) std::cerr << "cmake -E env: unknown option '" << a << "'" << std::endl; return 1; - } else if (a.find("=") != a.npos) { + } else if (a.find('=') != a.npos) { // Set environment variable. cmSystemTools::PutEnv(a); } else { -- cgit v0.12