-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathstrfunc.cpp
386 lines (363 loc) · 8.87 KB
/
strfunc.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
#include "strfunc.h"
#include <algorithm>
#include <cstdio>
#ifdef _WIN32
#include <objbase.h> //win api: CoCreateGuid
#pragma comment(lib, "Ole32.lib")
#else
#include <uuid/uuid.h> //libuuid
#endif
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
#endif
void strfunc::replaceOneSubStringRef(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0 /*=0*/)
{
if (oldstring.empty() || oldstring == newstring)
{
return;
}
auto pos = s.find(oldstring, pos0);
if (pos != std::string::npos)
{
s.replace(pos, oldstring.length(), newstring);
}
}
void strfunc::replaceAllSubStringRef(std::string& s, const std::string& oldstring, const std::string& newstring)
{
if (oldstring.empty() || oldstring == newstring)
{
return;
}
auto pos = s.find(oldstring);
while (pos != std::string::npos)
{
s.replace(pos, oldstring.length(), newstring);
pos = s.find(oldstring, pos + newstring.length());
}
}
std::string strfunc::replaceOneSubString(const std::string& s, const std::string& oldstring, const std::string& newstring, int pos0 /*= 0*/)
{
std::string s1 = s;
replaceOneSubStringRef(s1, oldstring, newstring, pos0);
return s1;
}
std::string strfunc::replaceAllSubString(const std::string& s, const std::string& oldstring, const std::string& newstring)
{
std::string s1 = s;
replaceAllSubStringRef(s1, oldstring, newstring);
return s1;
}
std::string strfunc::findANumber(const std::string& s)
{
bool findPoint = false;
bool findNumber = false;
bool findE = false;
std::string n;
for (int i = 0; i < s.length(); i++)
{
char c = s[i];
if ((c >= '0' && c <= '9') || c == '-' || c == '.' || c == 'e' || c == 'E')
{
if ((c >= '0' && c <= '9') || c == '-')
{
findNumber = true;
n += c;
}
if (c == '.')
{
if (!findPoint)
{
n += c;
}
findPoint = true;
}
if (c == 'e' || c == 'E')
{
if (findNumber && !(findE))
{
n += c;
findE = true;
}
}
}
else
{
if (findNumber)
{
break;
}
}
}
return n;
}
unsigned strfunc::findTheLast(const std::string& s, const std::string& content)
{
size_t pos = 0, prepos = 0;
while (pos != std::string::npos)
{
prepos = pos;
pos = s.find(content, prepos + 1);
//printf("%d\n",pos);
}
return prepos;
}
std::vector<std::string> strfunc::splitString(std::string str, std::string pattern, bool trim_space, bool quote)
{
std::vector<std::string> result;
if (str.empty())
{
return result;
}
if (pattern.empty())
{
pattern = ",;| ";
}
auto trim = [trim_space](const std::string& s) -> std::string
{
if (trim_space)
{
std::string s1 = s;
s1.erase(0, s1.find_first_not_of(" "));
s1.erase(s1.find_last_not_of(" ") + 1);
return s1;
}
return s;
};
str += pattern[0];
bool have_space = pattern.find(" ") != std::string::npos;
auto size = str.size();
if (quote)
{
bool inquote = false;
char quotechar = '\"';
std::string::size_type pos = 0;
if (have_space)
{
pos = str.find_first_not_of(" ");
}
for (int i = pos; i < size; i++)
{
if (have_space && !inquote)
{
//当空格作为分隔符时,连续空格视为一个
while (str[i + 1] == ' ')
{
i++;
}
}
if (!inquote && (str[i] == '\"' || str[i] == '\''))
{
inquote = true;
quotechar = str[i];
}
else if (inquote && str[i] == quotechar)
{
inquote = false;
}
if (!inquote)
{
if (pattern.find_first_of(str[i]) != std::string::npos)
{
result.push_back(trim(str.substr(pos, i - pos)));
pos = i + 1;
}
}
}
}
else
{
std::string::size_type pos = 0;
for (int i = 0; i < size; i++)
{
if (have_space)
{
//当空格作为分隔符时,连续空格视为一个
while (str[i] == ' ')
{
i++;
}
}
pos = str.find_first_of(pattern, i);
if (pos < size)
{
result.push_back(trim(str.substr(i, pos - i)));
i = pos;
}
}
}
return result;
}
bool strfunc::isProChar(char c)
{
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'z') || (c >= '(' && c <= ')');
}
std::string strfunc::toLowerCase(const std::string& s)
{
std::string s1 = s;
std::transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
return s1;
}
std::string strfunc::toUpperCase(const std::string& s)
{
std::string s1 = s;
std::transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
return s1;
}
std::string strfunc::ltrim(const std::string& s)
{
size_t start = s.find_first_not_of(" \n\r\t\f\v");
return (start == std::string::npos) ? "" : s.substr(start);
}
std::string strfunc::rtrim(const std::string& s)
{
size_t end = s.find_last_not_of(" \n\r\t\f\v");
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
std::string strfunc::trim(const std::string& s)
{
return rtrim(ltrim(s));
}
bool strfunc::meet_utf8(const std::string& str)
{
unsigned int n = 0;
bool all_ascii = true;
for (const unsigned char c : str)
{
if (all_ascii && c < 0x20 || c >= 0x80)
{
all_ascii = false;
}
if (n == 0)
{
//the first of multi byte
if (c >= 0x80)
{
if (c >= 0xFC && c <= 0xFD)
{
n = 6;
}
else if (c >= 0xF8)
{
n = 5;
}
else if (c >= 0xF0)
{
n = 4;
}
else if (c >= 0xE0)
{
n = 3;
}
else if (c >= 0xC0)
{
n = 2;
}
else
{
return false;
}
n--;
}
}
else
{
//it should be 10xxxxxx
if ((c & 0xC0) != 0x80)
{
return false;
}
n--;
}
}
if (n != 0)
{
return false;
}
if (all_ascii)
{
return true;
}
return true;
}
bool strfunc::meet_gbk(const std::string& str)
{
unsigned int n = 0;
bool all_ascii = true;
for (const unsigned char c : str)
{
if (all_ascii && c < 0x20 || c >= 0x80)
{
all_ascii = false;
}
if (n == 0)
{
if (c >= 0x80)
{
if (c >= 0x81 && c <= 0xFE)
{
n = +2;
}
else
{
return false;
}
n--;
}
}
else
{
if (c < 0x40 || c > 0xFE)
{
return false;
}
n--;
}
}
if (n != 0)
{
return false;
}
if (all_ascii)
{
return true;
}
return true;
}
std::string strfunc::get_cmd_output(const std::string& cmdstring)
{
std::string str;
FILE* p_file = NULL;
const int BUF_SIZE = 1024;
char buf[BUF_SIZE];
p_file = popen(cmdstring.c_str(), "r");
if (!p_file)
{
return "";
}
while (fgets(buf, BUF_SIZE, p_file) != NULL)
{
str += buf;
}
pclose(p_file);
return str;
}
const std::string strfunc::generateUUID()
{
#ifdef _WIN32
char buf[64] = { 0 };
GUID guid;
CoCreateGuid(&guid);
_snprintf_s(buf, sizeof(buf), "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
return std::string(buf);
#else //MacOS or iOS or Linux
uuid_t guid;
uuid_string_t outstr;
uuid_generate(guid);
uuid_unparse_upper(guid, outstr);
return std::string(outstr);
#endif
}