Skip to content

Commit 73a5bb4

Browse files
committed
ErrorLogger: extracted findAndReplace() to utils.{cpp|h}
1 parent 516107a commit 73a5bb4

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

lib/errorlogger.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -523,22 +523,6 @@ std::string ErrorMessage::toXML() const
523523
return printer.CStr();
524524
}
525525

526-
/**
527-
* Replace all occurrences of searchFor with replaceWith in the
528-
* given source.
529-
* @param source The string to modify
530-
* @param searchFor What should be searched for
531-
* @param replaceWith What will replace the found item
532-
*/
533-
static void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
534-
{
535-
std::string::size_type index = 0;
536-
while ((index = source.find(searchFor, index)) != std::string::npos) {
537-
source.replace(index, searchFor.length(), replaceWith);
538-
index += replaceWith.length();
539-
}
540-
}
541-
542526
// TODO: read info from some shared resource instead?
543527
static std::string readCode(const std::string &file, int linenr, int column, const char endl[])
544528
{

lib/utils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ std::string trim(const std::string& s, const std::string& t)
138138
const std::string::size_type end = s.find_last_not_of(t);
139139
return s.substr(beg, end - beg + 1);
140140
}
141+
142+
void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith)
143+
{
144+
std::string::size_type index = 0;
145+
while ((index = source.find(searchFor, index)) != std::string::npos) {
146+
source.replace(index, searchFor.length(), replaceWith);
147+
index += replaceWith.length();
148+
}
149+
}

lib/utils.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ static inline const char* bool_to_string(bool b)
355355
*/
356356
CPPCHECKLIB std::string trim(const std::string& s, const std::string& t = " \t");
357357

358+
/**
359+
* Replace all occurrences of searchFor with replaceWith in the
360+
* given source.
361+
* @param source The string to modify
362+
* @param searchFor What should be searched for
363+
* @param replaceWith What will replace the found item
364+
*/
365+
CPPCHECKLIB void findAndReplace(std::string &source, const std::string &searchFor, const std::string &replaceWith);
366+
358367
namespace cppcheck
359368
{
360369
NORETURN inline void unreachable()

test/testutils.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class TestUtils : public TestFixture {
3939
TEST_CASE(id_string);
4040
TEST_CASE(startsWith);
4141
TEST_CASE(trim);
42+
TEST_CASE(findAndReplace);
4243
}
4344

4445
void isValidGlobPattern() const {
@@ -393,6 +394,39 @@ class TestUtils : public TestFixture {
393394
ASSERT_EQUALS("test", ::trim("test\n", "\n"));
394395
ASSERT_EQUALS("test", ::trim("\ntest\n", "\n"));
395396
}
397+
398+
void findAndReplace() const {
399+
{
400+
std::string s{"test"};
401+
::findAndReplace(s, "test", "tset");
402+
ASSERT_EQUALS("tset", s);
403+
}
404+
{
405+
std::string s{"testtest"};
406+
::findAndReplace(s, "test", "tset");
407+
ASSERT_EQUALS("tsettset", s);
408+
}
409+
{
410+
std::string s{"1test1test1"};
411+
::findAndReplace(s, "test", "tset");
412+
ASSERT_EQUALS("1tset1tset1", s);
413+
}
414+
{
415+
std::string s{"1test1test1"};
416+
::findAndReplace(s, "test", "");
417+
ASSERT_EQUALS("111", s);
418+
}
419+
{
420+
std::string s{"111"};
421+
::findAndReplace(s, "test", "tset");
422+
ASSERT_EQUALS("111", s);
423+
}
424+
{
425+
std::string s;
426+
::findAndReplace(s, "test", "tset");
427+
ASSERT_EQUALS("", s);
428+
}
429+
}
396430
};
397431

398432
REGISTER_TEST(TestUtils)

0 commit comments

Comments
 (0)