forked from sw897/string_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.hpp
More file actions
694 lines (623 loc) · 21.2 KB
/
Copy pathstring_utils.hpp
File metadata and controls
694 lines (623 loc) · 21.2 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#ifndef UTIL_STRING_UTILS_HPP
#define UTIL_STRING_UTILS_HPP
//http://sourceforge.net/projects/utfcpp/
#ifdef USE_UTFCPP
#include <utf8.h>
#endif
#ifdef _WINDOWS
#include <windows.h>
#endif
#include <cstring>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <vector>
namespace string_utils {
template <typename Str> struct tokenizer;
template <typename Str>
static Str to_upper(const Str& str)
{
Str temp(str);
std::transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
return temp;
}
template <typename Str>
static Str to_lower(const Str& str)
{
Str temp(str);
std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
return temp;
}
template <typename Str>
static Str trim_left(const Str& str)
{
Str temp(str);
for (auto it = temp.begin(); it != temp.end(); it++) {
if (!isspace(*it)) {
break;
}
}
if (it == temp.end()) {
temp.clear();
} else {
temp.erase(temp.begin(), it);
}
}
template <typename Str>
static Str trim_right(const Str& str)
{
Str temp(str);
for (auto it = temp.end() - 1; ;it--) {
if (!isspace(*it)) {
temp.erase(it + 1, temp.end());
break;
}
if (it == temp.begin()) {
temp.clear();
break;
}
}
return temp;
}
template <typename Str>
static Str trim(const Str& str)
{
Str temp = trim_left(str);
return trim_right(temp);
}
template <typename Str>
static bool starts_with(Str const & value, Str const & starting)
{
if (starting.size() > value.size()) return false;
return std::equal(starting.begin(), starting.end(), value.begin());
}
template <typename Str>
static bool ends_with(Str const & value, Str const & ending)
{
if (ending.size() > value.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
template <typename Str>
static bool equals_ignore_case(const Str& str1, const Str& str2)
{
return to_lower(str1) == to_lower(str2);
}
template <typename T, typename Str>
static T from_string(const Str& str)
{
T obj;
std::basic_istringstream<typename Str::value_type> temp(str);
temp >> obj;
return obj;
}
template <typename Str>
static bool from_string(const Str& str)
{
bool obj;
std::basic_istringstream<typename Str::value_type> temp(str);
temp >> std::boolalpha >> obj;
return obj;
}
template <typename T, typename Str>
static T from_hex_string(const Str& str)
{
T obj;
std::basic_istringstream<typename Str::value_type> temp(str);
temp >> std::hex >> obj;
return obj;
}
template <typename Str, typename T>
static Str to_string(const T& var)
{
std::basic_ostringstream<typename Str::value_type> temp;
temp << var;
return temp.str();
}
template <typename Str>
static Str to_string(bool var)
{
std::basic_ostringstream<typename Str::value_type> temp;
temp << std::boolalpha << var;
return temp.str();
}
template <typename Str, typename T>
static Str to_hex_string(const T& var, int width)
{
std::basic_ostringstream<typename Str::value_type> temp;
temp << std::hex;
if(width > 0)
{
temp << std::setw(width) << std::setfill<typename Str::value_type>('0');
}
temp << var;
return temp.str();
}
template <typename Str>
static std::vector<Str> split(Str const& str, Str const& delimiters)
{
std::vector<Str> ss;
tokenizer<Str> token(str, delimiters);
while (token.next_token())
{
ss.push_back(token.get_token());
}
return ss;
}
static std::wstring utf8_to_unicode(std::string const& str)
{
size_t source_len = str.length();
if (source_len == 0)
{
return std::wstring();
}
const char* source = str.c_str();
size_t dest_len = 0;
#ifdef _WINDOWS
dest_len = MultiByteToWideChar(CP_UTF8, 0, source, source_len, 0, 0);
#else
dest_len = mbstowcs(nullptr, source, source_len);
#endif
if (dest_len <= 0)
{
return std::wstring();
}
std::wstring dest(dest_len, L'\0');
#ifdef _WINDOWS
dest_len = MultiByteToWideChar(CP_UTF8, 0, source, source_len, &dest[0], dest.length());
#else
dest_len = mbstowcs(&dest[0], source, source_len);
#endif
if (dest_len <= 0)
{
return std::wstring();
}
return dest;
}
static std::string unicode_to_utf8(std::wstring const& str)
{
size_t source_len = str.length();
if (source_len <= 0)
{
return std::string();
}
const wchar_t * source = str.c_str();
size_t dest_len = 0;
#ifdef _WINDOWS
dest_len = WideCharToMultiByte(CP_UTF8, 0, source, source_len, 0, 0, 0, 0);
#else
dest_len = wcstombs(nullptr, source, source_len);
#endif
if (dest_len <= 0)
{
return std::string();
}
std::string dest(dest_len, '\0');
#ifdef _WINDOWS
dest_len = WideCharToMultiByte(CP_UTF8, 0, source, source_len, &dest[0], dest_len, 0, 0);
#else
dest_len = wcstombs(&dest[0], source, source_len);
#endif
if (dest_len <= 0)
{
return std::string();
}
return dest;
}
static std::wstring ansi_to_unicode(std::string const& str)
{
const char* source = str.c_str();
size_t source_len = str.length();
if (source_len == 0)
{
return std::wstring();
}
size_t dest_len = 0;
#ifdef _WINDOWS
dest_len = MultiByteToWideChar(CP_ACP, 0, source, source_len, 0, 0);
#else
dest_len = mbstowcs(nullptr, source, source_len);
#endif
if (dest_len <= 0)
{
return std::wstring();
}
std::wstring dest(dest_len, L'\0');
#ifdef _WINDOWS
dest_len = MultiByteToWideChar(CP_ACP, 0, source, source_len, &dest[0], dest.length());
#else
dest_len = mbstowcs(&dest[0], source, source_len);
#endif
if (dest_len <= 0)
{
return std::wstring();
}
return dest;
}
static std::string unicode_to_ansi(std::wstring const& str)
{
size_t source_len = str.length();
if (source_len <= 0)
{
return std::string();
}
const wchar_t * source = str.c_str();
size_t dest_len = 0;
#ifdef _WINDOWS
dest_len = WideCharToMultiByte(CP_ACP, 0, source, source_len, 0, 0, 0, 0);
#else
dest_len = wcstombs(nullptr, source, source_len);
#endif
if (dest_len <= 0)
{
return std::string();
}
std::string dest(dest_len, '\0');
#ifdef _WINDOWS
dest_len = WideCharToMultiByte(CP_ACP, 0, source, source_len, &dest[0], dest_len, 0, 0);
#else
dest_len = wcstombs(&dest[0], source, source_len);
#endif
if (dest_len <= 0)
{
return std::string();
}
return dest;
}
static std::string utf8_to_gbk(std::string const& str)
{
std::wstring temp = utf8_to_unicode(str);
#ifdef _WINDOWS
#else
setlocale(LC_ALL, "zh_CN.GBK");
#endif
std::string dest = unicode_to_ansi(temp);
#ifdef _WINDOWS
#else
setlocale(LC_ALL, "");
#endif
return dest;
}
static std::string gbk_to_utf8(std::string const& str)
{
#ifdef _WINDOWS
#else
setlocale(LC_ALL, "zh_CN.GBK");
#endif
std::wstring temp = ansi_to_unicode(str);
#ifdef _WINDOWS
#else
setlocale(LC_ALL, "");
#endif
std::string dest = unicode_to_utf8(temp);
return dest;
}
static std::string utf8_to_locale(std::string const& str)
{
#ifdef _WINDOWS
return utf8_to_gbk(str);
#else
return std::string(str);
#endif
}
static std::string locale_to_utf8(std::string const& str)
{
#ifdef _WINDOWS
return gbk_to_utf8(str);
#else
return std::string(str);
#endif
}
#ifdef USE_UTFCPP
static bool is_valid_utf8(std::string const& str)
{
return utf8::is_valid(str.begin(), str.end());
}
static bool starts_with_bom(std::string const&str)
{
return utf8::starts_with_bom(str.begin(), str.end());
}
#endif
static bool base64_decode(std::string const &input, std::string & output)
{
static const char nop = -1;
static const char decoding_data[] = {
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop, 62, nop,nop,nop, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,nop,nop, nop,nop,nop,nop,
nop, 0, 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,nop, nop,nop,nop,nop,
nop,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,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop,
nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop, nop,nop,nop,nop
};
unsigned int input_length=input.size();
const char * input_ptr = input.data();
// allocate space for output string
output.clear();
output.reserve(((input_length+2)/3)*4);
// for each 4-bytes sequence from the input, extract 4 6-bits sequences by droping first two bits
// and regenerate into 3 8-bits sequence
for (unsigned int i=0; i<input_length;i++) {
char base64code0;
char base64code1;
char base64code2 = 0; // initialized to 0 to suppress warnings
char base64code3;
base64code0 = decoding_data[static_cast<int>(input_ptr[i])];
if(base64code0==nop) // non base64 character
return false;
if(!(++i<input_length)) // we need at least two input bytes for first byte output
return false;
base64code1 = decoding_data[static_cast<int>(input_ptr[i])];
if(base64code1==nop) // non base64 character
return false;
output += ((base64code0 << 2) | ((base64code1 >> 4) & 0x3));
if(++i<input_length) {
char c = input_ptr[i];
if(c =='=') { // padding , end of input
// BOOST_ASSERT( (base64code1 & 0x0f)==0);
return true;
}
base64code2 = decoding_data[static_cast<int>(input_ptr[i])];
if(base64code2==nop) // non base64 character
return false;
output += ((base64code1 << 4) & 0xf0) | ((base64code2 >> 2) & 0x0f);
}
if(++i<input_length) {
char c = input_ptr[i];
if(c =='=') { // padding , end of input
// BOOST_ASSERT( (base64code2 & 0x03)==0);
return true;
}
base64code3 = decoding_data[static_cast<int>(input_ptr[i])];
if(base64code3==nop) // non base64 character
return false;
output += (((base64code2 << 6) & 0xc0) | base64code3 );
}
}
return true;
}
static bool base64_encode(std::string const &input, std::string & output)
{
static const char encoding_data[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned int input_length=input.size();
const char * input_ptr = input.data();
// allocate space for output string
output.clear();
output.reserve(((input_length+2)/3)*4);
// for each 3-bytes sequence from the input, extract 4 6-bits sequences and encode using
// encoding_data lookup table.
// if input do not contains enough chars to complete 3-byte sequence,use pad char '='
for (unsigned int i=0; i<input_length;i++) {
int base64code0=0;
int base64code1=0;
int base64code2=0;
int base64code3=0;
base64code0 = (input_ptr[i] >> 2) & 0x3f; // 1-byte 6 bits
output += encoding_data[base64code0];
base64code1 = (input_ptr[i] << 4 ) & 0x3f; // 1-byte 2 bits +
if (++i < input_length) {
base64code1 |= (input_ptr[i] >> 4) & 0x0f; // 2-byte 4 bits
output += encoding_data[base64code1];
base64code2 = (input_ptr[i] << 2) & 0x3f; // 2-byte 4 bits +
if (++i < input_length) {
base64code2 |= (input_ptr[i] >> 6) & 0x03; // 3-byte 2 bits
base64code3 = input_ptr[i] & 0x3f; // 3-byte 6 bits
output += encoding_data[base64code2];
output += encoding_data[base64code3];
} else {
output += encoding_data[base64code2];
output += '=';
}
} else {
output += encoding_data[base64code1];
output += '=';
output += '=';
}
}
return true;
}
static std::string url_decode(const std::string& str)
{
char decode_buf[3];
std::string result;
result.reserve(str.size());
for (std::string::size_type pos = 0; pos < str.size(); ++pos) {
switch(str[pos]) {
case '+':
// convert to space character
result += ' ';
break;
case '%':
// decode hexadecimal value
if (pos + 2 < str.size()) {
decode_buf[0] = str[++pos];
decode_buf[1] = str[++pos];
decode_buf[2] = '\0';
char decoded_char = static_cast<char>( strtol(decode_buf, 0, 16) );
// decoded_char will be '\0' if strtol can't parse decode_buf as hex
// (or if decode_buf == "00", which is also not valid).
// In this case, recover from error by not decoding.
if (decoded_char == '\0') {
result += '%';
pos -= 2;
} else
result += decoded_char;
} else {
// recover from error by not decoding character
result += '%';
}
break;
default:
// character does not need to be escaped
result += str[pos];
}
};
return result;
}
static std::string url_encode(const std::string& str)
{
char encode_buf[4];
std::string result;
encode_buf[0] = '%';
result.reserve(str.size());
// character selection for this algorithm is based on the following url:
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
for (std::string::size_type pos = 0; pos < str.size(); ++pos) {
switch(str[pos]) {
default:
if (str[pos] > 32 && str[pos] < 127) {
// character does not need to be escaped
result += str[pos];
break;
}
// else pass through to next case
case ' ':
case '$': case '&': case '+': case ',': case '/': case ':':
case ';': case '=': case '?': case '@': case '"': case '<':
case '>': case '#': case '%': case '{': case '}': case '|':
case '\\': case '^': case '~': case '[': case ']': case '`':
// the character needs to be encoded
sprintf(encode_buf+1, "%.2X", (unsigned char)(str[pos]));
result += encode_buf;
break;
}
};
return result;
}
static std::string xml_encode(const std::string& str)
{
std::string result;
result.reserve(str.size() + 20); // Assume ~5 characters converted (length increases)
const unsigned char *ptr = reinterpret_cast<const unsigned char*>(str.c_str());
const unsigned char *end_ptr = ptr + str.size();
while (ptr < end_ptr) {
// check byte ranges for valid UTF-8
// see http://en.wikipedia.org/wiki/UTF-8
// also, see http://www.w3.org/TR/REC-xml/#charsets
// this implementation is the strictest subset of both
if ((*ptr >= 0x20 && *ptr <= 0x7F) || *ptr == 0x9 || *ptr == 0xa || *ptr == 0xd) {
// regular ASCII character
switch(*ptr) {
// Escape special XML characters.
case '&':
result += "&";
break;
case '<':
result += "<";
break;
case '>':
result += ">";
break;
case '\"':
result += """;
break;
case '\'':
result += "'";
break;
default:
result += *ptr;
}
} else if (*ptr >= 0xC2 && *ptr <= 0xDF) {
// two-byte sequence
if (*(ptr+1) >= 0x80 && *(ptr+1) <= 0xBF) {
result += *ptr;
result += *(++ptr);
} else {
// insert replacement char
result += 0xef;
result += 0xbf;
result += 0xbd;
}
} else if (*ptr >= 0xE0 && *ptr <= 0xEF) {
// three-byte sequence
if (*(ptr+1) >= 0x80 && *(ptr+1) <= 0xBF
&& *(ptr+2) >= 0x80 && *(ptr+2) <= 0xBF) {
result += *ptr;
result += *(++ptr);
result += *(++ptr);
} else {
// insert replacement char
result += 0xef;
result += 0xbf;
result += 0xbd;
}
} else if (*ptr >= 0xF0 && *ptr <= 0xF4) {
// four-byte sequence
if (*(ptr+1) >= 0x80 && *(ptr+1) <= 0xBF
&& *(ptr+2) >= 0x80 && *(ptr+2) <= 0xBF
&& *(ptr+3) >= 0x80 && *(ptr+3) <= 0xBF) {
result += *ptr;
result += *(++ptr);
result += *(++ptr);
result += *(++ptr);
} else {
// insert replacement char
result += 0xef;
result += 0xbf;
result += 0xbd;
}
} else {
// insert replacement char
result += 0xef;
result += 0xbf;
result += 0xbd;
}
++ptr;
}
return result;
}
template <typename Str>
struct tokenizer
{
tokenizer(Str const& str)
: _string(str), _offset(0), _delimiters(" ")
{}
tokenizer(Str const& str, Str const& delimiters)
: _string(str), _offset(0), _delimiters(delimiters)
{}
bool next_token()
{
return next_token(_delimiters);
}
bool next_token(Str const& delimiters)
{
size_t i = _string.find_first_not_of(delimiters, _offset);
if (i == Str::npos)
{
_offset = _string.length();
return false;
}
size_t j = _string.find_first_of(delimiters, i);
if (j == Str::npos) {
_token = _string.substr(i);
_offset = _string.length();
return true;
}
_token = _string.substr(i, j - i);
_offset = j;
return true;
}
const Str get_token() const
{
return _token;
}
void reset()
{
_offset = 0;
}
size_t _offset;
const Str _string;
Str _token;
Str _delimiters;
};
}
#endif // UTIL_STRING_UTILS_HPP