From 4a12cb7394f6e80484d495ab62abb1066b7e8167 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Thu, 8 Oct 2020 18:55:55 +0200 Subject: [PATCH] trimOnce --- inc/util.h | 3 ++- src/util.cpp | 16 +++++++++++++--- tests/main.cpp | 3 ++- tests/utilstest.cpp | 41 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/inc/util.h b/inc/util.h index a753a56..b3f17d1 100644 --- a/inc/util.h +++ b/inc/util.h @@ -24,7 +24,8 @@ bool insertStart(std::string& str, const std::string& start); unsigned int split(const std::string& str, const std::string& token, std::string* out, unsigned int count); -void trim(std::string& str, char c = ' '); +bool trim(std::string& str, char c = ' '); //return true if the string was changed +bool trimOnce(std::string& str, char c = ' '); std::string bytesToBase16(char* buffer, unsigned int len); std::string bytesToBase64(char* buffer, unsigned int len); diff --git a/src/util.cpp b/src/util.cpp index 60bcdbd..d3d1a7f 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -119,14 +119,24 @@ unsigned int mrbesen::util::split(const std::string& str, const std::string& tok return count; } -void mrbesen::util::trim(std::string& str, char c) { +bool mrbesen::util::trim(std::string& str, char c) { size_t first = str.find_first_not_of(c); if(first == std::string::npos) { + bool changed = !str.empty(); str = ""; - return; + return changed; } size_t last = str.find_last_not_of(c)+1; - str = str.substr(first, last-first); + bool changed = (last-first) < str.size(); + if(changed) + str = str.substr(first, last-first); + return changed; +} + +bool mrbesen::util::trimOnce(std::string& str, char c) { + bool a = removeStart(str, std::string(1, c)); + bool b = removeEnd(str, std::string(1, c)); + return a || b; } std::string mrbesen::util::bytesToBase16(char* buffer, unsigned int len) { diff --git a/tests/main.cpp b/tests/main.cpp index 7384665..86de8e9 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -15,6 +15,7 @@ int testUtil_start_endWith(); int testUtil_removeStart_End(); int testUtil_insertStart_End(); int testUtil_trim(); +int testUtil_trimOnce(); int testUtil_base(); int testStringSpliterator(); @@ -23,7 +24,7 @@ int testUtilSplit(); int testGetDoy(); test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan, testFiles_readFile, - testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_base, + testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_trimOnce, testUtil_base, testStringSpliterator, testUtilSplit, testGetDoy, NULL}; diff --git a/tests/utilstest.cpp b/tests/utilstest.cpp index 6729e1b..6d475ab 100644 --- a/tests/utilstest.cpp +++ b/tests/utilstest.cpp @@ -152,19 +152,50 @@ int testUtil_insertStart_End() { int testUtil_trim() { std::string a = " abc def "; - trim(a); + ASSERT(trim(a), ""); ASSERT(a == "abc def", a); - trim(a, '_'); + ASSERT(!trim(a, '_'), ""); ASSERT(a == "abc def", a); - trim(a, 'f'); + ASSERT(trim(a, 'f'), ""); ASSERT(a == "abc de", a); a = " "; - trim(a); + ASSERT(trim(a), ""); ASSERT(a == "", a); - trim(a); + ASSERT(!trim(a), ""); + ASSERT(a == "", a); + + return TESTGOOD; +} + +int testUtil_trimOnce() { + std::string a = " abcd "; + + ASSERT(!trimOnce(a, 'a'), ""); + ASSERT(a == " abcd ", a); + ASSERT(trimOnce(a), ""); + ASSERT(a == " abcd ", a); + ASSERT(trimOnce(a), ""); + ASSERT(a == "abcd ", a); + ASSERT(trimOnce(a), ""); + ASSERT(a == "abcd", a); + ASSERT(!trimOnce(a), ""); + ASSERT(a == "abcd", a); + ASSERT(!trimOnce(a, 'b'), ""); + ASSERT(a == "abcd", a); + ASSERT(trimOnce(a, 'a'), ""); + ASSERT(a == "bcd", a); + + a = " "; + ASSERT(trimOnce(a), ""); + ASSERT(a == " ", a); + ASSERT(trimOnce(a), ""); + ASSERT(a == "", a); + ASSERT(!trimOnce(a), ""); + ASSERT(a == "", a); + ASSERT(!trimOnce(a, 'a'), ""); ASSERT(a == "", a); return TESTGOOD;