src/ucx/string.h

changeset 306
90b6d69bb499
parent 283
c3b6ff227481
parent 300
d1f814633049
child 315
5b97de37aada
equal deleted inserted replaced
305:353d71349e61 306:90b6d69bb499
65 #define PRIsstr ".*s" 65 #define PRIsstr ".*s"
66 66
67 #ifdef __cplusplus 67 #ifdef __cplusplus
68 extern "C" { 68 extern "C" {
69 #endif 69 #endif
70
71 /** 70 /**
72 * The UCX string structure. 71 * The UCX string structure.
73 */ 72 */
74 typedef struct { 73 typedef struct {
75 /** A reference to the string (<b>not necessarily <code>NULL</code> 74 /** A reference to the string (<b>not necessarily <code>NULL</code>
77 char *ptr; 76 char *ptr;
78 /** The length of the string */ 77 /** The length of the string */
79 size_t length; 78 size_t length;
80 } sstr_t; 79 } sstr_t;
81 80
81 typedef struct {
82 const char *ptr;
83 size_t length;
84 } scstr_t;
85
86 #ifdef __cplusplus
87 }
88 #endif
89
90
91 #ifdef __cplusplus
92 inline scstr_t s2scstr(sstr_t s) {
93 scstr_t c;
94 c.ptr = s.ptr;
95 c.length = s.ptr;
96 return c;
97 }
98 inline scstr_t s2scstr(scstr_t c) {
99 return c;
100 }
101 #define SCSTR s2scstr
102 #else
103
104 scstr_t ucx_sc2sc(scstr_t c);
105 scstr_t ucx_ss2sc(sstr_t str);
106 #if __STDC_VERSION__ >= 201112L
107 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
108 #elif defined(__GNUC__) || defined(__clang__)
109 #define SCSTR(str) __builtin_choose_expr( \
110 __builtin_types_compatible_p(typeof(str), sstr_t), \
111 ucx_ss2sc, \
112 ucx_sc2sc)(str)
113 #elif defined(__sun)
114 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
115 scstr_t ucx_tmp_var_c; \
116 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
117 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
118 ucx_tmp_var_c; })
119 #else
120 scstr_t ucx_ss2c_s();
121 #define SCSTR ucx_ss2c_s
122 #endif /* C11 feature test */
123
124 #endif /* C++ */
125
126 #ifdef __cplusplus
127 extern "C" {
128 #endif
129
130
82 /** 131 /**
83 * Creates a new sstr_t based on a C string. 132 * Creates a new sstr_t based on a C string.
84 * 133 *
85 * The length is implicitly inferred by using a call to <code>strlen()</code>. 134 * The length is implicitly inferred by using a call to <code>strlen()</code>.
86 * 135 *
107 * @see sstr() 156 * @see sstr()
108 * @see S() 157 * @see S()
109 */ 158 */
110 sstr_t sstrn(char *cstring, size_t length); 159 sstr_t sstrn(char *cstring, size_t length);
111 160
161
162 scstr_t scstr(const char *cstring);
163 scstr_t scstrn(const char *cstring, size_t length);
112 164
113 /** 165 /**
114 * Returns the cumulated length of all specified strings. 166 * Returns the cumulated length of all specified strings.
115 * 167 *
116 * At least one string must be specified. 168 * At least one string must be specified.
121 * @param count the total number of specified strings (so at least 1) 173 * @param count the total number of specified strings (so at least 1)
122 * @param string the first string 174 * @param string the first string
123 * @param ... all other strings 175 * @param ... all other strings
124 * @return the cumulated length of all strings 176 * @return the cumulated length of all strings
125 */ 177 */
126 size_t sstrnlen(size_t count, sstr_t string, ...); 178 size_t ucx_strnlen(size_t count, ...);
179
180 #define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__)
127 181
128 /** 182 /**
129 * Concatenates two or more strings. 183 * Concatenates two or more strings.
130 * 184 *
131 * The resulting string will be allocated by standard <code>malloc()</code>. 185 * The resulting string will be allocated by standard <code>malloc()</code>.
134 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 188 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
135 * terminated. 189 * terminated.
136 * 190 *
137 * @param count the total number of strings to concatenate 191 * @param count the total number of strings to concatenate
138 * @param s1 first string 192 * @param s1 first string
139 * @param s2 second string
140 * @param ... all remaining strings 193 * @param ... all remaining strings
141 * @return the concatenated string 194 * @return the concatenated string
142 */ 195 */
143 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); 196 sstr_t ucx_strcat(size_t count, scstr_t s1, ...);
197
198 #define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__)
144 199
145 /** 200 /**
146 * Concatenates two or more strings using a UcxAllocator. 201 * Concatenates two or more strings using a UcxAllocator.
147 * 202 *
148 * See sstrcat() for details. 203 * See sstrcat() for details.
149 * 204 *
150 * @param a the allocator to use 205 * @param a the allocator to use
151 * @param count the total number of strings to concatenate 206 * @param count the total number of strings to concatenate
152 * @param s1 first string 207 * @param s1 first string
153 * @param s2 second string
154 * @param ... all remaining strings 208 * @param ... all remaining strings
155 * @return the concatenated string 209 * @return the concatenated string
156 */ 210 */
157 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); 211 sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
158 212
213 #define sstrcat_a(count, s1, ...) ucx_strcat_a(count, SCSTR(s1), __VA_ARGS__)
159 214
160 /** 215 /**
161 * Returns a substring starting at the specified location. 216 * Returns a substring starting at the specified location.
162 * 217 *
163 * <b>Attention:</b> the new string references the same memory area as the 218 * <b>Attention:</b> the new string references the same memory area as the
189 * @see sstrsubs() 244 * @see sstrsubs()
190 * @see sstrchr() 245 * @see sstrchr()
191 */ 246 */
192 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); 247 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
193 248
249 scstr_t scstrsubs(scstr_t s, size_t start);
250 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
251
252
253 int ucx_strchr(const char *string, size_t length, int chr, size_t *pos);
254 int ucx_strrchr(const char *string, size_t length, int chr, size_t *pos);
255
194 /** 256 /**
195 * Returns a substring starting at the location of the first occurrence of the 257 * Returns a substring starting at the location of the first occurrence of the
196 * specified character. 258 * specified character.
197 * 259 *
198 * If the string does not contain the character, an empty string is returned. 260 * If the string does not contain the character, an empty string is returned.
216 * @return a substring starting at the last location of <code>chr</code> 278 * @return a substring starting at the last location of <code>chr</code>
217 * 279 *
218 * @see sstrsubs() 280 * @see sstrsubs()
219 */ 281 */
220 sstr_t sstrrchr(sstr_t string, int chr); 282 sstr_t sstrrchr(sstr_t string, int chr);
283
284
285 scstr_t scstrchr(scstr_t string, int chr);
286 scstr_t scstrrchr(scstr_t string, int chr);
287
288 const char* ucx_strstr(
289 const char *str,
290 size_t length,
291 const char *match,
292 size_t matchlen,
293 size_t *newlen);
221 294
222 /** 295 /**
223 * Returns a substring starting at the location of the first occurrence of the 296 * Returns a substring starting at the location of the first occurrence of the
224 * specified string. 297 * specified string.
225 * 298 *
232 * @param match string containing the sequence of characters to match 305 * @param match string containing the sequence of characters to match
233 * @return a substring starting at the first occurrence of 306 * @return a substring starting at the first occurrence of
234 * <code>match</code>, or an empty string, if the sequence is not 307 * <code>match</code>, or an empty string, if the sequence is not
235 * present in <code>string</code> 308 * present in <code>string</code>
236 */ 309 */
237 sstr_t sstrstr(sstr_t string, sstr_t match); 310 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
311 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
312
313 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
314 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
238 315
239 /** 316 /**
240 * Splits a string into parts by using a delimiter string. 317 * Splits a string into parts by using a delimiter string.
241 * 318 *
242 * This function will return <code>NULL</code>, if one of the following happens: 319 * This function will return <code>NULL</code>, if one of the following happens:
281 * @return a sstr_t array containing the split strings or 358 * @return a sstr_t array containing the split strings or
282 * <code>NULL</code> on error 359 * <code>NULL</code> on error
283 * 360 *
284 * @see sstrsplit_a() 361 * @see sstrsplit_a()
285 */ 362 */
286 sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); 363 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
364
365 #define sstrsplit(s, delim, count) ucx_strsplit(SCSTR(s), SCSTR(delim), count)
287 366
288 /** 367 /**
289 * Performing sstrsplit() using a UcxAllocator. 368 * Performing sstrsplit() using a UcxAllocator.
290 * 369 *
291 * <i>Read the description of sstrsplit() for details.</i> 370 * <i>Read the description of sstrsplit() for details.</i>
305 * @return a sstr_t array containing the split strings or 384 * @return a sstr_t array containing the split strings or
306 * <code>NULL</code> on error 385 * <code>NULL</code> on error
307 * 386 *
308 * @see sstrsplit() 387 * @see sstrsplit()
309 */ 388 */
310 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, 389 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
311 ssize_t *count); 390 ssize_t *count);
391
392 #define sstrsplit_a(a, s, d, c) ucx_strsplit_a(a, SCSTR(s), SCSTR(d, c))
312 393
313 /** 394 /**
314 * Compares two UCX strings with standard <code>memcmp()</code>. 395 * Compares two UCX strings with standard <code>memcmp()</code>.
315 * 396 *
316 * At first it compares the sstr_t.length attribute of the two strings. The 397 * At first it compares the sstr_t.length attribute of the two strings. The
320 * @param s2 the second string 401 * @param s2 the second string
321 * @return -1, if the length of s1 is less than the length of s2 or 1, if the 402 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
322 * length of s1 is greater than the length of s2 or the result of 403 * length of s1 is greater than the length of s2 or the result of
323 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match) 404 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
324 */ 405 */
325 int sstrcmp(sstr_t s1, sstr_t s2); 406 int ucx_str_cmp(scstr_t s1, scstr_t s2);
407
408 #define sstrcmp(s1, s2) ucx_str_cmp(SCSTR(s1), SCSTR(s2))
326 409
327 /** 410 /**
328 * Compares two UCX strings ignoring the case. 411 * Compares two UCX strings ignoring the case.
329 * 412 *
330 * At first it compares the sstr_t.length attribute of the two strings. If and 413 * At first it compares the sstr_t.length attribute of the two strings. If and
336 * @return -1, if the length of s1 is less than the length of s2 or 1, if the 419 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
337 * length of s1 is greater than the length of s2 or the difference between the 420 * length of s1 is greater than the length of s2 or the difference between the
338 * first two differing characters otherwise (i.e. 0 if the strings match and 421 * first two differing characters otherwise (i.e. 0 if the strings match and
339 * no characters differ) 422 * no characters differ)
340 */ 423 */
341 int sstrcasecmp(sstr_t s1, sstr_t s2); 424 int ucx_str_casecmp(scstr_t s1, scstr_t s2);
425
426 #define sstrcasecmp(s1, s2) ucx_str_casecmp(SCSTR(s1), SCSTR(s2))
342 427
343 /** 428 /**
344 * Creates a duplicate of the specified string. 429 * Creates a duplicate of the specified string.
345 * 430 *
346 * The new sstr_t will contain a copy allocated by standard 431 * The new sstr_t will contain a copy allocated by standard
352 * 437 *
353 * @param string the string to duplicate 438 * @param string the string to duplicate
354 * @return a duplicate of the string 439 * @return a duplicate of the string
355 * @see sstrdup_a() 440 * @see sstrdup_a()
356 */ 441 */
357 sstr_t sstrdup(sstr_t string); 442 sstr_t scstrdup(scstr_t string);
443
444 #define sstrdup(s) scstrdup(SCSTR(s))
358 445
359 /** 446 /**
360 * Creates a duplicate of the specified string using a UcxAllocator. 447 * Creates a duplicate of the specified string using a UcxAllocator.
361 * 448 *
362 * The new sstr_t will contain a copy allocated by the allocators 449 * The new sstr_t will contain a copy allocated by the allocators
370 * @param allocator a valid instance of a UcxAllocator 457 * @param allocator a valid instance of a UcxAllocator
371 * @param string the string to duplicate 458 * @param string the string to duplicate
372 * @return a duplicate of the string 459 * @return a duplicate of the string
373 * @see sstrdup() 460 * @see sstrdup()
374 */ 461 */
375 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); 462 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
463
464 #define sstrdup_a(allocator, s) scstrdup_a(allocator, SCSTR(s))
465
466
467 size_t ucx_strtrim(const char *str, size_t length, size_t *newlen);
376 468
377 /** 469 /**
378 * Omits leading and trailing spaces. 470 * Omits leading and trailing spaces.
379 * 471 *
380 * This function returns a new sstr_t containing a trimmed version of the 472 * This function returns a new sstr_t containing a trimmed version of the
391 * @param string the string that shall be trimmed 483 * @param string the string that shall be trimmed
392 * @return a new sstr_t containing the trimmed string 484 * @return a new sstr_t containing the trimmed string
393 */ 485 */
394 sstr_t sstrtrim(sstr_t string); 486 sstr_t sstrtrim(sstr_t string);
395 487
488 scstr_t scstrtrim(scstr_t string);
489
396 /** 490 /**
397 * Checks, if a string has a specific prefix. 491 * Checks, if a string has a specific prefix.
398 * @param string the string to check 492 * @param string the string to check
399 * @param prefix the prefix the string should have 493 * @param prefix the prefix the string should have
400 * @return 1, if and only if the string has the specified prefix, 0 otherwise 494 * @return 1, if and only if the string has the specified prefix, 0 otherwise
401 */ 495 */
402 int sstrprefix(sstr_t string, sstr_t prefix); 496 int ucx_strprefix(scstr_t string, scstr_t prefix);
497
498 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
403 499
404 /** 500 /**
405 * Checks, if a string has a specific suffix. 501 * Checks, if a string has a specific suffix.
406 * @param string the string to check 502 * @param string the string to check
407 * @param suffix the suffix the string should have 503 * @param suffix the suffix the string should have
408 * @return 1, if and only if the string has the specified suffix, 0 otherwise 504 * @return 1, if and only if the string has the specified suffix, 0 otherwise
409 */ 505 */
410 int sstrsuffix(sstr_t string, sstr_t suffix); 506 int ucx_strsuffix(scstr_t string, scstr_t suffix);
507
508 #define sstrsuffix(string, prefix) ucx_strsuffix(SCSTR(string), SCSTR(prefix))
411 509
412 /** 510 /**
413 * Returns a lower case version of a string. 511 * Returns a lower case version of a string.
414 * 512 *
415 * This function creates a duplicate of the input string, first. See the 513 * This function creates a duplicate of the input string, first. See the
417 * 515 *
418 * @param string the input string 516 * @param string the input string
419 * @return the resulting lower case string 517 * @return the resulting lower case string
420 * @see sstrdup() 518 * @see sstrdup()
421 */ 519 */
422 sstr_t sstrlower(sstr_t string); 520 sstr_t ucx_strlower(scstr_t string);
521
522 #define sstrlower(string) ucx_strlower(SCSTR(string))
423 523
424 /** 524 /**
425 * Returns a lower case version of a string. 525 * Returns a lower case version of a string.
426 * 526 *
427 * This function creates a duplicate of the input string, first. See the 527 * This function creates a duplicate of the input string, first. See the
430 * @param allocator the allocator used for duplicating the string 530 * @param allocator the allocator used for duplicating the string
431 * @param string the input string 531 * @param string the input string
432 * @return the resulting lower case string 532 * @return the resulting lower case string
433 * @see sstrdup_a() 533 * @see sstrdup_a()
434 */ 534 */
435 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); 535 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
536
537 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
436 538
437 /** 539 /**
438 * Returns a upper case version of a string. 540 * Returns a upper case version of a string.
439 * 541 *
440 * This function creates a duplicate of the input string, first. See the 542 * This function creates a duplicate of the input string, first. See the
442 * 544 *
443 * @param string the input string 545 * @param string the input string
444 * @return the resulting upper case string 546 * @return the resulting upper case string
445 * @see sstrdup() 547 * @see sstrdup()
446 */ 548 */
447 sstr_t sstrupper(sstr_t string); 549 sstr_t ucx_strupper(scstr_t string);
550
551 #define sstrupper(string) ucx_strupper(SCSTR(string))
448 552
449 /** 553 /**
450 * Returns a upper case version of a string. 554 * Returns a upper case version of a string.
451 * 555 *
452 * This function creates a duplicate of the input string, first. See the 556 * This function creates a duplicate of the input string, first. See the
455 * @param allocator the allocator used for duplicating the string 559 * @param allocator the allocator used for duplicating the string
456 * @param string the input string 560 * @param string the input string
457 * @return the resulting upper case string 561 * @return the resulting upper case string
458 * @see sstrdup_a() 562 * @see sstrdup_a()
459 */ 563 */
460 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); 564 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
565
566 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
461 567
462 #ifdef __cplusplus 568 #ifdef __cplusplus
463 } 569 }
464 #endif 570 #endif
465 571

mercurial