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// ============================================================
19 ZF_IN const zfchar **tokens
20 , ZF_IN zfindex tokenCount
21 , ZF_IN const zfchar *toCompare
22 , ZF_IN_OPT zfindex toCompareLength = zfindexMax()
23 );
24
25// ============================================================
31 ZF_IN const zfchar *s1
32 , ZF_IN const zfchar *s2
33 ) {
34 if(s1 == s2) {
35 return zftrue;
36 }
37 else if(!s1) {
38 return (*s2 == '\0');
39 }
40 else if(!s2) {
41 return (*s1 == '\0');
42 }
43 else {
44 return (zfscmp(s1, s2) == 0);
45 }
46}
47
52 ZF_IN const zfchar *s1
53 , ZF_IN zfindex s1Len
54 , ZF_IN const zfchar *s2
55 , ZF_IN zfindex s2Len
56 ) {
57 if(s1 == s2) {
58 return zftrue;
59 }
60 else if(!s1 || s1Len == 0) {
61 return (s2Len == 0 || *s2 == '\0');
62 }
63 else if(!s2 || s2Len == 0) {
64 return (s1Len == 0 || *s1 == '\0');
65 }
66 else {
67 if(s1Len == zfindexMax()) {
68 s1Len = zfslen(s1);
69 }
70 if(s2Len == zfindexMax()) {
71 s2Len = zfslen(s2);
72 }
73 return (s1Len == s2Len && zfsncmp(s1, s2, s1Len) == 0);
74 }
75}
76
80 return (s == zfnull || *s == '\0');
81}
82
86 return (s == zfnull || *s == '\0' || len == 0);
87}
88
89// ============================================================
90// zfstringFind
93 ZF_IN const zfchar *src
94 , ZF_IN zfindex srcLen
95 , ZF_IN const zfchar *find
96 , ZF_IN_OPT zfindex findLen = zfindexMax()
97 );
100 ZF_IN const zfchar *src
101 , ZF_IN const zfchar *find
102 , ZF_IN_OPT zfindex findLen = zfindexMax()
103 ) {
104 return zfstringFind(src, zfindexMax(), find, findLen);
105}
106
108 ZF_IN const zfstring &src
109 , ZF_IN const zfchar *find
110 , ZF_IN_OPT zfindex findLen = zfindexMax()
111 ) {
112 return zfstringFind(src, src.length(), find, findLen);
113}
114// ============================================================
115// zfstringFindReversely
118 ZF_IN const zfchar *src
119 , ZF_IN zfindex srcLen
120 , ZF_IN const zfchar *find
121 , ZF_IN_OPT zfindex findLen = zfindexMax()
122 );
125 ZF_IN const zfchar *src
126 , ZF_IN const zfchar *find
127 , ZF_IN_OPT zfindex findLen = zfindexMax()
128 ) {
129 return zfstringFindReversely(src, zfindexMax(), find, findLen);
130}
131
133 ZF_IN const zfstring &src
134 , ZF_IN const zfchar *find
135 , ZF_IN_OPT zfindex findLen = zfindexMax()
136 ) {
137 return zfstringFindReversely(src, src.length(), find, findLen);
138}
139// ============================================================
140// zfstringFindCaseInsensitive
143 ZF_IN const zfchar *src
144 , ZF_IN zfindex srcLen
145 , ZF_IN const zfchar *find
146 , ZF_IN_OPT zfindex findLen = zfindexMax()
147 );
150 ZF_IN const zfchar *src
151 , ZF_IN const zfchar *find
152 , ZF_IN_OPT zfindex findLen = zfindexMax()
153 ) {
154 return zfstringFindCaseInsensitive(src, zfindexMax(), find, findLen);
155}
156
158 ZF_IN const zfstring &src
159 , ZF_IN const zfchar *find
160 , ZF_IN_OPT zfindex findLen = zfindexMax()
161 ) {
162 return zfstringFindCaseInsensitive(src, src.length(), find, findLen);
163}
164// ============================================================
165// zfstringFindCaseInsensitiveReversely
168 ZF_IN const zfchar *src
169 , ZF_IN zfindex srcLen
170 , ZF_IN const zfchar *find
171 , ZF_IN_OPT zfindex findLen = zfindexMax()
172 );
175 ZF_IN const zfchar *src
176 , ZF_IN const zfchar *find
177 , ZF_IN_OPT zfindex findLen = zfindexMax()
178 ) {
179 return zfstringFindCaseInsensitiveReversely(src, zfindexMax(), find, findLen);
180}
181
183 ZF_IN const zfstring &src
184 , ZF_IN const zfchar *find
185 , ZF_IN_OPT zfindex findLen = zfindexMax()
186 ) {
187 return zfstringFindCaseInsensitiveReversely(src, src.length(), find, findLen);
188}
189// ============================================================
190// zfstringFindFirstOf
193 ZF_IN const zfchar *src
194 , ZF_IN zfindex srcLen
195 , ZF_IN const zfchar *find
196 , ZF_IN_OPT zfindex findLen = zfindexMax()
197 );
200 ZF_IN const zfchar *src
201 , ZF_IN const zfchar *find
202 , ZF_IN_OPT zfindex findLen = zfindexMax()
203 ) {
204 return zfstringFindFirstOf(src, zfindexMax(), find, findLen);
205}
206
208 ZF_IN const zfstring &src
209 , ZF_IN const zfchar *find
210 , ZF_IN_OPT zfindex findLen = zfindexMax()
211 ) {
212 return zfstringFindFirstOf(src, src.length(), find, findLen);
213}
214// ============================================================
215// zfstringFindFirstNotOf
218 ZF_IN const zfchar *src
219 , ZF_IN zfindex srcLen
220 , ZF_IN const zfchar *find
221 , ZF_IN_OPT zfindex findLen = zfindexMax()
222 );
225 ZF_IN const zfchar *src
226 , ZF_IN const zfchar *find
227 , ZF_IN_OPT zfindex findLen = zfindexMax()
228 ) {
229 return zfstringFindFirstNotOf(src, zfindexMax(), find, findLen);
230}
231
233 ZF_IN const zfstring &src
234 , ZF_IN const zfchar *find
235 , ZF_IN_OPT zfindex findLen = zfindexMax()
236 ) {
237 return zfstringFindFirstNotOf(src, src.length(), find, findLen);
238}
239// ============================================================
240// zfstringFindLastOf
243 ZF_IN const zfchar *src
244 , ZF_IN zfindex srcLen
245 , ZF_IN const zfchar *find
246 , ZF_IN_OPT zfindex findLen = zfindexMax()
247 );
250 ZF_IN const zfchar *src
251 , ZF_IN const zfchar *find
252 , ZF_IN_OPT zfindex findLen = zfindexMax()
253 ) {
254 return zfstringFindLastOf(src, zfindexMax(), find, findLen);
255}
256
258 ZF_IN const zfstring &src
259 , ZF_IN const zfchar *find
260 , ZF_IN_OPT zfindex findLen = zfindexMax()
261 ) {
262 return zfstringFindLastOf(src, src.length(), find, findLen);
263}
264// ============================================================
265// zfstringFindLastNotOf
268 ZF_IN const zfchar *src
269 , ZF_IN zfindex srcLen
270 , ZF_IN const zfchar *find
271 , ZF_IN_OPT zfindex findLen = zfindexMax()
272 );
275 ZF_IN const zfchar *src
276 , ZF_IN const zfchar *find
277 , ZF_IN_OPT zfindex findLen = zfindexMax()
278 ) {
279 return zfstringFindLastNotOf(src, zfindexMax(), find, findLen);
280}
281
283 ZF_IN const zfstring &src
284 , ZF_IN const zfchar *find
285 , ZF_IN_OPT zfindex findLen = zfindexMax()
286 ) {
287 return zfstringFindLastNotOf(src, src.length(), find, findLen);
288}
289// ============================================================
290// zfstringReplace
293 ZF_IN const zfchar *src
294 , ZF_IN const zfchar *replaceFrom
295 , ZF_IN const zfchar *replaceTo
296 , ZF_IN_OPT zfindex maxCount = zfindexMax()
297 , ZF_OUT_OPT zfindex *replacedCount = zfnull
298 );
301 ZF_IN const zfchar *src
302 , ZF_IN const zfchar *replaceFrom
303 , ZF_IN const zfchar *replaceTo
304 , ZF_IN_OPT zfindex maxCount = zfindexMax()
305 , ZF_OUT_OPT zfindex *replacedCount = zfnull
306 );
307
308// ============================================================
309// zfstringBeginWith
312 ZF_IN const zfchar *src
313 , ZF_IN zfindex srcLen
314 , ZF_IN const zfchar *find
315 , ZF_IN_OPT zfindex findLen = zfindexMax()
316 );
319 ZF_IN const zfchar *src
320 , ZF_IN const zfchar *find
321 , ZF_IN_OPT zfindex findLen = zfindexMax()
322 ) {
323 return zfstringBeginWith(src, zfindexMax(), find, findLen);
324}
325
327 ZF_IN const zfstring &src
328 , ZF_IN const zfchar *find
329 , ZF_IN_OPT zfindex findLen = zfindexMax()
330 ) {
331 return zfstringBeginWith(src, src.length(), find, findLen);
332}
333// ============================================================
334// zfstringEndWith
337 ZF_IN const zfchar *src
338 , ZF_IN zfindex srcLen
339 , ZF_IN const zfchar *find
340 , ZF_IN_OPT zfindex findLen = zfindexMax()
341 );
344 ZF_IN const zfchar *src
345 , ZF_IN const zfchar *find
346 , ZF_IN_OPT zfindex findLen = zfindexMax()
347 ) {
348 return zfstringEndWith(src, zfindexMax(), find, findLen);
349}
350
352 ZF_IN const zfstring &src
353 , ZF_IN const zfchar *find
354 , ZF_IN_OPT zfindex findLen = zfindexMax()
355 ) {
356 return zfstringEndWith(src, src.length(), find, findLen);
357}
358
359// ============================================================
360// zfstringSplit
364 , ZF_IN const zfchar *src
365 , ZF_IN const zfchar *separator
366 , ZF_IN_OPT zfbool keepEmpty = zffalse
367 );
370 ZF_IN const zfchar *src
371 , ZF_IN const zfchar *separator
372 , ZF_IN_OPT zfbool keepEmpty = zffalse
373 ) {
375 zfstringSplitT(ret, src, separator, keepEmpty);
376 return ret;
377}
378
381 , ZF_IN const zfchar *src
382 , ZF_IN const zfchar *separator
383 , ZF_IN_OPT zfbool keepEmpty = zffalse
384 );
387 ZF_IN const zfchar *src
388 , ZF_IN const zfchar *separator
389 , ZF_IN_OPT zfbool keepEmpty = zffalse
390 ) {
392 zfstringSplitIndexT(ret, src, separator, keepEmpty);
393 return ret;
394}
395
396// ============================================================
397// other
403 , ZF_IN const zfchar *src
404 , ZF_IN_OPT zfindex srcLen = zfindexMax()
405 );
408 ZF_IN const zfchar *src
409 , ZF_IN_OPT zfindex srcLen = zfindexMax()
410 ) {
411 zfstring ret;
412 zfstringToLowerT(ret, src, srcLen);
413 return ret;
414}
415
420 , ZF_IN const zfchar *src
421 , ZF_IN_OPT zfindex srcLen = zfindexMax()
422 );
425 ZF_IN const zfchar *src
426 , ZF_IN_OPT zfindex srcLen = zfindexMax()
427 ) {
428 zfstring ret;
429 zfstringToUpperT(ret, src, srcLen);
430 return ret;
431}
432
434inline void zfstringRepeatT(
436 , ZF_IN const zfchar *token
437 , ZF_IN zfindex count
438 ) {
439 if(count != 0 && !zfstringIsEmpty(token)) {
440 zfindex len = zfslen(token);
441 for(zfindex i = 0; i < count; ++i) {
442 ret.append(token, len);
443 }
444 }
445}
446
448 ZF_IN const zfchar *token
449 , ZF_IN zfindex count
450 ) {
451 zfstring ret;
452 zfstringRepeatT(ret, token, count);
453 return ret;
454}
455
476#define zfstringSwitch(v, c0, ...) _ZFP_zfstringSwitch(v, c0 \
477 ZFM_FIX_PARAM(_ZFP_zfstringSwitchExpand, ZFM_EMPTY, ##__VA_ARGS__) \
478 , zfnull)
479#define _ZFP_zfstringSwitchExpand(arg) , ((const zfchar *)(arg))
480extern ZFLIB_ZFCore zfindex _ZFP_zfstringSwitch(
481 ZF_IN const zfchar *v
482 , ZF_IN const zfchar *c0
483 , ...
484 );
485
487
488#endif // #ifndef _ZFI_ZFCoreStringUtil_h_
489
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:424
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:79
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:369
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:407
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:30
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:386
void zfstringToUpperT(zfstring &ret)
to upper case
zfstring zfstringRepeat(const zfchar *token, zfindex count)
repeat string
Definition ZFCoreStringUtil.h:447
zfstring zfstringReplace(const zfchar *src, const zfchar *replaceFrom, const zfchar *replaceTo, zfindex maxCount=((zfindex) -1), zfindex *replacedCount=zft_zfnull)
replace string
void zfstringRepeatT(zfstring &ret, const zfchar *token, zfindex count)
repeat string
Definition ZFCoreStringUtil.h:434
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:203
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:191
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:195
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:207
_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:348