From 73a5bb4b76d8626efbc6b49867f19fb36932ffc0 Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 22 Apr 2024 17:25:44 +0200 Subject: [PATCH] ErrorLogger: extracted `findAndReplace()` to `utils.{cpp|h}` --- lib/errorlogger.cpp | 16 ---------------- lib/utils.cpp | 9 +++++++++ lib/utils.h | 9 +++++++++ test/testutils.cpp | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index 752af2b3ded3..e04a973f5d6e 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -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[]) { diff --git a/lib/utils.cpp b/lib/utils.cpp index bfcf2c0bd3cd..ff52298deb63 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -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(); + } +} diff --git a/lib/utils.h b/lib/utils.h index 6721e5a189f7..345027368634 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -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() diff --git a/test/testutils.cpp b/test/testutils.cpp index ba811fb43792..531b2f477ed9 100644 --- a/test/testutils.cpp +++ b/test/testutils.cpp @@ -39,6 +39,7 @@ class TestUtils : public TestFixture { TEST_CASE(id_string); TEST_CASE(startsWith); TEST_CASE(trim); + TEST_CASE(findAndReplace); } void isValidGlobPattern() const { @@ -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)