Skip to content

Commit

Permalink
ErrorLogger: extracted findAndReplace() to utils.{cpp|h}
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Apr 22, 2024
1 parent 516107a commit 73a5bb4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
16 changes: 0 additions & 16 deletions lib/errorlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,22 +523,6 @@ std::string ErrorMessage::toXML() const
return printer.CStr();
}

/**
* Replace all occurrences of searchFor with replaceWith in the
* given source.
* @param source The string to modify
* @param searchFor What should be searched for
* @param replaceWith What will replace the found item
*/
static void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
{
std::string::size_type index = 0;
while ((index = source.find(searchFor, index)) != std::string::npos) {
source.replace(index, searchFor.length(), replaceWith);
index += replaceWith.length();
}
}

// TODO: read info from some shared resource instead?
static std::string readCode(const std::string &file, int linenr, int column, const char endl[])
{
Expand Down
9 changes: 9 additions & 0 deletions lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,12 @@ std::string trim(const std::string& s, const std::string& t)
const std::string::size_type end = s.find_last_not_of(t);
return s.substr(beg, end - beg + 1);
}

void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
{
std::string::size_type index = 0;
while ((index = source.find(searchFor, index)) != std::string::npos) {
source.replace(index, searchFor.length(), replaceWith);
index += replaceWith.length();
}
}
9 changes: 9 additions & 0 deletions lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ static inline const char* bool_to_string(bool b)
*/
CPPCHECKLIB std::string trim(const std::string& s, const std::string& t = " \t");

/**
* Replace all occurrences of searchFor with replaceWith in the
* given source.
* @param source The string to modify
* @param searchFor What should be searched for
* @param replaceWith What will replace the found item
*/
CPPCHECKLIB void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith);

namespace cppcheck
{
NORETURN inline void unreachable()
Expand Down
34 changes: 34 additions & 0 deletions test/testutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TestUtils : public TestFixture {
TEST_CASE(id_string);
TEST_CASE(startsWith);
TEST_CASE(trim);
TEST_CASE(findAndReplace);
}

void isValidGlobPattern() const {
Expand Down Expand Up @@ -393,6 +394,39 @@ class TestUtils : public TestFixture {
ASSERT_EQUALS("test", ::trim("test\n", "\n"));
ASSERT_EQUALS("test", ::trim("\ntest\n", "\n"));
}

void findAndReplace() const {
{
std::string s{"test"};
::findAndReplace(s, "test", "tset");
ASSERT_EQUALS("tset", s);
}
{
std::string s{"testtest"};
::findAndReplace(s, "test", "tset");
ASSERT_EQUALS("tsettset", s);
}
{
std::string s{"1test1test1"};
::findAndReplace(s, "test", "tset");
ASSERT_EQUALS("1tset1tset1", s);
}
{
std::string s{"1test1test1"};
::findAndReplace(s, "test", "");
ASSERT_EQUALS("111", s);
}
{
std::string s{"111"};
::findAndReplace(s, "test", "tset");
ASSERT_EQUALS("111", s);
}
{
std::string s;
::findAndReplace(s, "test", "tset");
ASSERT_EQUALS("", s);
}
}
};

REGISTER_TEST(TestUtils)

0 comments on commit 73a5bb4

Please sign in to comment.