Skip to content

Commit 6f7f5e6

Browse files
committed
SimpCfg:TemplatedDumbTrim; Test dumb and oversmart trim logics
1 parent a7ee4e3 commit 6f7f5e6

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

common/simpcfg.hpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <sstream>
3434
#include <format>
3535
#include <cuchar>
36+
#include <iostream>
3637

3738

3839
#define SC_DEBUG
@@ -74,14 +75,25 @@ size_t mbs_to_wcs(std::wstring &wDest, const std::string &sSrc) {
7475
return std::mbsrtowcs(wDest.data(), &sSrcP, wDest.length(), &mbState);
7576
}
7677

78+
void dumphex_string(const std::string &sIn, const std::string &msgTag){
79+
std::cout << msgTag << "[ ";
80+
for(auto c: sIn) {
81+
std::cout << std::format("{:02x}, ", (uint8_t)c);
82+
}
83+
std::cout << " ]" << std::endl;
84+
}
85+
7786
// Remove chars from begin and end of the passed string, provided the char belongs
7887
// to one of the chars in trimChars.
7988
// NOTE: Chars being trimmed (ie trimChars) needs to be 1byte encoded chars.
8089
// NOTE: This will work provided the string being trimmed as well the chars being
8190
// trimmed are made up of 1byte encoded chars including in utf8 encoding space.
8291
// If the string being trimmed includes multibyte encoded characters at the end,
8392
// then trimming can mess things up.
84-
std::string str_trim_dumb(std::string sin, const std::string &trimChars=" \t\n") {
93+
template <typename TString>
94+
TString str_trim_dumb(TString sin, const TString &trimChars=" \t\n") {
95+
dumphex_string(sin, "DBUG:TrimDumb:Str:");
96+
dumphex_string(trimChars, "DBUG:TrimDumb:Tim:");
8597
sin.erase(sin.find_last_not_of(trimChars)+1);
8698
sin.erase(0, sin.find_first_not_of(trimChars));
8799
return sin;
@@ -353,7 +365,7 @@ class SimpCfg {
353365
key = str_trim_single(key, "\"");
354366
std::string value = curL.substr(dPos+1);
355367
value = str_trim(value);
356-
value = str_trim(value, ",");
368+
value = str_trim(value, {","});
357369
std::string vtype = "bool";
358370
auto valueLower = str_tolower(value);
359371
if ((valueLower.compare("true") == 0) || (valueLower == "false")) {
@@ -381,8 +393,6 @@ class SimpCfg {
381393

382394
#ifdef SC_TEST_PRG
383395

384-
#include <iostream>
385-
386396
void check_string() {
387397
std::vector<std::string> vStandard = { "123", "1अ3" };
388398
std::cout << "**** string **** " << vStandard.size() << std::endl;
@@ -445,13 +455,29 @@ void check_wstring_cout() {
445455
}
446456
}
447457

458+
void check_nonenglish() {
459+
std::vector<std::string> vTest1 = { "\n\tAഅअಅ\n\t", "\n\tAഅअಅ " };
460+
for (auto sTest: vTest1) {
461+
std::string sGotDumb = str_trim_dumb(sTest, {" \n\t"});
462+
std::string sGotOSmart = str_trim_oversmart(sTest, {" \n\t"});
463+
std::cout << std::format("{}: Test1[{}] Dumb[{}] OverSmart[{}]", __func__, sTest, sGotDumb, sGotOSmart) << std::endl;
464+
}
465+
std::vector<std::string> vTest2 = { "\n\t this र remove 0s at end 000 ", "\n\tthis र remove 0s and अs at end 000रअ0अ "};
466+
for (auto sTest: vTest2) {
467+
std::string sGotDumb = str_trim_dumb(sTest, {" \n\t0अ"});
468+
std::string sGotOSmart = str_trim_oversmart(sTest, {" \n\t0अ"});
469+
std::cout << std::format("{}: Test2[{}] Dumb[{}] OverSmart[{}]", __func__, sTest, sGotDumb, sGotOSmart) << std::endl;
470+
}
471+
}
472+
448473
void check_strings() {
449474
std::string sSavedLocale;
450475
SimpCfg::locale_prepare(sSavedLocale);
451476
check_string();
452477
check_u8string();
453478
//check_wstring_wcout();
454479
check_wstring_cout();
480+
check_nonenglish();
455481
SimpCfg::locale_restore(sSavedLocale);
456482
}
457483

0 commit comments

Comments
 (0)