Minor: add endsWith() template, empy() check (#3472)
This commit is contained in:
parent
d87c599f1e
commit
0c16e346f1
|
@ -1872,7 +1872,7 @@ static bool isCPPCastKeyword(const Token* tok)
|
|||
{
|
||||
if (!tok)
|
||||
return false;
|
||||
return endsWith(tok->str(), "_cast", 5);
|
||||
return endsWith(tok->str(), "_cast");
|
||||
}
|
||||
|
||||
static bool isTrivialConstructor(const Token* tok)
|
||||
|
|
|
@ -258,7 +258,7 @@ void CheckString::checkIncorrectStringCompare()
|
|||
for (const Scope * scope : symbolDatabase->functionScopes) {
|
||||
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
|
||||
// skip "assert(str && ..)" and "assert(.. && str)"
|
||||
if ((endsWith(tok->str(), "assert", 6) || endsWith(tok->str(), "ASSERT", 6)) &&
|
||||
if ((endsWith(tok->str(), "assert") || endsWith(tok->str(), "ASSERT")) &&
|
||||
Token::Match(tok, "%name% (") &&
|
||||
(Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )")))
|
||||
tok = tok->next()->link();
|
||||
|
|
|
@ -1576,7 +1576,7 @@ void clangimport::parseClangAstDump(Tokenizer *tokenizer, std::istream &f)
|
|||
const std::string nodeType = line.substr(pos1+1, pos2 - pos1 - 1);
|
||||
const std::string ext = line.substr(pos2);
|
||||
|
||||
if (pos1 == 1 && endsWith(nodeType, "Decl", 4)) {
|
||||
if (pos1 == 1 && endsWith(nodeType, "Decl")) {
|
||||
if (!tree.empty())
|
||||
tree[0]->createTokens1(tokenList);
|
||||
tree.clear();
|
||||
|
|
|
@ -140,7 +140,7 @@ namespace {
|
|||
if (fileName.find(".") == std::string::npos)
|
||||
return getAddonInfo(fileName + ".py", exename);
|
||||
|
||||
if (endsWith(fileName, ".py", 3)) {
|
||||
if (endsWith(fileName, ".py")) {
|
||||
scriptFile = getFullPath(fileName, exename);
|
||||
if (scriptFile.empty())
|
||||
return "Did not find addon " + fileName;
|
||||
|
@ -160,7 +160,7 @@ namespace {
|
|||
return "";
|
||||
}
|
||||
|
||||
if (!endsWith(fileName, ".json", 5))
|
||||
if (!endsWith(fileName, ".json"))
|
||||
return "Failed to open addon " + fileName;
|
||||
|
||||
std::ifstream fin(fileName);
|
||||
|
@ -1312,7 +1312,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
|
|||
|
||||
std::string fileList;
|
||||
|
||||
if (files.size() >= 2 || endsWith(files[0], ".ctu-info", 9)) {
|
||||
if (files.size() >= 2 || endsWith(files[0], ".ctu-info")) {
|
||||
fileList = Path::getPathFromFilename(files[0]) + FILELIST;
|
||||
std::ofstream fout(fileList);
|
||||
for (const std::string& f: files)
|
||||
|
@ -1327,7 +1327,7 @@ void CppCheck::executeAddons(const std::vector<std::string>& files)
|
|||
mExitCode = 1;
|
||||
continue;
|
||||
}
|
||||
if (addon != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info", 9))
|
||||
if (addon != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
|
||||
continue;
|
||||
|
||||
const std::string results =
|
||||
|
|
|
@ -197,24 +197,24 @@ ImportProject::Type ImportProject::import(const std::string &filename, Settings
|
|||
|
||||
const std::string fileFilter = settings ? settings->fileFilter : std::string();
|
||||
|
||||
if (endsWith(filename, ".json", 5)) {
|
||||
if (endsWith(filename, ".json")) {
|
||||
importCompileCommands(fin);
|
||||
setRelativePaths(filename);
|
||||
return ImportProject::Type::COMPILE_DB;
|
||||
} else if (endsWith(filename, ".sln", 4)) {
|
||||
} else if (endsWith(filename, ".sln")) {
|
||||
importSln(fin, mPath, fileFilter);
|
||||
setRelativePaths(filename);
|
||||
return ImportProject::Type::VS_SLN;
|
||||
} else if (endsWith(filename, ".vcxproj", 8)) {
|
||||
} else if (endsWith(filename, ".vcxproj")) {
|
||||
std::map<std::string, std::string, cppcheck::stricmp> variables;
|
||||
importVcxproj(filename, variables, emptyString, fileFilter);
|
||||
setRelativePaths(filename);
|
||||
return ImportProject::Type::VS_VCXPROJ;
|
||||
} else if (endsWith(filename, ".bpr", 4)) {
|
||||
} else if (endsWith(filename, ".bpr")) {
|
||||
importBcb6Prj(filename);
|
||||
setRelativePaths(filename);
|
||||
return ImportProject::Type::BORLAND;
|
||||
} else if (settings && endsWith(filename, ".cppcheck", 9)) {
|
||||
} else if (settings && endsWith(filename, ".cppcheck")) {
|
||||
const bool success = importCppcheckGuiProject(fin, settings);
|
||||
setRelativePaths(filename);
|
||||
return success ? ImportProject::Type::CPPCHECK_GUI : ImportProject::Type::MISSING;
|
||||
|
@ -294,7 +294,7 @@ void ImportProject::FileSettings::parseCommand(std::string command)
|
|||
defs += fval;
|
||||
if (defval.size() >= 3 && defval.compare(0,2,"=\"")==0 && defval.back()=='\"')
|
||||
defval = "=" + unescape(defval.substr(2, defval.size() - 3));
|
||||
else if (defval.size() >= 5 && defval.compare(0,3,"=\\\"")==0 && endsWith(defval,"\\\"",2))
|
||||
else if (defval.size() >= 5 && defval.compare(0, 3, "=\\\"") == 0 && endsWith(defval, "\\\""))
|
||||
defval = "=\"" + unescape(defval.substr(3, defval.size() - 5)) + "\"";
|
||||
if (!defval.empty())
|
||||
defs += defval;
|
||||
|
|
|
@ -2279,7 +2279,7 @@ Function::Function(const Token *tokenDef, const std::string &clangType)
|
|||
|
||||
setFlags(tokenDef, tokenDef->scope());
|
||||
|
||||
if (endsWith(clangType, " const", 6))
|
||||
if (endsWith(clangType, " const"))
|
||||
isConst(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ struct EnumClassHash {
|
|||
|
||||
inline bool endsWith(const std::string &str, char c)
|
||||
{
|
||||
return str[str.size()-1U] == c;
|
||||
return !str.empty() && str.back() == c;
|
||||
}
|
||||
|
||||
inline bool endsWith(const std::string &str, const char end[], std::size_t endlen)
|
||||
|
@ -60,6 +60,12 @@ inline bool endsWith(const std::string &str, const char end[], std::size_t endle
|
|||
return (str.size() >= endlen) && (str.compare(str.size()-endlen, endlen, end)==0);
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
bool endsWith(const std::string& str, const char (&end)[N])
|
||||
{
|
||||
return endsWith(str, end, N - 1);
|
||||
}
|
||||
|
||||
inline static bool isPrefixStringCharLiteral(const std::string &str, char q, const std::string& p)
|
||||
{
|
||||
if (!endsWith(str, q))
|
||||
|
|
Loading…
Reference in New Issue