2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
29 * Bounded string implementation.
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.
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.
40 * The UCX string module provides some common string functions, known from
41 * standard libc, working with <code>sstr_t</code>.
45 * @author Olaf Wintermann
52 #include "allocator.h"
56 * Use this macro to disable the shortcuts if you experience macro collision.
58 #ifndef UCX_NO_SSTR_SHORTCUTS
60 * Shortcut for a <code>sstr_t struct</code>
61 * or <code>scstr_t struct</code> literal.
63 #define ST(s) { s, sizeof(s)-1 }
65 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
66 #define S(s) sstrn(s, sizeof(s)-1)
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 */
73 * Use this macro to disable the format macros.
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
79 /** Format specifier for a sstr_t or scstr_t. */
81 #endif /* UCX_NO_SSTR_FORMAT_MACROS */
88 * The UCX string structure.
91 /** A pointer to the string
92 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
94 /** The length of the string */
99 * The UCX string structure for immutable (constant) strings.
102 /** A constant pointer to the immutable string
103 * (<b>not necessarily <code>NULL</code>-terminated</b>) */
105 /** The length of the string */
116 * One of two type adjustment functions that return an scstr_t.
118 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
120 * <b>Do not use this function manually.</b>
122 * @param str some sstr_t
123 * @return an immutable (scstr_t) version of the provided string.
125 inline scstr_t s2scstr(sstr_t s) {
133 * One of two type adjustment functions that return an scstr_t.
135 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
136 * This variant is used, when the string is already immutable and no operation
137 * needs to be performed.
139 * <b>Do not use this function manually.</b>
141 * @param str some scstr_t
142 * @return the argument itself
144 inline scstr_t s2scstr(scstr_t str) {
149 * Converts a UCX string to an immutable UCX string (scstr_t).
150 * @param str some UCX string
151 * @return an immutable version of the provided string
153 #define SCSTR(s) s2scstr(s)
157 * One of two type adjustment functions that return an scstr_t.
159 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
160 * This variant is used, when the string is already immutable and no operation
161 * needs to be performed.
163 * <b>Do not use this function manually.</b>
165 * @param str some scstr_t
166 * @return the argument itself
168 scstr_t ucx_sc2sc(scstr_t str);
171 * One of two type adjustment functions that return an scstr_t.
173 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
175 * <b>Do not use this function manually.</b>
177 * @param str some sstr_t
178 * @return an immutable (scstr_t) version of the provided string.
180 scstr_t ucx_ss2sc(sstr_t str);
182 #if __STDC_VERSION__ >= 201112L
184 * Converts a UCX string to an immutable UCX string (scstr_t).
185 * @param str some UCX string
186 * @return an immutable version of the provided string
188 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
190 #elif defined(__GNUC__) || defined(__clang__)
193 * Converts a UCX string to an immutable UCX string (scstr_t).
194 * @param str some UCX string
195 * @return an immutable version of the provided string
197 #define SCSTR(str) __builtin_choose_expr( \
198 __builtin_types_compatible_p(typeof(str), sstr_t), \
205 * Converts a UCX string to an immutable UCX string (scstr_t).
206 * @param str some UCX string
207 * @return the an immutable version of the provided string
209 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
210 scstr_t ucx_tmp_var_c; \
211 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
212 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
214 #else /* no generics and no builtins */
217 * Converts a UCX string to an immutable UCX string (scstr_t).
219 * This <b>internal</b> function (ab)uses the C standard an expects one single
220 * argument which is then implicitly converted to scstr_t without a warning.
222 * <b>Do not use this function manually.</b>
224 * @return the an immutable version of the provided string
226 scstr_t ucx_ss2c_s();
229 * Converts a UCX string to an immutable UCX string (scstr_t).
230 * @param str some UCX string
231 * @return the an immutable version of the provided string
233 #define SCSTR(str) ucx_ss2c_s(str)
234 #endif /* C11 feature test */
244 * Creates a new sstr_t based on a C string.
246 * The length is implicitly inferred by using a call to <code>strlen()</code>.
248 * <b>Note:</b> the sstr_t will share the specified pointer to the C string.
249 * If you do want a copy, use sstrdup() on the return value of this function.
251 * If you need to wrap a constant string, use scstr().
253 * @param cstring the C string to wrap
254 * @return a new sstr_t containing the C string
258 sstr_t sstr(char *cstring);
261 * Creates a new sstr_t of the specified length based on a C string.
263 * <b>Note:</b> the sstr_t will share the specified pointer to the C string.
264 * If you do want a copy, use sstrdup() on the return value of this function.
266 * If you need to wrap a constant string, use scstrn().
268 * @param cstring the C string to wrap
269 * @param length the length of the string
270 * @return a new sstr_t containing the C string
275 sstr_t sstrn(char *cstring, size_t length);
278 * Creates a new scstr_t based on a constant C string.
280 * The length is implicitly inferred by using a call to <code>strlen()</code>.
282 * <b>Note:</b> the scstr_t will share the specified pointer to the C string.
283 * If you do want a copy, use scstrdup() on the return value of this function.
285 * @param cstring the C string to wrap
286 * @return a new scstr_t containing the C string
290 scstr_t scstr(const char *cstring);
294 * Creates a new scstr_t of the specified length based on a constant C string.
296 * <b>Note:</b> the scstr_t will share the specified pointer to the C string.
297 * If you do want a copy, use scstrdup() on the return value of this function. *
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
305 scstr_t scstrn(const char *cstring, size_t length);
308 * Returns the accumulated length of all specified strings.
310 * <b>Attention:</b> if the count argument is larger than the count of the
311 * specified strings, the behavior is undefined.
313 * @param count the total number of specified strings
314 * @param ... all strings
315 * @return the accumulated length of all strings
317 size_t scstrnlen(size_t count, ...);
320 * Returns the accumulated length of all specified strings.
322 * <b>Attention:</b> if the count argument is larger than the count of the
323 * specified strings, the behavior is undefined.
325 * @param count the total number of specified strings
326 * @param ... all strings
327 * @return the cumulated length of all strings
329 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
332 * Concatenates two or more strings.
334 * The resulting string will be allocated by standard <code>malloc()</code>.
335 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
337 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
340 * @param count the total number of strings to concatenate
341 * @param s1 first string
342 * @param ... all remaining strings
343 * @return the concatenated string
345 sstr_t scstrcat(size_t count, scstr_t s1, ...);
348 * Concatenates two or more strings.
350 * The resulting string will be allocated by standard <code>malloc()</code>.
351 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
353 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
356 * @param count the total number of strings to concatenate
357 * @param s1 first string
358 * @param ... all remaining strings
359 * @return the concatenated string
361 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
364 * Concatenates two or more strings using a UcxAllocator.
366 * The resulting string must be freed by the allocators <code>free()</code>
369 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
372 * @param alloc 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
380 sstr_t scstrcat_a(UcxAllocator *alloc, size_t count, scstr_t s1, ...);
383 * Concatenates two or more strings using a UcxAllocator.
385 * The resulting string must be freed by the allocators <code>free()</code>
388 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
391 * @param alloc the allocator to use
392 * @param count the total number of strings to concatenate
393 * @param s1 first string
394 * @param ... all remaining strings
395 * @return the concatenated string
399 #define sstrcat_a(alloc, count, s1, ...) \
400 scstrcat_a(alloc, count, SCSTR(s1), __VA_ARGS__)
403 * Returns a substring starting at the specified location.
405 * <b>Attention:</b> the new string references the same memory area as the
406 * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
407 * Use sstrdup() to get a copy.
409 * @param string input string
410 * @param start start location of the substring
411 * @return a substring of <code>string</code> starting at <code>start</code>
416 sstr_t sstrsubs(sstr_t string, size_t start);
419 * Returns a substring with the given length starting at the specified location.
421 * <b>Attention:</b> the new string references the same memory area as the
422 * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
423 * Use sstrdup() to get a copy.
425 * @param string input string
426 * @param start start location of the substring
427 * @param length the maximum length of the substring
428 * @return a substring of <code>string</code> starting at <code>start</code>
429 * with a maximum length of <code>length</code>
434 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
437 * Returns a substring of an immutable string starting at the specified
440 * <b>Attention:</b> the new string references the same memory area as the
441 * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
442 * Use scstrdup() to get a copy.
444 * @param string input string
445 * @param start start location of the substring
446 * @return a substring of <code>string</code> starting at <code>start</code>
451 scstr_t scstrsubs(scstr_t string, size_t start);
454 * Returns a substring of an immutable string with a maximum length starting
455 * at the specified location.
457 * <b>Attention:</b> the new string references the same memory area as the
458 * input string and is <b>NOT</b> required to be <code>NULL</code>-terminated.
459 * Use scstrdup() to get a copy.
461 * @param string input string
462 * @param start start location of the substring
463 * @param length the maximum length of the substring
464 * @return a substring of <code>string</code> starting at <code>start</code>
465 * with a maximum length of <code>length</code>
470 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
473 * Returns a substring starting at the location of the first occurrence of the
474 * specified character.
476 * If the string does not contain the character, an empty string is returned.
478 * @param string the string where to locate the character
479 * @param chr the character to locate
480 * @return a substring starting at the first location of <code>chr</code>
484 sstr_t sstrchr(sstr_t string, int chr);
487 * Returns a substring starting at the location of the last occurrence of the
488 * specified character.
490 * If the string does not contain the character, an empty string is returned.
492 * @param string the string where to locate the character
493 * @param chr the character to locate
494 * @return a substring starting at the last location of <code>chr</code>
498 sstr_t sstrrchr(sstr_t string, int chr);
501 * Returns an immutable substring starting at the location of the first
502 * occurrence of the specified character.
504 * If the string does not contain the character, an empty string is returned.
506 * @param string the string where to locate the character
507 * @param chr the character to locate
508 * @return a substring starting at the first location of <code>chr</code>
512 scstr_t scstrchr(scstr_t string, int chr);
515 * Returns an immutable substring starting at the location of the last
516 * occurrence of the specified character.
518 * If the string does not contain the character, an empty string is returned.
520 * @param string the string where to locate the character
521 * @param chr the character to locate
522 * @return a substring starting at the last location of <code>chr</code>
526 scstr_t scstrrchr(scstr_t string, int chr);
529 * Returns a substring starting at the location of the first occurrence of the
532 * If the string does not contain the other string, an empty string is returned.
534 * If <code>match</code> is an empty string, the complete <code>string</code> is
537 * @param string the string to be scanned
538 * @param match string containing the sequence of characters to match
539 * @return a substring starting at the first occurrence of
540 * <code>match</code>, or an empty string, if the sequence is not
541 * present in <code>string</code>
543 sstr_t scstrsstr(sstr_t string, scstr_t match);
546 * Returns a substring starting at the location of the first occurrence of the
549 * If the string does not contain the other string, an empty string is returned.
551 * If <code>match</code> is an empty string, the complete <code>string</code> is
554 * @param string the string to be scanned
555 * @param match string containing the sequence of characters to match
556 * @return a substring starting at the first occurrence of
557 * <code>match</code>, or an empty string, if the sequence is not
558 * present in <code>string</code>
560 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
563 * Returns an immutable substring starting at the location of the
564 * first occurrence of the specified immutable string.
566 * If the string does not contain the other string, an empty string is returned.
568 * If <code>match</code> is an empty string, the complete <code>string</code> is
571 * @param string the string to be scanned
572 * @param match string containing the sequence of characters to match
573 * @return a substring starting at the first occurrence of
574 * <code>match</code>, or an empty string, if the sequence is not
575 * present in <code>string</code>
577 scstr_t scstrscstr(scstr_t string, scstr_t match);
580 * Returns an immutable substring starting at the location of the
581 * first occurrence of the specified immutable string.
583 * If the string does not contain the other string, an empty string is returned.
585 * If <code>match</code> is an empty string, the complete <code>string</code> is
588 * @param string the string to be scanned
589 * @param match string containing the sequence of characters to match
590 * @return a substring starting at the first occurrence of
591 * <code>match</code>, or an empty string, if the sequence is not
592 * present in <code>string</code>
594 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
597 * Splits a string into parts by using a delimiter string.
599 * This function will return <code>NULL</code>, if one of the following happens:
601 * <li>the string length is zero</li>
602 * <li>the delimeter length is zero</li>
603 * <li>the string equals the delimeter</li>
604 * <li>memory allocation fails</li>
607 * The integer referenced by <code>count</code> is used as input and determines
608 * the maximum size of the resulting array, i.e. the maximum count of splits to
611 * The integer referenced by <code>count</code> is also used as output and is
614 * <li>-2, on memory allocation errors</li>
615 * <li>-1, if either the string or the delimiter is an empty string</li>
616 * <li>0, if the string equals the delimiter</li>
617 * <li>1, if the string does not contain the delimiter</li>
618 * <li>the count of array items, otherwise</li>
621 * If the string starts with the delimiter, the first item of the resulting
622 * array will be an empty string.
624 * If the string ends with the delimiter and the maximum list size is not
625 * exceeded, the last array item will be an empty string.
626 * In case the list size would be exceeded, the last array item will be the
627 * remaining string after the last split, <i>including</i> the terminating
630 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
631 * items must be manually passed to <code>free()</code>. Use scstrsplit_a() with
632 * an allocator to managed memory, to avoid this.
634 * @param string the string to split
635 * @param delim the delimiter string
636 * @param count IN: the maximum size of the resulting array (0 = no limit),
637 * OUT: the actual size of the array
638 * @return a sstr_t array containing the split strings or
639 * <code>NULL</code> on error
641 * @see scstrsplit_a()
643 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
646 * Splits a string into parts by using a delimiter string.
648 * This function will return <code>NULL</code>, if one of the following happens:
650 * <li>the string length is zero</li>
651 * <li>the delimeter length is zero</li>
652 * <li>the string equals the delimeter</li>
653 * <li>memory allocation fails</li>
656 * The integer referenced by <code>count</code> is used as input and determines
657 * the maximum size of the resulting array, i.e. the maximum count of splits to
660 * The integer referenced by <code>count</code> is also used as output and is
663 * <li>-2, on memory allocation errors</li>
664 * <li>-1, if either the string or the delimiter is an empty string</li>
665 * <li>0, if the string equals the delimiter</li>
666 * <li>1, if the string does not contain the delimiter</li>
667 * <li>the count of array items, otherwise</li>
670 * If the string starts with the delimiter, the first item of the resulting
671 * array will be an empty string.
673 * If the string ends with the delimiter and the maximum list size is not
674 * exceeded, the last array item will be an empty string.
675 * In case the list size would be exceeded, the last array item will be the
676 * remaining string after the last split, <i>including</i> the terminating
679 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
680 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
681 * an allocator to managed memory, to avoid this.
683 * @param string the string to split
684 * @param delim the delimiter string
685 * @param count IN: the maximum size of the resulting array (0 = no limit),
686 * OUT: the actual size of the array
687 * @return a sstr_t array containing the split strings or
688 * <code>NULL</code> on error
692 #define sstrsplit(string, delim, count) \
693 scstrsplit(SCSTR(string), SCSTR(delim), count)
696 * Performing scstrsplit() using a UcxAllocator.
698 * <i>Read the description of scstrsplit() for details.</i>
700 * The memory for the sstr_t.ptr pointers of the array items and the memory for
701 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
704 * @param allocator the UcxAllocator used for allocating memory
705 * @param string the string to split
706 * @param delim the delimiter string
707 * @param count IN: the maximum size of the resulting array (0 = no limit),
708 * OUT: the actual size of the array
709 * @return a sstr_t array containing the split strings or
710 * <code>NULL</code> on error
714 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
718 * Performing sstrsplit() using a UcxAllocator.
720 * <i>Read the description of sstrsplit() for details.</i>
722 * The memory for the sstr_t.ptr pointers of the array items and the memory for
723 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
726 * @param allocator the UcxAllocator used for allocating memory
727 * @param string the string to split
728 * @param delim the delimiter string
729 * @param count IN: the maximum size of the resulting array (0 = no limit),
730 * OUT: the actual size of the array
731 * @return a sstr_t array containing the split strings or
732 * <code>NULL</code> on error
736 #define sstrsplit_a(allocator, string, delim, count) \
737 scstrsplit_a(allocator, SCSTR(string), SCSTR(delim), count)
740 * Compares two UCX strings with standard <code>memcmp()</code>.
742 * At first it compares the scstr_t.length attribute of the two strings. The
743 * <code>memcmp()</code> function is called, if and only if the lengths match.
745 * @param s1 the first string
746 * @param s2 the second string
747 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
748 * length of s1 is greater than the length of s2 or the result of
749 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
751 int scstrcmp(scstr_t s1, scstr_t s2);
754 * Compares two UCX strings with standard <code>memcmp()</code>.
756 * At first it compares the sstr_t.length attribute of the two strings. The
757 * <code>memcmp()</code> function is called, if and only if the lengths match.
759 * @param s1 the first string
760 * @param s2 the second string
761 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
762 * length of s1 is greater than the length of s2 or the result of
763 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
765 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
768 * Compares two UCX strings ignoring the case.
770 * At first it compares the scstr_t.length attribute of the two strings. If and
771 * only if the lengths match, both strings are compared char by char ignoring
774 * @param s1 the first string
775 * @param s2 the second string
776 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
777 * length of s1 is greater than the length of s2 or the result of the platform
778 * specific string comparison function ignoring the case.
780 int scstrcasecmp(scstr_t s1, scstr_t s2);
783 * Compares two UCX strings ignoring the case.
785 * At first it compares the sstr_t.length attribute of the two strings. If and
786 * only if the lengths match, both strings are compared char by char ignoring
789 * @param s1 the first string
790 * @param s2 the second string
791 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
792 * length of s1 is greater than the length of s2 or the result of the platform
793 * specific string comparison function ignoring the case.
795 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
798 * Creates a duplicate of the specified string.
800 * The new sstr_t will contain a copy allocated by standard
801 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
802 * <code>free()</code>.
804 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
805 * terminated and mutable, regardless of the argument.
807 * @param string the string to duplicate
808 * @return a duplicate of the string
811 sstr_t scstrdup(scstr_t string);
814 * Creates a duplicate of the specified string.
816 * The new sstr_t will contain a copy allocated by standard
817 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
818 * <code>free()</code>.
820 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
821 * terminated, regardless of the argument.
823 * @param string the string to duplicate
824 * @return a duplicate of the string
827 #define sstrdup(string) scstrdup(SCSTR(string))
830 * Creates a duplicate of the specified string using a UcxAllocator.
832 * The new sstr_t will contain a copy allocated by the allocators
833 * UcxAllocator.malloc() function. So it is implementation depended, whether the
834 * returned sstr_t.ptr pointer must be passed to the allocators
835 * UcxAllocator.free() function manually.
837 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
838 * terminated and mutable, regardless of the argument.
840 * @param allocator a valid instance of a UcxAllocator
841 * @param string the string to duplicate
842 * @return a duplicate of the string
845 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
848 * Creates a duplicate of the specified string using a UcxAllocator.
850 * The new sstr_t will contain a copy allocated by the allocators
851 * UcxAllocator.malloc() function. So it is implementation depended, whether the
852 * returned sstr_t.ptr pointer must be passed to the allocators
853 * UcxAllocator.free() function manually.
855 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
856 * terminated, regardless of the argument.
858 * @param allocator a valid instance of a UcxAllocator
859 * @param string the string to duplicate
860 * @return a duplicate of the string
863 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
867 * Omits leading and trailing spaces.
869 * This function returns a new sstr_t containing a trimmed version of the
872 * <b>Note:</b> the new sstr_t references the same memory, thus you
873 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
874 * <code>free()</code>. It is also highly recommended to avoid assignments like
875 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
876 * source string. Assignments of this type are only permitted, if the
877 * sstr_t.ptr of the source string does not need to be freed or if another
878 * reference to the source string exists.
880 * @param string the string that shall be trimmed
881 * @return a new sstr_t containing the trimmed string
883 sstr_t sstrtrim(sstr_t string);
886 * Omits leading and trailing spaces.
888 * This function returns a new scstr_t containing a trimmed version of the
891 * <b>Note:</b> the new scstr_t references the same memory, thus you
892 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
893 * <code>free()</code>. It is also highly recommended to avoid assignments like
894 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
895 * source string. Assignments of this type are only permitted, if the
896 * scstr_t.ptr of the source string does not need to be freed or if another
897 * reference to the source string exists.
899 * @param string the string that shall be trimmed
900 * @return a new scstr_t containing the trimmed string
902 scstr_t scstrtrim(scstr_t string);
905 * Checks, if a string has a specific prefix.
907 * @param string the string to check
908 * @param prefix the prefix the string should have
909 * @return 1, if and only if the string has the specified prefix, 0 otherwise
911 int scstrprefix(scstr_t string, scstr_t prefix);
914 * Checks, if a string has a specific prefix.
916 * @param string the string to check
917 * @param prefix the prefix the string should have
918 * @return 1, if and only if the string has the specified prefix, 0 otherwise
920 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
923 * Checks, if a string has a specific suffix.
925 * @param string the string to check
926 * @param suffix the suffix the string should have
927 * @return 1, if and only if the string has the specified suffix, 0 otherwise
929 int scstrsuffix(scstr_t string, scstr_t suffix);
932 * Checks, if a string has a specific suffix.
934 * @param string the string to check
935 * @param suffix the suffix the string should have
936 * @return 1, if and only if the string has the specified suffix, 0 otherwise
938 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
941 * Checks, if a string has a specific prefix, ignoring the case.
943 * @param string the string to check
944 * @param prefix the prefix the string should have
945 * @return 1, if and only if the string has the specified prefix, 0 otherwise
947 int scstrcaseprefix(scstr_t string, scstr_t prefix);
950 * Checks, if a string has a specific prefix, ignoring the case.
952 * @param string the string to check
953 * @param prefix the prefix the string should have
954 * @return 1, if and only if the string has the specified prefix, 0 otherwise
956 #define sstrcaseprefix(string, prefix) \
957 scstrcaseprefix(SCSTR(string), SCSTR(prefix))
960 * Checks, if a string has a specific suffix, ignoring the case.
962 * @param string the string to check
963 * @param suffix the suffix the string should have
964 * @return 1, if and only if the string has the specified suffix, 0 otherwise
966 int scstrcasesuffix(scstr_t string, scstr_t suffix);
969 * Checks, if a string has a specific suffix, ignoring the case.
971 * @param string the string to check
972 * @param suffix the suffix the string should have
973 * @return 1, if and only if the string has the specified suffix, 0 otherwise
975 #define sstrcasesuffix(string, suffix) \
976 scstrcasesuffix(SCSTR(string), SCSTR(suffix))
979 * Returns a lower case version of a string.
981 * This function creates a duplicate of the input string, first
984 * @param string the input string
985 * @return the resulting lower case string
988 sstr_t scstrlower(scstr_t string);
991 * Returns a lower case version of a string.
993 * This function creates a duplicate of the input string, first
996 * @param string the input string
997 * @return the resulting lower case string
999 #define sstrlower(string) scstrlower(SCSTR(string))
1002 * Returns a lower case version of a string.
1004 * This function creates a duplicate of the input string, first
1005 * (see scstrdup_a()).
1007 * @param allocator the allocator used for duplicating the string
1008 * @param string the input string
1009 * @return the resulting lower case string
1012 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
1016 * Returns a lower case version of a string.
1018 * This function creates a duplicate of the input string, first
1019 * (see sstrdup_a()).
1021 * @param allocator the allocator used for duplicating the string
1022 * @param string the input string
1023 * @return the resulting lower case string
1025 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
1028 * Returns a upper case version of a string.
1030 * This function creates a duplicate of the input string, first
1033 * @param string the input string
1034 * @return the resulting upper case string
1037 sstr_t scstrupper(scstr_t string);
1040 * Returns a upper case version of a string.
1042 * This function creates a duplicate of the input string, first
1045 * @param string the input string
1046 * @return the resulting upper case string
1048 #define sstrupper(string) scstrupper(SCSTR(string))
1051 * Returns a upper case version of a string.
1053 * This function creates a duplicate of the input string, first
1054 * (see scstrdup_a()).
1056 * @param allocator the allocator used for duplicating the string
1057 * @param string the input string
1058 * @return the resulting upper case string
1061 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
1064 * Returns a upper case version of a string.
1066 * This function creates a duplicate of the input string, first
1067 * (see sstrdup_a()).
1069 * @param allocator the allocator used for duplicating the string
1070 * @param string the input string
1071 * @return the resulting upper case string
1073 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
1077 * Replaces a pattern in a string with another string.
1079 * The pattern is taken literally and is no regular expression.
1080 * Replaces at most <code>replmax</code> occurrences.
1082 * The resulting string is allocated by the specified allocator. I.e. it
1083 * depends on the used allocator, whether the sstr_t.ptr must be freed
1086 * If allocation fails, the sstr_t.ptr of the return value is NULL.
1088 * @param allocator the allocator to use
1089 * @param str the string where replacements should be applied
1090 * @param pattern the pattern to search for
1091 * @param replacement the replacement string
1092 * @param replmax maximum number of replacements
1093 * @return the resulting string after applying the replacements
1095 sstr_t scstrreplacen_a(UcxAllocator *allocator, scstr_t str,
1096 scstr_t pattern, scstr_t replacement, size_t replmax);
1099 * Replaces a pattern in a string with another string.
1101 * The pattern is taken literally and is no regular expression.
1102 * Replaces at most <code>replmax</code> occurrences.
1104 * The sstr_t.ptr of the resulting string must be freed manually.
1106 * If allocation fails, the sstr_t.ptr of the return value is NULL.
1108 * @param str the string where replacements should be applied
1109 * @param pattern the pattern to search for
1110 * @param replacement the replacement string
1111 * @param replmax maximum number of replacements
1112 * @return the resulting string after applying the replacements
1114 sstr_t scstrreplacen(scstr_t str, scstr_t pattern,
1115 scstr_t replacement, size_t replmax);
1118 * Replaces a pattern in a string with another string.
1120 * The pattern is taken literally and is no regular expression.
1121 * Replaces at most <code>replmax</code> occurrences.
1123 * The resulting string is allocated by the specified allocator. I.e. it
1124 * depends on the used allocator, whether the sstr_t.ptr must be freed
1127 * @param allocator the allocator to use
1128 * @param str the string where replacements should be applied
1129 * @param pattern the pattern to search for
1130 * @param replacement the replacement string
1131 * @param replmax maximum number of replacements
1132 * @return the resulting string after applying the replacements
1134 #define sstrreplacen_a(allocator, str, pattern, replacement, replmax) \
1135 scstrreplacen_a(allocator, SCSTR(str), SCSTR(pattern), \
1136 SCSTR(replacement), replmax)
1139 * Replaces a pattern in a string with another string.
1141 * The pattern is taken literally and is no regular expression.
1142 * Replaces at most <code>replmax</code> occurrences.
1144 * The sstr_t.ptr of the resulting string must be freed manually.
1146 * If allocation fails, the sstr_t.ptr of the return value is NULL.
1148 * @param str the string where replacements should be applied
1149 * @param pattern the pattern to search for
1150 * @param replacement the replacement string
1151 * @param replmax maximum number of replacements
1152 * @return the resulting string after applying the replacements
1154 #define sstrreplacen(str, pattern, replacement, replmax) \
1155 scstrreplacen(SCSTR(str), SCSTR(pattern), SCSTR(replacement), replmax)
1158 * Replaces a pattern in a string with another string.
1160 * The pattern is taken literally and is no regular expression.
1161 * Replaces at most <code>replmax</code> occurrences.
1163 * The resulting string is allocated by the specified allocator. I.e. it
1164 * depends on the used allocator, whether the sstr_t.ptr must be freed
1167 * If allocation fails, the sstr_t.ptr of the return value is NULL.
1169 * @param allocator the allocator to use
1170 * @param str the string where replacements should be applied
1171 * @param pattern the pattern to search for
1172 * @param replacement the replacement string
1173 * @return the resulting string after applying the replacements
1175 #define sstrreplace_a(allocator, str, pattern, replacement) \
1176 scstrreplacen_a(allocator, SCSTR(str), SCSTR(pattern), \
1177 SCSTR(replacement), SIZE_MAX)
1180 * Replaces a pattern in a string with another string.
1182 * The pattern is taken literally and is no regular expression.
1183 * Replaces at most <code>replmax</code> occurrences.
1185 * The sstr_t.ptr of the resulting string must be freed manually.
1187 * If allocation fails, the sstr_t.ptr of the return value is NULL.
1189 * @param str the string where replacements should be applied
1190 * @param pattern the pattern to search for
1191 * @param replacement the replacement string
1192 * @return the resulting string after applying the replacements
1194 #define sstrreplace(str, pattern, replacement) \
1195 scstrreplacen(SCSTR(str), SCSTR(pattern), SCSTR(replacement), SIZE_MAX)
1201 #endif /* UCX_STRING_H */