-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.cpccStringUtil.h
executable file
·530 lines (395 loc) · 16.4 KB
/
core.cpccStringUtil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* *****************************************
* File: core.cpccStringUtil.h
* Purpose: Portable (cross-platform), light-weight library
* String utility functions
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2015 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#include <string>
#include <sstream>
#include <time.h>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include "cpccUnicodeSupport.h"
#include "cpccTesting.h"
typedef std::vector<cpcc_string> cpcc_stringList;
// /////////////////////////////////////////////////////////////////////////////////////////////////
//
// class stringUtils
//
// /////////////////////////////////////////////////////////////////////////////////////////////////
class stringUtils
{
public:
static std::basic_string<TCHAR> trimWhiteSpaces(const TCHAR* txt, const TCHAR* whiteSpaceChars = _T(" \t"));
static std::basic_string<TCHAR> removeExtraSpaces(const TCHAR* txt, const TCHAR* whiteSpaceChars = _T(" \t"));
static inline void stringTrimL(cpcc_string &s, const cpcc_string &charsToRemove = _T("\t\n\v\f\r "));
static inline void stringTrimR(cpcc_string &s, const cpcc_string &charsToRemove = _T("\t\n\v\f\r "));
static inline void stringTrim(cpcc_string &s, const cpcc_string &charsToRemove = _T("\t\n\v\f\r "));
static inline void removeFromStringToTheEnd(cpcc_string& str, const cpcc_char* aChar);
template<typename aPCharType>
static bool stringEndsWith(const std::basic_string<aPCharType> &aStr, const aPCharType *aSuffix)
{
std::basic_string<aPCharType> suffixStr(aSuffix);
return stringEndsWith(aStr, suffixStr);
}
template<typename aPCharType>
static bool stringEndsWith(const std::basic_string<aPCharType> &aStr, const std::basic_string<aPCharType> &aSuffix)
{
if (aSuffix.size() > aStr.size()) return false;
return std::equal(aSuffix.rbegin(), aSuffix.rend(), aStr.rbegin());
}
static bool stringStartsWith(const cpcc_char *aStr, const cpcc_char *aPrefix)
{
#ifdef _WIN32
#pragma warning( suppress : 4996 )
return (cpcc_strnicmp(aStr, aPrefix, cpcc_strlen(aPrefix)) == 0);
#else
// Unix libraries use strcasecmp, from <strings.h>.
return (strncasecmp(aStr, aPrefix, strlen(aPrefix)) == 0);
#endif
}
// todo: to delete. Check if needed for the mac #define stricmp strcasecmp
static bool stringsAreEqual(const cpcc_char *aStr1, const cpcc_char *aStr2)
{
#ifdef _WIN32
#pragma warning( suppress : 4996 )
return (cpcc_stricmp(aStr1, aStr2) == 0);
#else
// Unix libraries use strcasecmp, from <strings.h>.
return (strcasecmp(aStr1, aStr2) == 0);
#endif
}
// a pity the whole std library does not contain a ready function for this tasl
static void findAndReplaceAll(cpcc_string& source, const cpcc_char* find, const cpcc_char* replace)
{
if (!find || !replace)
return;
size_t findLen = cpcc_strlen(find);
size_t replaceLen = cpcc_strlen(replace);
size_t pos = 0;
while ((pos = source.find(find, pos)) != std::string::npos)
{
source.replace(pos, findLen, replace);
pos += replaceLen;
}
}
static void stringSplit(const cpcc_string &inputStr, const cpcc_char delimiter, cpcc_stringList &outputList)
{
size_t startPos = 0;
size_t endPos = inputStr.find_first_of(delimiter);
outputList.clear();
while (endPos <= cpcc_string::npos)
{
outputList.push_back(inputStr.substr(startPos, endPos - startPos));
if (endPos == cpcc_string::npos)
break;
startPos = endPos + 1;
endPos = inputStr.find_first_of(delimiter, startPos);
}
}
};
// --------------------------------------
//
// Implementation of stringUtils
//
// --------------------------------------
inline std::basic_string<TCHAR> stringUtils::trimWhiteSpaces(const TCHAR* txt, const TCHAR* whiteSpaceChars)
{
if (!txt)
return _T("");
std::basic_string<TCHAR> result(txt);
const auto trimmedBegin = result.find_first_not_of(whiteSpaceChars);
if (trimmedBegin == std::string::npos)
return _T(""); // no content
const auto trimmedEnd = result.find_last_not_of(whiteSpaceChars);
const auto trimmedRange = trimmedEnd - trimmedBegin + 1;
return result.substr(trimmedBegin, trimmedRange);
}
inline std::basic_string<TCHAR> stringUtils::removeExtraSpaces(const TCHAR* txt, const TCHAR* whiteSpaceChars)
{
if (!txt)
return _T("");
// https://stackoverflow.com/questions/1798112/removing-leading-and-trailing-spaces-from-a-string
auto result = trimWhiteSpaces(txt); // now it is trimmed.
const std::basic_string<TCHAR> fill = _T(" ");
auto beginSpace = result.find_first_of(whiteSpaceChars);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whiteSpaceChars, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whiteSpaceChars, newStart);
}
return result;
}
// trim from start (in place)
inline void stringUtils::stringTrimL(cpcc_string &s, const cpcc_string &charsToRemove)
{
// 1st Way
// s.erase(s.begin(), std::find_if(s.begin(), s.end(),
// std::not1(std::ptr_fun<int, int>(std::isspace))));
// 2nd way
// s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); } ));
// 3rd way
s.erase(0, s.find_first_not_of(charsToRemove));
}
// trim from end (in place)
inline void stringUtils::stringTrimR(cpcc_string &s, const cpcc_string &charsToRemove)
{
// 1st Way
//s.erase(std::find_if(s.rbegin(), s.rend(),
// std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
// 2nd way
// s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch);} ).base(), s.end());
// 3rd way
s.erase(s.find_last_not_of(charsToRemove) + 1);
}
// trim from both ends (in place)
inline void stringUtils::stringTrim(cpcc_string &s, const cpcc_string &charsToRemove)
{
stringTrimL(s, charsToRemove);
stringTrimR(s, charsToRemove);
}
inline void stringUtils::removeFromStringToTheEnd(cpcc_string& str, const cpcc_char* aSubString)
{
if (!aSubString)
return;
if (cpcc_strlen(aSubString) == 0)
return;
size_t pos = str.find(aSubString);
if (pos == std::string::npos)
return;
str.erase(pos);
}
// --------------------------------------
// class strConvertionsV3
// --------------------------------------
class strConvertionsV3
{
public:
static cpcc_string toString(const bool value) { return (value) ? _T("yes") : _T("no"); }
static cpcc_string toString(const cpcc_string &value) { return value; }
static cpcc_string toString(const cpcc_char *value) { return (value) ? value : _T(""); }
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type >
static cpcc_string toString(const T value)
{
cpcc_stringstream ss;
ss.precision(14);
ss << value;
return ss.str();
}
// ////////////////////////////////////////////////////
static const cpcc_string fromString(const cpcc_string &aStr, const cpcc_string &defaultValue) { return aStr; }
static const cpcc_string fromString(const cpcc_string &aStr, const cpcc_char *defaultValue) { return aStr; }
static const cpcc_string fromString(const cpcc_char *aStr, const cpcc_char *defaultValue) { return (aStr)?aStr:(defaultValue?defaultValue:_T("")); }
static const bool fromString(const cpcc_char *aStr, const bool aDefaultValue)
{
cpcc_string strValue(aStr ? aStr : _T(""));
if (strValue.compare(_T("yes")) == 0 || strValue.compare(_T("true")) == 0 || strValue.compare(_T("1")) == 0 || strValue.compare(_T("on")) == 0)
return true;
if (strValue.compare(_T("no")) == 0 || strValue.compare(_T("false")) == 0 || strValue.compare(_T("0")) == 0 || strValue.compare(_T("off")) == 0)
return false;
return aDefaultValue;
}
/*
template <typename T>
static T fromString(const cpcc_string &aStr, const T defaultValue)
{
cpcc_stringstream ss(aStr);
T result;
return (ss >> result) ? result : defaultValue;
}
*/
template <typename T>
static const T fromString(const cpcc_char *aStr, const T defaultValue)
{
if (!aStr)
return defaultValue;
cpcc_stringstream ss(aStr);
T result(0);
return (ss >> result)? result : defaultValue;
}
};
// ////////////////////////////////////////////////////////////////////////////////////////////////
// stringConversions
// ////////////////////////////////////////////////////////////////////////////////////////////////
// Todo: maybe not needed anymore. To delete?
/*
class stringConversions
{
// new way
static const bool fromStrNew(const cpcc_char* strValue, cpcc_string& aValue)
{
aValue = strValue;
return true;
}
static const bool fromStrNew(const cpcc_string& strValue, bool& aValue)
{
if (strValue.compare(_T("yes")) == 0 || strValue.compare(_T("true")) == 0 || strValue.compare(_T("1")) == 0 || strValue.compare(_T("on")) == 0)
{ aValue = true; return true; }
if (strValue.compare(_T("no")) == 0 || strValue.compare(_T("false")) == 0 || strValue.compare(_T("0")) == 0 || strValue.compare(_T("off")) == 0)
{ aValue = true; return true; }
return false;
}
static const bool fromStrNew(const cpcc_char* strValue, double& aValue)
{
cpcc_char* end;
double n = cpcc_strtod(strValue, &end);
if (end <= strValue) return false;
aValue = n;
return true;
}
static const bool fromStrNew(const cpcc_char* strValue, float& aValue)
{
cpcc_char* end;
float n = (float)cpcc_strtod(strValue, &end);
if (end <= strValue) return false;
aValue = n;
return true;
}
// old way
static const double fromStr(const cpcc_char* strValue, const double aDefaultValue)
{
cpcc_char* end;
double n = cpcc_strtod(strValue, &end);
return end > strValue ? n : aDefaultValue;
}
static const float fromStr(const cpcc_string &strValue, const float aDefaultValue)
{
// http://www.cplusplus.com/reference/string/stod/
// http://www.cplusplus.com/reference/string/stof/
// http://www.cplusplus.com/forum/articles/9645/
cpcc_stringstream ss(strValue);
float result;
return (ss >> result) ? result : aDefaultValue;
}
static const float fromStr(const cpcc_char* strValue, const float aDefaultValue)
{
cpcc_char* end;
float n = (float) cpcc_strtod(strValue, &end);
return end > strValue ? n : aDefaultValue;
// return fromStr(cpcc_string(strValue), aDefaultValue);
}
static const long fromStr(const cpcc_char* strValue, const long aDefaultValue)
{
cpcc_char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
long n = cpcc_strtol(strValue, &end, 0);
return end > strValue ? n : aDefaultValue;
}
#ifndef __APPLE__
// in OSX, time_t is defined as long, so there is a ready function for it
static const time_t fromStr(const cpcc_char* strValue, const time_t aDefaultValue)
{
cpcc_char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
time_t n = cpcc_strtol(strValue, &end, 0);
return end > strValue ? n : aDefaultValue;
}
#endif
static const bool fromStrNew(const cpcc_char* strValue, int& aValue)
{
cpcc_char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
int n = (int)cpcc_strtol(strValue, &end, 0);
if (end <= strValue) return false;
aValue = n;
return true;
}
static const int fromStr(const cpcc_char* strValue, const int aDefaultValue)
{
cpcc_char* end;
// This parses "1234" (decimal) and also "0x4D2" (hex)
int n = (int) cpcc_strtol(strValue, &end, 0);
return end > strValue ? n : aDefaultValue;
}
//static cpcc_string toStr(bool value) { return cpcc_string(value ? "yes" : "no"); }
#ifndef __APPLE__
// in OSX, time_t is defined as long, so there is a ready function for it
// in Windows time_t is _int64
static cpcc_string toStr(const time_t value) { cpcc_ostringstream ss; ss << value; return ss.str(); }
#endif
static cpcc_string toStr(const long int value) { cpcc_ostringstream ss; ss << value; return ss.str(); }
static cpcc_string toStr(const int value) { cpcc_ostringstream ss; ss << value; return ss.str(); }
static cpcc_string toStr(const float value)
{
cpcc_ostringstream ss;
ss.precision(12);
ss << value;
return ss.str();
// cpcc_char buf[100];
// #pragma warning(disable : 4996)
// cpcc_sprintf(buf, _T("%.12f"), value);
// return cpcc_string(buf);
}
static cpcc_string toStr(const double value)
{
cpcc_ostringstream ss;
ss.precision(15);
ss << value;
return ss.str();
//cpcc_char buf[200];
//#pragma warning(disable : 4996)
//cpcc_sprintf(buf, _T("%.12f"), value);
//return cpcc_string(buf);
}
};
*/
TEST_RUN(stringUtils_test)
{
const bool skipThisTest = false;
if (skipThisTest)
{
TEST_ADDNOTE("Test skipped");
return;
}
TEST_EXPECT((stringUtils::trimWhiteSpaces(_T(" text 1 ")) == _T("text 1")), _T("SelfTest #9138a: trimWhiteSpaces() failed"));
TEST_EXPECT((stringUtils::trimWhiteSpaces(_T("text 2")) == _T("text 2")), _T("SelfTest #9138b: trimWhiteSpaces() failed"));
TEST_EXPECT((stringUtils::trimWhiteSpaces(_T(" t ext 3 ")) == _T("t ext 3")), _T("SelfTest #9138c: trimWhiteSpaces() failed"));
TEST_EXPECT((stringUtils::trimWhiteSpaces(_T("one\ttwo")) == _T("one\ttwo")), _T("SelfTest #9138d: trimWhiteSpaces() failed"));
TEST_EXPECT((stringUtils::removeExtraSpaces(_T("te x t 2")) == _T("te x t 2")), _T("SelfTest #9138e: removeExtraSpaces() failed"));
TEST_EXPECT((stringUtils::removeExtraSpaces(_T(" t ext 3 ")) == _T("t ext 3")), _T("SelfTest #9138f: removeExtraSpaces() failed"));
TEST_EXPECT((stringUtils::removeExtraSpaces(_T(" te xt 1 ")) == _T("te xt 1")), _T("SelfTest #9138g: removeExtraSpaces() failed"));
cpcc_string s(_T(" hello "));
stringUtils::stringTrimL(s);
TEST_EXPECT((s.compare(_T("hello ")) == 0) , _T("#2381a: stringTrimL"));
stringUtils::stringTrimR(s);
TEST_EXPECT((s.compare(_T("hello")) == 0), _T("#2381b: stringTrimR"));
s = _T(" ");
stringUtils::stringTrim(s);
TEST_EXPECT((s.compare(_T("")) == 0), _T("#2381c: stringTrim"));
stringUtils::stringTrim(s);
TEST_EXPECT((s.compare(_T("")) == 0), _T("#2381d: stringTrim"));
s = _T("word1.word2 ");
stringUtils::removeFromStringToTheEnd(s, _T("."));
TEST_EXPECT((s.compare(_T("word1")) == 0), _T("#2381e: removeFromStringToTheEnd"));
stringUtils::removeFromStringToTheEnd(s, _T("2"));
TEST_EXPECT((s.compare(_T("word1")) == 0), _T("#2381f: removeFromStringToTheEnd"));
stringUtils::removeFromStringToTheEnd(s, _T("ord1"));
TEST_EXPECT((s.compare(_T("w")) == 0), _T("#2381h: removeFromStringToTheEnd"));
s = _T("w");
stringUtils::removeFromStringToTheEnd(s, _T(""));
TEST_EXPECT((s.compare(_T("w")) == 0), _T("#2381g: removeFromStringToTheEnd"));
s = _T("");
stringUtils::removeFromStringToTheEnd(s, _T("xxx"));
TEST_EXPECT((s.compare(_T("")) == 0), _T("#2381k: removeFromStringToTheEnd"));
}
TEST_RUN(strConvertionsV3_test)
{
time_t aTime = time(NULL);
cpcc_string str(strConvertionsV3::toString(aTime));
time_t bTime = strConvertionsV3::fromString(str.c_str(), (time_t)1000);
TEST_EXPECT(aTime == bTime , _T("#9687: stringConversions problem with time_t"));
}