This website uses cookies and other technology to provide you a more personalized
experience. By
continuing the view of our web-pages you accept the terms of using these files. If you
don't
want your personal data to be processed, please, leave this site.
Learn More →
V817. It is more efficient to search for 'X' character rather than a string.
The analyzer detected a function that looks for a character in a string and can be optimized.
Consider the following example of inefficient code:
bool isSharpPresent(const std::string& str)
{
return str.find("#") != std::string::npos;
}
In this code, it is better to use an overridden version of the 'find()' function that receives a character instead of a string.
Optimized code:
bool isSharpPresent(const std::string& str)
{
return str.find('#') != std::string::npos;
}
The following example also uses inefficient code that can be optimized:
const char* GetSharpSubStr(const char* str)
{
return strstr(str, "#");
}
In this code, it is better to use the function 'strchr()' to search for a character instead of a string:
const char* GetSharpSubStr(const char* str)
{
return strchr(str, '#');
}