Commit 5784747d authored by Daniel Pfeifer's avatar Daniel Pfeifer
Browse files

Improve string find: prefer character overloads.

Apply fix-its from clang-tidy's performance-faster-string-find checker.
Ignore findings in kwsys.
parent 5cec953e
......@@ -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);
}
......
......@@ -68,7 +68,7 @@ int cpackDefinitionArgument(const char* argument, const char* cValue,
(void)argument;
cpackDefinitions* def = static_cast<cpackDefinitions*>(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);
......
......@@ -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);
......
......@@ -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;
}
}
......
......@@ -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<std::string>& 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,
......
......@@ -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;
......
......@@ -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") {
......
......@@ -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);
}
......
......@@ -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."
......
......@@ -77,7 +77,7 @@ cmDependsFortran::cmDependsFortran(cmLocalGenerator* lg)
for (std::vector<std::string>::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);
}
......
......@@ -608,8 +608,8 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos) {
std::string::size_type nameStartPos =
pos + sizeof("$<TARGET_PROPERTY:") - 1;
std::string::size_type closePos = 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("$<TARGET_NAME:", lastPos)) != input.npos) {
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) {
errorString = "$<TARGET_NAME:...> expression incomplete";
break;
......@@ -659,7 +659,7 @@ void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
while (errorString.empty() &&
(pos = input.find("$<LINK_ONLY:", lastPos)) != input.npos) {
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) {
errorString = "$<LINK_ONLY:...> expression incomplete";
break;
......
......@@ -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;
......
......@@ -145,7 +145,7 @@ struct DoAccept<true>
// 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);
}
......
......@@ -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 "
......
......@@ -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) {
......
......@@ -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);
......
......@@ -661,9 +661,9 @@ void cmMakefileTargetGenerator::WriteObjectBuildFile(
if (do_preprocess_rules || do_assembly_rules) {
std::vector<std::string> 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 +=
......
......@@ -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);
}
......
......@@ -386,7 +386,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& 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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment