-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring_helper.hpp
More file actions
427 lines (387 loc) · 10.9 KB
/
Copy pathstring_helper.hpp
File metadata and controls
427 lines (387 loc) · 10.9 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
/*
* Author: LowBoyTeam (https://github.com/LowBoyTeam)
* License: Code Project Open License
* Disclaimer: The software is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided.
* Copyright (c) 2016-2017.
*/
#ifndef _STRING_HELPER_HPP_INCLUDED_
#define _STRING_HELPER_HPP_INCLUDED_
#include <stdarg.h>
#include <string>
#include <vector>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <iomanip>
namespace string_helper
{
inline std::string format(const char *fmt, ...)
{
std::string strResult = "";
if (NULL != fmt)
{
va_list marker = NULL;
va_start(marker, fmt);
size_t nLength = _vscprintf(fmt, marker) + 1;
std::vector<char> vBuffer(nLength, '\0');
int nRet = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
if (nRet > 0)
{
strResult = &vBuffer[0];
}
va_end(marker);
}
return strResult;
}
inline std::wstring format(const wchar_t *fmt, ...)
{
std::wstring strResult = L"";
if (NULL != fmt)
{
va_list marker = NULL;
va_start(marker, fmt);
size_t nLength = _vscwprintf(fmt, marker) + 1;
std::vector<wchar_t> vBuffer(nLength, L'\0');
int nWritten = _vsnwprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
if (nWritten > 0)
{
strResult = &vBuffer[0];
}
va_end(marker);
}
return strResult;
}
inline std::vector<std::string> compact(const std::vector<std::string> &tokens)
{
std::vector<std::string> compacted;
for (size_t i = 0; i < tokens.size(); ++i) {
if (!tokens[i].empty()) {
compacted.push_back(tokens[i]);
}
}
return compacted;
}
inline std::vector<std::wstring> compact(const std::vector<std::wstring> &tokens)
{
std::vector<std::wstring> compacted;
for (size_t i = 0; i < tokens.size(); ++i) {
if (!tokens[i].empty()) {
compacted.push_back(tokens[i]);
}
}
return compacted;
}
inline std::vector<std::string> split(const std::string& str, const std::string& delim, const bool trim_empty = false)
{
size_t pos, last_pos = 0, len;
std::vector<std::string> tokens;
while (true) {
pos = str.find(delim, last_pos);
if (pos == std::string::npos) {
pos = str.size();
}
len = pos - last_pos;
if (!trim_empty || len != 0) {
tokens.push_back(str.substr(last_pos, len));
}
if (pos == str.size()) {
break;
}
else {
last_pos = pos + delim.size();
}
}
return tokens;
}
inline std::vector<std::wstring> split(const std::wstring& str, const std::wstring& delim, const bool trim_empty = false)
{
size_t pos, last_pos = 0, len;
std::vector<std::wstring> tokens;
while (true) {
pos = str.find(delim, last_pos);
if (pos == std::wstring::npos) {
pos = str.size();
}
len = pos - last_pos;
if (!trim_empty || len != 0) {
tokens.push_back(str.substr(last_pos, len));
}
if (pos == str.size()) {
break;
}
else {
last_pos = pos + delim.size();
}
}
return tokens;
}
inline std::string join(const std::vector<std::string> &tokens,const std::string& delim, const bool trim_empty = false)
{
if (trim_empty) {
return join(compact(tokens), delim, false);
}
else {
std::stringstream ss;
for (size_t i = 0; i < tokens.size() - 1; ++i) {
ss << tokens[i] << delim;
}
ss << tokens[tokens.size() - 1];
return ss.str();
}
}
inline std::wstring join(const std::vector<std::wstring> &tokens, const std::wstring& delim, const bool trim_empty = false)
{
if (trim_empty) {
return join(compact(tokens), delim, false);
}
else {
std::wstringstream ss;
for (size_t i = 0; i < tokens.size() - 1; ++i) {
ss << tokens[i] << delim;
}
ss << tokens[tokens.size() - 1];
return ss.str();
}
}
inline std::string trim(const std::string& str)
{
if (str.empty()) {
return str;
}
std::string sstr(str);
std::string::iterator c;
// Erase whitespace before the string
for (c = sstr.begin(); c != sstr.end() && iswspace(*c++);); sstr.erase(sstr.begin(), --c);
// Erase whitespace after the string
for (c = sstr.end(); c != sstr.begin() && iswspace(*--c);); sstr.erase(++c, sstr.end());
return sstr;
}
inline std::wstring trim(const std::wstring& str)
{
if (str.empty()) {
return str;
}
std::wstring sstr(str);
std::wstring::iterator c;
// Erase whitespace before the string
for (c = sstr.begin(); c != sstr.end() && iswspace(*c++);); sstr.erase(sstr.begin(), --c);
// Erase whitespace after the string
for (c = sstr.end(); c != sstr.begin() && iswspace(*--c);); sstr.erase(++c, sstr.end());
return sstr;
}
inline std::string repeat(const std::string& str, unsigned int times)
{
std::stringstream ss;
for (unsigned int i = 0; i < times; ++i) {
ss << str;
}
return ss.str();
}
inline std::wstring repeat(const std::wstring& str, unsigned int times)
{
std::wstringstream ss;
for (unsigned int i = 0; i < times; ++i) {
ss << str;
}
return ss.str();
}
inline std::string Toupper(const std::string& str)
{
if (str.empty()) {
return str;
}
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), toupper);
return s;
}
inline std::wstring Toupper(const std::wstring& str)
{
if (str.empty()) {
return str;
}
std::wstring s(str);
std::transform(s.begin(), s.end(), s.begin(), toupper);
return s;
}
inline std::string Tolower(const std::string& str)
{
if (str.empty()) {
return str;
}
std::string s(str);
std::transform(s.begin(), s.end(), s.begin(), tolower);
return s;
}
inline std::wstring Tolower(const std::wstring& str)
{
if (str.empty()) {
return str;
}
std::wstring s(str);
std::transform(s.begin(), s.end(), s.begin(), tolower);
return s;
}
inline std::string replace(const std::string& source,const std::string& target,const std::string& replacement)
{
std::string s(source);
std::string::size_type pos = 0;
std::string::size_type srclen = target.size();
std::string::size_type dstlen = replacement.size();
while ((pos = s.find(target, pos)) != std::string::npos)
{
s.replace(pos, srclen, replacement);
pos += dstlen;
}
return s;
}
inline std::wstring replace(const std::wstring& source, const std::wstring& target, const std::wstring& replacement)
{
std::wstring s(source);
std::wstring::size_type pos = 0;
std::wstring::size_type srclen = target.size();
std::wstring::size_type dstlen = replacement.size();
while ((pos = s.find(target, pos)) != std::wstring::npos)
{
s.replace(pos, srclen, replacement);
pos += dstlen;
}
return s;
}
inline std::string between(const std::string& str, const std::string& left, const std::string& right) {
size_t left_pos, right_pos, last_pos = 0;
left_pos = str.find(left, last_pos);
if (left_pos == std::string::npos)
return std::string();
last_pos = left_pos + left.size();
right_pos = str.find(right, last_pos);
if (right_pos == std::string::npos)
return std::string();
return str.substr(left_pos + left.size(), right_pos - left_pos - left.size());
}
inline std::wstring between(const std::wstring& str, const std::wstring& left, const std::wstring& right) {
size_t left_pos, right_pos, last_pos = 0;
left_pos = str.find(left, last_pos);
if (left_pos == std::wstring::npos)
return std::wstring();
last_pos = left_pos + left.size();
right_pos = str.find(right, last_pos);
if (right_pos == std::string::npos)
return std::wstring();
return str.substr(left_pos + left.size(), right_pos - left_pos - left.size());
}
inline std::vector<std::string> between_array(const std::string& str, const std::string& left, const std::string&right, const bool trim_empty = false)
{
size_t left_pos, right_pos, last_pos = 0, len;
std::vector<std::string> result;
while (true) {
left_pos = str.find(left, last_pos);
if (left_pos == std::string::npos) {
break;
}
else
{
last_pos = left_pos + left.size();
right_pos = str.find(right, last_pos);
if (right_pos == std::string::npos) {
break;
}
len = right_pos - left_pos - left.size();
if (len != 0 || !trim_empty)
{
result.push_back(str.substr(last_pos, len));
}
}
last_pos = right_pos + right.size();
}
return result;
}
inline std::vector<std::wstring> between_array(const std::wstring& str, const std::wstring& left, const std::wstring&right, const bool trim_empty = false)
{
size_t left_pos, right_pos, last_pos = 0, len;
std::vector<std::wstring> result;
while (true) {
left_pos = str.find(left, last_pos);
if (left_pos == std::wstring::npos) {
break;
}
else
{
last_pos = left_pos + left.size();
right_pos = str.find(right, last_pos);
if (right_pos == std::wstring::npos) {
break;
}
len = right_pos - left_pos - left.size();
if (len != 0 || !trim_empty)
{
result.push_back(str.substr(last_pos, len));
}
}
last_pos = right_pos + right.size();
}
return result;
}
inline std::string left(const std::string& str, const std::string& left)
{
std::string s(str);
size_t pos, last_pos = 0;
pos = s.find(left, last_pos);
if (pos != std::string::npos)
return s.substr(0, pos);
else
return std::string();
}
inline std::wstring left(const std::wstring& str, const std::wstring& left)
{
std::wstring s(str);
size_t pos, last_pos = 0;
pos = s.find(left, last_pos);
if (pos != std::wstring::npos)
return s.substr(0, pos);
else
return std::wstring();
}
inline std::string right(const std::string& str, const std::string& right)
{
std::string s(str);
size_t pos, last_pos = 0;
pos = s.find(right, last_pos);
if (pos != std::string::npos)
{
pos += right.length();
return s.substr(pos, str.length() - pos);
}
else
return std::string();
}
inline std::wstring right(const std::wstring& str, const std::wstring& right)
{
std::wstring s(str);
size_t pos, last_pos = 0;
pos = s.find(right, last_pos);
if (pos != std::wstring::npos)
{
pos += right.length();
return s.substr(pos, str.length() - pos);
}
else
return std::wstring();
}
inline bool is_start_with(const std::string& str, const std::string& src)
{
return str.compare(0, src.length(), src) == 0;
}
inline bool is_start_with(const std::wstring& str, const std::wstring& src)
{
return str.compare(0, src.length(), src) == 0;
}
inline bool is_end_with(const std::string& str, const std::string& src)
{
return str.compare(str.length() - src.length(), src.length(), src) == 0;
}
inline bool is_end_with(const std::wstring& str, const std::wstring& src)
{
return str.compare(str.length() - src.length(), src.length(), src) == 0;
}
}
#endif // _STRING_HELPER_HPP_INCLUDED_