ZFFramework
 
Loading...
Searching...
No Matches
ZFCoreStringUtil.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreStringUtil_h_
7#define _ZFI_ZFCoreStringUtil_h_
8
9#include "ZFCoreArray.h"
10#include "ZFCoreStringConvert.h"
11
13
14// ============================================================
19inline zfchar *zfsCopy(ZF_IN const zfchar *src) {
20 zfchar *ret = zfnull;
21 if(src) {
22 zfindex size = (zfslen(src) + 1);
23 ret = (zfchar *)zfmalloc(size);
24 zfmemcpy(ret, src, size);
25 }
26 return ret;
27}
28
33 ZF_IN const zfchar *src
34 , ZF_IN zfindex size
35 ) {
36 zfchar *ret = zfnull;
37 if(src) {
38 if(size == zfindexMax()) {
39 size = zfslen(src);
40 }
41 ret = (zfchar *)zfmalloc(size + 1);
42 zfmemcpy(ret, src, size);
43 ret[size] = '\0';
44 }
45 return ret;
46}
47
53 ZF_IN_OUT zfchar *&dst
54 , ZF_IN const zfchar *src
55 ) {
56 if(src) {
57 zfindex size = zfslen(src) + 1;
58 dst = (zfchar *)zfrealloc(dst, size);
59 zfmemcpy(dst, src, size);
60 }
61 else {
62 zffree(dst);
63 dst = zfnull;
64 }
65 return dst;
66}
67
73 ZF_IN_OUT zfchar *&dst
74 , ZF_IN const zfchar *src
75 , ZF_IN zfindex size
76 ) {
77 if(!src) {
78 zffree(dst);
79 dst = zfnull;
80 return dst;
81 }
82
83 if(size == zfindexMax()) {
84 size = zfslen(src);
85 }
86 dst = (zfchar *)zfrealloc(dst, size + 1);
87 zfmemcpy(dst, src, size);
88 dst[size] = '\0';
89 return dst;
90}
91
95 ZF_IN_OUT zfchar *&old
96 , ZF_IN const zfchar *append
97 ) {
98 if(!old) {
99 old = zfsCopy(append);
100 return old;
101 }
102
103 zfindex oldSize = zfslen(old);
104 zfindex size = oldSize + zfslen(append) + 1;
105 old = (zfchar *)zfrealloc(old, size);
106 zfmemcpy(old + oldSize, append, size);
107 return old;
108}
109
113 ZF_IN_OUT zfchar *&old
114 , ZF_IN const zfchar *append
115 , ZF_IN zfindex size
116 ) {
117 if(!old) {
118 old = zfsCopy(append, size);
119 return old;
120 }
121
122 zfindex oldSize = zfslen(old);
123 if(size == zfindexMax()) {
124 size = zfslen(append);
125 }
126 old = (zfchar *)zfrealloc(old, oldSize + size + 1);
127 zfmemcpy(old + oldSize, append, size);
128 old[oldSize + size] = '\0';
129 return old;
130}
131
132// ============================================================
137 ZF_IN const zfchar **tokens
138 , ZF_IN zfindex tokenCount
139 , ZF_IN const zfchar *toCompare
140 , ZF_IN_OPT zfindex toCompareLength = zfindexMax()
141 );
142
143// ============================================================
149 ZF_IN const zfchar *s1
150 , ZF_IN const zfchar *s2
151 ) {
152 if(s1 == s2) {
153 return zftrue;
154 }
155 else if(!s1) {
156 return (*s2 == '\0');
157 }
158 else if(!s2) {
159 return (*s1 == '\0');
160 }
161 else {
162 return (zfscmp(s1, s2) == 0);
163 }
164}
165
170 ZF_IN const zfchar *s1
171 , ZF_IN zfindex s1Len
172 , ZF_IN const zfchar *s2
173 , ZF_IN zfindex s2Len
174 ) {
175 if(s1 == s2) {
176 return zftrue;
177 }
178 else if(!s1 || s1Len == 0) {
179 return (s2Len == 0 || *s2 == '\0');
180 }
181 else if(!s2) {
182 return (s1Len == 0 || *s1 == '\0');
183 }
184 else {
185 if(s1Len == zfindexMax()) {
186 s1Len = zfslen(s1);
187 }
188 if(s2Len == zfindexMax()) {
189 s2Len = zfslen(s2);
190 }
191 return (s1Len == s2Len && zfsncmp(s1, s2, s1Len) == 0);
192 }
193}
194
198 return (s == zfnull || *s == '\0');
199}
200
204 return (s == zfnull || *s == '\0' || len == 0);
205}
206
207// ============================================================
208// zfstringFind
211 ZF_IN const zfchar *src
212 , ZF_IN zfindex srcLen
213 , ZF_IN const zfchar *find
214 , ZF_IN_OPT zfindex findLen = zfindexMax()
215 );
218 ZF_IN const zfchar *src
219 , ZF_IN const zfchar *find
220 , ZF_IN_OPT zfindex findLen = zfindexMax()
221 ) {
222 return zfstringFind(src, zfindexMax(), find, findLen);
223}
224
226 ZF_IN const zfstring &src
227 , ZF_IN const zfchar *find
228 , ZF_IN_OPT zfindex findLen = zfindexMax()
229 ) {
230 return zfstringFind(src, src.length(), find, findLen);
231}
232// ============================================================
233// zfstringFindReversely
236 ZF_IN const zfchar *src
237 , ZF_IN zfindex srcLen
238 , ZF_IN const zfchar *find
239 , ZF_IN_OPT zfindex findLen = zfindexMax()
240 );
243 ZF_IN const zfchar *src
244 , ZF_IN const zfchar *find
245 , ZF_IN_OPT zfindex findLen = zfindexMax()
246 ) {
247 return zfstringFindReversely(src, zfindexMax(), find, findLen);
248}
249
251 ZF_IN const zfstring &src
252 , ZF_IN const zfchar *find
253 , ZF_IN_OPT zfindex findLen = zfindexMax()
254 ) {
255 return zfstringFindReversely(src, src.length(), find, findLen);
256}
257// ============================================================
258// zfstringFindCaseInsensitive
261 ZF_IN const zfchar *src
262 , ZF_IN zfindex srcLen
263 , ZF_IN const zfchar *find
264 , ZF_IN_OPT zfindex findLen = zfindexMax()
265 );
268 ZF_IN const zfchar *src
269 , ZF_IN const zfchar *find
270 , ZF_IN_OPT zfindex findLen = zfindexMax()
271 ) {
272 return zfstringFindCaseInsensitive(src, zfindexMax(), find, findLen);
273}
274
276 ZF_IN const zfstring &src
277 , ZF_IN const zfchar *find
278 , ZF_IN_OPT zfindex findLen = zfindexMax()
279 ) {
280 return zfstringFindCaseInsensitive(src, src.length(), find, findLen);
281}
282// ============================================================
283// zfstringFindCaseInsensitiveReversely
286 ZF_IN const zfchar *src
287 , ZF_IN zfindex srcLen
288 , ZF_IN const zfchar *find
289 , ZF_IN_OPT zfindex findLen = zfindexMax()
290 );
293 ZF_IN const zfchar *src
294 , ZF_IN const zfchar *find
295 , ZF_IN_OPT zfindex findLen = zfindexMax()
296 ) {
297 return zfstringFindCaseInsensitiveReversely(src, zfindexMax(), find, findLen);
298}
299
301 ZF_IN const zfstring &src
302 , ZF_IN const zfchar *find
303 , ZF_IN_OPT zfindex findLen = zfindexMax()
304 ) {
305 return zfstringFindCaseInsensitiveReversely(src, src.length(), find, findLen);
306}
307// ============================================================
308// zfstringFindFirstOf
311 ZF_IN const zfchar *src
312 , ZF_IN zfindex srcLen
313 , ZF_IN const zfchar *find
314 , ZF_IN_OPT zfindex findLen = zfindexMax()
315 );
318 ZF_IN const zfchar *src
319 , ZF_IN const zfchar *find
320 , ZF_IN_OPT zfindex findLen = zfindexMax()
321 ) {
322 return zfstringFindFirstOf(src, zfindexMax(), find, findLen);
323}
324
326 ZF_IN const zfstring &src
327 , ZF_IN const zfchar *find
328 , ZF_IN_OPT zfindex findLen = zfindexMax()
329 ) {
330 return zfstringFindFirstOf(src, src.length(), find, findLen);
331}
332// ============================================================
333// zfstringFindFirstNotOf
336 ZF_IN const zfchar *src
337 , ZF_IN zfindex srcLen
338 , ZF_IN const zfchar *find
339 , ZF_IN_OPT zfindex findLen = zfindexMax()
340 );
343 ZF_IN const zfchar *src
344 , ZF_IN const zfchar *find
345 , ZF_IN_OPT zfindex findLen = zfindexMax()
346 ) {
347 return zfstringFindFirstNotOf(src, zfindexMax(), find, findLen);
348}
349
351 ZF_IN const zfstring &src
352 , ZF_IN const zfchar *find
353 , ZF_IN_OPT zfindex findLen = zfindexMax()
354 ) {
355 return zfstringFindFirstNotOf(src, src.length(), find, findLen);
356}
357// ============================================================
358// zfstringFindLastOf
361 ZF_IN const zfchar *src
362 , ZF_IN zfindex srcLen
363 , ZF_IN const zfchar *find
364 , ZF_IN_OPT zfindex findLen = zfindexMax()
365 );
368 ZF_IN const zfchar *src
369 , ZF_IN const zfchar *find
370 , ZF_IN_OPT zfindex findLen = zfindexMax()
371 ) {
372 return zfstringFindLastOf(src, zfindexMax(), find, findLen);
373}
374
376 ZF_IN const zfstring &src
377 , ZF_IN const zfchar *find
378 , ZF_IN_OPT zfindex findLen = zfindexMax()
379 ) {
380 return zfstringFindLastOf(src, src.length(), find, findLen);
381}
382// ============================================================
383// zfstringFindLastNotOf
386 ZF_IN const zfchar *src
387 , ZF_IN zfindex srcLen
388 , ZF_IN const zfchar *find
389 , ZF_IN_OPT zfindex findLen = zfindexMax()
390 );
393 ZF_IN const zfchar *src
394 , ZF_IN const zfchar *find
395 , ZF_IN_OPT zfindex findLen = zfindexMax()
396 ) {
397 return zfstringFindLastNotOf(src, zfindexMax(), find, findLen);
398}
399
401 ZF_IN const zfstring &src
402 , ZF_IN const zfchar *find
403 , ZF_IN_OPT zfindex findLen = zfindexMax()
404 ) {
405 return zfstringFindLastNotOf(src, src.length(), find, findLen);
406}
407// ============================================================
408// zfstringReplace
411 ZF_IN const zfchar *src
412 , ZF_IN const zfchar *replaceFrom
413 , ZF_IN const zfchar *replaceTo
414 , ZF_IN_OPT zfindex maxCount = zfindexMax()
415 , ZF_OUT_OPT zfindex *replacedCount = zfnull
416 );
419 ZF_IN const zfchar *src
420 , ZF_IN const zfchar *replaceFrom
421 , ZF_IN const zfchar *replaceTo
422 , ZF_IN_OPT zfindex maxCount = zfindexMax()
423 , ZF_OUT_OPT zfindex *replacedCount = zfnull
424 );
425
426// ============================================================
427// zfstringBeginWith
430 ZF_IN const zfchar *src
431 , ZF_IN zfindex srcLen
432 , ZF_IN const zfchar *find
433 , ZF_IN_OPT zfindex findLen = zfindexMax()
434 );
437 ZF_IN const zfchar *src
438 , ZF_IN const zfchar *find
439 , ZF_IN_OPT zfindex findLen = zfindexMax()
440 ) {
441 return zfstringBeginWith(src, zfindexMax(), find, findLen);
442}
443
445 ZF_IN const zfstring &src
446 , ZF_IN const zfchar *find
447 , ZF_IN_OPT zfindex findLen = zfindexMax()
448 ) {
449 return zfstringBeginWith(src, src.length(), find, findLen);
450}
451// ============================================================
452// zfstringEndWith
455 ZF_IN const zfchar *src
456 , ZF_IN zfindex srcLen
457 , ZF_IN const zfchar *find
458 , ZF_IN_OPT zfindex findLen = zfindexMax()
459 );
462 ZF_IN const zfchar *src
463 , ZF_IN const zfchar *find
464 , ZF_IN_OPT zfindex findLen = zfindexMax()
465 ) {
466 return zfstringEndWith(src, zfindexMax(), find, findLen);
467}
468
470 ZF_IN const zfstring &src
471 , ZF_IN const zfchar *find
472 , ZF_IN_OPT zfindex findLen = zfindexMax()
473 ) {
474 return zfstringEndWith(src, src.length(), find, findLen);
475}
476
477// ============================================================
478// zfstringSplit
482 , ZF_IN const zfchar *src
483 , ZF_IN const zfchar *separator
484 , ZF_IN_OPT zfbool keepEmpty = zffalse
485 );
488 ZF_IN const zfchar *src
489 , ZF_IN const zfchar *separator
490 , ZF_IN_OPT zfbool keepEmpty = zffalse
491 ) {
493 zfstringSplitT(ret, src, separator, keepEmpty);
494 return ret;
495}
496
499 , ZF_IN const zfchar *src
500 , ZF_IN const zfchar *separator
501 , ZF_IN_OPT zfbool keepEmpty = zffalse
502 );
505 ZF_IN const zfchar *src
506 , ZF_IN const zfchar *separator
507 , ZF_IN_OPT zfbool keepEmpty = zffalse
508 ) {
510 zfstringSplitIndexT(ret, src, separator, keepEmpty);
511 return ret;
512}
513
514// ============================================================
515// other
520 ZF_IN const zfchar *src
521 , ZF_IN_OPT zfindex srcLen = zfindexMax()
522 ) {
523 zfstring ret(src, srcLen);
524 zfstringToLowerT(ret);
525 return ret;
526}
527
531 ZF_IN const zfchar *src
532 , ZF_IN_OPT zfindex srcLen = zfindexMax()
533 ) {
534 zfstring ret(src, srcLen);
535 zfstringToUpperT(ret);
536 return ret;
537}
538
540inline void zfstringRepeatT(
542 , ZF_IN const zfchar *token
543 , ZF_IN zfindex count
544 ) {
545 if(count != 0 && !zfstringIsEmpty(token)) {
546 zfindex len = zfslen(token);
547 for(zfindex i = 0; i < count; ++i) {
548 ret.append(token, len);
549 }
550 }
551}
552
554 ZF_IN const zfchar *token
555 , ZF_IN zfindex count
556 ) {
557 zfstring ret;
558 zfstringRepeatT(ret, token, count);
559 return ret;
560}
561
582#define zfstringSwitch(v, c0, ...) _ZFP_zfstringSwitch(v, c0 \
583 ZFM_FIX_PARAM(_ZFP_zfstringSwitchExpand, ZFM_EMPTY, ##__VA_ARGS__) \
584 , zfnull)
585#define _ZFP_zfstringSwitchExpand(arg) , ((const zfchar *)(arg))
586extern ZFLIB_ZFCore zfindex _ZFP_zfstringSwitch(
587 ZF_IN const zfchar *v
588 , ZF_IN const zfchar *c0
589 , ...
590 );
591
593
594#endif // #ifndef _ZFI_ZFCoreStringUtil_h_
595
light weight array
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
utilities for ZFFramework
zfstring zfstringToUpper(const zfchar *src, zfindex srcLen=((zfindex) -1))
to upper case
Definition ZFCoreStringUtil.h:530
zfindex zfstringFindFirstNotOf(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfbool zfstringBeginWith(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
void zfstringSplitIndexT(ZFCoreArray< ZFIndexRange > &ret, const zfchar *src, const zfchar *separator, zfbool keepEmpty=_ZFT_t_zffalse)
split string
void zfstringToLowerT(zfstring &ret)
to lower case
zfbool zfstringIsEmpty(const zfchar *s)
util to check whether string is empty (null or empty string)
Definition ZFCoreStringUtil.h:197
zfindex zfstringFindCaseInsensitiveReversely(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfindex zfstringFindReversely(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfindex zfstringFindLastNotOf(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfindex zfstringFindFirstOf(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfbool zfstringEndWith(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfindex zfstringFindCaseInsensitive(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfstring zfstringReplaceReversely(const zfchar *src, const zfchar *replaceFrom, const zfchar *replaceTo, zfindex maxCount=((zfindex) -1), zfindex *replacedCount=zft_zfnull)
replace string
zfindex zfstringFind(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
ZFCoreArray< zfstring > zfstringSplit(const zfchar *src, const zfchar *separator, zfbool keepEmpty=_ZFT_t_zffalse)
split string
Definition ZFCoreStringUtil.h:487
zfindex zfsCheckMatch(const zfchar **tokens, zfindex tokenCount, const zfchar *toCompare, zfindex toCompareLength=((zfindex) -1))
check whether the toCompare matches the tokens, return the index in tokens or zfindexMax() if not mat...
zfstring zfstringToLower(const zfchar *src, zfindex srcLen=((zfindex) -1))
to lower case
Definition ZFCoreStringUtil.h:519
zfchar * zfsChange(zfchar *&dst, const zfchar *src)
util method to free old string then copy and change to new string, free and set dst to null if src is...
Definition ZFCoreStringUtil.h:52
zfbool zfstringIsEqual(const zfchar *s1, const zfchar *s2)
util to compare two c strings, while null is regarded as equal to empty string
Definition ZFCoreStringUtil.h:148
void zfstringSplitT(ZFCoreArray< zfstring > &ret, const zfchar *src, const zfchar *separator, zfbool keepEmpty=_ZFT_t_zffalse)
split string
ZFCoreArray< ZFIndexRange > zfstringSplitIndex(const zfchar *src, const zfchar *separator, zfbool keepEmpty=_ZFT_t_zffalse)
split string
Definition ZFCoreStringUtil.h:504
void zfstringToUpperT(zfstring &ret)
to upper case
zfchar * zfsAppend(zfchar *&old, const zfchar *append)
util method to append string to existing string buffer, realloc if need
Definition ZFCoreStringUtil.h:94
zfstring zfstringRepeat(const zfchar *token, zfindex count)
repeat string
Definition ZFCoreStringUtil.h:553
zfstring zfstringReplace(const zfchar *src, const zfchar *replaceFrom, const zfchar *replaceTo, zfindex maxCount=((zfindex) -1), zfindex *replacedCount=zft_zfnull)
replace string
zfchar * zfsCopy(const zfchar *src)
util method to copy the contents of src, return null if src null, or empty string if src is empty str...
Definition ZFCoreStringUtil.h:19
void zfstringRepeatT(zfstring &ret, const zfchar *token, zfindex count)
repeat string
Definition ZFCoreStringUtil.h:540
zfindex zfstringFindLastOf(const zfchar *src, zfindex srcLen, const zfchar *find, zfindex findLen=((zfindex) -1))
find string
zfindex zfslen(const zfchar *s)
strlen wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:144
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
zfint zfsncmp(const zfchar *s1, const zfchar *s2, zfindex count)
strncmp wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:158
zfint zfscmp(const zfchar *s1, const zfchar *s2)
strcmp wrapper as zfchar type
Definition ZFCoreTypeDef_CharType.h:152
#define ZF_OUT_OPT
dummy macro that shows the param used as optional output
Definition ZFCoreTypeDef_ClassType.h:192
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:112
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
#define zfrealloc(oldPtr, newSize)
same as realloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:106
void * zfmemcpy(void *dst, const void *src, zfindex size)
wrapper to memcpy
Definition ZFCoreTypeDef_ClassType.h:140
#define zfmalloc(size)
same as malloc defined for future use
Definition ZFCoreTypeDef_ClassType.h:100
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
light weight array
Definition ZFCoreArray.h:331