Skip to content

Commit

Permalink
use startsWith() instead of suboptimal std::string::find() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Aug 31, 2023
1 parent 8d72c82 commit afc8d15
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ bool ImportProject::importSln(std::istream &istr, const std::string &path, const
return false;
}

if (line.find("Microsoft Visual Studio Solution File") != 0) {
if (!startsWith(line, "Microsoft Visual Studio Solution File")) {
// Skip BOM
if (!std::getline(istr, line) || line.find("Microsoft Visual Studio Solution File") != 0) {
if (!std::getline(istr, line) || !startsWith(line, "Microsoft Visual Studio Solution File")) {
printError("Visual Studio solution file header not found");
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ const Library::Container* Library::detectContainerInternal(const Token* const ty
if (container.startPattern.empty())
continue;

const int offset = (withoutStd && container.startPattern2.find("std :: ") == 0) ? 7 : 0;
const int offset = (withoutStd && startsWith(container.startPattern2, "std :: ")) ? 7 : 0;

// If endPattern is undefined, it will always match, but itEndPattern has to be defined.
if (detect != IteratorOnly && container.endPattern.empty()) {
Expand Down
9 changes: 5 additions & 4 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "suppressions.h"
#include "fixture.h"
#include "timer.h"
#include "utils.h"

#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -272,23 +273,23 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck"};
ASSERT(parser->parseFromArgs(1, argv));
ASSERT_EQUALS(true, parser->getShowHelp());
ASSERT(GET_REDIRECT_OUTPUT.find("Cppcheck - A tool for static C/C++ code analysis") == 0);
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
}

void helpshort() {
REDIRECT;
const char * const argv[] = {"cppcheck", "-h"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT_EQUALS(true, parser->getShowHelp());
ASSERT(GET_REDIRECT_OUTPUT.find("Cppcheck - A tool for static C/C++ code analysis") == 0);
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
}

void helplong() {
REDIRECT;
const char * const argv[] = {"cppcheck", "--help"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT_EQUALS(true, parser->getShowHelp());
ASSERT(GET_REDIRECT_OUTPUT.find("Cppcheck - A tool for static C/C++ code analysis") == 0);
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "Cppcheck - A tool for static C/C++ code analysis"));
}

void showversion() {
Expand Down Expand Up @@ -1594,7 +1595,7 @@ class TestCmdlineParser : public TestFixture {
const char * const argv[] = {"cppcheck", "--doc"};
ASSERT(parser->parseFromArgs(2, argv));
ASSERT(parser->exitAfterPrinting());
ASSERT(GET_REDIRECT_OUTPUT.find("## ") == 0);
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "## "));
}

void showtime() {
Expand Down

0 comments on commit afc8d15

Please sign in to comment.