From 8ed0180279c30aae0ffcf868553775111a1be677 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Sat, 25 Jul 2015 17:19:53 +0200 Subject: [PATCH] Use C++11 string.back() instead of string[string.length()-1] --- cli/cmdlineparser.cpp | 6 +++--- cli/filelister.cpp | 4 ++-- lib/checkclass.cpp | 2 +- lib/errorlogger.cpp | 2 +- lib/preprocessor.cpp | 8 ++++---- lib/token.cpp | 4 ++-- lib/tokenlist.cpp | 4 ++-- test/testcppcheck.cpp | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 99be75a3d..a3b0ccb71 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -80,7 +80,7 @@ static void AddInclPathsToList(const std::string& FileList, std::listpush_back(PathName); @@ -450,7 +450,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) path = Path::removeQuotationMarks(path); // If path doesn't end with / or \, add it - if (path[path.length()-1] != '/') + if (path.back() != '/') path += '/'; _settings->_includePaths.push_back(path); @@ -504,7 +504,7 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[]) if (FileLister::isDirectory(path)) { // If directory name doesn't end with / or \, add it - if (path[path.length()-1] != '/') + if (path.back() != '/') path += '/'; } _ignoredPaths.push_back(path); diff --git a/cli/filelister.cpp b/cli/filelister.cpp index 43da14936..a5c7e9443 100644 --- a/cli/filelister.cpp +++ b/cli/filelister.cpp @@ -201,7 +201,7 @@ void FileLister::addFiles2(std::set &seen_paths, { std::ostringstream oss; oss << path; - if (path.length() > 0 && path[path.length()-1] == '/') + if (path.length() > 0 && path.back() == '/') oss << "*"; glob_t glob_results; @@ -220,7 +220,7 @@ void FileLister::addFiles2(std::set &seen_paths, if (seen_paths.find(absolute_path) != seen_paths.end()) continue; - if (filename[filename.length()-1] != '/') { + if (filename.back() != '/') { // File if ((Path::sameFileName(path,filename) || Path::acceptFile(filename, extra)) && !ignored.Match(filename)) { diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index b326f944c..a273a9dbe 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1686,7 +1686,7 @@ void CheckClass::checkConst() continue; } else if (func->isOperator() && Token::Match(previous, ";|{|}|public:|private:|protected:")) { // Operator without return type: conversion operator const std::string& opName = func->tokenDef->str(); - if (opName.compare(8, 5, "const") != 0 && opName[opName.size()-1] == '&') + if (opName.compare(8, 5, "const") != 0 && opName.back() == '&') continue; } else { // don't warn for unknown types.. diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 4d14657a4..30c7294dd 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -83,7 +83,7 @@ void ErrorLogger::ErrorMessage::setmsg(const std::string &msg) // as an empty message to the user if --verbose is used. // Even this doesn't cause problems with messages that have multiple // lines, none of the the error messages should end into it. - assert(!(msg[msg.size()-1]=='\n')); + assert(!(msg.back() =='\n')); // The summary and verbose message are separated by a newline // If there is no newline then both the summary and verbose messages diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index d357ecf52..17d3d6cc1 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -355,7 +355,7 @@ std::string Preprocessor::preprocessCleanupDirectives(const std::string &process // Trim lines.. if (!line.empty() && line[0] == ' ') line.erase(0, line.find_first_not_of(" ")); - if (!line.empty() && line[line.size()-1] == ' ') + if (!line.empty() && line.back() == ' ') line.erase(line.find_last_not_of(" ") + 1); // Preprocessor @@ -1460,7 +1460,7 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const while (cfg.length() > 0 && cfg[0] == ';') cfg.erase(0, 1); - while (cfg.length() > 0 && cfg[cfg.length()-1] == ';') + while (cfg.length() > 0 && cfg.back() == ';') cfg.erase(cfg.length() - 1); std::string::size_type pos = 0; @@ -2496,7 +2496,7 @@ static void getparams(const std::string &line, // spaces are only added if needed else if (line[pos] == ' ') { // Add space only if it is needed - if (par.size() && std::isalnum((unsigned char)par[par.length()-1])) { + if (par.size() && std::isalnum((unsigned char)par.back())) { par += ' '; } } @@ -2563,7 +2563,7 @@ private: for (std::size_t ipar = 0; ipar < params1.size(); ++ipar) { const std::string s(innerMacroName + "("); const std::string param(params1[ipar]); - if (param.compare(0,s.length(),s)==0 && param[param.length()-1]==')') { + if (param.compare(0,s.length(),s)==0 && param.back() == ')') { std::vector innerparams; std::string::size_type pos = s.length() - 1; unsigned int num = 0; diff --git a/lib/token.cpp b/lib/token.cpp index 9ae8df5a7..1fb0f930f 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -70,9 +70,9 @@ void Token::update_property_info() _type = eName; } else if (std::isdigit((unsigned char)_str[0]) || (_str.length() > 1 && _str[0] == '-' && std::isdigit((unsigned char)_str[1]))) _type = eNumber; - else if (_str.length() > 1 && _str[0] == '"' && _str[_str.length()-1] == '"') + else if (_str.length() > 1 && _str[0] == '"' && _str.back() == '"') _type = eString; - else if (_str.length() > 1 && _str[0] == '\'' && _str[_str.length()-1] == '\'') + else if (_str.length() > 1 && _str[0] == '\'' && _str.back() == '\'') _type = eChar; else if (_str == "=" || _str == "<<=" || _str == ">>=" || (_str.size() == 2U && _str[1] == '=' && std::strchr("+-*/%&^|", _str[0]))) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 36d322ba8..33d003678 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -303,8 +303,8 @@ bool TokenList::createTokens(std::istream &code, const std::string& file0) } else if (std::strchr("+-", ch) && CurrentToken.length() > 0 && std::isdigit((unsigned char)CurrentToken[0]) && - (CurrentToken[CurrentToken.length()-1] == 'e' || - CurrentToken[CurrentToken.length()-1] == 'E') && + (CurrentToken.back() == 'e' || + CurrentToken.back() == 'E') && !MathLib::isHex(CurrentToken)) { // Don't separate doubles "4.2e+10" } else if (CurrentToken.empty() && ch == '.' && std::isdigit((unsigned char)code.peek())) { diff --git a/test/testcppcheck.cpp b/test/testcppcheck.cpp index 65749606c..6d5bf80cb 100644 --- a/test/testcppcheck.cpp +++ b/test/testcppcheck.cpp @@ -66,8 +66,8 @@ private: for (std::list::const_iterator i = Check::instances().begin(); i != Check::instances().end(); ++i) { const std::string info = (*i)->classInfo(); if (!info.empty()) { - ASSERT('\n' != info[0]); // No \n in the beginning - ASSERT('\n' == info[info.length()-1]); // \n at end + ASSERT('\n' != info[0]); // No \n in the beginning + ASSERT('\n' == info.back()); // \n at end if (info.size() > 1) ASSERT('\n' != info[info.length()-2]); // Only one \n at end }