Improve string find: prefer character overloads.

Apply fix-its from clang-tidy's performance-faster-string-find checker.
Ignore findings in kwsys.
This commit is contained in:
Daniel Pfeifer 2016-05-24 21:24:10 +02:00
parent 5cec953e6a
commit 5784747d1b
19 changed files with 37 additions and 37 deletions

View File

@ -56,13 +56,13 @@ cmCPackIFWPackage::DependenceStruct::DependenceStruct(
} else if ((pos = dependence.find(">=")) != std::string::npos) { } else if ((pos = dependence.find(">=")) != std::string::npos) {
Compare.Type = CompareGreaterOrEqual; Compare.Type = CompareGreaterOrEqual;
Compare.Value = dependence.substr(pos + 2); 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.Type = CompareLess;
Compare.Value = dependence.substr(pos + 1); 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.Type = CompareEqual;
Compare.Value = dependence.substr(pos + 1); 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.Type = CompareGreater;
Compare.Value = dependence.substr(pos + 1); Compare.Value = dependence.substr(pos + 1);
} }

View File

@ -68,7 +68,7 @@ int cpackDefinitionArgument(const char* argument, const char* cValue,
(void)argument; (void)argument;
cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data); cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
std::string value = cValue; std::string value = cValue;
size_t pos = value.find_first_of("="); size_t pos = value.find_first_of('=');
if (pos == std::string::npos) { if (pos == std::string::npos) {
cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR, cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
"Please specify CPack definitions as: KEY=VALUE" << std::endl); "Please specify CPack definitions as: KEY=VALUE" << std::endl);

View File

@ -617,7 +617,7 @@ void cmCTestBuildHandler::GenerateXMLLogScraped(cmXMLWriter& xml)
cmSystemTools::ConvertToUnixSlashes(cm->SourceFile); cmSystemTools::ConvertToUnixSlashes(cm->SourceFile);
if (cm->SourceFile.find("/.../") != cm->SourceFile.npos) { if (cm->SourceFile.find("/.../") != cm->SourceFile.npos) {
cmSystemTools::ReplaceString(cm->SourceFile, "/.../", ""); 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) { if (p != cm->SourceFile.npos) {
cm->SourceFile = cm->SourceFile =
cm->SourceFile.substr(p + 1, cm->SourceFile.size() - p); cm->SourceFile.substr(p + 1, cm->SourceFile.size() - p);

View File

@ -1238,7 +1238,7 @@ int cmCTestCoverageHandler::HandleGCovCoverage(
// Initially all entries are -1 (not used). If we get coverage // Initially all entries are -1 (not used). If we get coverage
// information, increment it to 0 first. // information, increment it to 0 first.
if (vec[lineIdx] < 0) { if (vec[lineIdx] < 0) {
if (cov > 0 || prefix.find("#") != prefix.npos) { if (cov > 0 || prefix.find('#') != prefix.npos) {
vec[lineIdx] = 0; vec[lineIdx] = 0;
} }
} }
@ -1543,7 +1543,7 @@ int cmCTestCoverageHandler::HandleLCovCoverage(
// Initially all entries are -1 (not used). If we get coverage // Initially all entries are -1 (not used). If we get coverage
// information, increment it to 0 first. // information, increment it to 0 first.
if (vec[lineIdx] < 0) { if (vec[lineIdx] < 0) {
if (cov > 0 || prefix.find("#") != prefix.npos) { if (cov > 0 || prefix.find('#') != prefix.npos) {
vec[lineIdx] = 0; vec[lineIdx] = 0;
} }
} }

View File

@ -417,7 +417,7 @@ bool cmCTestSubmitHandler::SubmitUsingHTTP(const std::string& localprefix,
} }
} }
std::string upload_as = url + std::string upload_as = url +
((url.find("?", 0) == std::string::npos) ? "?" : "&") + "FileName=" + ((url.find('?') == std::string::npos) ? '?' : '&') + "FileName=" +
ofile; ofile;
upload_as += "&MD5="; upload_as += "&MD5=";
@ -706,7 +706,7 @@ bool cmCTestSubmitHandler::TriggerUsingHTTP(const std::set<std::string>& files,
} }
} }
std::string turl = url + std::string turl = url +
((url.find("?", 0) == std::string::npos) ? "?" : "&") + "xmlfile=" + ((url.find('?') == std::string::npos) ? '?' : '&') + "xmlfile=" +
ofile; ofile;
*this->LogFile << "Trigger url: " << turl << std::endl; *this->LogFile << "Trigger url: " << turl << std::endl;
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,

