ucx/ucx/string.h

changeset 505
481802342fdf
parent 335
c1bc13faadaa
equal deleted inserted replaced
504:bf3695fee719 505:481802342fdf
50 50
51 #include "ucx.h" 51 #include "ucx.h"
52 #include "allocator.h" 52 #include "allocator.h"
53 #include <stddef.h> 53 #include <stddef.h>
54 54
55 /** Shortcut for a <code>sstr_t struct</code> literal. */ 55 /*
56 #define ST(s) { (char*)s, sizeof(s)-1 } 56 * Use this macro to disable the shortcuts if you experience macro collision.
57 */
58 #ifndef UCX_NO_SSTR_SHORTCUTS
59 /**
60 * Shortcut for a <code>sstr_t struct</code>
61 * or <code>scstr_t struct</code> literal.
62 */
63 #define ST(s) { s, sizeof(s)-1 }
57 64
58 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */ 65 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
59 #define S(s) sstrn((char*)s, sizeof(s)-1) 66 #define S(s) sstrn(s, sizeof(s)-1)
67
68 /** Shortcut for the conversion of a C string to a <code>scstr_t</code>. */
69 #define SC(s) scstrn(s, sizeof(s)-1)
70 #endif /* UCX_NO_SSTR_SHORTCUTS */
71
72 /*
73 * Use this macro to disable the format macros.
74 */
75 #ifndef UCX_NO_SSTR_FORMAT_MACROS
76 /** Expands a sstr_t or scstr_t to printf arguments. */
77 #define SFMT(s) (int) (s).length, (s).ptr
78
79 /** Format specifier for a sstr_t or scstr_t. */
80 #define PRIsstr ".*s"
81 #endif /* UCX_NO_SSTR_FORMAT_MACROS */
60 82
61 #ifdef __cplusplus 83 #ifdef __cplusplus
62 extern "C" { 84 extern "C" {
63 #endif 85 #endif
64
65 /** 86 /**
66 * The UCX string structure. 87 * The UCX string structure.
67 */ 88 */
68 typedef struct { 89 typedef struct {
69 /** A reference to the string (<b>not necessarily <code>NULL</code> 90 /** A pointer to the string
70 * -terminated</b>) */ 91 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
71 char *ptr; 92 char *ptr;
72 /** The length of the string */ 93 /** The length of the string */
73 size_t length; 94 size_t length;
74 } sstr_t; 95 } sstr_t;
75 96
76 /** 97 /**
98 * The UCX string structure for immutable (constant) strings.
99 */
100 typedef struct {
101 /** A constant pointer to the immutable string
102 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
103 const char *ptr;
104 /** The length of the string */
105 size_t length;
106 } scstr_t;
107
108 #ifdef __cplusplus
109 }
110 #endif
111
112
113 #ifdef __cplusplus
114 /**
115 * One of two type adjustment functions that return a scstr_t.
116 *
117 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
118 *
119 * <b>Do not use this function manually.</b>
120 *
121 * @param str some sstr_t
122 * @return an immutable (scstr_t) version of the provided string.
123 */
124 inline scstr_t s2scstr(sstr_t s) {
125 scstr_t c;
126 c.ptr = s.ptr;
127 c.length = s.length;
128 return c;
129 }
130
131 /**
132 * One of two type adjustment functions that return a scstr_t.
133 *
134 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
135 * This variant is used, when the string is already immutable and no operation
136 * needs to be performed.
137 *
138 * <b>Do not use this function manually.</b>
139 *
140 * @param str some scstr_t
141 * @return the argument itself
142 */
143 inline scstr_t s2scstr(scstr_t str) {
144 return str;
145 }
146
147 /**
148 * Converts a UCX string to an immutable UCX string (scstr_t).
149 * @param str some UCX string
150 * @return the an immutable version of the provided string
151 */
152 #define SCSTR(s) s2scstr(s)
153 #else
154
155 /**
156 * One of two type adjustment functions that return a scstr_t.
157 *
158 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
159 * This variant is used, when the string is already immutable and no operation
160 * needs to be performed.
161 *
162 * <b>Do not use this function manually.</b>
163 *
164 * @param str some scstr_t
165 * @return the argument itself
166 */
167 scstr_t ucx_sc2sc(scstr_t str);
168
169 /**
170 * One of two type adjustment functions that return a scstr_t.
171 *
172 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
173 *
174 * <b>Do not use this function manually.</b>
175 *
176 * @param str some sstr_t
177 * @return an immutable (scstr_t) version of the provided string.
178 */
179 scstr_t ucx_ss2sc(sstr_t str);
180
181 #if __STDC_VERSION__ >= 201112L
182 /**
183 * Converts a UCX string to an immutable UCX string (scstr_t).
184 * @param str some UCX string
185 * @return the an immutable version of the provided string
186 */
187 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
188
189 #elif defined(__GNUC__) || defined(__clang__)
190
191 /**
192 * Converts a UCX string to an immutable UCX string (scstr_t).
193 * @param str some UCX string
194 * @return the an immutable version of the provided string
195 */
196 #define SCSTR(str) __builtin_choose_expr( \
197 __builtin_types_compatible_p(typeof(str), sstr_t), \
198 ucx_ss2sc, \
199 ucx_sc2sc)(str)
200
201 #elif defined(__sun)
202
203 /**
204 * Converts a UCX string to an immutable UCX string (scstr_t).
205 * @param str some UCX string
206 * @return the an immutable version of the provided string
207 */
208 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
209 scstr_t ucx_tmp_var_c; \
210 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
211 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
212 ucx_tmp_var_c; })
213 #else /* no generics and no builtins */
214
215 /**
216 * Converts a UCX string to an immutable UCX string (scstr_t).
217 *
218 * This <b>internal</b> function (ab)uses the C standard an expects one single
219 * argument which is then implicitly converted to scstr_t without a warning.
220 *
221 * <b>Do not use this function manually.</b>
222 *
223 * @return the an immutable version of the provided string
224 */
225 scstr_t ucx_ss2c_s();
226
227 /**
228 * Converts a UCX string to an immutable UCX string (scstr_t).
229 * @param str some UCX string
230 * @return the an immutable version of the provided string
231 */
232 #define SCSTR(str) ucx_ss2c_s(str)
233 #endif /* C11 feature test */
234
235 #endif /* C++ */
236
237 #ifdef __cplusplus
238 extern "C" {
239 #endif
240
241
242 /**
77 * Creates a new sstr_t based on a C string. 243 * Creates a new sstr_t based on a C string.
78 * 244 *
79 * The length is implicitly inferred by using a call to <code>strlen()</code>. 245 * The length is implicitly inferred by using a call to <code>strlen()</code>.
80 * 246 *
81 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you 247 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
82 * do want a copy, use sstrdup() on the return value of this function. 248 * do want a copy, use sstrdup() on the return value of this function.
83 * 249 *
250 * If you need to wrap a constant string, use scstr().
251 *
84 * @param cstring the C string to wrap 252 * @param cstring the C string to wrap
85 * @return a new sstr_t containing the C string 253 * @return a new sstr_t containing the C string
86 * 254 *
87 * @see sstrn() 255 * @see sstrn()
88 */ 256 */
91 /** 259 /**
92 * Creates a new sstr_t of the specified length based on a C string. 260 * Creates a new sstr_t of the specified length based on a C string.
93 * 261 *
94 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you 262 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
95 * do want a copy, use sstrdup() on the return value of this function. 263 * do want a copy, use sstrdup() on the return value of this function.
264 *
265 * If you need to wrap a constant string, use scstrn().
96 * 266 *
97 * @param cstring the C string to wrap 267 * @param cstring the C string to wrap
98 * @param length the length of the string 268 * @param length the length of the string
99 * @return a new sstr_t containing the C string 269 * @return a new sstr_t containing the C string
100 * 270 *
101 * @see sstr() 271 * @see sstr()
102 * @see S() 272 * @see S()
103 */ 273 */
104 sstr_t sstrn(char *cstring, size_t length); 274 sstr_t sstrn(char *cstring, size_t length);
105 275
276 /**
277 * Creates a new scstr_t based on a constant C string.
278 *
279 * The length is implicitly inferred by using a call to <code>strlen()</code>.
280 *
281 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
282 * do want a copy, use scstrdup() on the return value of this function.
283 *
284 * @param cstring the C string to wrap
285 * @return a new scstr_t containing the C string
286 *
287 * @see scstrn()
288 */
289 scstr_t scstr(const char *cstring);
290
291
292 /**
293 * Creates a new scstr_t of the specified length based on a constant C string.
294 *
295 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
296 * do want a copy, use scstrdup() on the return value of this function.
297 *
298 *
299 * @param cstring the C string to wrap
300 * @param length the length of the string
301 * @return a new scstr_t containing the C string
302 *
303 * @see scstr()
304 */
305 scstr_t scstrn(const char *cstring, size_t length);
106 306
107 /** 307 /**
108 * Returns the cumulated length of all specified strings. 308 * Returns the cumulated length of all specified strings.
109 *
110 * At least one string must be specified.
111 * 309 *
112 * <b>Attention:</b> if the count argument does not match the count of the 310 * <b>Attention:</b> if the count argument does not match the count of the
113 * specified strings, the behavior is undefined. 311 * specified strings, the behavior is undefined.
114 * 312 *
115 * @param count the total number of specified strings (so at least 1) 313 * @param count the total number of specified strings (so at least 1)
116 * @param string the first string 314 * @param ... all strings
117 * @param ... all other strings
118 * @return the cumulated length of all strings 315 * @return the cumulated length of all strings
119 */ 316 */
120 size_t sstrnlen(size_t count, sstr_t string, ...); 317 size_t scstrnlen(size_t count, ...);
318
319 /**
320 * Alias for scstrnlen() which automatically converts the arguments.
321 *
322 * @param count the total number of specified strings (so at least 1)
323 * @param ... all strings
324 * @return the cumulated length of all strings
325 */
326 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
121 327
122 /** 328 /**
123 * Concatenates two or more strings. 329 * Concatenates two or more strings.
124 * 330 *
125 * The resulting string will be allocated by standard <code>malloc()</code>. 331 * The resulting string will be allocated by standard <code>malloc()</code>.
128 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 334 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
129 * terminated. 335 * terminated.
130 * 336 *
131 * @param count the total number of strings to concatenate 337 * @param count the total number of strings to concatenate
132 * @param s1 first string 338 * @param s1 first string
133 * @param s2 second string
134 * @param ... all remaining strings 339 * @param ... all remaining strings
135 * @return the concatenated string 340 * @return the concatenated string
136 */ 341 */
137 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); 342 sstr_t scstrcat(size_t count, scstr_t s1, ...);
343
344 /**
345 * Alias for scstrcat() which automatically converts the arguments.
346 *
347 * @param count the total number of strings to concatenate
348 * @param s1 first string
349 * @param ... all remaining strings
350 * @return the concatenated string
351 */
352 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
138 353
139 /** 354 /**
140 * Concatenates two or more strings using a UcxAllocator. 355 * Concatenates two or more strings using a UcxAllocator.
141 * 356 *
142 * See sstrcat() for details. 357 * See scstrcat() for details.
143 * 358 *
144 * @param a the allocator to use 359 * @param a the allocator to use
145 * @param count the total number of strings to concatenate 360 * @param count the total number of strings to concatenate
146 * @param s1 first string 361 * @param s1 first string
147 * @param s2 second string
148 * @param ... all remaining strings 362 * @param ... all remaining strings
149 * @return the concatenated string 363 * @return the concatenated string
150 */ 364 */
151 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); 365 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
152 366
367 /**
368 * Alias for scstrcat_a() which automatically converts the arguments.
369 *
370 * See sstrcat() for details.
371 *
372 * @param a the allocator to use
373 * @param count the total number of strings to concatenate
374 * @param s1 first string
375 * @param ... all remaining strings
376 * @return the concatenated string
377 */
378 #define sstrcat_a(a, count, s1, ...) \
379 scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__)
153 380
154 /** 381 /**
155 * Returns a substring starting at the specified location. 382 * Returns a substring starting at the specified location.
156 * 383 *
157 * <b>Attention:</b> the new string references the same memory area as the 384 * <b>Attention:</b> the new string references the same memory area as the
184 * @see sstrchr() 411 * @see sstrchr()
185 */ 412 */
186 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length); 413 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
187 414
188 /** 415 /**
416 * Returns a substring of an immutable string starting at the specified
417 * location.
418 *
419 * <b>Attention:</b> the new string references the same memory area as the
420 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
421 * Use scstrdup() to get a copy.
422 *
423 * @param string input string
424 * @param start start location of the substring
425 * @return a substring of <code>string</code> starting at <code>start</code>
426 *
427 * @see scstrsubsl()
428 * @see scstrchr()
429 */
430 scstr_t scstrsubs(scstr_t string, size_t start);
431
432 /**
433 * Returns a substring of an immutable string with a maximum length starting
434 * at the specified location.
435 *
436 * <b>Attention:</b> the new string references the same memory area as the
437 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
438 * Use scstrdup() to get a copy.
439 *
440 * @param string input string
441 * @param start start location of the substring
442 * @param length the maximum length of the substring
443 * @return a substring of <code>string</code> starting at <code>start</code>
444 * with a maximum length of <code>length</code>
445 *
446 * @see scstrsubs()
447 * @see scstrchr()
448 */
449 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
450
451 /**
189 * Returns a substring starting at the location of the first occurrence of the 452 * Returns a substring starting at the location of the first occurrence of the
190 * specified character. 453 * specified character.
191 * 454 *
192 * If the string does not contain the character, an empty string is returned. 455 * If the string does not contain the character, an empty string is returned.
193 * 456 *
210 * @return a substring starting at the last location of <code>chr</code> 473 * @return a substring starting at the last location of <code>chr</code>
211 * 474 *
212 * @see sstrsubs() 475 * @see sstrsubs()
213 */ 476 */
214 sstr_t sstrrchr(sstr_t string, int chr); 477 sstr_t sstrrchr(sstr_t string, int chr);
478
479 /**
480 * Returns an immutable substring starting at the location of the first
481 * occurrence of the specified character.
482 *
483 * If the string does not contain the character, an empty string is returned.
484 *
485 * @param string the string where to locate the character
486 * @param chr the character to locate
487 * @return a substring starting at the first location of <code>chr</code>
488 *
489 * @see scstrsubs()
490 */
491 scstr_t scstrchr(scstr_t string, int chr);
492
493 /**
494 * Returns an immutable substring starting at the location of the last
495 * occurrence of the specified character.
496 *
497 * If the string does not contain the character, an empty string is returned.
498 *
499 * @param string the string where to locate the character
500 * @param chr the character to locate
501 * @return a substring starting at the last location of <code>chr</code>
502 *
503 * @see scstrsubs()
504 */
505 scstr_t scstrrchr(scstr_t string, int chr);
215 506
216 /** 507 /**
217 * Returns a substring starting at the location of the first occurrence of the 508 * Returns a substring starting at the location of the first occurrence of the
218 * specified string. 509 * specified string.
219 * 510 *
226 * @param match string containing the sequence of characters to match 517 * @param match string containing the sequence of characters to match
227 * @return a substring starting at the first occurrence of 518 * @return a substring starting at the first occurrence of
228 * <code>match</code>, or an empty string, if the sequence is not 519 * <code>match</code>, or an empty string, if the sequence is not
229 * present in <code>string</code> 520 * present in <code>string</code>
230 */ 521 */
231 sstr_t sstrstr(sstr_t string, sstr_t match); 522 sstr_t scstrsstr(sstr_t string, scstr_t match);
523
524 /**
525 * Alias for scstrsstr() which automatically converts the match string.
526 *
527 * @param string the string to be scanned
528 * @param match string containing the sequence of characters to match
529 * @return a substring starting at the first occurrence of
530 * <code>match</code>, or an empty string, if the sequence is not
531 * present in <code>string</code>
532 */
533 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
534
535 /**
536 * Returns an immutable substring starting at the location of the
537 * first occurrence of the specified immutable string.
538 *
539 * If the string does not contain the other string, an empty string is returned.
540 *
541 * If <code>match</code> is an empty string, the complete <code>string</code> is
542 * returned.
543 *
544 * @param string the string to be scanned
545 * @param match string containing the sequence of characters to match
546 * @return a substring starting at the first occurrence of
547 * <code>match</code>, or an empty string, if the sequence is not
548 * present in <code>string</code>
549 */
550 scstr_t scstrscstr(scstr_t string, scstr_t match);
551
552 /**
553 * Alias for scstrscstr() which automatically converts the match string.
554 *
555 * @param string the string to be scanned
556 * @param match string containing the sequence of characters to match
557 * @return a substring starting at the first occurrence of
558 * <code>match</code>, or an empty string, if the sequence is not
559 * present in <code>string</code>
560 */
561 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
232 562
233 /** 563 /**
234 * Splits a string into parts by using a delimiter string. 564 * Splits a string into parts by using a delimiter string.
235 * 565 *
236 * This function will return <code>NULL</code>, if one of the following happens: 566 * This function will return <code>NULL</code>, if one of the following happens:
271 * @param string the string to split 601 * @param string the string to split
272 * @param delim the delimiter string 602 * @param delim the delimiter string
273 * @param count IN: the maximum size of the resulting array (0 = no limit), 603 * @param count IN: the maximum size of the resulting array (0 = no limit),
274 * OUT: the actual size of the array 604 * OUT: the actual size of the array
275 * @return a sstr_t array containing the split strings or 605 * @return a sstr_t array containing the split strings or
276 * <code>NULL</code> on error 606 * <code>NULL</code> on error
607 *
608 * @see scstrsplit_a()
609 */
610 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
611
612 /**
613 * Alias for scstrsplit() which automatically converts the arguments.
614 *
615 * @param string the string to split
616 * @param delim the delimiter string
617 * @param count IN: the maximum size of the resulting array (0 = no limit),
618 * OUT: the actual size of the array
619 * @return a sstr_t array containing the split strings or
620 * <code>NULL</code> on error
277 * 621 *
278 * @see sstrsplit_a() 622 * @see sstrsplit_a()
279 */ 623 */
280 sstr_t* sstrsplit(sstr_t string, sstr_t delim, ssize_t *count); 624 #define sstrsplit(string, delim, count) \
281 625 scstrsplit(SCSTR(string), SCSTR(delim), count)
282 /** 626
283 * Performing sstrsplit() using a UcxAllocator. 627 /**
284 * 628 * Performing scstrsplit() using a UcxAllocator.
285 * <i>Read the description of sstrsplit() for details.</i> 629 *
630 * <i>Read the description of scstrsplit() for details.</i>
286 * 631 *
287 * The memory for the sstr_t.ptr pointers of the array items and the memory for 632 * The memory for the sstr_t.ptr pointers of the array items and the memory for
288 * the sstr_t array itself are allocated by using the UcxAllocator.malloc() 633 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
289 * function. 634 * function.
290 * 635 *
295 * @param string the string to split 640 * @param string the string to split
296 * @param delim the delimiter string 641 * @param delim the delimiter string
297 * @param count IN: the maximum size of the resulting array (0 = no limit), 642 * @param count IN: the maximum size of the resulting array (0 = no limit),
298 * OUT: the actual size of the array 643 * OUT: the actual size of the array
299 * @return a sstr_t array containing the split strings or 644 * @return a sstr_t array containing the split strings or
300 * <code>NULL</code> on error 645 * <code>NULL</code> on error
646 *
647 * @see scstrsplit()
648 */
649 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
650 ssize_t *count);
651
652 /**
653 * Alias for scstrsplit_a() which automatically converts the arguments.
654 *
655 * @param allocator the UcxAllocator used for allocating memory
656 * @param string the string to split
657 * @param delim the delimiter string
658 * @param count IN: the maximum size of the resulting array (0 = no limit),
659 * OUT: the actual size of the array
660 * @return a sstr_t array containing the split strings or
661 * <code>NULL</code> on error
301 * 662 *
302 * @see sstrsplit() 663 * @see sstrsplit()
303 */ 664 */
304 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t string, sstr_t delim, 665 #define sstrsplit_a(allocator, string, delim, count) \
305 ssize_t *count); 666 scstrsplit_a(allocator, SCSTR(string), SCSTR(delim), count)
306 667
307 /** 668 /**
308 * Compares two UCX strings with standard <code>memcmp()</code>. 669 * Compares two UCX strings with standard <code>memcmp()</code>.
309 * 670 *
310 * At first it compares the sstr_t.length attribute of the two strings. The 671 * At first it compares the scstr_t.length attribute of the two strings. The
311 * <code>memcmp()</code> function is called, if and only if the lengths match. 672 * <code>memcmp()</code> function is called, if and only if the lengths match.
312 * 673 *
313 * @param s1 the first string 674 * @param s1 the first string
314 * @param s2 the second string 675 * @param s2 the second string
315 * @return -1, if the length of s1 is less than the length of s2 or 1, if the 676 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
316 * length of s1 is greater than the length of s2 or the result of 677 * length of s1 is greater than the length of s2 or the result of
317 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match) 678 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
318 */ 679 */
319 int sstrcmp(sstr_t s1, sstr_t s2); 680 int scstrcmp(scstr_t s1, scstr_t s2);
320 681
321 /** 682 /**
322 * Compares two UCX strings ignoring the case. 683 * Alias for scstrcmp() which automatically converts its arguments.
323 *
324 * At first it compares the sstr_t.length attribute of the two strings. If and
325 * only if the lengths match, both strings are compared char by char ignoring
326 * the case.
327 * 684 *
328 * @param s1 the first string 685 * @param s1 the first string
329 * @param s2 the second string 686 * @param s2 the second string
330 * @return -1, if the length of s1 is less than the length of s2 or 1, if the 687 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
331 * length of s1 is greater than the length of s2 or the difference between the 688 * length of s1 is greater than the length of s2 or the result of
332 * first two differing characters otherwise (i.e. 0 if the strings match and 689 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
333 * no characters differ) 690 */
334 */ 691 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
335 int sstrcasecmp(sstr_t s1, sstr_t s2); 692
693 /**
694 * Compares two UCX strings ignoring the case.
695 *
696 * At first it compares the scstr_t.length attribute of the two strings. If and
697 * only if the lengths match, both strings are compared char by char ignoring
698 * the case.
699 *
700 * @param s1 the first string
701 * @param s2 the second string
702 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
703 * length of s1 is greater than the length of s2 or the result of the platform
704 * specific string comparison function ignoring the case.
705 */
706 int scstrcasecmp(scstr_t s1, scstr_t s2);
707
708 /**
709 * Alias for scstrcasecmp() which automatically converts the arguments.
710 *
711 * @param s1 the first string
712 * @param s2 the second string
713 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
714 * length of s1 is greater than the length of s2 or the result of the platform
715 * specific string comparison function ignoring the case.
716 */
717 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
336 718
337 /** 719 /**
338 * Creates a duplicate of the specified string. 720 * Creates a duplicate of the specified string.
339 * 721 *
340 * The new sstr_t will contain a copy allocated by standard 722 * The new sstr_t will contain a copy allocated by standard
341 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to 723 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
342 * <code>free()</code>. 724 * <code>free()</code>.
343 * 725 *
344 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 726 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
345 * terminated. 727 * terminated and mutable, regardless of the argument.
728 *
729 * @param string the string to duplicate
730 * @return a duplicate of the string
731 * @see scstrdup_a()
732 */
733 sstr_t scstrdup(scstr_t string);
734
735 /**
736 * Alias for scstrdup() which automatically converts the argument.
346 * 737 *
347 * @param string the string to duplicate 738 * @param string the string to duplicate
348 * @return a duplicate of the string 739 * @return a duplicate of the string
349 * @see sstrdup_a() 740 * @see sstrdup_a()
350 */ 741 */
351 sstr_t sstrdup(sstr_t string); 742 #define sstrdup(string) scstrdup(SCSTR(string))
352 743
353 /** 744 /**
354 * Creates a duplicate of the specified string using a UcxAllocator. 745 * Creates a duplicate of the specified string using a UcxAllocator.
355 * 746 *
356 * The new sstr_t will contain a copy allocated by the allocators 747 * The new sstr_t will contain a copy allocated by the allocators
357 * ucx_allocator_malloc function. So it is implementation depended, whether the 748 * UcxAllocator.malloc() function. So it is implementation depended, whether the
358 * returned sstr_t.ptr pointer must be passed to the allocators 749 * returned sstr_t.ptr pointer must be passed to the allocators
359 * ucx_allocator_free function manually. 750 * UcxAllocator.free() function manually.
360 * 751 *
361 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>- 752 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
362 * terminated. 753 * terminated and mutable, regardless of the argument.
363 * 754 *
364 * @param allocator a valid instance of a UcxAllocator 755 * @param allocator a valid instance of a UcxAllocator
365 * @param string the string to duplicate 756 * @param string the string to duplicate
366 * @return a duplicate of the string 757 * @return a duplicate of the string
367 * @see sstrdup() 758 * @see scstrdup()
368 */ 759 */
369 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t string); 760 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
761
762 /**
763 * Alias for scstrdup_a() which automatically converts the argument.
764 *
765 * @param allocator a valid instance of a UcxAllocator
766 * @param string the string to duplicate
767 * @return a duplicate of the string
768 * @see scstrdup()
769 */
770 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
771
370 772
371 /** 773 /**
372 * Omits leading and trailing spaces. 774 * Omits leading and trailing spaces.
373 * 775 *
374 * This function returns a new sstr_t containing a trimmed version of the 776 * This function returns a new sstr_t containing a trimmed version of the
386 * @return a new sstr_t containing the trimmed string 788 * @return a new sstr_t containing the trimmed string
387 */ 789 */
388 sstr_t sstrtrim(sstr_t string); 790 sstr_t sstrtrim(sstr_t string);
389 791
390 /** 792 /**
793 * Omits leading and trailing spaces.
794 *
795 * This function returns a new scstr_t containing a trimmed version of the
796 * specified string.
797 *
798 * <b>Note:</b> the new scstr_t references the same memory, thus you
799 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
800 * <code>free()</code>. It is also highly recommended to avoid assignments like
801 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
802 * source string. Assignments of this type are only permitted, if the
803 * scstr_t.ptr of the source string does not need to be freed or if another
804 * reference to the source string exists.
805 *
806 * @param string the string that shall be trimmed
807 * @return a new scstr_t containing the trimmed string
808 */
809 scstr_t scstrtrim(scstr_t string);
810
811 /**
391 * Checks, if a string has a specific prefix. 812 * Checks, if a string has a specific prefix.
392 * @param string the string to check 813 * @param string the string to check
393 * @param prefix the prefix the string should have 814 * @param prefix the prefix the string should have
394 * @return 1, if and only if the string has the specified prefix, 0 otherwise 815 * @return 1, if and only if the string has the specified prefix, 0 otherwise
395 */ 816 */
396 int sstrprefix(sstr_t string, sstr_t prefix); 817 int scstrprefix(scstr_t string, scstr_t prefix);
818
819 /**
820 * Alias for scstrprefix() which automatically converts the arguments.
821 *
822 * @param string the string to check
823 * @param prefix the prefix the string should have
824 * @return 1, if and only if the string has the specified prefix, 0 otherwise
825 */
826 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
397 827
398 /** 828 /**
399 * Checks, if a string has a specific suffix. 829 * Checks, if a string has a specific suffix.
400 * @param string the string to check 830 * @param string the string to check
401 * @param suffix the suffix the string should have 831 * @param suffix the suffix the string should have
402 * @return 1, if and only if the string has the specified suffix, 0 otherwise 832 * @return 1, if and only if the string has the specified suffix, 0 otherwise
403 */ 833 */
404 int sstrsuffix(sstr_t string, sstr_t suffix); 834 int scstrsuffix(scstr_t string, scstr_t suffix);
835
836 /**
837 * Alias for scstrsuffix() which automatically converts the arguments.
838 *
839 * @param string the string to check
840 * @param suffix the suffix the string should have
841 * @return 1, if and only if the string has the specified suffix, 0 otherwise
842 */
843 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
405 844
406 /** 845 /**
407 * Returns a lower case version of a string. 846 * Returns a lower case version of a string.
408 * 847 *
409 * This function creates a duplicate of the input string, first. See the 848 * This function creates a duplicate of the input string, first. See the
410 * documentation of sstrdup() for the implications. 849 * documentation of scstrdup() for the implications.
411 * 850 *
412 * @param string the input string 851 * @param string the input string
413 * @return the resulting lower case string 852 * @return the resulting lower case string
414 * @see sstrdup() 853 * @see scstrdup()
415 */ 854 */
416 sstr_t sstrlower(sstr_t string); 855 sstr_t scstrlower(scstr_t string);
856
857 /**
858 * Alias for scstrlower() which automatically converts the argument.
859 *
860 * @param string the input string
861 * @return the resulting lower case string
862 */
863 #define sstrlower(string) scstrlower(SCSTR(string))
417 864
418 /** 865 /**
419 * Returns a lower case version of a string. 866 * Returns a lower case version of a string.
420 * 867 *
421 * This function creates a duplicate of the input string, first. See the 868 * This function creates a duplicate of the input string, first. See the
422 * documentation of sstrdup_a() for the implications. 869 * documentation of scstrdup_a() for the implications.
423 * 870 *
424 * @param allocator the allocator used for duplicating the string 871 * @param allocator the allocator used for duplicating the string
425 * @param string the input string 872 * @param string the input string
426 * @return the resulting lower case string 873 * @return the resulting lower case string
427 * @see sstrdup_a() 874 * @see scstrdup_a()
428 */ 875 */
429 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string); 876 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
877
878
879 /**
880 * Alias for scstrlower_a() which automatically converts the argument.
881 *
882 * @param allocator the allocator used for duplicating the string
883 * @param string the input string
884 * @return the resulting lower case string
885 */
886 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
430 887
431 /** 888 /**
432 * Returns a upper case version of a string. 889 * Returns a upper case version of a string.
433 * 890 *
434 * This function creates a duplicate of the input string, first. See the 891 * This function creates a duplicate of the input string, first. See the
435 * documentation of sstrdup() for the implications. 892 * documentation of scstrdup() for the implications.
436 * 893 *
437 * @param string the input string 894 * @param string the input string
438 * @return the resulting upper case string 895 * @return the resulting upper case string
439 * @see sstrdup() 896 * @see scstrdup()
440 */ 897 */
441 sstr_t sstrupper(sstr_t string); 898 sstr_t scstrupper(scstr_t string);
899
900 /**
901 * Alias for scstrupper() which automatically converts the argument.
902 *
903 * @param string the input string
904 * @return the resulting upper case string
905 */
906 #define sstrupper(string) scstrupper(SCSTR(string))
442 907
443 /** 908 /**
444 * Returns a upper case version of a string. 909 * Returns a upper case version of a string.
445 * 910 *
446 * This function creates a duplicate of the input string, first. See the 911 * This function creates a duplicate of the input string, first. See the
447 * documentation of sstrdup_a() for the implications. 912 * documentation of scstrdup_a() for the implications.
448 * 913 *
449 * @param allocator the allocator used for duplicating the string 914 * @param allocator the allocator used for duplicating the string
450 * @param string the input string 915 * @param string the input string
451 * @return the resulting upper case string 916 * @return the resulting upper case string
452 * @see sstrdup_a() 917 * @see scstrdup_a()
453 */ 918 */
454 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string); 919 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
920
921 /**
922 * Alias for scstrupper_a() which automatically converts the argument.
923 *
924 * @param allocator the allocator used for duplicating the string
925 * @param string the input string
926 * @return the resulting upper case string
927 */
928 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
455 929
456 #ifdef __cplusplus 930 #ifdef __cplusplus
457 } 931 }
458 #endif 932 #endif
459 933

mercurial