Wed, 16 May 2018 14:02:59 +0200
adds remaining documentation for the scstr functions
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 /**
29 * Bounded string implementation.
30 *
31 * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings.
32 * The main difference to C strings is, that <code>sstr_t</code> does <b>not
33 * need to be <code>NULL</code>-terminated</b>. Instead the length is stored
34 * within the structure.
35 *
36 * When using <code>sstr_t</code>, developers must be full aware of what type
37 * of string (<code>NULL</code>-terminated) or not) they are using, when
38 * accessing the <code>char* ptr</code> directly.
39 *
40 * The UCX string module provides some common string functions, known from
41 * standard libc, working with <code>sstr_t</code>.
42 *
43 * @file string.h
44 * @author Mike Becker
45 * @author Olaf Wintermann
46 */
48 #ifndef UCX_STRING_H
49 #define UCX_STRING_H
51 #include "ucx.h"
52 #include "allocator.h"
53 #include <stddef.h>
55 /** Shortcut for a <code>sstr_t struct</code> literal. */
56 #define ST(s) { (char*)s, sizeof(s)-1 }
58 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
59 #define S(s) sstrn((char*)s, sizeof(s)-1)
61 /** Expands a sstr_t or scstr_t to printf arguments. */
62 #define SFMT(s) (int) (s).length, (s).ptr
64 /** Format specifier for a sstr_t or scstr_t. */
65 #define PRIsstr ".*s"
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 /**
71 * The UCX string structure.
72 */
73 typedef struct {
74 /** A pointer to the string
75 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
76 char *ptr;
77 /** The length of the string */
78 size_t length;
79 } sstr_t;
81 /**
82 * The UCX string structure for immutable (constant) strings.
83 */
84 typedef struct {
85 /** A constant pointer to the immutable string
86 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
87 const char *ptr;
88 /** The length of the string */
89 size_t length;
90 } scstr_t;
92 #ifdef __cplusplus
93 }
94 #endif
97 #ifdef __cplusplus
98 inline scstr_t s2scstr(sstr_t s) {
99 scstr_t c;
100 c.ptr = s.ptr;
101 c.length = s.ptr;
102 return c;
103 }
104 inline scstr_t s2scstr(scstr_t c) {
105 return c;
106 }
107 #define SCSTR s2scstr
108 #else
110 /**
111 * One of two type adjustment functions that return a scstr_t.
112 *
113 * Used internally to cast a UCX string to an immutable UCX string.
114 * This variant is used, when the string is already immutable and no operation
115 * needs to be performed.
116 *
117 * @param str some scstr_t
118 * @return the argument itself
119 */
120 scstr_t ucx_sc2sc(scstr_t str);
122 /**
123 * One of two type adjustment functions that return a scstr_t.
124 *
125 * Used internally to cast a UCX string to an immutable UCX string.
126 *
127 * @param str some sstr_t
128 * @return an immutable (scstr_t) version of the provided string.
129 */
130 scstr_t ucx_ss2sc(sstr_t str);
132 #if __STDC_VERSION__ >= 201112L
133 /**
134 * Casts a UCX string to an immutable UCX string (scstr_t).
135 * @param str some UCX string
136 * @return the an immutable version of the provided string
137 */
138 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
140 #elif defined(__GNUC__) || defined(__clang__)
142 /**
143 * Casts a UCX string to an immutable UCX string (scstr_t).
144 * @param str some UCX string
145 * @return the an immutable version of the provided string
146 */
147 #define SCSTR(str) __builtin_choose_expr( \
148 __builtin_types_compatible_p(typeof(str), sstr_t), \
149 ucx_ss2sc, \
150 ucx_sc2sc)(str)
152 #elif defined(__sun)
154 /**
155 * Casts a UCX string to an immutable UCX string (scstr_t).
156 * @param str some UCX string
157 * @return the an immutable version of the provided string
158 */
159 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
160 scstr_t ucx_tmp_var_c; \
161 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
162 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
163 ucx_tmp_var_c; })
164 #else /* no generics and no builtins */
166 /**
167 * Casts a UCX string to an immutable UCX string (scstr_t).
168 *
169 * This internal function (ab)uses the C standard an expects one single
170 * argument which is then implicitly casted to scstr_t without a warning.
171 *
172 * @return the an immutable version of the provided string
173 */
174 scstr_t ucx_ss2c_s();
176 /**
177 * Casts a UCX string to an immutable UCX string (scstr_t).
178 * @param str some UCX string
179 * @return the an immutable version of the provided string
180 */
181 #define SCSTR(str) ucx_ss2c_s(str)
182 #endif /* C11 feature test */
184 #endif /* C++ */
186 #ifdef __cplusplus
187 extern "C" {
188 #endif
191 /**
192 * Creates a new sstr_t based on a C string.
193 *
194 * The length is implicitly inferred by using a call to <code>strlen()</code>.
195 *
196 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
197 * do want a copy, use sstrdup() on the return value of this function.
198 *
199 * If you need to wrap a constant string, use scstr().
200 *
201 * @param cstring the C string to wrap
202 * @return a new sstr_t containing the C string
203 *
204 * @see sstrn()
205 */
206 sstr_t sstr(char *cstring);
208 /**
209 * Creates a new sstr_t of the specified length based on a C string.
210 *
211 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
212 * do want a copy, use sstrdup() on the return value of this function.
213 *
214 * If you need to wrap a constant string, use scstrn().
215 *
216 * @param cstring the C string to wrap
217 * @param length the length of the string
218 * @return a new sstr_t containing the C string
219 *
220 * @see sstr()
221 * @see S()
222 */
223 sstr_t sstrn(char *cstring, size_t length);
225 /**
226 * Creates a new scstr_t based on a constant C string.
227 *
228 * The length is implicitly inferred by using a call to <code>strlen()</code>.
229 *
230 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
231 * do want a copy, use scstrdup() on the return value of this function.
232 *
233 * @param cstring the C string to wrap
234 * @return a new scstr_t containing the C string
235 *
236 * @see scstrn()
237 */
238 scstr_t scstr(const char *cstring);
241 /**
242 * Creates a new scstr_t of the specified length based on a constant C string.
243 *
244 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
245 * do want a copy, use scstrdup() on the return value of this function.
246 *
247 *
248 * @param cstring the C string to wrap
249 * @param length the length of the string
250 * @return a new scstr_t containing the C string
251 *
252 * @see scstr()
253 */
254 scstr_t scstrn(const char *cstring, size_t length);
256 /**
257 * Returns the cumulated length of all specified strings.
258 *
259 * You may arbitrarily mix up mutable (<code>sstr_t</code>) and immutable
260 * (<code>scstr_t</code>) strings.
261 *
262 * <b>Attention:</b> if the count argument does not match the count of the
263 * specified strings, the behavior is undefined.
264 *
265 * @param count the total number of specified strings (so at least 1)
266 * @param ... all strings
267 * @return the cumulated length of all strings
268 */
269 size_t ucx_strnlen(size_t count, ...);
271 /**
272 * Alias for ucx_strnlen().
273 *
274 * @param count the total number of specified strings (so at least 1)
275 * @param ... all strings
276 * @return the cumulated length of all strings
277 */
278 #define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__)
280 /**
281 * Concatenates two or more strings.
282 *
283 * The resulting string will be allocated by standard <code>malloc()</code>.
284 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
285 *
286 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
287 * terminated.
288 *
289 * @param count the total number of strings to concatenate
290 * @param s1 first string
291 * @param ... all remaining strings
292 * @return the concatenated string
293 */
294 sstr_t ucx_strcat(size_t count, scstr_t s1, ...);
296 /**
297 * Alias for ucx_strcat() which automatically casts the first string.
298 *
299 * @param count the total number of strings to concatenate
300 * @param s1 first string
301 * @param ... all remaining strings
302 * @return the concatenated string
303 */
304 #define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__)
306 /**
307 * Concatenates two or more strings using a UcxAllocator.
308 *
309 * See sstrcat() for details.
310 *
311 * @param a the allocator to use
312 * @param count the total number of strings to concatenate
313 * @param s1 first string
314 * @param ... all remaining strings
315 * @return the concatenated string
316 */
317 sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
319 /**
320 * Alias for ucx_strcat_a() which automatically casts the first string.
321 *
322 * See sstrcat() for details.
323 *
324 * @param a the allocator to use
325 * @param count the total number of strings to concatenate
326 * @param s1 first string
327 * @param ... all remaining strings
328 * @return the concatenated string
329 */
330 #define sstrcat_a(a, count, s1, ...) \
331 ucx_strcat_a(a, count, SCSTR(s1), __VA_ARGS__)
333 /**
334 * Returns a substring starting at the specified location.
335 *
336 * <b>Attention:</b> the new string references the same memory area as the
337 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
338 * Use sstrdup() to get a copy.
339 *
340 * @param string input string
341 * @param start start location of the substring
342 * @return a substring of <code>string</code> starting at <code>start</code>
343 *
344 * @see sstrsubsl()
345 * @see sstrchr()
346 */
347 sstr_t sstrsubs(sstr_t string, size_t start);
349 /**
350 * Returns a substring with a maximum length starting at the specified location.
351 *
352 * <b>Attention:</b> the new string references the same memory area as the
353 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
354 * Use sstrdup() to get a copy.
355 *
356 * @param string input string
357 * @param start start location of the substring
358 * @param length the maximum length of the substring
359 * @return a substring of <code>string</code> starting at <code>start</code>
360 * with a maximum length of <code>length</code>
361 *
362 * @see sstrsubs()
363 * @see sstrchr()
364 */
365 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
367 /**
368 * Returns a substring of an immutable string starting at the specified
369 * location.
370 *
371 * <b>Attention:</b> the new string references the same memory area as the
372 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
373 * Use scstrdup() to get a copy.
374 *
375 * @param string input string
376 * @param start start location of the substring
377 * @return a substring of <code>string</code> starting at <code>start</code>
378 *
379 * @see scstrsubsl()
380 * @see scstrchr()
381 */
382 scstr_t scstrsubs(scstr_t string, size_t start);
384 /**
385 * Returns a substring of an immutable string with a maximum length starting
386 * at the specified location.
387 *
388 * <b>Attention:</b> the new string references the same memory area as the
389 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
390 * Use scstrdup() to get a copy.
391 *
392 * @param string input string
393 * @param start start location of the substring
394 * @param length the maximum length of the substring
395 * @return a substring of <code>string</code> starting at <code>start</code>
396 * with a maximum length of <code>length</code>
397 *
398 * @see scstrsubs()
399 * @see scstrchr()
400 */
401 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
403 /**
404 * Returns a substring starting at the location of the first occurrence of the
405 * specified character.
406 *
407 * If the string does not contain the character, an empty string is returned.
408 *
409 * @param string the string where to locate the character
410 * @param chr the character to locate
411 * @return a substring starting at the first location of <code>chr</code>
412 *
413 * @see sstrsubs()
414 */
415 sstr_t sstrchr(sstr_t string, int chr);
417 /**
418 * Returns a substring starting at the location of the last occurrence of the
419 * specified character.
420 *
421 * If the string does not contain the character, an empty string is returned.
422 *
423 * @param string the string where to locate the character
424 * @param chr the character to locate
425 * @return a substring starting at the last location of <code>chr</code>
426 *
427 * @see sstrsubs()
428 */
429 sstr_t sstrrchr(sstr_t string, int chr);
431 /**
432 * Returns an immutable substring starting at the location of the first
433 * occurrence of the specified character.
434 *
435 * If the string does not contain the character, an empty string is returned.
436 *
437 * @param string the string where to locate the character
438 * @param chr the character to locate
439 * @return a substring starting at the first location of <code>chr</code>
440 *
441 * @see scstrsubs()
442 */
443 scstr_t scstrchr(scstr_t string, int chr);
445 /**
446 * Returns an immutable substring starting at the location of the last
447 * occurrence of the specified character.
448 *
449 * If the string does not contain the character, an empty string is returned.
450 *
451 * @param string the string where to locate the character
452 * @param chr the character to locate
453 * @return a substring starting at the last location of <code>chr</code>
454 *
455 * @see scstrsubs()
456 */
457 scstr_t scstrrchr(scstr_t string, int chr);
459 /**
460 * Returns a substring starting at the location of the first occurrence of the
461 * specified string.
462 *
463 * If the string does not contain the other string, an empty string is returned.
464 *
465 * If <code>match</code> is an empty string, the complete <code>string</code> is
466 * returned.
467 *
468 * @param string the string to be scanned
469 * @param match string containing the sequence of characters to match
470 * @return a substring starting at the first occurrence of
471 * <code>match</code>, or an empty string, if the sequence is not
472 * present in <code>string</code>
473 */
474 sstr_t ucx_sstrstr(sstr_t string, scstr_t match);
476 /**
477 * Alias for ucx_sstrstr() which automatically casts the match string.
478 *
479 * @param string the string to be scanned
480 * @param match string containing the sequence of characters to match
481 * @return a substring starting at the first occurrence of
482 * <code>match</code>, or an empty string, if the sequence is not
483 * present in <code>string</code>
484 */
485 #define sstrstr(string, match) ucx_sstrstr(string, SCSTR(match))
487 /**
488 * Returns an immutable substring starting at the location of the
489 * first occurrence of the specified immutable string.
490 *
491 * If the string does not contain the other string, an empty string is returned.
492 *
493 * If <code>match</code> is an empty string, the complete <code>string</code> is
494 * returned.
495 *
496 * @param string the string to be scanned
497 * @param match string containing the sequence of characters to match
498 * @return a substring starting at the first occurrence of
499 * <code>match</code>, or an empty string, if the sequence is not
500 * present in <code>string</code>
501 */
502 scstr_t ucx_scstrstr(scstr_t string, scstr_t match);
504 /**
505 * Alias for ucx_scstrstr() which automatically casts the match string.
506 *
507 * @param string the string to be scanned
508 * @param match string containing the sequence of characters to match
509 * @return a substring starting at the first occurrence of
510 * <code>match</code>, or an empty string, if the sequence is not
511 * present in <code>string</code>
512 */
513 #define scstrstr(string, match) ucx_scstrstr(string, SCSTR(match))
515 /**
516 * Splits a string into parts by using a delimiter string.
517 *
518 * This function will return <code>NULL</code>, if one of the following happens:
519 * <ul>
520 * <li>the string length is zero</li>
521 * <li>the delimeter length is zero</li>
522 * <li>the string equals the delimeter</li>
523 * <li>memory allocation fails</li>
524 * </ul>
525 *
526 * The integer referenced by <code>count</code> is used as input and determines
527 * the maximum size of the resulting array, i.e. the maximum count of splits to
528 * perform + 1.
529 *
530 * The integer referenced by <code>count</code> is also used as output and is
531 * set to
532 * <ul>
533 * <li>-2, on memory allocation errors</li>
534 * <li>-1, if either the string or the delimiter is an empty string</li>
535 * <li>0, if the string equals the delimiter</li>
536 * <li>1, if the string does not contain the delimiter</li>
537 * <li>the count of array items, otherwise</li>
538 * </ul>
539 *
540 * If the string starts with the delimiter, the first item of the resulting
541 * array will be an empty string.
542 *
543 * If the string ends with the delimiter and the maximum list size is not
544 * exceeded, the last array item will be an empty string.
545 * In case the list size would be exceeded, the last array item will be the
546 * remaining string after the last split, <i>including</i> the terminating
547 * delimiter.
548 *
549 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
550 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
551 * an allocator to managed memory, to avoid this.
552 *
553 * @param string the string to split
554 * @param delim the delimiter string
555 * @param count IN: the maximum size of the resulting array (0 = no limit),
556 * OUT: the actual size of the array
557 * @return a sstr_t array containing the split strings or
558 * <code>NULL</code> on error
559 *
560 * @see ucx_strsplit_a()
561 */
562 sstr_t* ucx_strsplit(scstr_t string, scstr_t delim, ssize_t *count);
564 /**
565 * Alias for ucx_strsplit() which automatically casts the arguments.
566 *
567 * @param string the string to split
568 * @param delim the delimiter string
569 * @param count IN: the maximum size of the resulting array (0 = no limit),
570 * OUT: the actual size of the array
571 * @return a sstr_t array containing the split strings or
572 * <code>NULL</code> on error
573 *
574 * @see sstrsplit_a()
575 */
576 #define sstrsplit(string, delim, count) \
577 ucx_strsplit(SCSTR(string), SCSTR(delim), count)
579 /**
580 * Performing sstrsplit() using a UcxAllocator.
581 *
582 * <i>Read the description of sstrsplit() for details.</i>
583 *
584 * The memory for the sstr_t.ptr pointers of the array items and the memory for
585 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
586 * function.
587 *
588 * <b>Note:</b> the allocator is not used for memory that is freed within the
589 * same call of this function (locally scoped variables).
590 *
591 * @param allocator the UcxAllocator used for allocating memory
592 * @param string the string to split
593 * @param delim the delimiter string
594 * @param count IN: the maximum size of the resulting array (0 = no limit),
595 * OUT: the actual size of the array
596 * @return a sstr_t array containing the split strings or
597 * <code>NULL</code> on error
598 *
599 * @see ucx_strsplit()
600 */
601 sstr_t* ucx_strsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
602 ssize_t *count);
604 /**
605 * Alias for ucx_strsplit_a() which automatically casts the arguments.
606 *
607 * @param allocator the UcxAllocator used for allocating memory
608 * @param string the string to split
609 * @param delim the delimiter string
610 * @param count IN: the maximum size of the resulting array (0 = no limit),
611 * OUT: the actual size of the array
612 * @return a sstr_t array containing the split strings or
613 * <code>NULL</code> on error
614 *
615 * @see sstrsplit()
616 */
617 #define sstrsplit_a(allocator, string, delim, count) \
618 ucx_strsplit_a(allocator, SCSTR(string), SCSTR(delim, count))
620 /**
621 * Compares two UCX strings with standard <code>memcmp()</code>.
622 *
623 * At first it compares the scstr_t.length attribute of the two strings. The
624 * <code>memcmp()</code> function is called, if and only if the lengths match.
625 *
626 * @param s1 the first string
627 * @param s2 the second string
628 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
629 * length of s1 is greater than the length of s2 or the result of
630 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
631 */
632 int ucx_strcmp(scstr_t s1, scstr_t s2);
634 /**
635 * Alias for ucx_strcmp() which automatically casts its arguments.
636 *
637 * @param s1 the first string
638 * @param s2 the second string
639 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
640 * length of s1 is greater than the length of s2 or the result of
641 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
642 */
643 #define sstrcmp(s1, s2) ucx_strcmp(SCSTR(s1), SCSTR(s2))
645 /**
646 * Compares two UCX strings ignoring the case.
647 *
648 * At first it compares the sstr_t.length attribute of the two strings. If and
649 * only if the lengths match, both strings are compared char by char ignoring
650 * the case.
651 *
652 * @param s1 the first string
653 * @param s2 the second string
654 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
655 * length of s1 is greater than the length of s2 or the result of the platform
656 * specific string comparison function ignoring the case.
657 */
658 int ucx_strcasecmp(scstr_t s1, scstr_t s2);
660 /**
661 * Alias for ucx_strcasecmp() which automatically casts the arguments.
662 *
663 * @param s1 the first string
664 * @param s2 the second string
665 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
666 * length of s1 is greater than the length of s2 or the result of the platform
667 * specific string comparison function ignoring the case.
668 */
669 #define sstrcasecmp(s1, s2) ucx_strcasecmp(SCSTR(s1), SCSTR(s2))
671 /**
672 * Creates a duplicate of the specified string.
673 *
674 * The new sstr_t will contain a copy allocated by standard
675 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
676 * <code>free()</code>.
677 *
678 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
679 * terminated and mutable, regardless of the argument.
680 *
681 * @param string the string to duplicate
682 * @return a duplicate of the string
683 * @see ucx_strdup_a()
684 */
685 sstr_t ucx_strdup(scstr_t string);
687 /**
688 * Alias for ucx_strdup() which automatically casts the argument.
689 *
690 * @param string the string to duplicate
691 * @return a duplicate of the string
692 * @see sstrdup_a()
693 */
694 #define sstrdup(string) ucx_strdup(SCSTR(string))
696 /**
697 * Creates a duplicate of the specified string using a UcxAllocator.
698 *
699 * The new sstr_t will contain a copy allocated by the allocators
700 * ucx_allocator_malloc function. So it is implementation depended, whether the
701 * returned sstr_t.ptr pointer must be passed to the allocators
702 * ucx_allocator_free function manually.
703 *
704 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
705 * terminated and mutable, regardless of the argument.
706 *
707 * @param allocator a valid instance of a UcxAllocator
708 * @param string the string to duplicate
709 * @return a duplicate of the string
710 * @see ucx_strdup()
711 */
712 sstr_t ucx_strdup_a(UcxAllocator *allocator, scstr_t string);
714 /**
715 * Alias for ucx_strdup_a() which automatically casts the argument.
716 *
717 * @param allocator a valid instance of a UcxAllocator
718 * @param string the string to duplicate
719 * @return a duplicate of the string
720 * @see ucx_strdup()
721 */
722 #define sstrdup_a(allocator, string) ucx_strdup_a(allocator, SCSTR(string))
725 /**
726 * Omits leading and trailing spaces.
727 *
728 * This function returns a new sstr_t containing a trimmed version of the
729 * specified string.
730 *
731 * <b>Note:</b> the new sstr_t references the same memory, thus you
732 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
733 * <code>free()</code>. It is also highly recommended to avoid assignments like
734 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
735 * source string. Assignments of this type are only permitted, if the
736 * sstr_t.ptr of the source string does not need to be freed or if another
737 * reference to the source string exists.
738 *
739 * @param string the string that shall be trimmed
740 * @return a new sstr_t containing the trimmed string
741 */
742 sstr_t sstrtrim(sstr_t string);
744 /**
745 * Omits leading and trailing spaces.
746 *
747 * This function returns a new scstr_t containing a trimmed version of the
748 * specified string.
749 *
750 * <b>Note:</b> the new scstr_t references the same memory, thus you
751 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
752 * <code>free()</code>. It is also highly recommended to avoid assignments like
753 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
754 * source string. Assignments of this type are only permitted, if the
755 * scstr_t.ptr of the source string does not need to be freed or if another
756 * reference to the source string exists.
757 *
758 * @param string the string that shall be trimmed
759 * @return a new scstr_t containing the trimmed string
760 */
761 scstr_t scstrtrim(scstr_t string);
763 /**
764 * Checks, if a string has a specific prefix.
765 * @param string the string to check
766 * @param prefix the prefix the string should have
767 * @return 1, if and only if the string has the specified prefix, 0 otherwise
768 */
769 int ucx_strprefix(scstr_t string, scstr_t prefix);
771 /**
772 * Alias for ucx_strprefix() which automatically casts the arguments.
773 *
774 * @param string the string to check
775 * @param prefix the prefix the string should have
776 * @return 1, if and only if the string has the specified prefix, 0 otherwise
777 */
778 #define sstrprefix(string, prefix) ucx_strprefix(SCSTR(string), SCSTR(prefix))
780 /**
781 * Checks, if a string has a specific suffix.
782 * @param string the string to check
783 * @param suffix the suffix the string should have
784 * @return 1, if and only if the string has the specified suffix, 0 otherwise
785 */
786 int ucx_strsuffix(scstr_t string, scstr_t suffix);
788 /**
789 * Alias for ucx_strsuffix() which automatically casts the arguments.
790 *
791 * @param string the string to check
792 * @param suffix the suffix the string should have
793 * @return 1, if and only if the string has the specified suffix, 0 otherwise
794 */
795 #define sstrsuffix(string, suffix) ucx_strsuffix(SCSTR(string), SCSTR(suffix))
797 /**
798 * Returns a lower case version of a string.
799 *
800 * This function creates a duplicate of the input string, first. See the
801 * documentation of scstrdup() for the implications.
802 *
803 * @param string the input string
804 * @return the resulting lower case string
805 * @see scstrdup()
806 */
807 sstr_t ucx_strlower(scstr_t string);
809 /**
810 * Alias for ucx_strlower() which automatically casts the argument.
811 *
812 * @param string the input string
813 * @return the resulting lower case string
814 */
815 #define sstrlower(string) ucx_strlower(SCSTR(string))
817 /**
818 * Returns a lower case version of a string.
819 *
820 * This function creates a duplicate of the input string, first. See the
821 * documentation of scstrdup_a() for the implications.
822 *
823 * @param allocator the allocator used for duplicating the string
824 * @param string the input string
825 * @return the resulting lower case string
826 * @see scstrdup_a()
827 */
828 sstr_t ucx_strlower_a(UcxAllocator *allocator, scstr_t string);
831 /**
832 * Alias for ucx_strlower_a() which automatically casts the argument.
833 *
834 * @param allocator the allocator used for duplicating the string
835 * @param string the input string
836 * @return the resulting lower case string
837 */
838 #define sstrlower_a(allocator, string) ucx_strlower_a(allocator, SCSTR(string))
840 /**
841 * Returns a upper case version of a string.
842 *
843 * This function creates a duplicate of the input string, first. See the
844 * documentation of scstrdup() for the implications.
845 *
846 * @param string the input string
847 * @return the resulting upper case string
848 * @see scstrdup()
849 */
850 sstr_t ucx_strupper(scstr_t string);
852 /**
853 * Alias for ucx_strupper() which automatically casts the argument.
854 *
855 * @param string the input string
856 * @return the resulting upper case string
857 */
858 #define sstrupper(string) ucx_strupper(SCSTR(string))
860 /**
861 * Returns a upper case version of a string.
862 *
863 * This function creates a duplicate of the input string, first. See the
864 * documentation of scstrdup_a() for the implications.
865 *
866 * @param allocator the allocator used for duplicating the string
867 * @param string the input string
868 * @return the resulting upper case string
869 * @see scstrdup_a()
870 */
871 sstr_t ucx_strupper_a(UcxAllocator *allocator, scstr_t string);
873 /**
874 * Alias for ucx_strupper_a() which automatically casts the argument.
875 *
876 * @param allocator the allocator used for duplicating the string
877 * @param string the input string
878 * @return the resulting upper case string
879 */
880 #define sstrupper_a(allocator, string) ucx_strupper_a(allocator, string)
882 #ifdef __cplusplus
883 }
884 #endif
886 #endif /* UCX_STRING_H */