View File

@ -1869,7 +1869,7 @@ bool cmCTestTestHandler::SetTestsProperties(
cmSystemTools::ExpandListArgument(val, rtit->Labels); cmSystemTools::ExpandListArgument(val, rtit->Labels);
} }
if (key == "MEASUREMENT") { if (key == "MEASUREMENT") {
size_t pos = val.find_first_of("="); size_t pos = val.find_first_of('=');
if (pos != val.npos) { if (pos != val.npos) {
std::string mKey = val.substr(0, pos); std::string mKey = val.substr(0, pos);
const char* mVal = val.c_str() + pos + 1; const char* mVal = val.c_str() + pos + 1;

View File

@ -171,7 +171,7 @@ public:
} }
lastoffset = line.find("class="); lastoffset = line.find("class=");
endcovpos = line.find(">", lastoffset); endcovpos = line.find('>', lastoffset);
lineresult = line.substr(lastoffset + 7, (endcovpos - 8) - lastoffset); lineresult = line.substr(lastoffset + 7, (endcovpos - 8) - lastoffset);
if (lineresult == "covered") { if (lineresult == "covered") {

View File

@ -729,7 +729,7 @@ void cmCursesMainForm::FillCacheManagerFromUI()
void cmCursesMainForm::FixValue(cmState::CacheEntryType type, void cmCursesMainForm::FixValue(cmState::CacheEntryType type,
const std::string& in, std::string& out) const 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) { if (type == cmState::PATH || type == cmState::FILEPATH) {
cmSystemTools::ConvertToUnixSlashes(out); cmSystemTools::ConvertToUnixSlashes(out);
} }

View File

@ -721,7 +721,7 @@ bool cmCTest::UpdateCTestConfiguration()
if (line[0] == '#') { if (line[0] == '#') {
continue; continue;
} }
std::string::size_type cpos = line.find_first_of(":"); std::string::size_type cpos = line.find_first_of(':');
if (cpos == line.npos) { if (cpos == line.npos) {
continue; continue;
} }
@ -2480,7 +2480,7 @@ void cmCTest::AddSubmitFile(Part part, const char* name)
void cmCTest::AddCTestConfigurationOverwrite(const std::string& overStr) void cmCTest::AddCTestConfigurationOverwrite(const std::string& overStr)
{ {
size_t epos = overStr.find("="); size_t epos = overStr.find('=');
if (epos == overStr.npos) { if (epos == overStr.npos) {
cmCTestLog(this, ERROR_MESSAGE, cmCTestLog(this, ERROR_MESSAGE,
"CTest configuration overwrite specified in the wrong format." "CTest configuration overwrite specified in the wrong format."

View File

@ -77,7 +77,7 @@ cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg)
for (std::vector<std::string>::const_iterator it = definitions.begin(); for (std::vector<std::string>::const_iterator it = definitions.begin();
it != definitions.end(); ++it) { it != definitions.end(); ++it) {
std::string def = *it; std::string def = *it;
std::string::size_type assignment = def.find("="); std::string::size_type assignment = def.find('=');
if (assignment != std::string::npos) { if (assignment != std::string::npos) {
def = it->substr(0, assignment); def = it->substr(0, assignment);
} }

View File

@ -608,8 +608,8 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos) { while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos) {
std::string::size_type nameStartPos = std::string::size_type nameStartPos =
pos + sizeof("$<TARGET_PROPERTY:") - 1; pos + sizeof("$<TARGET_PROPERTY:") - 1;
std::string::size_type closePos = input.find(">", nameStartPos); std::string::size_type closePos = input.find('>', nameStartPos);
std::string::size_type commaPos = input.find(",", nameStartPos); std::string::size_type commaPos = input.find(',', nameStartPos);
std::string::size_type nextOpenPos = input.find("$<", nameStartPos); std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
if (commaPos == input.npos // Implied 'this' target if (commaPos == input.npos // Implied 'this' target
|| closePos == input.npos // Imcomplete expression. || closePos == input.npos // Imcomplete expression.
@ -634,7 +634,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
lastPos = pos; lastPos = pos;
while ((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos) { while ((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos) {
std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1; std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
std::string::size_type endPos = input.find(">", nameStartPos); std::string::size_type endPos = input.find('>', nameStartPos);
if (endPos == input.npos) { if (endPos == input.npos) {
errorString = "$<TARGET_NAME:...> expression incomplete"; errorString = "$<TARGET_NAME:...> expression incomplete";
break; break;
@ -659,7 +659,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
while (errorString.empty() && while (errorString.empty() &&
(pos = input.find("$<LINK_ONLY:", lastPos)) != input.npos) { (pos = input.find("$<LINK_ONLY:", lastPos)) != input.npos) {
std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1; std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1;
std::string::size_type endPos = input.find(">", nameStartPos); std::string::size_type endPos = input.find('>', nameStartPos);
if (endPos == input.npos) { if (endPos == input.npos) {
errorString = "$<LINK_ONLY:...> expression incomplete"; errorString = "$<LINK_ONLY:...> expression incomplete";
break; break;

View File

@ -300,7 +300,7 @@ void cmGeneratorExpression::Split(const std::string& input,
std::string part = input.substr(lastPos, pos - lastPos); std::string part = input.substr(lastPos, pos - lastPos);
std::string preGenex; std::string preGenex;
if (!part.empty()) { if (!part.empty()) {
std::string::size_type startPos = input.rfind(";", pos); std::string::size_type startPos = input.rfind(';', pos);
if (startPos == std::string::npos) { if (startPos == std::string::npos) {
preGenex = part; preGenex = part;
part = ""; part = "";
@ -364,7 +364,7 @@ std::string::size_type cmGeneratorExpression::Find(const std::string& input)
{ {
const std::string::size_type openpos = input.find("$<"); const std::string::size_type openpos = input.find("$<");
if (openpos != std::string::npos && if (openpos != std::string::npos &&
input.find(">", openpos) != std::string::npos) { input.find('>', openpos) != std::string::npos) {
return openpos; return openpos;
} }
return std::string::npos; return std::string::npos;

View File

@ -145,7 +145,7 @@ struct DoAccept<true>
// where the user supplied the file name and Visual Studio // where the user supplied the file name and Visual Studio
// appended the suffix. // appended the suffix.
std::string resx = f->GetFullPath(); 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.ExpectedResxHeaders.insert(hFileName);
data.ResxSources.push_back(f); data.ResxSources.push_back(f);
} }

View File

@ -640,7 +640,7 @@ std::string cmLocalGenerator::ExpandRuleVariable(
if (variable == "TARGET_BASE") { if (variable == "TARGET_BASE") {
// Strip the last extension off the target name. // Strip the last extension off the target name.
std::string targetBase = replaceValues.Target; std::string targetBase = replaceValues.Target;
std::string::size_type pos = targetBase.rfind("."); std::string::size_type pos = targetBase.rfind('.');
if (pos != targetBase.npos) { if (pos != targetBase.npos) {
return targetBase.substr(0, pos); return targetBase.substr(0, pos);
} else { } else {
@ -2306,7 +2306,7 @@ std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
std::string ssin = sin; std::string ssin = sin;
// Avoid full paths by removing leading slashes. // 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. // Avoid full paths by removing colons.
std::replace(ssin.begin(), ssin.end(), ':', '_'); std::replace(ssin.begin(), ssin.end(), ':', '_');
@ -2464,7 +2464,7 @@ std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
// Remove the source extension if it is to be replaced. // Remove the source extension if it is to be replaced.
if (replaceExt) { if (replaceExt) {
keptSourceExtension = false; keptSourceExtension = false;
std::string::size_type dot_pos = objectName.rfind("."); std::string::size_type dot_pos = objectName.rfind('.');
if (dot_pos != std::string::npos) { if (dot_pos != std::string::npos) {
objectName = objectName.substr(0, dot_pos); 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. // 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; std::ostringstream e;
/* clang-format off */ /* clang-format off */
e << "WARNING: Preprocessor definitions containing '#' may not be " e << "WARNING: Preprocessor definitions containing '#' may not be "

View File

@ -205,7 +205,7 @@ void cmLocalNinjaGenerator::WritePools(std::ostream& os)
cmSystemTools::ExpandListArgument(jobpools, pools); cmSystemTools::ExpandListArgument(jobpools, pools);
for (size_t i = 0; i < pools.size(); ++i) { for (size_t i = 0; i < pools.size(); ++i) {
const std::string pool = pools[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; unsigned int jobs;
if (eq != std::string::npos && if (eq != std::string::npos &&
sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) { sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {

View File

@ -71,7 +71,7 @@ private:
static std::string cmSplitExtension(std::string const& in, std::string& base) static std::string cmSplitExtension(std::string const& in, std::string& base)
{ {
std::string ext; 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) { if (dot_pos != std::string::npos) {
// Remove the extension first in case &base == &in. // Remove the extension first in case &base == &in.
ext = in.substr(dot_pos, std::string::npos); ext = in.substr(dot_pos, std::string::npos);
@ -949,11 +949,11 @@ void cmLocalUnixMakefileGenerator3::AppendCustomCommand(
cmSystemTools::ReplaceString(cmd, "/./", "/"); cmSystemTools::ReplaceString(cmd, "/./", "/");
// Convert the command to a relative path only if the current // Convert the command to a relative path only if the current
// working directory will be the start-output directory. // 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()) { if (workingDir.empty()) {
cmd = this->Convert(cmd, START_OUTPUT); 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) { if (had_slash && !has_slash) {
// This command was specified as a path to a file in the // This command was specified as a path to a file in the
// current directory. Add a leading "./" so it can run // current directory. Add a leading "./" so it can run
@ -975,9 +975,9 @@ void cmLocalUnixMakefileGenerator3::AppendCustomCommand(
// must be written {{} instead of just {. Otherwise some // must be written {{} instead of just {. Otherwise some
// curly braces are removed. The hack can be skipped if the // curly braces are removed. The hack can be skipped if the
// first curly brace is the last character. // 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)) { 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) { if (rcurly == cmd.npos || rcurly > lcurly) {
// The first curly is a left curly. Use the hack. // The first curly is a left curly. Use the hack.
std::string hack_cmd = cmd.substr(0, lcurly); std::string hack_cmd = cmd.substr(0, lcurly);

View File

@ -661,9 +661,9 @@ void cmMakefileTargetGenerator::WriteObjectBuildFile(
if (do_preprocess_rules || do_assembly_rules) { if (do_preprocess_rules || do_assembly_rules) {
std::vector<std::string> force_depends; std::vector<std::string> force_depends;
force_depends.push_back("cmake_force"); 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); std::string relativeObjBase = relativeObj.substr(0, dot_pos);
dot_pos = obj.rfind("."); dot_pos = obj.rfind('.');
std::string objBase = obj.substr(0, dot_pos); std::string objBase = obj.substr(0, dot_pos);
if (do_preprocess_rules) { if (do_preprocess_rules) {
@ -1479,7 +1479,7 @@ void cmMakefileTargetGenerator::CreateLinkLibs(
useResponseFile, useWatcomQuote); useResponseFile, useWatcomQuote);
linkLibs = frameworkPath + linkPath + linkLibs; 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. // Lookup the response file reference flag.
std::string responseFlagVar = "CMAKE_"; std::string responseFlagVar = "CMAKE_";
responseFlagVar += responseFlagVar +=

View File

@ -1298,7 +1298,7 @@ cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment()
eit != currentEnv.end(); ++eit) { eit != currentEnv.end(); ++eit) {
std::string var(*eit); std::string var(*eit);
std::string::size_type pos = var.find("="); std::string::size_type pos = var.find('=');
if (pos != std::string::npos) { if (pos != std::string::npos) {
var = var.substr(0, pos); var = var.substr(0, pos);
} }

View File

@ -386,7 +386,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
std::cerr << "cmake -E env: unknown option '" << a << "'" std::cerr << "cmake -E env: unknown option '" << a << "'"
<< std::endl; << std::endl;
return 1; return 1;
} else if (a.find("=") != a.npos) { } else if (a.find('=') != a.npos) {
// Set environment variable. // Set environment variable.
cmSystemTools::PutEnv(a); cmSystemTools::PutEnv(a);
} else { } else {