Skip to content

Commit 5cc7625

Browse files
committed
use startsWith() instead of suboptimal std::string::find() calls
1 parent 26168bd commit 5cc7625

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

lib/importproject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ bool ImportProject::importSln(std::istream &istr, const std::string &path, const
436436
return false;
437437
}
438438

439-
if (line.find("Microsoft Visual Studio Solution File") != 0) {
439+
if (!startsWith(line, "Microsoft Visual Studio Solution File")) {
440440
// Skip BOM
441-
if (!std::getline(istr, line) || line.find("Microsoft Visual Studio Solution File") != 0) {
441+
if (!std::getline(istr, line) || !startsWith(line, "Microsoft Visual Studio Solution File")) {
442442
printError("Visual Studio solution file header not found");
443443
return false;
444444
}

lib/library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ const Library::Container* Library::detectContainerInternal(const Token* const ty
11741174
if (container.startPattern.empty())
11751175
continue;
11761176

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

11791179
// If endPattern is undefined, it will always match, but itEndPattern has to be defined.
11801180
if (detect != IteratorOnly && container.endPattern.empty()) {

test/testcmdlineparser.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "suppressions.h"
2929
#include "fixture.h"
3030
#include "timer.h"
31+
#include "utils.h"
3132

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

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

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

294295
void showversion() {
@@ -1594,7 +1595,7 @@ class TestCmdlineParser : public TestFixture {
15941595
const char * const argv[] = {"cppcheck", "--doc"};
15951596
ASSERT(parser->parseFromArgs(2, argv));
15961597
ASSERT(parser->exitAfterPrinting());
1597-
ASSERT(GET_REDIRECT_OUTPUT.find("## ") == 0);
1598+
ASSERT(startsWith(GET_REDIRECT_OUTPUT, "## "));
15981599
}
15991600

16001601
void showtime() {

0 commit comments

Comments
 (0)