diff --git a/lib/astutils.cpp b/lib/astutils.cpp index f2f0f10d1..6878db264 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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) diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index d4a0853f9..e81a3ecde 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -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(); diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 315af9998..0a92fe03d 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -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(); diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 1f8dd3e92..b59e593c2 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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& 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& 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 = diff --git a/lib/importproject.cpp b/lib/importproject.cpp index a1d0f28bb..4ce6ac4f0 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -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 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; diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 54e9a2cf3..587c76ea8 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -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); } diff --git a/lib/utils.h b/lib/utils.h index e04224507..19d752e44 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -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 +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))