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
|
// license:BSD-3-Clause
// copyright-holders:Couriersud
#ifndef PSTRUTIL_H_
#define PSTRUTIL_H_
///
/// \file pstrutil.h
///
#include "pgsl.h"
#include "pstring.h"
#include "ptypes.h"
#include <exception>
#include <iterator>
#include <limits>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
namespace plib
{
template<typename T>
pstring to_string(const T &v)
{
return pstring(putf8string(std::to_string(v)));
}
template<typename T>
pwstring to_wstring(const T &v)
{
return pwstring(std::to_wstring(v));
}
template<typename T>
typename T::size_type find_first_not_of(const T &str, const T &no)
{
typename T::size_type pos = 0;
for (auto it = str.begin(); it != str.end(); ++it, ++pos)
{
bool f = true;
for (typename T::value_type const jt : no)
{
if (*it == jt)
{
f = false;
break;
}
}
if (f)
return pos;
}
return T::npos;
}
template<typename T>
typename T::size_type find_last_not_of(const T &str, const T &no)
{
// FIXME: use reverse iterator
typename T::size_type last_found = T::npos;
typename T::size_type pos = 0;
for (auto it = str.begin(); it != str.end(); ++it, ++pos)
{
bool f = true;
for (typename T::value_type const jt : no)
{
if (*it == jt)
{
f = false;
break;
}
}
if (f)
last_found = pos;
}
return last_found;
}
template<typename T>
typename T::size_type find_last_of(const T &str, const T &no)
{
typename T::size_type last_found = T::npos;
typename T::size_type pos = 0;
for (auto it = str.begin(); it != str.end(); ++it, ++pos)
{
bool f = false;
for (typename T::value_type const jt : no)
{
if (*it == jt)
{
f = true;
break;
}
}
if (f)
last_found = pos;
}
return last_found;
}
template<typename T>
T ltrim(const T &str, const T &ws = T(" \t\n\r"))
{
auto f = find_first_not_of(str, ws);
return (f == T::npos) ? T() : str.substr(f);
}
template<typename T>
T rtrim(const T &str, const T &ws = T(" \t\n\r"))
{
auto f = find_last_not_of(str, ws);
return (f == T::npos) ? T() : str.substr(0, f + 1);
}
template<typename T>
T trim(const T &str, const T &ws = T(" \t\n\r"))
{
return rtrim(ltrim(str, ws), ws);
}
template<typename T>
T left(const T &str, typename T::size_type len)
{
return str.substr(0, len);
}
template<typename T>
T right(const T &str, typename T::size_type nlen)
{
return nlen >= str.length() ? str : str.substr(str.length() - nlen, nlen);
}
template<typename T>
bool startsWith(const T &str, const T &arg)
{
return (arg == left(str, arg.length()));
}
template<typename T>
bool endsWith(const T &str, const T &arg)
{
return (right(str, arg.length()) == arg);
}
template<typename T, typename TA>
bool startsWith(const T &str, const TA &arg)
{
return startsWith(str, static_cast<T>(arg));
}
template<typename T, typename TA>
bool endsWith(const T &str, const TA &arg)
{
return endsWith(str, static_cast<T>(arg));
}
template<typename T>
std::size_t strlen(const T *str)
{
const T *p = str;
while (*p != 0)
p++;
return narrow_cast<std::size_t>(p - str);
}
template<typename T>
T ucase(const T &str)
{
T ret;
for (const auto &c : str)
if (c >= 'a' && c <= 'z')
ret += (c - 'a' + 'A');
else
ret += c;
return ret;
}
template<typename T>
T lcase(const T &str)
{
T ret;
for (const auto &c : str)
if (c >= 'A' && c <= 'Z')
ret += (c - 'A' + 'a');
else
ret += c;
return ret;
}
template<typename T>
T rpad(const T &str, const T &ws, const typename T::size_type cnt)
{
// FIXME: pstringbuffer ret(*this);
T ret(str);
typename T::size_type wsl = ws.length();
for (auto i = ret.length(); i < cnt; i+=wsl)
ret += ws;
return ret;
}
template<typename T>
T replace_all(const T &str, typename T::value_type search, typename T::value_type replace)
{
T ret;
for (auto &c : str)
ret += (c == search) ? replace : c;
return ret;
}
template<typename T>
T replace_all(const T &str, const T &search, const T &replace)
{
T ret;
const typename T::size_type slen = search.length();
typename T::size_type last_s = 0;
typename T::size_type s = str.find(search, last_s);
while (s != T::npos)
{
ret += str.substr(last_s, s - last_s);
ret += replace;
last_s = s + slen;
s = str.find(search, last_s);
}
ret += str.substr(last_s);
return ret;
}
template<typename T, typename T1, typename T2>
std::enable_if_t<!std::is_integral<T1>::value, T>
replace_all(const T &str, const T1 &search, const T2 &replace)
{
return replace_all(str, static_cast<T>(search), static_cast<T>(replace));
}
template <typename T>
std::vector<T> psplit(const T &str, const T &onstr, bool ignore_empty = false)
{
std::vector<T> ret;
auto p = str.begin();
auto pn = std::search(p, str.end(), onstr.begin(), onstr.end());
const auto ol = static_cast<typename T::difference_type>(onstr.length());
while (pn != str.end())
{
if (!ignore_empty || p != pn)
ret.emplace_back(p, pn);
p = std::next(pn, ol);
pn = std::search(p, str.end(), onstr.begin(), onstr.end());
}
if (p != str.end())
{
ret.emplace_back(p, str.end());
}
return ret;
}
template <typename T>
std::vector<T> psplit(const T &str, const typename T::value_type &onstr, bool ignore_empty = false)
{
std::vector<T> ret;
auto p = str.begin();
auto pn = std::find(p, str.end(), onstr);
while (pn != str.end())
{
if (!ignore_empty || p != pn)
ret.emplace_back(p, pn);
p = std::next(pn, 1);
pn = std::find(p, str.end(), onstr);
}
if (p != str.end())
{
ret.emplace_back(p, str.end());
}
return ret;
}
template <typename T>
std::vector<T> psplit_r(const T &stri,
const T &token,
const std::size_t maxsplit)
{
T str(stri);
std::vector<T> result;
std::size_t splits = 0;
while(!str.empty())
{
std::size_t index = str.rfind(token);
bool found = index!=T::npos;
if (found)
splits++;
if ((splits <= maxsplit || maxsplit == 0) && found)
{
result.push_back(str.substr(index+token.size()));
str = str.substr(0, index);
if (str.empty())
result.push_back(str);
}
else
{
result.push_back(str);
str.clear();
}
}
return result;
}
template <typename T>
std::vector<T> psplit(const T &str, const std::vector<T> &onstrl)
{
T col = "";
std::vector<T> ret;
auto i = str.begin();
while (i != str.end())
{
auto p = T::npos;
for (std::size_t j=0; j < onstrl.size(); j++)
{
if (std::equal(onstrl[j].begin(), onstrl[j].end(), i))
{
p = j;
break;
}
}
if (p != T::npos)
{
if (!col.empty())
ret.push_back(col);
col.clear();
ret.push_back(onstrl[p]);
i = std::next(i, narrow_cast<typename T::difference_type>(onstrl[p].length()));
}
else
{
typename T::value_type c = *i;
col += c;
i++;
}
}
if (!col.empty())
ret.push_back(col);
return ret;
}
} // namespace plib
#endif // PSTRUTIL_H_
|