forked from abduld/libwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbString.h
305 lines (251 loc) · 10 KB
/
wbString.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
#ifndef __WB_STRING_H__
#define __WB_STRING_H__
#include <wb.h>
#include <vector>
#include <string>
#include <ostream>
#include <iostream>
#include <sstream>
#include <cstring>
using std::string;
using std::vector;
using std::stringstream;
using std::exception;
template <typename T> static inline string wbString(const T & x);
static inline void wbString_replace(string & value, string const & search, string const & replace) {
for (string::size_type next = value.find(search); next != string::npos; next = value.find(search, next)) {
value.replace(next, search.length(), replace);
next += replace.length();
}
}
static inline string wbString_quote(string str) {
string s = str;
wbString_replace(s, "\\", "\\\\");
s = "\"" + s + "\"";
return s;
}
static inline string wbString_quote(const char * str) {
if (str == NULL) {
return wbString_quote("");
} else {
return wbString_quote(string(str));
}
}
static inline char * wbString_duplicate(const char * str) {
if (str == NULL) {
return NULL;
} else {
char * newstr;
size_t len = strlen(str);
newstr = wbNewArray(char, len+1);
memcpy(newstr, str, len * sizeof(char));
newstr[len] = '\0';
return newstr;
}
}
static inline char * wbString_duplicate(std::string str) {
return wbString_duplicate(str.c_str());
}
static inline string wbString(void) {
string s = "";
return s;
}
template <typename T>
static inline string wbString(const T & x) {
try {
stringstream ss;
ss << x;
return ss.str();
} catch (exception & e) {
return string();
}
}
template <>
inline string wbString(const bool & x) {
return x ? "True" : "False";
}
template <>
inline string wbString(const vector<string> & x) {
stringstream ss;
ss << "{";
for (size_t ii = 0; ii < x.size(); ii++) {
ss << wbString_quote(x[ii]);
if (ii != x.size() - 1) {
ss << ", ";
}
}
ss << "}";
return ss.str();
}
template <>
inline string wbString(const vector<int> & x) {
stringstream ss;
ss << "{";
for (size_t ii = 0; ii < x.size(); ii++) {
ss << x[ii];
if (ii != x.size() - 1) {
ss << ", ";
}
}
ss << "}";
return ss.str();
}
template <>
inline string wbString(const vector<double> & x) {
stringstream ss;
ss << "{";
for (size_t ii = 0; ii < x.size(); ii++) {
ss << x[ii];
if (ii != x.size() - 1) {
ss << ", ";
}
}
ss << "}";
return ss.str();
}
template <typename T0, typename T1>
static inline string wbString(const T0 & x0, const T1 & x1) {
stringstream ss;
ss << wbString(x0) << wbString(x1);
return ss.str();
}
template <typename T0, typename T1, typename T2>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7, const T8 & x8) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7) << wbString(x8);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7, const T8 & x8, const T9 & x9) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7) << wbString(x8) << wbString(x9);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9, typename T10>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7, const T8 & x8, const T9 & x9,
const T10 & x10) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7) << wbString(x8) << wbString(x9)
<< wbString(x1);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9, typename T10, typename T11>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7, const T8 & x8, const T9 & x9,
const T10 & x10, const T11 & x11) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7) << wbString(x8) << wbString(x9)
<< wbString(x10) << wbString(x11);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9, typename T10, typename T11,
typename T12>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7, const T8 & x8, const T9 & x9,
const T10 & x10, const T11 & x11, const T12 & x12) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7) << wbString(x8) << wbString(x9)
<< wbString(x10) << wbString(x11) << wbString(x12);
return ss.str();
}
template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7, typename T8, typename T9, typename T10, typename T11,
typename T12, typename T13>
static inline string wbString(const T0 & x0, const T1 & x1, const T2 & x2, const T3 & x3, const T4 & x4,
const T5 & x5, const T6 & x6, const T7 & x7, const T8 & x8, const T9 & x9,
const T10 & x10, const T11 & x11, const T12 & x12, const T13 & x13) {
stringstream ss;
ss << wbString(x0) << wbString(x1) << wbString(x2) << wbString(x3) << wbString(x4)
<< wbString(x5) << wbString(x6) << wbString(x7) << wbString(x8) << wbString(x9)
<< wbString(x10) << wbString(x11) << wbString(x12) << wbString(x13);
return ss.str();
}
template <typename X, typename Y>
static inline wbBool wbString_sameQ(const X & x, const Y & y) {
string xs = wbString(x);
string ys = wbString(y);
return strcmp(xs.c_str(), ys.c_str()) == 0;
}
static inline wbBool wbString_sameQ(const string & x, const string & y) {
return x.compare(y) == 0;
}
static inline char * wbString_toLower(const char * str) {
if (str == NULL) {
return NULL;
} else {
char * res, * iter;
res = iter = wbString_duplicate(str);
while (*iter != '\0') {
*iter++ = tolower(*str++);
}
return res;
}
}
static inline wbBool wbString_startsWith(const char * str, const char * prefix) {
while (*prefix != '\0') {
if (*str == '\0' || *str != *prefix) {
return wbFalse;
}
str++;
prefix++;
}
return wbTrue;
}
#endif /* __WB_STRING_H__ */