Skip to content

Commit b98c1bf

Browse files
committed
SimpCfg: Templatize str_trim_single
Also use NativeCharSize and MultiNativeCharSize wording to make the note more generic
1 parent 2cdb6b4 commit b98c1bf

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

common/simpcfg.hpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#endif
6060

6161

62+
// **** **** **** String related helpers **** **** **** //
63+
64+
6265
size_t wcs_to_mbs(std::string &sDest, const std::wstring &wSrc) {
6366
std::mbstate_t mbState = std::mbstate_t();
6467
const wchar_t *wSrcP = wSrc.c_str();
@@ -156,18 +159,27 @@ std::string str_trim_oversmart(std::string sIn, const std::string &trimChars=" \
156159

157160
// Remove atmost 1 char at the begin and 1 char at the end of the passed string,
158161
// provided the char belongs to one of the chars in trimChars.
159-
// NOTE: Chars being trimmed (ie in trimChars) needs to be FixedSize encoded chars,
160-
// to avoid mix up when working with strings which can utf-8/variable length encoded strings.
161-
// NOTE: This will work provided the string being trimmed as well the chars being
162-
// trimmed are made up of 1byte encoded chars including in utf8 encoding space.
163-
// If the string being trimmed includes multibyte encoded characters at the end,
164-
// then trimming can mess things up, if you have multibyte encoded utf-8 chars
165-
// in the trimChars set.
166-
std::string str_trim_single(std::string sin, std::string trimChars=" \t\n") {
162+
//
163+
// NOTE: Chars being trimmed (ie in trimChars) needs to be part of NativeCharSize
164+
// subset of the string's encoded char space, to avoid mix up when working with
165+
// strings which can be utf-8/utf-16/utf-32/sane-variable-length encoded strings.
166+
// NOTE:UTF8: This will work provided the string being trimmed as well the chars
167+
// being trimmed are made up of 1byte encoded chars in case of utf8 encoding space.
168+
// If the string being trimmed includes multibyte (ie MultiNativeCharSize) encoded
169+
// characters at the end, then trimming can mess things up, if you have multibyte
170+
// encoded utf-8 chars in the trimChars set.
171+
//
172+
// Currently given that SimpCfg only uses this with NativeCharSize chars in the
173+
// trimChars and most of the platforms are likely to be using utf-8 based char
174+
// space (which is a realtively sane variable length char encoding from this
175+
// logics perspective), so not providing oversmart variant.
176+
//
177+
template <typename TString>
178+
TString str_trim_single(TString sin, const TString& trimChars=" \t\n") {
167179
if (sin.empty()) return sin;
168180
for(auto c: trimChars) {
169181
if (c == sin.front()) {
170-
sin = sin.substr(1, std::string::npos);
182+
sin = sin.substr(1, TString::npos);
171183
break;
172184
}
173185
}
@@ -223,6 +235,9 @@ std::string str(std::vector<TypeWithStrSupp> values) {
223235
}
224236

225237

238+
// **** **** **** SimpCfg related helpers **** **** **** //
239+
240+
226241
typedef std::variant<std::string, bool, int64_t, double> SimpCfgData;
227242

228243
class SimpCfg {
@@ -385,7 +400,7 @@ class SimpCfg {
385400
bool bGroup = !isspace(curL[0]);
386401
curL = str_trim(curL);
387402
if (bGroup) {
388-
curL = str_trim_single(curL, "\"");
403+
curL = str_trim_single(curL, {"\""});
389404
group = curL;
390405
LDBUG_LN("DBUG:SC:%s:group:%s", __func__, group.c_str());
391406
continue;
@@ -402,7 +417,7 @@ class SimpCfg {
402417
}
403418
std::string key = curL.substr(0, dPos);
404419
key = str_trim(key);
405-
key = str_trim_single(key, "\"");
420+
key = str_trim_single(key, {"\""});
406421
std::string value = curL.substr(dPos+1);
407422
value = str_trim(value);
408423
value = str_trim(value, {","});
@@ -421,7 +436,7 @@ class SimpCfg {
421436
if (!value.empty() && (value.front() != '"')) {
422437
LWARN_LN("WARN:SC:%s:%d:%s:k:%s:v:%s:is this string?", __func__, iLine, group.c_str(), key.c_str(), value.c_str());
423438
}
424-
value = str_trim_single(value, "\"");
439+
value = str_trim_single(value, {"\""});
425440
set_string(group, key, value);
426441
}
427442
//LDBUG_LN("DBUG:SC:%s:%d:kv:%s:%s:%s:%s", __func__, iLine, group.c_str(), key.c_str(), vtype.c_str(), value.c_str());

0 commit comments

Comments
 (0)