|
33 | 33 | #include <sstream>
|
34 | 34 | #include <format>
|
35 | 35 | #include <cuchar>
|
| 36 | +#include <iostream> |
36 | 37 |
|
37 | 38 |
|
38 | 39 | #define SC_DEBUG
|
@@ -74,14 +75,25 @@ size_t mbs_to_wcs(std::wstring &wDest, const std::string &sSrc) {
|
74 | 75 | return std::mbsrtowcs(wDest.data(), &sSrcP, wDest.length(), &mbState);
|
75 | 76 | }
|
76 | 77 |
|
| 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 | + |
77 | 86 | // Remove chars from begin and end of the passed string, provided the char belongs
|
78 | 87 | // to one of the chars in trimChars.
|
79 | 88 | // NOTE: Chars being trimmed (ie trimChars) needs to be 1byte encoded chars.
|
80 | 89 | // NOTE: This will work provided the string being trimmed as well the chars being
|
81 | 90 | // trimmed are made up of 1byte encoded chars including in utf8 encoding space.
|
82 | 91 | // If the string being trimmed includes multibyte encoded characters at the end,
|
83 | 92 | // 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:"); |
85 | 97 | sin.erase(sin.find_last_not_of(trimChars)+1);
|
86 | 98 | sin.erase(0, sin.find_first_not_of(trimChars));
|
87 | 99 | return sin;
|
@@ -353,7 +365,7 @@ class SimpCfg {
|
353 | 365 | key = str_trim_single(key, "\"");
|
354 | 366 | std::string value = curL.substr(dPos+1);
|
355 | 367 | value = str_trim(value);
|
356 |
| - value = str_trim(value, ","); |
| 368 | + value = str_trim(value, {","}); |
357 | 369 | std::string vtype = "bool";
|
358 | 370 | auto valueLower = str_tolower(value);
|
359 | 371 | if ((valueLower.compare("true") == 0) || (valueLower == "false")) {
|
@@ -381,8 +393,6 @@ class SimpCfg {
|
381 | 393 |
|
382 | 394 | #ifdef SC_TEST_PRG
|
383 | 395 |
|
384 |
| -#include <iostream> |
385 |
| - |
386 | 396 | void check_string() {
|
387 | 397 | std::vector<std::string> vStandard = { "123", "1अ3" };
|
388 | 398 | std::cout << "**** string **** " << vStandard.size() << std::endl;
|
@@ -445,13 +455,29 @@ void check_wstring_cout() {
|
445 | 455 | }
|
446 | 456 | }
|
447 | 457 |
|
| 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 | + |
448 | 473 | void check_strings() {
|
449 | 474 | std::string sSavedLocale;
|
450 | 475 | SimpCfg::locale_prepare(sSavedLocale);
|
451 | 476 | check_string();
|
452 | 477 | check_u8string();
|
453 | 478 | //check_wstring_wcout();
|
454 | 479 | check_wstring_cout();
|
| 480 | + check_nonenglish(); |
455 | 481 | SimpCfg::locale_restore(sSavedLocale);
|
456 | 482 | }
|
457 | 483 |
|
|
0 commit comments