59
59
#endif
60
60
61
61
62
+ // **** **** **** String related helpers **** **** **** //
63
+
64
+
62
65
size_t wcs_to_mbs (std::string &sDest , const std::wstring &wSrc) {
63
66
std::mbstate_t mbState = std::mbstate_t ();
64
67
const wchar_t *wSrcP = wSrc.c_str ();
@@ -156,18 +159,27 @@ std::string str_trim_oversmart(std::string sIn, const std::string &trimChars=" \
156
159
157
160
// Remove atmost 1 char at the begin and 1 char at the end of the passed string,
158
161
// 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 " ) {
167
179
if (sin.empty ()) return sin;
168
180
for (auto c: trimChars) {
169
181
if (c == sin.front ()) {
170
- sin = sin.substr (1 , std::string ::npos);
182
+ sin = sin.substr (1 , TString ::npos);
171
183
break ;
172
184
}
173
185
}
@@ -223,6 +235,9 @@ std::string str(std::vector<TypeWithStrSupp> values) {
223
235
}
224
236
225
237
238
+ // **** **** **** SimpCfg related helpers **** **** **** //
239
+
240
+
226
241
typedef std::variant<std::string, bool , int64_t , double > SimpCfgData;
227
242
228
243
class SimpCfg {
@@ -385,7 +400,7 @@ class SimpCfg {
385
400
bool bGroup = !isspace (curL[0 ]);
386
401
curL = str_trim (curL);
387
402
if (bGroup) {
388
- curL = str_trim_single (curL, " \" " );
403
+ curL = str_trim_single (curL, { " \" " } );
389
404
group = curL;
390
405
LDBUG_LN (" DBUG:SC:%s:group:%s" , __func__, group.c_str ());
391
406
continue ;
@@ -402,7 +417,7 @@ class SimpCfg {
402
417
}
403
418
std::string key = curL.substr (0 , dPos);
404
419
key = str_trim (key);
405
- key = str_trim_single (key, " \" " );
420
+ key = str_trim_single (key, { " \" " } );
406
421
std::string value = curL.substr (dPos+1 );
407
422
value = str_trim (value);
408
423
value = str_trim (value, {" ," });
@@ -421,7 +436,7 @@ class SimpCfg {
421
436
if (!value.empty () && (value.front () != ' "' )) {
422
437
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 ());
423
438
}
424
- value = str_trim_single (value, " \" " );
439
+ value = str_trim_single (value, { " \" " } );
425
440
set_string (group, key, value);
426
441
}
427
442
// 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