forked from eteran/cpp-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.h
317 lines (269 loc) · 10 KB
/
string.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Evan Teran
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef UTILITIES_STRING_HPP_
#define UTILITIES_STRING_HPP_
#include <algorithm>
#include <cctype>
#include <iostream>
#include <iterator>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
namespace string {
//------------------------------------------------------------------------------
// Name: safe_ctype
//------------------------------------------------------------------------------
template<int (&F)(int)>
int safe_ctype(unsigned char c) {
return F(c);
}
//------------------------------------------------------------------------------
// Name: ends_with
//------------------------------------------------------------------------------
inline bool ends_with(const std::string &s, char ch) {
return !s.empty() && s.back() == ch;
}
//------------------------------------------------------------------------------
// Name: ends_with
//------------------------------------------------------------------------------
inline bool ends_with(const std::string &s, const std::string &suffix) {
return std::mismatch(suffix.rbegin(), suffix.rend(), s.rbegin()).first == suffix.rend();
}
//------------------------------------------------------------------------------
// Name: starts_with
//------------------------------------------------------------------------------
inline bool starts_with(const std::string &s, char ch) {
return !s.empty() && s.front() == ch;
}
//------------------------------------------------------------------------------
// Name: starts_with
//------------------------------------------------------------------------------
inline bool starts_with(const std::string &s, const std::string &prefix) {
return std::mismatch(prefix.begin(), prefix.end(), s.begin()).first == prefix.end();
}
//------------------------------------------------------------------------------
// Name: rtrim
//------------------------------------------------------------------------------
inline void rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(safe_ctype<std::isspace>))).base(), s.end());
}
//------------------------------------------------------------------------------
// Name: ltrim
//------------------------------------------------------------------------------
inline void ltrim(std::string &s) {
s.erase(s.begin(), find_if(s.begin(), s.end(), std::not1(std::ptr_fun(safe_ctype<std::isspace>))));
}
//------------------------------------------------------------------------------
// Name: trim
//------------------------------------------------------------------------------
inline void trim(std::string &s) {
ltrim(s);
rtrim(s);
}
//------------------------------------------------------------------------------
// Name: rtrim_copy
//------------------------------------------------------------------------------
inline std::string rtrim_copy(std::string s) {
rtrim(s);
return s;
}
//------------------------------------------------------------------------------
// Name: ltrim_copy
//------------------------------------------------------------------------------
inline std::string ltrim_copy(std::string s) {
ltrim(s);
return s;
}
//------------------------------------------------------------------------------
// Name: trim_copy
//------------------------------------------------------------------------------
inline std::string trim_copy(std::string &s) {
trim(s);
return s;
}
//------------------------------------------------------------------------------
// Name: toupper
//------------------------------------------------------------------------------
inline void toupper(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::toupper));
}
//------------------------------------------------------------------------------
// Name: tolower
//------------------------------------------------------------------------------
inline void tolower(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), std::ptr_fun<int, int>(std::tolower));
}
//------------------------------------------------------------------------------
// Name: toupper_copy
//------------------------------------------------------------------------------
inline std::string toupper_copy(std::string s) {
toupper(s);
return s;
}
//------------------------------------------------------------------------------
// Name: tolower_copy
//------------------------------------------------------------------------------
inline std::string tolower_copy(std::string s) {
tolower(s);
return s;
}
//------------------------------------------------------------------------------
// Name: implode
// Desc: join elements with a char
//------------------------------------------------------------------------------
inline std::string implode(char glue, const std::vector<std::string> &pieces) {
std::string s;
if(!pieces.empty()) {
s.append(pieces[0]);
for(size_t i = 1; i < pieces.size(); ++i) {
s.push_back(glue);
s.append(pieces[i]);
}
}
return s;
}
//------------------------------------------------------------------------------
// Name: implode
// Desc: join elements with a string
//------------------------------------------------------------------------------
inline std::string implode(const std::string &glue, const std::vector<std::string> &pieces) {
std::string s;
if(!pieces.empty()) {
s.append(pieces[0]);
for(size_t i = 1; i < pieces.size(); ++i) {
s.append(glue);
s.append(pieces[i]);
}
}
return s;
}
//------------------------------------------------------------------------------
// Name: explode
//------------------------------------------------------------------------------
inline std::vector<std::string> explode(const std::string &delimeter, const std::string &string, int limit) {
std::vector<std::string> r;
if(!string.empty()) {
if(limit >= 0) {
if(limit == 0) {
limit = 1;
}
size_t first = 0;
size_t last = string.find(delimeter);
while(last != std::string::npos) {
if(--limit == 0) {
break;
}
r.emplace_back(string.substr(first, last - first));
first = last + delimeter.size();
last = string.find(delimeter, last + delimeter.size());
}
r.emplace_back(string.substr(first));
} else {
size_t first = 0;
size_t last = string.find(delimeter);
while(last != std::string::npos) {
r.emplace_back(string.substr(first, last - first));
first = last + delimeter.size();
last = string.find(delimeter, last + delimeter.size());
}
r.emplace_back(string.substr(first));
while(limit < 0) {
r.pop_back();
++limit;
}
}
}
return r;
}
//------------------------------------------------------------------------------
// Name: explode
//------------------------------------------------------------------------------
inline std::vector<std::string> explode(const std::string &delimeter, const std::string &string) {
return explode(delimeter, string, std::numeric_limits<int>::max());
}
//------------------------------------------------------------------------------
// Name: explode
//------------------------------------------------------------------------------
inline std::vector<std::string> explode(char delimeter, const std::string &string, int limit) {
std::vector<std::string> r;
if(!string.empty()) {
if(limit >= 0) {
if(limit == 0) {
limit = 1;
}
size_t first = 0;
size_t last = string.find(delimeter);
while(last != std::string::npos) {
if(--limit == 0) {
break;
}
r.emplace_back(string.substr(first, last - first));
first = last + 1;
last = string.find(delimeter, last + 1);
}
r.emplace_back(string.substr(first));
} else {
size_t first = 0;
size_t last = string.find(delimeter);
while(last != std::string::npos) {
r.emplace_back(string.substr(first, last - first));
first = last + 1;
last = string.find(delimeter, last + 1);
}
r.emplace_back(string.substr(first));
while(limit < 0) {
r.pop_back();
++limit;
}
}
}
return r;
}
//------------------------------------------------------------------------------
// Name: explode
//------------------------------------------------------------------------------
inline std::vector<std::string> explode(char delimeter, const std::string &string) {
return explode(delimeter, string, std::numeric_limits<int>::max());
}
//------------------------------------------------------------------------------
// Name: split
//------------------------------------------------------------------------------
template <class Op>
void split(const std::string &s, char delim, Op op) {
std::stringstream ss(s);
for(std::string item; std::getline(ss, item, delim); ) {
*op++ = item;
}
}
//------------------------------------------------------------------------------
// Name: split
//------------------------------------------------------------------------------
inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
}
#endif