From 083535b487268218072c0dff6ae51ce798e1f33c Mon Sep 17 00:00:00 2001 From: Yaison Date: Mon, 30 Oct 2023 21:47:38 -0400 Subject: [PATCH] Refactor String Utils. --- include/ylib/core/lang.h | 8 +- include/ylib/core/sutils.h | 271 +++++++++++++---------- include/ylib/core/time.h | 4 +- run.sh | 9 + test-include/ylib/core/StringUtilsTest.h | 40 ++++ test.cpp | 2 + 6 files changed, 216 insertions(+), 118 deletions(-) create mode 100644 test-include/ylib/core/StringUtilsTest.h diff --git a/include/ylib/core/lang.h b/include/ylib/core/lang.h index 771d566..80db115 100644 --- a/include/ylib/core/lang.h +++ b/include/ylib/core/lang.h @@ -148,13 +148,13 @@ void println(int val) { fflush(stdout); } -void println(long int val) { - fprintf(stdout, "%ld\n", val); +void println(long long val) { + fprintf(stdout, "%lld\n", val); fflush(stdout); } -void println(unsigned long int val) { - fprintf(stdout, "%lu\n", val); +void println(unsigned long long val) { + fprintf(stdout, "%llu\n", val); fflush(stdout); } diff --git a/include/ylib/core/sutils.h b/include/ylib/core/sutils.h index 0c7cf51..942840a 100644 --- a/include/ylib/core/sutils.h +++ b/include/ylib/core/sutils.h @@ -3,145 +3,192 @@ #include #include #include +#include #include -namespace ylib { -namespace core { - - -string fieldToString(const char* field, const optional& val){ - if(val.has_value() == true){ - stringstream ss; - ss << field; - ss << ": '"; - ss << val.value(); - ss << "'"; - return ss.str(); - } - - stringstream ss; - ss << field; - ss << ": "; - ss << "(unassigned)"; - return ss.str(); -} +namespace ylib::core { -string fieldToString(const char* field, const optional& val){ - if(val.has_value() == true){ - stringstream ss; - ss << field; - ss << ": "; - ss << val.value(); - return ss.str(); - } - - stringstream ss; - ss << field; - ss << ": "; - ss << "(unassigned)"; - return ss.str(); -} + using namespace std; + + + string fieldToString(const char *field, const optional &val) { + if (val.has_value() == true) { + stringstream ss; + ss << field; + ss << ": '"; + ss << val.value(); + ss << "'"; + return ss.str(); + } -void findAndReplaceInplace(string& txt, const string& search, const string& replace) { - size_t pos = 0; - while ((pos = txt.find(search, pos)) != std::string::npos) { - txt.replace(pos, search.length(), replace); - pos += replace.length(); + stringstream ss; + ss << field; + ss << ": "; + ss << "(unassigned)"; + return ss.str(); } -} -string findAndReplace(const string& txt, const string& search, const string& replace){ - string copy = txt; - findAndReplaceInplace(copy, search, replace); - return copy; -} - -Bool endsWith(string& txt, const char c) { - if (txt.length() == 0) { - return False; - } - - size_t idx = txt.length() - 1; - if( txt.at(idx) == c){ - return True; - } - - return False; -} + string fieldToString(const char *field, const optional &val) { + if (val.has_value() == true) { + stringstream ss; + ss << field; + ss << ": "; + ss << val.value(); + return ss.str(); + } + + stringstream ss; + ss << field; + ss << ": "; + ss << "(unassigned)"; + return ss.str(); + } + void findAndReplaceInplace(string &txt, const string &search, const string &replace) { + size_t pos = 0; + while ((pos = txt.find(search, pos)) != string::npos) { + txt.replace(pos, search.length(), replace); + pos += replace.length(); + } + } + string findAndReplace(const string &txt, const string &search, const string &replace) { + string copy = txt; + findAndReplaceInplace(copy, search, replace); + return copy; + } -Bool endsWith(const std::string& str, const std::string& suffix){ - if( str.size() >= suffix.size() && - 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix)){ - return True; - } + Bool endsWith(string &txt, const char c) { + if (txt.length() == 0) { + return False; + } - return False; -} + size_t idx = txt.length() - 1; + if (txt.at(idx) == c) { + return True; + } -Bool endsWith(const std::string& str, const char* suffix){ - std::string s = suffix; - return endsWith(str, s); -} + return False; + } + + + Bool endsWith(const std::string &str, const std::string &suffix) { + if (str.size() >= suffix.size() && + 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix)) { + return True; + } + return False; + } + Bool endsWith(const std::string &str, const char *suffix) { + std::string s = suffix; + return endsWith(str, s); + } -string repeat(Int16 count, const char c) { - stringstream ss; - for (Int16 i = 0; i < count; i++) { - ss << c; - } + string repeat(Int16 count, const char c) { + stringstream ss; - return ss.str(); -} + for (Int16 i = 0; i < count; i++) { + ss << c; + } -string numFill(Int64 num, UInt16 count, const char c) { - checkParamIsPositive("count", count); + return ss.str(); + } - string snum = std::to_string(num); + string numFill(Int64 num, UInt16 count, const char c) { + checkParamIsPositive("count", count); - size_t len = snum.length(); + string snum = std::to_string(num); - if (len >= (size_t)count) { - //nothing to fill. - return snum; - } + size_t len = snum.length(); - size_t diff = count - len; - stringstream ss; + if (len >= (size_t) count) { + //nothing to fill. + return snum; + } - - for (size_t i = 0; i < diff; i++) { - ss << c; - } + size_t diff = count - len; + stringstream ss; - ss << snum; - return ss.str(); -} -void toLowerInplace(string& txt) { + for (size_t i = 0; i < diff; i++) { + ss << c; + } - std::transform(txt.begin(), txt.end(), txt.begin(), [](int c) { - return std::tolower(c); - }); -} + ss << snum; + return ss.str(); + } -string toLower(const string& txt) { - string ans = txt; + void toLowerInplace(string &txt) { - std::transform(ans.begin(), ans.end(), ans.begin(), [](int c) { - return std::tolower(c); - }); + std::transform(txt.begin(), txt.end(), txt.begin(), [](int c) { + return std::tolower(c); + }); + } - return ans; -} + string toLower(const string &txt) { + string ans = txt; -string toLower(const char* txt) { - string stxt{ txt }; - return toLower(stxt); -} + std::transform(ans.begin(), ans.end(), ans.begin(), [](int c) { + return std::tolower(c); + }); + + return ans; + } -}//namespace core -}//namespace ylib + string toLower(const char *txt) { + string stxt{txt}; + return toLower(stxt); + } + + class ToStringBuilder { + private: + vector keys; + vector values; + void doAppend(string key, string val) { + keys.push_back(key); + values.push_back(val); + } + public: + + void append(const char *key, string val) { + string sval = "\"" + val + "\""; + doAppend(string(key), sval); + } + + void append(string key, string val){ + string sval = "\"" + val + "\""; + doAppend(key, sval); + } + + void append(const char *key, UInt64 val) { + doAppend(string(key), std::to_string(val)); + } + + void append(string key, Int64 val) { + doAppend(string(key), std::to_string(val)); + } + + string str() { + size_t len = keys.size(); + ostringstream oss; + oss << "{"; + for (size_t i = 0; i < len; i++) { + string &k = keys[i]; + string &v = values[i]; + + oss << k; + oss << ": "; + oss << v; + + if (i + 1 < len) { + oss << ", "; + } + } + oss << "}"; + return oss.str(); + } + }; +} diff --git a/include/ylib/core/time.h b/include/ylib/core/time.h index 3b4cace..44e7446 100644 --- a/include/ylib/core/time.h +++ b/include/ylib/core/time.h @@ -363,7 +363,7 @@ Represents the current time at GMT-0 up to milliseconds resolution. validate(); } - Time(tm &t, UInt16 milli) : Time((UInt8) t.tm_hour, (UInt8) t.tm_min, (UInt8) t.tm_sec, (UInt16) milli) { + Time(tm &t, UInt16 milli) : Time((UInt8) t.tm_hour, (UInt8) t.tm_min, (UInt8) t.tm_sec, milli) { } Time(UInt8 hour, UInt8 min, UInt8 sec) : Time(hour, min, sec, 0) { @@ -522,7 +522,7 @@ Represents the current time, using the current system locale, up to milliseconds return _time; } - string toString() { + string toString() const { return sfput("{} {}", _date.toString(), _time.toString()); } }; diff --git a/run.sh b/run.sh index 613df7d..51bb251 100755 --- a/run.sh +++ b/run.sh @@ -8,4 +8,13 @@ docker run -it \ cpplib-core bash -c " echo \"Docker container created. About to run program main.\" && \ ./main + " +echo "=====================================================================" +docker run -it \ + --rm \ + -v ${PWD}/Docker_Debug:${CPD}/Debug \ + -w ${CPD}/Debug \ + cpplib-core bash -c " + echo \"Docker container created. About to run program test.\" && \ + ./test " \ No newline at end of file diff --git a/test-include/ylib/core/StringUtilsTest.h b/test-include/ylib/core/StringUtilsTest.h new file mode 100644 index 0000000..6e169c9 --- /dev/null +++ b/test-include/ylib/core/StringUtilsTest.h @@ -0,0 +1,40 @@ +// +// Created by Yaison Alcantara 3 on 3/2/23. +// + +#pragma once + +#include +#include + +namespace ylib::core { + + using namespace ylib::test; + + + void testSUtils(){ + TEST("test ToStringBuilder", []{ + // Given + ToStringBuilder sb1; + string k1 = "pop"; + string v1 = "push"; + + string k2 = "peak"; + Int32 v2 = 34; + + ToStringBuilder sb2; //empty + + // When + sb1.append(k1, v1); + sb1.append(k2, v2); + string ans1 = sb1.str(); + + string ans2 = sb2.str(); + + // Then + assertEquals("{pop: \"push\", peak: 34}", ans1); + assertEquals("{}", ans2); + }); + } + +} \ No newline at end of file diff --git a/test.cpp b/test.cpp index ff68a0b..dc3ad66 100644 --- a/test.cpp +++ b/test.cpp @@ -4,6 +4,7 @@ #include #include +#include #include using namespace ylib::core; @@ -13,6 +14,7 @@ int main(){ testTime(); testLang(); testProperties(); + testSUtils(); return EXIT_SUCCESS; }