avoid some unnecessary `std::string()` construction in comparisons (#4357)

* avoid unnecessary string wrapping

* consistently use `strcmp()`
This commit is contained in:
Oliver Stöneberg 2022-08-14 12:44:09 +02:00 committed by GitHub
parent 4b00fbfda6
commit f7caf7dc93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View File

@ -340,14 +340,14 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
} else if (tok->str() == comp && tok->astOperand2() && match(tok->astOperand2(), rhs)) {
ret = tok->astOperand1();
}
} else if (comp == "!=" && rhs == std::string("0")) {
} else if (comp == "!=" && rhs == "0") {
if (tok->str() == "!") {
ret = tok->astOperand1();
// handle (!(x==0)) as (x!=0)
astIsVariableComparison(ret, "==", "0", &ret);
} else
ret = tok;
} else if (comp == "==" && rhs == std::string("0")) {
} else if (comp == "==" && rhs == "0") {
if (tok->str() == "!") {
ret = tok->astOperand1();
// handle (!(x!=0)) as (x==0)

View File

@ -1197,7 +1197,7 @@ bool ImportProject::importCppcheckGuiProject(std::istream &istr, Settings *setti
else if (strcmp(node->Name(), CppcheckXml::ToolsElementName) == 0) {
const std::list<std::string> toolList = readXmlStringList(node, emptyString, CppcheckXml::ToolElementName, nullptr);
for (const std::string &toolName : toolList) {
if (toolName == std::string(CppcheckXml::ClangTidy))
if (toolName == CppcheckXml::ClangTidy)
temp.clangTidy = true;
}
} else if (strcmp(node->Name(), CppcheckXml::CheckHeadersElementName) == 0)

View File

@ -442,19 +442,19 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
container.itEndPattern = itEndPattern;
const char* const opLessAllowed = node->Attribute("opLessAllowed");
if (opLessAllowed)
container.opLessAllowed = std::string(opLessAllowed) == "true";
container.opLessAllowed = strcmp(opLessAllowed, "true") == 0;
const char* const hasInitializerListConstructor = node->Attribute("hasInitializerListConstructor");
if (hasInitializerListConstructor)
container.hasInitializerListConstructor = std::string(hasInitializerListConstructor) == "true";
container.hasInitializerListConstructor = strcmp(hasInitializerListConstructor, "true") == 0;
const char* const view = node->Attribute("view");
if (view)
container.view = std::string(view) == "true";
container.view = strcmp(view, "true") == 0;
for (const tinyxml2::XMLElement *containerNode = node->FirstChildElement(); containerNode; containerNode = containerNode->NextSiblingElement()) {
const std::string containerNodeName = containerNode->Name();
if (containerNodeName == "size" || containerNodeName == "access" || containerNodeName == "other") {
for (const tinyxml2::XMLElement *functionNode = containerNode->FirstChildElement(); functionNode; functionNode = functionNode->NextSiblingElement()) {
if (std::string(functionNode->Name()) != "function") {
if (strcmp(functionNode->Name(), "function") != 0) {
unknown_elements.insert(functionNode->Name());
continue;
}
@ -492,7 +492,7 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
} else if (containerNodeName == "access") {
const char* const indexArg = containerNode->Attribute("indexOperator");
if (indexArg)
container.arrayLike_indexOp = std::string(indexArg) == "array-like";
container.arrayLike_indexOp = strcmp(indexArg, "array-like") == 0;
}
} else if (containerNodeName == "type") {
const char* const templateArg = containerNode->Attribute("templateParameter");
@ -501,10 +501,10 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
const char* const string = containerNode->Attribute("string");
if (string)
container.stdStringLike = std::string(string) == "std-like";
container.stdStringLike = strcmp(string, "std-like") == 0;
const char* const associative = containerNode->Attribute("associative");
if (associative)
container.stdAssociativeLike = std::string(associative) == "std-like";
container.stdAssociativeLike = strcmp(associative, "std-like") == 0;
const char* const unstable = containerNode->Attribute("unstable");
if (unstable) {
std::string unstableType = unstable;

View File

@ -16,9 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sstream>
#include "type2.h"
#include <cstring>
#include <sstream>
static int getValue(const uint8_t *data, size_t dataSize, uint8_t maxValue, bool *done = nullptr)
{
static size_t pos; // current "data" position
@ -92,9 +94,9 @@ static std::string generateExpression2_Expr(const uint8_t *data, size_t dataSize
}
case 2: {
const char *u = unop[getValue(data,dataSize,sizeof(unop)/sizeof(*unop))];
if (u == std::string("()"))
if (strcmp(u, "()") == 0)
return "(" + generateExpression2_Expr(data, dataSize, numberOfGlobalConstants, depth) + ")";
else if (u == std::string("++") || u == std::string("--"))
else if (strcmp(u, "++") == 0 || strcmp(u, "--") == 0)
return u + generateExpression2_lvalue(data, dataSize);
return u + generateExpression2_Expr(data, dataSize, numberOfGlobalConstants, depth);
}