-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrFunc.cpp
More file actions
477 lines (412 loc) · 8.75 KB
/
StrFunc.cpp
File metadata and controls
477 lines (412 loc) · 8.75 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "StrFunc.h"
#include <set>
int compCH(const void* argv1, const void* argv2)
{
char* pch1 = (char*)argv1;
char* pch2 = (char*)argv2;
return pch1 - pch2;
}
char* StrFunc::strcat_(char* pszDest, const char* pszSrc)
{
assert(pszDest == NULL || pszSrc == NULL);
char* p = pszDest;
while (*p++);
while (*p++ = *pszSrc++);
return pszDest; //目的是方便程序中语句内联,比如strlen(strcpy(s,t))
}
char* StrFunc::strcpy_(char*pszDest, const char* pszSrc)
{
assert(pszDest == NULL || pszSrc == NULL);
char* p = pszDest;
while (*p++ = *pszSrc++);
return pszDest;
}
int StrFunc::strcmp_(const char* pszSrc, const char* pszDest)
{
int nRet = 0;
while (!(nRet = *(unsigned char*)pszSrc - *(unsigned char*)pszDest) && *pszDest)
pszDest++, pszSrc++;
if (nRet < 0)
nRet = -1;
else if (nRet > 0)
nRet = 1;
return nRet;
}
char* StrFunc::strrchr_(const char* pszDest, char chr)
{
assert(pszDest == nullptr);
if (0 == strlen(pszDest))
return nullptr;
char* p = const_cast<char*> (pszDest + strlen(pszDest) - 1);
while (p != pszDest)
{
if (*p == chr)
return p;
p--;
}
return nullptr;
}
char* StrFunc::strchr_(const char* pszDest, char chr)
{
assert(pszDest == nullptr);
if (0 == strlen(pszDest))
return nullptr;
char* p = const_cast<char*>(pszDest);
int nSize = strlen(pszDest);
while (nSize--)
{
if (*p == chr)
return p;
}
return p;
}
unsigned long StrFunc::strtol_(const char* nptr, char** endPtr, int base)
{
const char* p = nptr;
unsigned long lRet = 0;
int ch;
unsigned long Overflow;
int sign = 0, flag, LimitRemainder;
do
{
ch = *p++;
} while (isspace(ch));
if (ch == '-')
{
sign = 1;
ch = *p++;
}
else if (ch == '+')
ch = *p++;
if (base == 0 || base == 16 && ch == '0' && (*p == 'x' || *p == 'X'))
{
ch = p[1];
p += 2;
base = 16;
}
if (base == 0)
base = ch == '0' ? 8 : 10;
Overflow = sign ? -(unsigned long)LONG_MIN : LONG_MAX;
LimitRemainder = Overflow % (unsigned long)base;
Overflow /= (unsigned long)base;
for (lRet = 0, flag = 0;; ch = *p++)
{
if (isdigit(ch))
ch -= '0';
else if (isalpha(ch))
ch -= isupper(ch) ? 'A' - 10 : 'a' - 10;
else
break;
if (ch > base)
break;
if (flag <0 || lRet > Overflow || (lRet == Overflow && ch > LimitRemainder))
flag = -1;
else
{
flag = 1;
lRet = lRet * base + ch;
}
}
if (flag < 0)
lRet = sign ? LONG_MIN : LONG_MAX;
else if (sign)
lRet = -lRet;
if (endPtr != nullptr)
*endPtr = (char*)(flag ? (p - 1) : nptr);
return lRet;
}
int StrFunc::myAtoi(std::string str)
{
int nLen = str.length();
int nIndex = 0;
int nFlag = 1;
int nValue = 0;
while (nIndex < nLen)
{
if (' ' != str.at(nIndex))
break;
nIndex++;
}
if (nIndex == nLen)
return 0;
if (str.at(nIndex) == '-' || str.at(nIndex) == '+')
{
if (str.at(nIndex) == '-')
nFlag = -1;
nIndex++;
}
int nTmp = 0;
while (nIndex < nLen)
{
if (isdigit(str.at(nIndex)))
{
nTmp = str.at(nIndex) - '0';
}
else
{
break;
}
//
nValue *= nFlag;
if (nValue > (INT_MAX / 10) || (nValue == (INT_MAX / 10) && nTmp >= 8))
{
if (nFlag > 0)
return INT_MAX;
else
return INT_MIN;
}
nValue *= nFlag;
nValue = nValue * 10 + nTmp * nFlag;
nIndex++;
}
return nValue;
}
int StrFunc::strstr_(const char* pszDest, const char* pszSrc)
{
if (nullptr == pszDest || nullptr == pszSrc || 0 == strlen(pszDest) || 0 == strlen(pszSrc))
return -1;
char* pch1 = const_cast<char*>(pszDest);
char* pch2 = nullptr;
int nPos = 0;
while (*pch1)
{
char* pTmp = pch1;
pch2 = const_cast<char*>(pszSrc);
while (*pTmp != 0 && * pch2 != 0 && (*pch2 == *pTmp))
{
pTmp++;
pch2++;
}
if (*pch2 == 0)
{
return nPos;
}
nPos++;
}
return -1;
}
int StrFunc::strstr_28(std::string haystack, std::string needle)
{
if (0 == haystack.length() && 0 == needle.length())
return 0;
if (haystack.length() < needle.length())
return -1;
int nLen1 = haystack.length();
int nLen2 = needle.length();
int nPos1 = 0;
int nPos2 = 0;
while (nPos1 < nLen1)
{
int nTmp = nPos1;
nPos2 = 0;
while ((nTmp < nLen1) && (nPos2 < nLen2) && (haystack.at(nTmp) == needle.at(nPos2)))
{
nTmp++;
nPos2++;
}
if (nPos2 == nLen2)
{
return nPos1;
}
nPos1++;
}
return -1;
}
int StrFunc::strstr_KMP(char* pszDest, const char* pszSrc)
{
if (nullptr == pszDest || nullptr == pszSrc || 0 == strlen(pszDest) || 0 == strlen(pszSrc))
return -1;
int* pNext = new int[strlen(pszSrc)];
int i = 0, j = -1;
int nLen1 = strlen(pszDest);
int nLen2 = strlen(pszSrc);
get_next(pszSrc, pNext);
// for (i = 0; i < nLen1; i++)
// {
// while (j > -1 && pszSrc[i] != pszDest[j + 1])///str和str1不匹配,且j>-1(表示str和str1有部分匹配)
// j = pNext[j];///往前回溯
// if (pszSrc[i] == pszDest[j + 1])
// j = j + 1;
// if (j == nLen2 - 1)///说明 j 移动到str1的最末端
// {
// j = -1;///重新初始化,寻找下一个
// i = i - nLen2 + 1;///i定位到该位置,外层for循环i++可以继续找下一个
// }
// }
// int i = 0;
// int j = 0;
while (i < strlen(pszDest) && j < strlen(pszSrc))
{
if (j == -1 || pszDest[i] == pszSrc[j])
{
i++;
j++;
}
else
j = pNext[j];
}
delete pNext;
if (j == strlen(pszSrc))
return i - j;
else
return -1;
}
int StrFunc::strDivid(std::vector<char*> vecStr)
{
// 字符串排序后,引入Set表比较
vector<string> vecTmp;
std::set<string> strCount;
char* pTmp = nullptr;
for (int iLoop = 0; iLoop < vecStr.size(); ++iLoop)
{
pTmp = vecStr[iLoop];
if(nullptr == pTmp)
continue;
int nLen = strlen(pTmp);
qsort(pTmp, 0, nLen - 1, compCH);
string str = pTmp;
strCount.insert(str);
}
return strCount.size();
}
void StrFunc::memset_(void* pValue, int nValue, int nSize)
{
assert(pValue == nullptr);
char* pData = (char*)pValue;
while (nSize--)
{
*pData++ = (char)nValue;
}
}
void* StrFunc::memcpy_(char* pszDest, const char* pszSrc, size_t len)
{
if (pszDest == nullptr || pszSrc == nullptr)
return nullptr;
void* pRet = pszDest;
if (pszDest < pszSrc || (char*)pszDest >= (char*)pszSrc + len)
{
while (len--)
{
*(char*)pszDest = *(char*)pszSrc;
pszDest = (char*)pszDest + 1;
pszSrc = (char*)pszSrc + 1;
}
pszDest = (char*)pRet;
}
else
{
//有内存重叠时,逆序
pszSrc = (char*)pszSrc + len - 1;
pszDest = (char*)pszDest + len - 1;
while (len--)
{
*(char*)pszDest = *(char*)pszSrc;
pszDest = (char*)pszDest - 1;
pszSrc = (char*)pszSrc - 1;
}
}
return pRet;
}
void* StrFunc::memmove_(char* pszDest, const char* pszSrc, size_t len)
{
if (pszDest == nullptr || pszSrc == nullptr)
return nullptr;
void* pRet = pszDest;
if (pszDest < pszSrc || (char*)pszDest >= (char*)pszSrc + len)
{
while (len--)
{
*(char*)pszDest = *(char*)pszSrc;
pszDest = (char*)pszDest + 1;
pszSrc = (char*)pszSrc + 1;
}
pszDest = (char*)pRet;
}
else
{
//有内存重叠时,逆序
pszSrc = (char*)pszSrc + len - 1;
pszDest = (char*)pszDest + len - 1;
while (len--)
{
*(char*)pszDest = *(char*)pszSrc;
pszDest = (char*)pszDest - 1;
pszSrc = (char*)pszSrc - 1;
}
}
return pRet;
}
void StrFunc::swap_add(int &x, int &y)
{
x = x + y;
y = x - y;
x = x - y;
}
void StrFunc::swap_memcpy(int &x, int &y)
{
char sztmp[10] = { 0 };
memcpy(sztmp, &x, sizeof(x));
memcpy(&x, &y, sizeof(y));
memcpy(&y, sztmp, sizeof(y));
}
void StrFunc::swap_xor(int& x, int& y)
{
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
int StrFunc::Convert(const char* pszNum)
{
if (pszNum == nullptr || 0 == strlen(pszNum))
return 0;
int nLen = strlen(pszNum);
int nRet = 0;
while (*pszNum)
{
int nTmp = 0;
if (*pszNum >= 'a' && *pszNum <= 'f')
nTmp = *pszNum - 'a' + 10;
else if (*pszNum >= 'A' && *pszNum <= 'F')
nTmp = *pszNum - 'A' + 10;
else if (*pszNum >= '0' && *pszNum <= '9')
nTmp = *pszNum - '0';
else
return 0;
if (nRet > (INT_MAX >> 4))
return 0;
nRet = nRet * 16 + nTmp;
}
return nRet;
}
void StrFunc::get_next(const char* pStr, int *nextArray)
{
// void get_next(char str1[], int next[])///str1为子串(模式串)
// {
// int i = 1, j = -1;
// next[0] = -1;///next[0]初始化为-1,-1表示不存在相同的最大前缀和最大后缀
//
// for (i = 1; i < strlen(str1) - 1; i++)
// {
// while (j > -1 && str1[j + 1] != str1[i])///如果下一个不同,那么j就变成next[j]
// j = next[j];///往前回溯
// if (str1[j + 1] == str1[i])///如果相同,j++
// {
// j = j + 1;
// }
// next[i] = j;///没有相同,next[i]=-1
// }
// }
//int iLoop = 1;
int j = -1;
nextArray[0] = -1;
for (int iLoop = 1; iLoop < strlen(pStr) - 1; iLoop++)
{
while (j > -1 && pStr[j + 1] != pStr[iLoop])
j = nextArray[j];
if (pStr[j + 1] == pStr[iLoop])
{
j += 1;
}
nextArray[iLoop] = j;
}
}