-
Notifications
You must be signed in to change notification settings - Fork 1
/
string17.h
366 lines (339 loc) · 11.3 KB
/
string17.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
#include <vector>
#include <string>
#include <regex>
#ifdef __GNUC__
#include <experimental/string_view>
template<class CharT, class Traits>
using basic_string_view = std::experimental::basic_string_view<CharT, Traits>;
using string_view = std::experimental::string_view;
#else
#include <string_view>
using std::basic_string_view;
#endif // __GNUC__
using std::basic_string;
using std::vector;
using std::basic_regex;
template<
class CharT,
class Traits
>
class basic_string_view17;
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
>
class basic_string17:public basic_string<CharT,Traits,Allocator>
{
public:
using basic_string<CharT,Traits,Allocator>::basic_string;
vector<basic_string<CharT,Traits,Allocator> > splits(const basic_string<CharT,Traits,Allocator> Separator) const
{
basic_string_view17<CharT, Traits> tmp(this->data(), this->length()); // Should work with tmp(*this)
return tmp.splits(Separator);
}
// Overload with separator as simple char to allow optimization.
vector<basic_string<CharT, Traits, Allocator> > splits(const typename basic_string<CharT, Traits, Allocator>::value_type Separator) const
{
basic_string_view17<CharT, Traits> tmp(this->data(), this->length()); // Should work with tmp(*this)
return tmp.splits(Separator);
}
vector<basic_string_view<CharT, Traits> > splitsv(const basic_string_view<CharT, Traits> &Separator) const
{
basic_string_view17<CharT, Traits> tmp(this->data(), this->length()); // Should work with tmp(*this)
return tmp.splitsv(Separator);
}
vector<basic_string_view<CharT, Traits> > splitsv(const typename basic_string<CharT, Traits, Allocator>::value_type Separator) const
{
basic_string_view17<CharT, Traits> tmp(this->data(), this->length()); // Should work with tmp(*this)
return tmp.splitsv(Separator);
}
template<class T,class U>
static basic_string<CharT, Traits, Allocator> join(T &InputStringList, U Separator)
{
basic_string<CharT, Traits, Allocator> result_string;
typename basic_string<CharT, Traits, Allocator>::size_type StrLen = 0;
auto it = InputStringList.begin();
/* //Possible optimisation on string with size() method, not supported on Gcc 6.2
if constexpr( std::is_function_v<decltype(T::value_type::size)> ) {
for (; it != InputStringList.end(); ++it)
StrLen += it->size() + Separator.size();
result_string.reserve(StrLen);
} */
for (it = InputStringList.begin(); it != InputStringList.end(); it++)
{
if (it != InputStringList.begin())
result_string += Separator;
result_string += *it;
}
return result_string;
}
template<class T>
basic_string<CharT, Traits, Allocator> join(T &InputStringList)
{
basic_string<CharT, Traits, Allocator> result_string;
for (auto it = InputStringList.begin(); it != InputStringList.end(); it++)
{
if (it != InputStringList.begin())
result_string += *this;
result_string += *it;
}
return result_string;
}
};
typedef basic_string17<char> string17;
template<class T>
basic_string<typename T::value_type::value_type, typename T::value_type::traits_type>
string_join(const T &InputStringList, const basic_string_view<typename T::value_type::value_type,typename T::value_type::traits_type> Separator)
{
basic_string<typename T::value_type::value_type,typename T::value_type::traits_type> result_string;
size_t StrLen = 0;
if (InputStringList.empty())
return result_string;
auto it = InputStringList.begin();
for (; it != InputStringList.end(); ++it)
StrLen += it->size() + Separator.size();
result_string.reserve(StrLen);
result_string += *InputStringList.begin();
for (it = ++InputStringList.begin(); it != InputStringList.end(); ++it)
{
result_string += Separator;
result_string += *it;
}
return result_string;
}
template<
class CharT,
class Traits = std::char_traits<CharT>
>
class basic_string_view17 :public basic_string_view<CharT, Traits>
{
public:
using basic_string_view<CharT, Traits>::basic_string_view;
vector<basic_string<CharT, Traits> > splits(const basic_string_view<CharT, Traits> &Separator) const
{
vector<basic_string<CharT, Traits> > Result;
if(this->empty()) return Result;
typename basic_string_view<CharT, Traits>::size_type p, start = 0;
while (1)
{
p = basic_string_view<CharT, Traits>::find(Separator, start);
if (p == basic_string_view<CharT, Traits>::npos)
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start));
return Result;
}
else
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, p - start));
start = p + Separator.length();
}
}
}
// Overload with separator as simple char to allow optimization.
vector<basic_string<CharT, Traits> > splits(const typename basic_string_view<CharT, Traits>::value_type Separator) const
{
vector<basic_string<CharT, Traits> > Result;
if(this->empty()) return Result;
typename basic_string_view<CharT, Traits>::size_type p = 0, start = 0;
while (1)
{
if (p >= this->size())
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start));
return Result;
}
if ((*this)[p] == Separator)
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, p - start));
++p;
start = p;
}
else
{
++p;
}
}
}
vector<basic_string_view<CharT, Traits> > splitsv(const basic_string_view<CharT, Traits> &Separator) const
{
if(Separator.size()<=1){
if(Separator.size()==0)
return this->splitsv(0);
return this->splitsv(Separator[0]);
}
vector<basic_string_view<CharT, Traits> > Result;
if(this->empty()) return Result;
typename basic_string_view<CharT, Traits>::size_type p, start = 0;
while (1)
{
p = basic_string_view<CharT, Traits>::find(Separator, start);
if (p == basic_string_view<CharT, Traits>::npos)
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, basic_string_view<CharT, Traits>::npos));
return Result;
}
else
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, p - start));
start = p + Separator.length();
}
}
}
// Overload with separator as simple char to allow optimization.
vector<basic_string_view<CharT, Traits> > splitsv(const typename basic_string_view<CharT, Traits>::value_type Separator) const
{
vector<basic_string_view<CharT, Traits> > Result;
if(this->empty()) return Result;
typename basic_string_view<CharT, Traits>::size_type p = 0, start = 0;
if(Separator==0) {
for(p=0;p<this->size();++p)
Result.emplace_back(basic_string_view<CharT, Traits>::substr(p,1));
return Result;
}
while (1)
{
if (p >= this->size())
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start));
return Result;
}
if ((*this)[p] == Separator)
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, p - start));
++p;
start = p;
}
else
{
++p;
}
}
}
//******************************************************************************
template <class F>
void splitf(const basic_string_view<CharT, Traits> &Separator,F functor) const
{
if(Separator.size()<=1){
if(Separator.size()==0)
this->splitf(0,functor);
else
this->splitf(Separator[0],functor);
return;
}
if(this->empty()) return ;
typename basic_string_view<CharT, Traits>::size_type p, start = 0;
while (1)
{
p = basic_string_view<CharT, Traits>::find(Separator, start);
if (p == basic_string_view<CharT, Traits>::npos)
{
functor(basic_string_view<CharT, Traits>::substr(start, basic_string_view<CharT, Traits>::npos));
return ;
}
else
{
functor(basic_string_view<CharT, Traits>::substr(start, p - start));
start = p + Separator.length();
}
}
}
// Overload with separator as simple char to allow optimization.
template <class F>
void splitf(const typename basic_string_view<CharT, Traits>::value_type Separator,F functor) const
{
if(this->empty()) return ;
typename basic_string_view<CharT, Traits>::size_type p = 0, start = 0;
if(Separator==0) {
for(p=0;p<this->size();++p)
functor(basic_string_view<CharT, Traits>::substr(p,1));
return ;
}
while (1)
{
if (p >= this->size())
{
functor(basic_string_view<CharT, Traits>::substr(start));
return;
}
if ((*this)[p] == Separator)
{
functor(basic_string_view<CharT, Traits>::substr(start, p - start));
++p;
start = p;
}
else
{
++p;
}
}
}
//******************************************************************************
template <class F>
void splitf(const basic_regex<CharT> &Separator,F functor) const
{
if(this->empty()) return ;
/* // Used alternate option As I've met difficulties when the separator is at the end
using token_iterator = std::regex_token_iterator<typename basic_string_view17<CharT, Traits>::iterator, CharT>;
token_iterator it_(this->begin(),this->end(),Separator,-1);
while (1)
{
if(it_==token_iterator())
return;
auto match = *it_;
std::cout<<"match.first='"<<(&*match.first)<<"', Len="<<match.length()<<std::endl;
functor(basic_string_view17<CharT, Traits>(&*match.first, match.length()));
++it_;
}
*/
using sregex_iterator = std::regex_iterator<typename basic_string_view17<CharT, Traits>::iterator, CharT>;
sregex_iterator it_(this->begin(),this->end(),Separator);
typename basic_string_view17<CharT, Traits>::size_type prev=0;
for(;it_!=sregex_iterator();++it_)
{
auto m = *it_;
//std::cout << m.str() << " at position " << m.position()<< " len= " << m.length() << '\n';
functor(this->substr(prev,m.position()-prev));
prev=m.position()+m.length();
}
functor(this->substr(prev));
//std::cout << "End with " << this->substr(prev) << '\n';
}
//******************************************************************************
vector<basic_string<CharT, Traits> > splits(const basic_regex<CharT> &Separator) const
{
vector<basic_string<CharT, Traits> > Result;
splitf(Separator,[&](const basic_string_view<CharT, Traits> &s){Result.emplace_back(s);});
return Result;
}
//******************************************************************************
vector<basic_string_view<CharT, Traits> > splitsv(const basic_regex<CharT> &Separator) const
{
vector<basic_string_view<CharT, Traits> > Result;
splitf(Separator,[&](const basic_string_view<CharT, Traits> &s){Result.emplace_back(s);});
return Result;
}
//******************************************************************************
template <class T>
void splitc(const basic_string_view<CharT, Traits> &Separator,T &Result) const
{
typename basic_string_view<CharT, Traits>::size_type p, start = 0;
Result.clear();
if(this->empty()) return ;
while (1)
{
p = basic_string_view<CharT, Traits>::find(Separator, start);
if (p == basic_string_view<CharT, Traits>::npos)
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, basic_string_view<CharT, Traits>::npos));
return ;
}
else
{
Result.emplace_back(basic_string_view<CharT, Traits>::substr(start, p - start));
start = p + Separator.length();
}
}
}
};
typedef basic_string_view17<char, std::char_traits<char> > string_view17;