Repository navigation
Expand file tree
/
Copy pathstringUtil.cpp
More file actions
401 lines (372 loc) · 11.1 KB
/
Copy pathstringUtil.cpp
File metadata and controls
401 lines (372 loc) · 11.1 KB
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
400
401
#include <iostream>
#include <string>
#include <algorithm>
#include "common/stdExtension.h"
#include "common/common.h"
#include "stringUtil.h"
USING_NAMESPACE(std)
NAMESPACE_SETUP(Util)
const string StringUtil::QUOTE_SAVE_CHARS = "-_.;/?:@&=+$,";
bool StringUtil::StringFormat(string& res, const char* fmt, ...) {
// default size 256 bytes
size_t size = 256;
va_list ap;
while (true) {
res.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)res.c_str(), size, fmt, ap);
va_end(ap);
// success write to res and buffer < size
if (n > -1 && n < (int)size) {
res.resize(n);
return true;
}
// fail write
else if (n > -1) {
size = n + 1;
}
// buffer >= size
else {
size *= 2;
}
}
return false;
}
string StringUtil::StringFormat(const char* fmt, ...) {
// default size 256 bytes
size_t size = 256;
va_list ap;
string res = "";
while (true) {
res.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *)res.c_str(), size, fmt, ap);
va_end(ap);
// success write to res and buffer < size
if (n > -1 && n < (int)size) {
res.resize(n);
return res;
}
// fail write
else if (n > -1) {
size = n + 1;
}
// buffer >= size
else {
size *= 2;
}
}
return res;
}
bool StringUtil::Split(const string& str, vector<string>& vec, const string& sep, bool saveEmpty) {
if (str == ""){
return false;
}
vec.clear();
size_t start = 0;
size_t strSize = str.size();
while (start < strSize) {
size_t pos = str.find_first_of(sep, start);
if (pos == string::npos){
if (saveEmpty || pos-start > 0) {
vec.push_back(str.substr(start));
}
return true;
}
if (saveEmpty || pos-start > 0) {
vec.push_back(str.substr(start, pos-start));
}
start = pos+sep.size();
}
return true;
}
size_t StringUtil::Split(const string& str, const string& sep, bool saveEmpty) {
if (str == ""){
return 0;
}
size_t start = 0;
size_t strSize = str.size();
size_t cnt = 0;
while (start < strSize) {
size_t pos = str.find_first_of(sep, start);
if (pos == string::npos){
if (saveEmpty || pos-start > 0) {
cnt++;
}
break;
}
if (saveEmpty || pos-start > 0) {
cnt++;
}
start = pos+sep.size();
}
return cnt;
}
void StringUtil::Upper(string& str) {
size_t strSize = str.size();
for (size_t i = 0; i < strSize; ++i) {
str[i] = toupper(str[i]);
}
}
void StringUtil::Lower(string& str) {
size_t strSize = str.size();
for (size_t i = 0; i < strSize; ++i) {
str[i] = tolower(str[i]);
}
}
string StringUtil::Ltrim(string& str, const char x) {
size_t start = 0;
size_t len = str.length();
while (start < len && str[start] == x) {
++start;
}
return str.substr(start);
}
string StringUtil::Rtrim(string& str, const char x) {
// var end type must be int not size_t
// because when str is empty length()-1 set to size_t will be return mas number
int end = str.length() - 1;
while (end > (-1) && str[end] == x) {
--end;
}
return str.substr(0, end+1);
}
string StringUtil::Trim(string& str, const char x) {
str = Rtrim(str, x);
return Ltrim(str, x);
}
string StringUtil::Trim(char* ptr, const char x) {
string str = string(ptr);
str = Rtrim(str, x);
return Ltrim(str, x);
}
bool StringUtil::StartsWith(const string& str, const string& prefix) {
size_t strSize = str.length();
size_t prefixSize = prefix.length();
if (strSize < prefixSize) {
return false;
}
return str.compare(0, prefixSize, prefix) == 0;
}
bool StringUtil::StartsWith(const char* ptr, const string& prefix) {
string str = string(ptr);
return StartsWith(str, prefix);
}
bool StringUtil::EndsWith(const string& str, const string& suffix) {
size_t strSize = str.length();
size_t suffixSize = suffix.length();
if (strSize < suffixSize) {
return false;
}
return str.compare((strSize-suffixSize), suffixSize, suffix) == 0;
}
bool StringUtil::IsInStr(const string& str, char x) {
return str.find(x) != string::npos;
}
uint16_t StringUtil::TwoCharToUint16(char high, char low) {
return (((uint16_t)high & 0x00ff) << 8) | ((uint16_t)low & 0x00ff);
}
string StringUtil::GetCurrentTime(const string format) {
time_t timeNow;
time(&timeNow);
size_t maxSize = 64;
// string must be has enough storage space
string timeStr = "";
timeStr.resize(64);
size_t len = strftime(const_cast<char*>(timeStr.c_str()), (size_t)maxSize, format.c_str(), localtime(&timeNow));
timeStr.resize(len);
return timeStr;
}
/*
| Unicode符号范围 | UTF-8编码方式
n | (十六进制) | (二进制)
---+-----------------------+------------------------------------------------------
1 | 0000 0000 ~ 0000 007F | 0xxxxxxx
2 | 0000 0080 ~ 0000 07FF | 110xxxxx 10xxxxxx
3 | 0000 0800 ~ 0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
4 | 0001 0000 ~ 0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
* */
bool StringUtil::Utf8ToUnicode(const char* const ptr, const size_t len, UnicodeContainer& vec) {
if (NULL == ptr) {
return false;
}
vec.clear();
for(size_t i = 0; i < len; i++) {
// ptr[i] must be uint8_t type
// utf8 -> 0xxxxxxx
if((uint8_t)ptr[i] <= 0x7f) {
// ptr[i] -> 0xxx xxx, total 7 bit
uint32_t uVal = (uint8_t)(ptr[i]) & 0x7f;
vec.push_back(uVal);
}
// utf-8 -> 110xxxxx 10xxxxxx
else if ((uint8_t)ptr[i] <= 0xdf && (i+1 < len)) {
// ptr[i] -> 110x xxxx, total 5 bit
uint32_t u1 = ((uint8_t)(ptr[i]) & 0x1f) << 6;
// ptr[i+1] -> 10xx xxxx, total 6 bit
uint32_t u2 = (uint8_t)(ptr[++i]) & 0x3f;
// unicode bit
uint32_t uVal = u1 | u2;
vec.push_back(uVal);
}
// utf-8 -> 1110xxxx 10xxxxxx 10xxxxxx
else if((uint8_t)ptr[i] <= 0xef && (i+2 < len)) {
// ptr[i] -> 1110xxxx, total 4 bit
uint32_t u1 = ((uint8_t)(ptr[i]) & 0x0f) << 12;
// ptr[i+1] -> 10xxxxx, total 6 bit
uint32_t u2 = ((uint8_t)(ptr[++i]) & 0x3f) << 6;
// ptr[i+2] -> 10xxxxx, total 6 bit
uint32_t u3 = ((uint8_t)(ptr[++i]) & 0x3f);
uint32_t uVal = u1 | u2 | u3;
vec.push_back(uVal);
}
// utf-8 -> 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
else if((uint8_t)ptr[i] <= 0xf7 && (i+3 < len)) {
// ptr[i] -> 11110xxx, total 3 bit
uint32_t u1 = ((uint8_t)(ptr[i]) & 0x07) << 18;
// ptr[i+1] -> 10xxxxxx, total 6 bit
uint32_t u2 = ((uint8_t)(ptr[++i]) & 0x3f) << 12;
// ptr[i+2] -> 10xxxxxx, total 6 bit
uint32_t u3 = ((uint8_t)(ptr[++i]) & 0x3f) << 6;
// ptr[i+3] -> 10xxxxxx, total 6 bit
uint32_t u4 = ((uint8_t)(ptr[++i]) & 0x3f);
uint32_t uVal = (u1 | u2) | (u3 | u4);
vec.push_back(uVal);
}
else {
return false;
}
}
return true;
}
bool StringUtil::Utf8ToUnicode(const string& str, UnicodeContainer& vec) {
return Utf8ToUnicode(str.c_str(), str.size(), vec);
}
/*
| Unicode符号范围 | UTF-8编码方式
n | (十六进制) | (二进制)
---+-----------------------+------------------------------------------------------
1 | 0000 0000 ~ 0000 007F | 0xxxxxxx
2 | 0000 0080 ~ 0000 07FF | 110xxxxx 10xxxxxx
3 | 0000 0800 ~ 0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
4 | 0001 0000 ~ 0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
*/
bool StringUtil::UnicodeToUtf8(UnicodeContainerIter begin, UnicodeContainerIter end, string& res) {
if (begin > end) {
return false;
}
res = "";
while (begin != end) {
uint32_t uVal = *begin;
if (uVal <= 0x7f) {
res += char(uVal);
}
else if (uVal <= 0x7ff) {
res += char(((uVal>>6) & 0x1f) | 0xc0);
res += char(((uVal) & 0x3f) | 0x80);
}
else if (uVal <= 0xffff) {
res += char(((uVal>>12) & 0x0f) | 0xe0);
res += char(((uVal>>6) & 0x3f) | 0x80);
res += char(((uVal) & 0x3f) | 0x80);
}
else if (uVal <= 0x10ffff){
res += char(((uVal>>18) & 0x07) | 0xf0);
res += char(((uVal>>12) & 0x3f) | 0x80);
res += char(((uVal>>6) & 0x3f) | 0x80);
res += char(((uVal) & 0x3f) | 0x80);
}
else {
return false;
}
++begin;
}
return true;
}
bool StringUtil::GBKToUnicode(const char* const ptr, const size_t len, UnicodeContainer& vec) {
vec.clear();
if (NULL == ptr) {
return false;
}
for (size_t i = 0; i < len; i++) {
// english letters ptr[i] <= 127
if ((uint8_t)ptr[i] <= 0x7f) {
uint32_t uVal = (uint8_t)ptr[i];
vec.push_back(uVal);
}
// chinese letters
else if (i+1 < len) {
uint32_t u1 = ((uint8_t(ptr[i]) & 0xff) << 8);
uint32_t u2 = (uint8_t(ptr[++i]) & 0xff);
uint32_t uVal = u1 | u2;
vec.push_back(uVal);
}
else {
return false;
}
}
return true;
}
bool StringUtil::GBKToUnicode(const string& str, UnicodeContainer& vec) {
return GBKToUnicode(str.c_str(), str.size(), vec);
}
bool StringUtil::UnicodeToGBK(UnicodeContainerIter begin, UnicodeContainerIter end, string& res) {
if (begin > end) {
return false;
}
res = "";
while (begin != end) {
uint32_t uVal = *begin;
char c1 = char((uVal >> 8) & 0xff);
char c2 = char(uVal & 0xff);
// if c1 > 127
if ((uint32_t)c1 & 0x80) {
res += c1;
}
res += c2;
++begin;
}
return true;
}
char StringUtil::ToHex(unsigned int value) {
return value > 9 ? value-10+'A' : value+'0';
}
unsigned int StringUtil::FromHex(char hex) {
return isdigit(hex) ? hex-'0' : hex-'A'+10;
}
string StringUtil::Quote(const string& rawStr) {
string quoteStr = "";
size_t rawStrSz = rawStr.size();
for (size_t i = 0; i < rawStrSz; i++) {
char c = rawStr[i];
char buff[4];
memset(buff, '\0', 4);
if (isalnum(c) || IsInStr(QUOTE_SAVE_CHARS, c)) {
buff[0] = c;
}
else {
buff[0] = '%';
buff[1] = ToHex((unsigned int)c >> 4);
buff[2] = ToHex((unsigned int)c % 16);
}
quoteStr += string(buff);
}
return quoteStr;
}
string StringUtil::UnQuote(const string& quoteStr) {
string rawStr = "";
size_t quoteStrSz = quoteStr.size();
for (size_t i = 0; i < quoteStrSz; i++) {
char c = quoteStr[i];
if (c != '%') {
rawStr += c;
}
else {
i++;
rawStr += (char)((FromHex(quoteStr[i]) << 4) + (FromHex(quoteStr[i+1])));
i++;
}
}
return rawStr;
}
NAMESPACE_END(Util)