Wed, 16 May 2018 19:01:21 +0200
use 'convert' as more precise term than 'cast' for the conversion from sstr_t to scstr_t
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 convert 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 convert 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 converted 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 * <b>Attention:</b> if the count argument does not match the count of the
260 * specified strings, the behavior is undefined.
261 *
262 * @param count the total number of specified strings (so at least 1)
263 * @param ... all strings
264 * @return the cumulated length of all strings
265 */
266 size_t scstrnlen(size_t count, ...);
268 /**
269 * Alias for scstrnlen() which automatically converts the arguments.
270 *
271 * @param count the total number of specified strings (so at least 1)
272 * @param ... all strings
273 * @return the cumulated length of all strings
274 */
275 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
277 /**
278 * Concatenates two or more strings.
279 *
280 * The resulting string will be allocated by standard <code>malloc()</code>.
281 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
282 *
283 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
284 * terminated.
285 *
286 * @param count the total number of strings to concatenate
287 * @param s1 first string
288 * @param ... all remaining strings
289 * @return the concatenated string
290 */
291 sstr_t scstrcat(size_t count, scstr_t s1, ...);
293 /**
294 * Alias for scstrcat() which automatically converts the arguments.
295 *
296 * @param count the total number of strings to concatenate
297 * @param s1 first string
298 * @param ... all remaining strings
299 * @return the concatenated string
300 */
301 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
303 /**
304 * Concatenates two or more strings using a UcxAllocator.
305 *
306 * See scstrcat() for details.
307 *
308 * @param a the allocator to use
309 * @param count the total number of strings to concatenate
310 * @param s1 first string
311 * @param ... all remaining strings
312 * @return the concatenated string
313 */
314 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
316 /**
317 * Alias for scstrcat_a() which automatically converts the arguments.
318 *
319 * See sstrcat() for details.
320 *
321 * @param a the allocator to use
322 * @param count the total number of strings to concatenate
323 * @param s1 first string
324 * @param ... all remaining strings
325 * @return the concatenated string
326 */
327 #define sstrcat_a(a, count, s1, ...) \
328 scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__)
330 /**
331 * Returns a substring starting at the specified location.
332 *
333 * <b>Attention:</b> the new string references the same memory area as the
334 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
335 * Use sstrdup() to get a copy.
336 *
337 * @param string input string
338 * @param start start location of the substring
339 * @return a substring of <code>string</code> starting at <code>start</code>
340 *
341 * @see sstrsubsl()
342 * @see sstrchr()
343 */
344 sstr_t sstrsubs(sstr_t string, size_t start);
346 /**
347 * Returns a substring with a maximum length starting at the specified location.
348 *
349 * <b>Attention:</b> the new string references the same memory area as the
350 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
351 * Use sstrdup() to get a copy.
352 *
353 * @param string input string
354 * @param start start location of the substring
355 * @param length the maximum length of the substring
356 * @return a substring of <code>string</code> starting at <code>start</code>
357 * with a maximum length of <code>length</code>
358 *
359 * @see sstrsubs()
360 * @see sstrchr()
361 */
362 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
364 /**
365 * Returns a substring of an immutable string starting at the specified
366 * location.
367 *
368 * <b>Attention:</b> the new string references the same memory area as the
369 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
370 * Use scstrdup() to get a copy.
371 *
372 * @param string input string
373 * @param start start location of the substring
374 * @return a substring of <code>string</code> starting at <code>start</code>
375 *
376 * @see scstrsubsl()
377 * @see scstrchr()
378 */
379 scstr_t scstrsubs(scstr_t string, size_t start);
381 /**
382 * Returns a substring of an immutable string with a maximum length starting
383 * at the specified location.
384 *
385 * <b>Attention:</b> the new string references the same memory area as the
386 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
387 * Use scstrdup() to get a copy.
388 *
389 * @param string input string
390 * @param start start location of the substring
391 * @param length the maximum length of the substring
392 * @return a substring of <code>string</code> starting at <code>start</code>
393 * with a maximum length of <code>length</code>
394 *
395 * @see scstrsubs()
396 * @see scstrchr()
397 */
398 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
400 /**
401 * Returns a substring starting at the location of the first occurrence of the
402 * specified character.
403 *
404 * If the string does not contain the character, an empty string is returned.
405 *
406 * @param string the string where to locate the character
407 * @param chr the character to locate
408 * @return a substring starting at the first location of <code>chr</code>
409 *
410 * @see sstrsubs()
411 */
412 sstr_t sstrchr(sstr_t string, int chr);
414 /**
415 * Returns a substring starting at the location of the last occurrence of the
416 * specified character.
417 *
418 * If the string does not contain the character, an empty string is returned.
419 *
420 * @param string the string where to locate the character
421 * @param chr the character to locate
422 * @return a substring starting at the last location of <code>chr</code>
423 *
424 * @see sstrsubs()
425 */
426 sstr_t sstrrchr(sstr_t string, int chr);
428 /**
429 * Returns an immutable substring starting at the location of the first
430 * occurrence of the specified character.
431 *
432 * If the string does not contain the character, an empty string is returned.
433 *
434 * @param string the string where to locate the character
435 * @param chr the character to locate
436 * @return a substring starting at the first location of <code>chr</code>
437 *
438 * @see scstrsubs()
439 */
440 scstr_t scstrchr(scstr_t string, int chr);
442 /**
443 * Returns an immutable substring starting at the location of the last
444 * occurrence of the specified character.
445 *
446 * If the string does not contain the character, an empty string is returned.
447 *
448 * @param string the string where to locate the character
449 * @param chr the character to locate
450 * @return a substring starting at the last location of <code>chr</code>
451 *
452 * @see scstrsubs()
453 */
454 scstr_t scstrrchr(scstr_t string, int chr);
456 /**
457 * Returns a substring starting at the location of the first occurrence of the
458 * specified string.
459 *
460 * If the string does not contain the other string, an empty string is returned.
461 *
462 * If <code>match</code> is an empty string, the complete <code>string</code> is
463 * returned.
464 *
465 * @param string the string to be scanned
466 * @param match string containing the sequence of characters to match
467 * @return a substring starting at the first occurrence of
468 * <code>match</code>, or an empty string, if the sequence is not
469 * present in <code>string</code>
470 */
471 sstr_t scstrsstr(sstr_t string, scstr_t match);
473 /**
474 * Alias for scstrsstr() which automatically converts the match string.
475 *
476 * @param string the string to be scanned
477 * @param match string containing the sequence of characters to match
478 * @return a substring starting at the first occurrence of
479 * <code>match</code>, or an empty string, if the sequence is not
480 * present in <code>string</code>
481 */
482 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
484 /**
485 * Returns an immutable substring starting at the location of the
486 * first occurrence of the specified immutable string.
487 *
488 * If the string does not contain the other string, an empty string is returned.
489 *
490 * If <code>match</code> is an empty string, the complete <code>string</code> is
491 * returned.
492 *
493 * @param string the string to be scanned
494 * @param match string containing the sequence of characters to match
495 * @return a substring starting at the first occurrence of
496 * <code>match</code>, or an empty string, if the sequence is not
497 * present in <code>string</code>
498 */
499 scstr_t scstrscstr(scstr_t string, scstr_t match);
501 /**
502 * Alias for scstrscstr() which automatically converts the match string.
503 *
504 * @param string the string to be scanned
505 * @param match string containing the sequence of characters to match
506 * @return a substring starting at the first occurrence of
507 * <code>match</code>, or an empty string, if the sequence is not
508 * present in <code>string</code>
509 */
510 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
512 /**
513 * Splits a string into parts by using a delimiter string.
514 *
515 * This function will return <code>NULL</code>, if one of the following happens:
516 * <ul>
517 * <li>the string length is zero</li>
518 * <li>the delimeter length is zero</li>
519 * <li>the string equals the delimeter</li>
520 * <li>memory allocation fails</li>
521 * </ul>
522 *
523 * The integer referenced by <code>count</code> is used as input and determines
524 * the maximum size of the resulting array, i.e. the maximum count of splits to
525 * perform + 1.
526 *
527 * The integer referenced by <code>count</code> is also used as output and is
528 * set to
529 * <ul>
530 * <li>-2, on memory allocation errors</li>
531 * <li>-1, if either the string or the delimiter is an empty string</li>
532 * <li>0, if the string equals the delimiter</li>
533 * <li>1, if the string does not contain the delimiter</li>
534 * <li>the count of array items, otherwise</li>
535 * </ul>
536 *
537 * If the string starts with the delimiter, the first item of the resulting
538 * array will be an empty string.
539 *
540 * If the string ends with the delimiter and the maximum list size is not
541 * exceeded, the last array item will be an empty string.
542 * In case the list size would be exceeded, the last array item will be the
543 * remaining string after the last split, <i>including</i> the terminating
544 * delimiter.
545 *
546 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
547 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
548 * an allocator to managed memory, to avoid this.
549 *
550 * @param string the string to split
551 * @param delim the delimiter string
552 * @param count IN: the maximum size of the resulting array (0 = no limit),
553 * OUT: the actual size of the array
554 * @return a sstr_t array containing the split strings or
555 * <code>NULL</code> on error
556 *
557 * @see scstrsplit_a()
558 */
559 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
561 /**
562 * Alias for scstrsplit() which automatically converts the arguments.
563 *
564 * @param string the string to split
565 * @param delim the delimiter string
566 * @param count IN: the maximum size of the resulting array (0 = no limit),
567 * OUT: the actual size of the array
568 * @return a sstr_t array containing the split strings or
569 * <code>NULL</code> on error
570 *
571 * @see sstrsplit_a()
572 */
573 #define sstrsplit(string, delim, count) \
574 scstrsplit(SCSTR(string), SCSTR(delim), count)
576 /**
577 * Performing scstrsplit() using a UcxAllocator.
578 *
579 * <i>Read the description of scstrsplit() for details.</i>
580 *
581 * The memory for the sstr_t.ptr pointers of the array items and the memory for
582 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
583 * function.
584 *
585 * <b>Note:</b> the allocator is not used for memory that is freed within the
586 * same call of this function (locally scoped variables).
587 *
588 * @param allocator the UcxAllocator used for allocating memory
589 * @param string the string to split
590 * @param delim the delimiter string
591 * @param count IN: the maximum size of the resulting array (0 = no limit),
592 * OUT: the actual size of the array
593 * @return a sstr_t array containing the split strings or
594 * <code>NULL</code> on error
595 *
596 * @see scstrsplit()
597 */
598 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
599 ssize_t *count);
601 /**
602 * Alias for scstrsplit_a() which automatically converts the arguments.
603 *
604 * @param allocator the UcxAllocator used for allocating memory
605 * @param string the string to split
606 * @param delim the delimiter string
607 * @param count IN: the maximum size of the resulting array (0 = no limit),
608 * OUT: the actual size of the array
609 * @return a sstr_t array containing the split strings or
610 * <code>NULL</code> on error
611 *
612 * @see sstrsplit()
613 */
614 #define sstrsplit_a(allocator, string, delim, count) \
615 scstrsplit_a(allocator, SCSTR(string), SCSTR(delim, count))
617 /**
618 * Compares two UCX strings with standard <code>memcmp()</code>.
619 *
620 * At first it compares the scstr_t.length attribute of the two strings. The
621 * <code>memcmp()</code> function is called, if and only if the lengths match.
622 *
623 * @param s1 the first string
624 * @param s2 the second string
625 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
626 * length of s1 is greater than the length of s2 or the result of
627 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
628 */
629 int scstrcmp(scstr_t s1, scstr_t s2);
631 /**
632 * Alias for scstrcmp() which automatically converts its arguments.
633 *
634 * @param s1 the first string
635 * @param s2 the second string
636 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
637 * length of s1 is greater than the length of s2 or the result of
638 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
639 */
640 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
642 /**
643 * Compares two UCX strings ignoring the case.
644 *
645 * At first it compares the scstr_t.length attribute of the two strings. If and
646 * only if the lengths match, both strings are compared char by char ignoring
647 * the case.
648 *
649 * @param s1 the first string
650 * @param s2 the second string
651 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
652 * length of s1 is greater than the length of s2 or the result of the platform
653 * specific string comparison function ignoring the case.
654 */
655 int scstrcasecmp(scstr_t s1, scstr_t s2);
657 /**
658 * Alias for scstrcasecmp() which automatically converts the arguments.
659 *
660 * @param s1 the first string
661 * @param s2 the second string
662 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
663 * length of s1 is greater than the length of s2 or the result of the platform
664 * specific string comparison function ignoring the case.
665 */
666 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
668 /**
669 * Creates a duplicate of the specified string.
670 *
671 * The new sstr_t will contain a copy allocated by standard
672 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
673 * <code>free()</code>.
674 *
675 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
676 * terminated and mutable, regardless of the argument.
677 *
678 * @param string the string to duplicate
679 * @return a duplicate of the string
680 * @see scstrdup_a()
681 */
682 sstr_t scstrdup(scstr_t string);
684 /**
685 * Alias for scstrdup() which automatically converts the argument.
686 *
687 * @param string the string to duplicate
688 * @return a duplicate of the string
689 * @see sstrdup_a()
690 */
691 #define sstrdup(string) scstrdup(SCSTR(string))
693 /**
694 * Creates a duplicate of the specified string using a UcxAllocator.
695 *
696 * The new sstr_t will contain a copy allocated by the allocators
697 * UcxAllocator.malloc() function. So it is implementation depended, whether the
698 * returned sstr_t.ptr pointer must be passed to the allocators
699 * UcxAllocator.free() function manually.
700 *
701 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
702 * terminated and mutable, regardless of the argument.
703 *
704 * @param allocator a valid instance of a UcxAllocator
705 * @param string the string to duplicate
706 * @return a duplicate of the string
707 * @see scstrdup()
708 */
709 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
711 /**
712 * Alias for scstrdup_a() which automatically converts the argument.
713 *
714 * @param allocator a valid instance of a UcxAllocator
715 * @param string the string to duplicate
716 * @return a duplicate of the string
717 * @see scstrdup()
718 */
719 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
722 /**
723 * Omits leading and trailing spaces.
724 *
725 * This function returns a new sstr_t containing a trimmed version of the
726 * specified string.
727 *
728 * <b>Note:</b> the new sstr_t references the same memory, thus you
729 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
730 * <code>free()</code>. It is also highly recommended to avoid assignments like
731 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
732 * source string. Assignments of this type are only permitted, if the
733 * sstr_t.ptr of the source string does not need to be freed or if another
734 * reference to the source string exists.
735 *
736 * @param string the string that shall be trimmed
737 * @return a new sstr_t containing the trimmed string
738 */
739 sstr_t sstrtrim(sstr_t string);
741 /**
742 * Omits leading and trailing spaces.
743 *
744 * This function returns a new scstr_t containing a trimmed version of the
745 * specified string.
746 *
747 * <b>Note:</b> the new scstr_t references the same memory, thus you
748 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
749 * <code>free()</code>. It is also highly recommended to avoid assignments like
750 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
751 * source string. Assignments of this type are only permitted, if the
752 * scstr_t.ptr of the source string does not need to be freed or if another
753 * reference to the source string exists.
754 *
755 * @param string the string that shall be trimmed
756 * @return a new scstr_t containing the trimmed string
757 */
758 scstr_t scstrtrim(scstr_t string);
760 /**
761 * Checks, if a string has a specific prefix.
762 * @param string the string to check
763 * @param prefix the prefix the string should have
764 * @return 1, if and only if the string has the specified prefix, 0 otherwise
765 */
766 int scstrprefix(scstr_t string, scstr_t prefix);
768 /**
769 * Alias for scstrprefix() which automatically converts the arguments.
770 *
771 * @param string the string to check
772 * @param prefix the prefix the string should have
773 * @return 1, if and only if the string has the specified prefix, 0 otherwise
774 */
775 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
777 /**
778 * Checks, if a string has a specific suffix.
779 * @param string the string to check
780 * @param suffix the suffix the string should have
781 * @return 1, if and only if the string has the specified suffix, 0 otherwise
782 */
783 int scstrsuffix(scstr_t string, scstr_t suffix);
785 /**
786 * Alias for scstrsuffix() which automatically converts the arguments.
787 *
788 * @param string the string to check
789 * @param suffix the suffix the string should have
790 * @return 1, if and only if the string has the specified suffix, 0 otherwise
791 */
792 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
794 /**
795 * Returns a lower case version of a string.
796 *
797 * This function creates a duplicate of the input string, first. See the
798 * documentation of scstrdup() for the implications.
799 *
800 * @param string the input string
801 * @return the resulting lower case string
802 * @see scstrdup()
803 */
804 sstr_t scstrlower(scstr_t string);
806 /**
807 * Alias for scstrlower() which automatically converts the argument.
808 *
809 * @param string the input string
810 * @return the resulting lower case string
811 */
812 #define sstrlower(string) scstrlower(SCSTR(string))
814 /**
815 * Returns a lower case version of a string.
816 *
817 * This function creates a duplicate of the input string, first. See the
818 * documentation of scstrdup_a() for the implications.
819 *
820 * @param allocator the allocator used for duplicating the string
821 * @param string the input string
822 * @return the resulting lower case string
823 * @see scstrdup_a()
824 */
825 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
828 /**
829 * Alias for scstrlower_a() which automatically converts the argument.
830 *
831 * @param allocator the allocator used for duplicating the string
832 * @param string the input string
833 * @return the resulting lower case string
834 */
835 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
837 /**
838 * Returns a upper case version of a string.
839 *
840 * This function creates a duplicate of the input string, first. See the
841 * documentation of scstrdup() for the implications.
842 *
843 * @param string the input string
844 * @return the resulting upper case string
845 * @see scstrdup()
846 */
847 sstr_t scstrupper(scstr_t string);
849 /**
850 * Alias for scstrupper() which automatically converts the argument.
851 *
852 * @param string the input string
853 * @return the resulting upper case string
854 */
855 #define sstrupper(string) scstrupper(SCSTR(string))
857 /**
858 * Returns a upper case version of a string.
859 *
860 * This function creates a duplicate of the input string, first. See the
861 * documentation of scstrdup_a() for the implications.
862 *
863 * @param allocator the allocator used for duplicating the string
864 * @param string the input string
865 * @return the resulting upper case string
866 * @see scstrdup_a()
867 */
868 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
870 /**
871 * Alias for scstrupper_a() which automatically converts the argument.
872 *
873 * @param allocator the allocator used for duplicating the string
874 * @param string the input string
875 * @return the resulting upper case string
876 */
877 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
879 #ifdef __cplusplus
880 }
881 #endif
883 #endif /* UCX_STRING_H */