/
strutil.cpp
399 lines (359 loc) · 8.25 KB
/
strutil.cpp
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
/**
*
* Copyright (C) 2012-2015 Hiroyuki Baba, All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR POURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "strutil.h"
namespace strutil
{
/**
* Remove white space, tab, \n and \r from right side of the specified string
*
* @param s Input string
* @return Trimed string
*/
std::string rtrim(const std::string& s)
{
int i = s.size() - 1;
while ( i >= 0 && ( s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') ) i--;
return s.substr(0, i+1);
}
/**
* Remove white space, tab, \n and \r from left side of the specified string
*
* @param s Input string
* @return Trimed string
*/
std::string ltrim(const std::string& s)
{
int i = 0;
while ( i < s.size() && ( s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r') ) i++;
return s.substr(i, s.size() - i);
}
/**
* Remove white space, tab, \n and \r from both right and left sides of the specified string
*
* @param s Input string
* @return Trimed string
*/
std::string trim(const std::string& s)
{
return ltrim(rtrim(s));
}
/**
* Join specified strings with delimiter
*
* @param delim Delimiter string
* @param s Array of input strings
* @param Size of s
* @return Joined string
*/
std::string join(const std::string& delim, const std::string* s, int size)
{
int retLen = 0;
for(int i = 0; i < size; ++i)
{
retLen += s[i].size();
}
retLen += delim.size() * (size-1);
std::string ret;
ret.reserve(retLen);
for(int i = 0; i < size; ++i)
{
ret += s[i];
if( i < size-1 )
{
ret += delim;
}
}
return ret;
}
/**
* Join specified strings with delimiter
*
* @param delim Delimiter string
* @param s Array of input strings
* @param Size of s
* @return Joined string
*/
std::string join(const std::string& delim, const std::vector<std::string>& s)
{
return join(delim, &s[0], s.size());
}
/**
* Test if s0 is contained in s1
*
* return true if contained, otherwise false
*/
bool contained(const std::string& s0, const std::string& s1)
{
return std::string::npos != s1.find(s0, 0);
}
/**
* Test if c is contained in s1
*
* return true if contained, otherwise false
*/
bool contained(char c, const std::string& s1)
{
return std::string::npos != s1.find(c, 0);
}
/**
* Split specified string with specified delimiter
*
* @param s Input string
* @param delim Delimiter string
* @return vector of string
*
* @note Each characters in delim is considered as delimiter character
*/
std::vector<std::string> split(const std::string& s, const std::string& delim)
{
std::vector<std::string> strings;
std::string tmp;
for(int i = 0; i < s.size(); ++i)
{
if(contained(s[i], delim))
{
if(tmp.length() != 0)
{
strings.push_back(tmp);
}
tmp = "";
}else
{
tmp += s[i];
}
}
if(tmp.length() != 0)
{
strings.push_back(tmp);
}
return strings;
}
static void split_ibs0(parsecontext& pc, const std::string& s, const std::string& delim, char quote, std::vector<std::string>& ret)
{
if(pc.i == s.size())
{
throw "Parse error";
}
else if(s[pc.i] == quote || s[pc.i] == '\\' || contained(s[pc.i], delim))
{
pc.tmp += s[pc.i];
pc.i++;
split_st(pc, s, delim, quote, ret);
}else
{
throw "Parse error";
}
}
static void split_ibs1(parsecontext& pc, const std::string& s, const std::string& delim, char quote, std::vector<std::string>& ret)
{
if(pc.i == s.size())
{
throw "Parse error";
}
else if(s[pc.i] == quote || s[pc.i] == '\\' || contained(s[pc.i], delim))
{
pc.tmp += s[pc.i];
pc.i++;
split_inq(pc, s, delim, quote, ret);
}else
{
throw "Parse error";
}
}
static void split_inq(parsecontext& pc, const std::string& s, const std::string& delim, char quote, std::vector<std::string>& ret)
{
if(pc.i == s.size())
{
throw "Parse error";
}else if(s[pc.i] == '\\')
{
pc.i++;
split_ibs1(pc, s, delim, quote, ret);
}else if(s[pc.i] == quote)
{
pc.i++;
split_st(pc, s, delim, quote, ret);
}else
{
pc.tmp += s[pc.i];
pc.i++;
split_inq(pc, s, delim, quote, ret);
}
}
static void split_st(parsecontext& pc, const std::string& s, const std::string& delim, char quote, std::vector<std::string>& ret)
{
if( pc.i == s.size() || contained(s[pc.i], delim) )
{
if(pc.tmp.length() != 0)
{
ret.push_back(pc.tmp);
}
pc.tmp = "";
if(pc.i == s.size())
{
// Parse end
return;
}else
{
pc.i++;
split_st(pc, s, delim, quote, ret);
}
}else if(s[pc.i] == '\\')
{
pc.i++;
split_ibs0(pc, s, delim, quote, ret);
}else if(s[pc.i] == quote)
{
pc.i++;
split_inq(pc, s, delim, quote, ret);
}else
{
pc.tmp += s[pc.i];
pc.i++;
split_st(pc, s, delim, quote, ret);
}
}
/**
* Split specified string with specified delimiter considering quote symbol
*
* @param s Input string
* @param delim Delimiter string
* @param quote Single caracter used as quote
* @return vector of string
*
* @note Each characters in delim is considered as delimiter character
*/
std::vector<std::string> split(const std::string& s, const std::string& delim, char quote)
{
std::vector<std::string> ret;
parsecontext pc;
pc.i = 0;
split_st(pc, s, delim, quote, ret);
return ret;
}
/**
* Convert string to double
*
* @param s String
* @param success true if conversion succeed, otherwise false.
*
* @return Converted value if succeed, otherwise 0
*/
double toDouble(const std::string& s, bool& success)
{
char* e;
double v = strtod(s.c_str(), &e);
if(*e == '\0')
{
success = true;
}else
{
success = false;
}
return v;
}
/**
* Convert string to decimal integer
*
* @param s String
* @param success true if conversion succeed, otherwise false.
*
* @return Converted value if succeed, otherwise 0
*/
int toInt(const std::string& s, bool& success)
{
char* e;
int v = strtol(s.c_str(), &e, 10);
if(*e == '\0')
{
success = true;
}else
{
success = false;
}
return v;
}
/**
* Format string of double value
*
* @param f Format string ( printf form )
* @param v Value to convert
*
* @return Formatted string
*/
std::string format(const std::string& f, double v)
{
char buf[128];
snprintf(buf, sizeof(buf), f.c_str(), v);
return std::string(buf);
}
/**
* Format string of integer value
*
* @param f Format string ( printf form )
* @param v Value to convert
*
* @return Formatted string
*/
std::string format(const std::string& f, int v)
{
char buf[128];
snprintf(buf, sizeof(buf), f.c_str(), v);
return std::string(buf);
}
/**
* Convert double value to string
*
* @param v Value to convert
*
* @return String
*/
std::string toString(double v)
{
return format("%f", v);
}
/**
* Convert integer value to string
*
* @param v Value to convert
*
* @return String
*/
std::string toString(int v)
{
return format("%d", v);
}
} // namespace strutil
#if 1
#include <iostream>
int main(int argc, char** argv)
{
char in[1024];
std::cin.getline(in, sizeof(in));
std::vector<std::string> ret = strutil::split(in, ",", '\"');
std::cout << "input:[" << in << "]" << std::endl;
for(int i = 0; i < ret.size(); ++i)
{
std::cout << i << " : " << ret[i] << std::endl;
}
}
#endif