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, '#');
}

Bugs Found

Checked Projects
364
Collected Errors
13 504