From f92ef7d8e2f5b35a7d870aa10342b8b4c4aef3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 Apr 2017 10:17:35 +0200 Subject: [PATCH] Refactoring. Use 'endsWith()' --- lib/checkstring.cpp | 3 ++- lib/utils.h | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index 28c6bc559..09dd49933 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -20,6 +20,7 @@ //--------------------------------------------------------------------------- #include "checkstring.h" #include "symboldatabase.h" +#include "utils.h" //--------------------------------------------------------------------------- @@ -270,7 +271,7 @@ void CheckString::checkIncorrectStringCompare() const Scope * scope = symbolDatabase->functionScopes[i]; for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) { // skip "assert(str && ..)" and "assert(.. && str)" - if (tok->str().size() >= 6U && (tok->str().find("assert") == tok->str().size() - 6U || tok->str().find("ASSERT") == tok->str().size() - 6U) && + if ((endsWith(tok->str(), "assert", 6) || endsWith(tok->str(), "ASSERT", 6)) && 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/utils.h b/lib/utils.h index fc68fc2c6..053857fa8 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -60,4 +60,9 @@ inline bool endsWith(const std::string &str, char c) return str.back() == c; } +inline bool endsWith(const std::string &str, const char end[], std::size_t endlen) +{ + return (str.size() >= endlen) && (str.compare(str.size()-endlen, endlen, end)==0); +} + #endif