Skip to content

Commit fefe5f1

Browse files
authored
extracted trim() and findAndReplace() to utils.{cpp|h} (#6325)
1 parent e431446 commit fefe5f1

5 files changed

Lines changed: 105 additions & 30 deletions

File tree

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/preprocessor.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,6 @@ static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)
4343
return tok1 && tok2 && tok1->location.sameline(tok2->location);
4444
}
4545

46-
/**
47-
* Remove heading and trailing whitespaces from the input parameter.
48-
* If string is all spaces/tabs, return empty string.
49-
* @param s The string to trim.
50-
*/
51-
static std::string trim(const std::string& s)
52-
{
53-
const std::string::size_type beg = s.find_first_not_of(" \t");
54-
if (beg == std::string::npos)
55-
return "";
56-
const std::string::size_type end = s.find_last_not_of(" \t");
57-
return s.substr(beg, end - beg + 1);
58-
}
59-
6046
Directive::Directive(std::string _file, const int _linenr, const std::string &_str) :
6147
file(std::move(_file)),
6248
linenr(_linenr),

lib/utils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,21 @@ void strTolower(std::string& str)
129129
return std::tolower(c);
130130
});
131131
}
132+
133+
std::string trim(const std::string& s, const std::string& t)
134+
{
135+
const std::string::size_type beg = s.find_first_not_of(t);
136+
if (beg == std::string::npos)
137+
return "";
138+
const std::string::size_type end = s.find_last_not_of(t);
139+
return s.substr(beg, end - beg + 1);
140+
}
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ static inline const char* bool_to_string(bool b)
347347
return b ? "true" : "false";
348348
}
349349

350+
/**
351+
* Remove heading and trailing whitespaces from the input parameter.
352+
* If string is all spaces/tabs, return empty string.
353+
* @param s The string to trim.
354+
* @param t The characters to trim.
355+
*/
356+
CPPCHECKLIB std::string trim(const std::string& s, const std::string& t = " \t");
357+
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+
350367
namespace cppcheck
351368
{
352369
NORETURN inline void unreachable()

test/testutils.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class TestUtils : public TestFixture {
3838
TEST_CASE(strToInt);
3939
TEST_CASE(id_string);
4040
TEST_CASE(startsWith);
41+
TEST_CASE(trim);
42+
TEST_CASE(findAndReplace);
4143
}
4244

4345
void isValidGlobPattern() const {
@@ -357,6 +359,74 @@ class TestUtils : public TestFixture {
357359
ASSERT(!::startsWith("2test", "t"));
358360
ASSERT(!::startsWith("t", "test"));
359361
}
362+
363+
void trim() const
364+
{
365+
ASSERT_EQUALS("test", ::trim("test"));
366+
ASSERT_EQUALS("test", ::trim(" test"));
367+
ASSERT_EQUALS("test", ::trim("test "));
368+
ASSERT_EQUALS("test", ::trim(" test "));
369+
ASSERT_EQUALS("test", ::trim(" test"));
370+
ASSERT_EQUALS("test", ::trim("test "));
371+
ASSERT_EQUALS("test", ::trim(" test "));
372+
ASSERT_EQUALS("test", ::trim("\ttest"));
373+
ASSERT_EQUALS("test", ::trim("test\t"));
374+
ASSERT_EQUALS("test", ::trim("\ttest\t"));
375+
ASSERT_EQUALS("test", ::trim("\t\ttest"));
376+
ASSERT_EQUALS("test", ::trim("test\t\t"));
377+
ASSERT_EQUALS("test", ::trim("\t\ttest\t\t"));
378+
ASSERT_EQUALS("test", ::trim(" \ttest"));
379+
ASSERT_EQUALS("test", ::trim("test\t "));
380+
ASSERT_EQUALS("test", ::trim(" \ttest\t"));
381+
ASSERT_EQUALS("test", ::trim("\t \ttest"));
382+
ASSERT_EQUALS("test", ::trim("test\t \t"));
383+
ASSERT_EQUALS("test", ::trim("\t \ttest\t \t"));
384+
385+
ASSERT_EQUALS("test test", ::trim("test test"));
386+
ASSERT_EQUALS("test test", ::trim(" test test"));
387+
ASSERT_EQUALS("test test", ::trim("test test "));
388+
ASSERT_EQUALS("test test", ::trim(" test test "));
389+
ASSERT_EQUALS("test\ttest", ::trim(" test\ttest"));
390+
ASSERT_EQUALS("test\ttest", ::trim("test\ttest "));
391+
ASSERT_EQUALS("test\ttest", ::trim(" test\ttest "));
392+
393+
ASSERT_EQUALS("test", ::trim("\ntest", "\n"));
394+
ASSERT_EQUALS("test", ::trim("test\n", "\n"));
395+
ASSERT_EQUALS("test", ::trim("\ntest\n", "\n"));
396+
}
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+
}
360430
};
361431

362432
REGISTER_TEST(TestUtils)

0 commit comments

Comments
 (0)