Wed, 16 May 2018 19:33:31 +0200
Tags finalization of the scstr_t integration.
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 /**
99 * One of two type adjustment functions that return a scstr_t.
100 *
101 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
102 *
103 * <b>Do not use this function manually.</b>
104 *
105 * @param str some sstr_t
106 * @return an immutable (scstr_t) version of the provided string.
107 */
108 inline scstr_t s2scstr(sstr_t s) {
109 scstr_t c;
110 c.ptr = s.ptr;
111 c.length = s.ptr;
112 return c;
113 }
115 /**
116 * One of two type adjustment functions that return a scstr_t.
117 *
118 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
119 * This variant is used, when the string is already immutable and no operation
120 * needs to be performed.
121 *
122 * <b>Do not use this function manually.</b>
123 *
124 * @param str some scstr_t
125 * @return the argument itself
126 */
127 inline scstr_t s2scstr(scstr_t str) {
128 return str;
129 }
131 /**
132 * Converts a UCX string to an immutable UCX string (scstr_t).
133 * @param str some UCX string
134 * @return the an immutable version of the provided string
135 */
136 #define SCSTR(s) s2scstr(s)
137 #else
139 /**
140 * One of two type adjustment functions that return a scstr_t.
141 *
142 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
143 * This variant is used, when the string is already immutable and no operation
144 * needs to be performed.
145 *
146 * <b>Do not use this function manually.</b>
147 *
148 * @param str some scstr_t
149 * @return the argument itself
150 */
151 scstr_t ucx_sc2sc(scstr_t str);
153 /**
154 * One of two type adjustment functions that return a scstr_t.
155 *
156 * Used <b>internally</b> to convert a UCX string to an immutable UCX string.
157 *
158 * <b>Do not use this function manually.</b>
159 *
160 * @param str some sstr_t
161 * @return an immutable (scstr_t) version of the provided string.
162 */
163 scstr_t ucx_ss2sc(sstr_t str);
165 #if __STDC_VERSION__ >= 201112L
166 /**
167 * Converts a UCX string to an immutable UCX string (scstr_t).
168 * @param str some UCX string
169 * @return the an immutable version of the provided string
170 */
171 #define SCSTR(str) _Generic(str, sstr_t: ucx_ss2sc, scstr_t: ucx_sc2sc)(str)
173 #elif defined(__GNUC__) || defined(__clang__)
175 /**
176 * Converts a UCX string to an immutable UCX string (scstr_t).
177 * @param str some UCX string
178 * @return the an immutable version of the provided string
179 */
180 #define SCSTR(str) __builtin_choose_expr( \
181 __builtin_types_compatible_p(typeof(str), sstr_t), \
182 ucx_ss2sc, \
183 ucx_sc2sc)(str)
185 #elif defined(__sun)
187 /**
188 * Converts a UCX string to an immutable UCX string (scstr_t).
189 * @param str some UCX string
190 * @return the an immutable version of the provided string
191 */
192 #define SCSTR(str) ({typeof(str) ucx_tmp_var_str = str; \
193 scstr_t ucx_tmp_var_c; \
194 ucx_tmp_var_c.ptr = ucx_tmp_var_str.ptr;\
195 ucx_tmp_var_c.length = ucx_tmp_var_str.length;\
196 ucx_tmp_var_c; })
197 #else /* no generics and no builtins */
199 /**
200 * Converts a UCX string to an immutable UCX string (scstr_t).
201 *
202 * This <b>internal</b> function (ab)uses the C standard an expects one single
203 * argument which is then implicitly converted to scstr_t without a warning.
204 *
205 * <b>Do not use this function manually.</b>
206 *
207 * @return the an immutable version of the provided string
208 */
209 scstr_t ucx_ss2c_s();
211 /**
212 * Converts a UCX string to an immutable UCX string (scstr_t).
213 * @param str some UCX string
214 * @return the an immutable version of the provided string
215 */
216 #define SCSTR(str) ucx_ss2c_s(str)
217 #endif /* C11 feature test */
219 #endif /* C++ */
221 #ifdef __cplusplus
222 extern "C" {
223 #endif
226 /**
227 * Creates a new sstr_t based on a C string.
228 *
229 * The length is implicitly inferred by using a call to <code>strlen()</code>.
230 *
231 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
232 * do want a copy, use sstrdup() on the return value of this function.
233 *
234 * If you need to wrap a constant string, use scstr().
235 *
236 * @param cstring the C string to wrap
237 * @return a new sstr_t containing the C string
238 *
239 * @see sstrn()
240 */
241 sstr_t sstr(char *cstring);
243 /**
244 * Creates a new sstr_t of the specified length based on a C string.
245 *
246 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
247 * do want a copy, use sstrdup() on the return value of this function.
248 *
249 * If you need to wrap a constant string, use scstrn().
250 *
251 * @param cstring the C string to wrap
252 * @param length the length of the string
253 * @return a new sstr_t containing the C string
254 *
255 * @see sstr()
256 * @see S()
257 */
258 sstr_t sstrn(char *cstring, size_t length);
260 /**
261 * Creates a new scstr_t based on a constant C string.
262 *
263 * The length is implicitly inferred by using a call to <code>strlen()</code>.
264 *
265 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
266 * do want a copy, use scstrdup() on the return value of this function.
267 *
268 * @param cstring the C string to wrap
269 * @return a new scstr_t containing the C string
270 *
271 * @see scstrn()
272 */
273 scstr_t scstr(const char *cstring);
276 /**
277 * Creates a new scstr_t of the specified length based on a constant C string.
278 *
279 * <b>Note:</b> the scstr_t will hold a <i>reference</i> to the C string. If you
280 * do want a copy, use scstrdup() on the return value of this function.
281 *
282 *
283 * @param cstring the C string to wrap
284 * @param length the length of the string
285 * @return a new scstr_t containing the C string
286 *
287 * @see scstr()
288 */
289 scstr_t scstrn(const char *cstring, size_t length);
291 /**
292 * Returns the cumulated length of all specified strings.
293 *
294 * <b>Attention:</b> if the count argument does not match the count of the
295 * specified strings, the behavior is undefined.
296 *
297 * @param count the total number of specified strings (so at least 1)
298 * @param ... all strings
299 * @return the cumulated length of all strings
300 */
301 size_t scstrnlen(size_t count, ...);
303 /**
304 * Alias for scstrnlen() which automatically converts the arguments.
305 *
306 * @param count the total number of specified strings (so at least 1)
307 * @param ... all strings
308 * @return the cumulated length of all strings
309 */
310 #define sstrnlen(count, ...) scstrnlen(count, __VA_ARGS__)
312 /**
313 * Concatenates two or more strings.
314 *
315 * The resulting string will be allocated by standard <code>malloc()</code>.
316 * So developers <b>MUST</b> pass the sstr_t.ptr to <code>free()</code>.
317 *
318 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
319 * terminated.
320 *
321 * @param count the total number of strings to concatenate
322 * @param s1 first string
323 * @param ... all remaining strings
324 * @return the concatenated string
325 */
326 sstr_t scstrcat(size_t count, scstr_t s1, ...);
328 /**
329 * Alias for scstrcat() which automatically converts the arguments.
330 *
331 * @param count the total number of strings to concatenate
332 * @param s1 first string
333 * @param ... all remaining strings
334 * @return the concatenated string
335 */
336 #define sstrcat(count, s1, ...) scstrcat(count, SCSTR(s1), __VA_ARGS__)
338 /**
339 * Concatenates two or more strings using a UcxAllocator.
340 *
341 * See scstrcat() for details.
342 *
343 * @param a the allocator to use
344 * @param count the total number of strings to concatenate
345 * @param s1 first string
346 * @param ... all remaining strings
347 * @return the concatenated string
348 */
349 sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...);
351 /**
352 * Alias for scstrcat_a() which automatically converts the arguments.
353 *
354 * See sstrcat() for details.
355 *
356 * @param a the allocator to use
357 * @param count the total number of strings to concatenate
358 * @param s1 first string
359 * @param ... all remaining strings
360 * @return the concatenated string
361 */
362 #define sstrcat_a(a, count, s1, ...) \
363 scstrcat_a(a, count, SCSTR(s1), __VA_ARGS__)
365 /**
366 * Returns a substring starting at the specified 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 sstrdup() 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 sstrsubsl()
377 * @see sstrchr()
378 */
379 sstr_t sstrsubs(sstr_t string, size_t start);
381 /**
382 * Returns a substring with a maximum length starting at the specified location.
383 *
384 * <b>Attention:</b> the new string references the same memory area as the
385 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
386 * Use sstrdup() to get a copy.
387 *
388 * @param string input string
389 * @param start start location of the substring
390 * @param length the maximum length of the substring
391 * @return a substring of <code>string</code> starting at <code>start</code>
392 * with a maximum length of <code>length</code>
393 *
394 * @see sstrsubs()
395 * @see sstrchr()
396 */
397 sstr_t sstrsubsl(sstr_t string, size_t start, size_t length);
399 /**
400 * Returns a substring of an immutable string starting at the specified
401 * location.
402 *
403 * <b>Attention:</b> the new string references the same memory area as the
404 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
405 * Use scstrdup() to get a copy.
406 *
407 * @param string input string
408 * @param start start location of the substring
409 * @return a substring of <code>string</code> starting at <code>start</code>
410 *
411 * @see scstrsubsl()
412 * @see scstrchr()
413 */
414 scstr_t scstrsubs(scstr_t string, size_t start);
416 /**
417 * Returns a substring of an immutable string with a maximum length starting
418 * at the specified location.
419 *
420 * <b>Attention:</b> the new string references the same memory area as the
421 * input string and will <b>NOT</b> be <code>NULL</code>-terminated.
422 * Use scstrdup() to get a copy.
423 *
424 * @param string input string
425 * @param start start location of the substring
426 * @param length the maximum length of the substring
427 * @return a substring of <code>string</code> starting at <code>start</code>
428 * with a maximum length of <code>length</code>
429 *
430 * @see scstrsubs()
431 * @see scstrchr()
432 */
433 scstr_t scstrsubsl(scstr_t string, size_t start, size_t length);
435 /**
436 * Returns a substring starting at the location of the first occurrence of the
437 * specified character.
438 *
439 * If the string does not contain the character, an empty string is returned.
440 *
441 * @param string the string where to locate the character
442 * @param chr the character to locate
443 * @return a substring starting at the first location of <code>chr</code>
444 *
445 * @see sstrsubs()
446 */
447 sstr_t sstrchr(sstr_t string, int chr);
449 /**
450 * Returns a substring starting at the location of the last occurrence of the
451 * specified character.
452 *
453 * If the string does not contain the character, an empty string is returned.
454 *
455 * @param string the string where to locate the character
456 * @param chr the character to locate
457 * @return a substring starting at the last location of <code>chr</code>
458 *
459 * @see sstrsubs()
460 */
461 sstr_t sstrrchr(sstr_t string, int chr);
463 /**
464 * Returns an immutable substring starting at the location of the first
465 * occurrence of the specified character.
466 *
467 * If the string does not contain the character, an empty string is returned.
468 *
469 * @param string the string where to locate the character
470 * @param chr the character to locate
471 * @return a substring starting at the first location of <code>chr</code>
472 *
473 * @see scstrsubs()
474 */
475 scstr_t scstrchr(scstr_t string, int chr);
477 /**
478 * Returns an immutable substring starting at the location of the last
479 * occurrence of the specified character.
480 *
481 * If the string does not contain the character, an empty string is returned.
482 *
483 * @param string the string where to locate the character
484 * @param chr the character to locate
485 * @return a substring starting at the last location of <code>chr</code>
486 *
487 * @see scstrsubs()
488 */
489 scstr_t scstrrchr(scstr_t string, int chr);
491 /**
492 * Returns a substring starting at the location of the first occurrence of the
493 * specified string.
494 *
495 * If the string does not contain the other string, an empty string is returned.
496 *
497 * If <code>match</code> is an empty string, the complete <code>string</code> is
498 * returned.
499 *
500 * @param string the string to be scanned
501 * @param match string containing the sequence of characters to match
502 * @return a substring starting at the first occurrence of
503 * <code>match</code>, or an empty string, if the sequence is not
504 * present in <code>string</code>
505 */
506 sstr_t scstrsstr(sstr_t string, scstr_t match);
508 /**
509 * Alias for scstrsstr() which automatically converts the match string.
510 *
511 * @param string the string to be scanned
512 * @param match string containing the sequence of characters to match
513 * @return a substring starting at the first occurrence of
514 * <code>match</code>, or an empty string, if the sequence is not
515 * present in <code>string</code>
516 */
517 #define sstrstr(string, match) scstrsstr(string, SCSTR(match))
519 /**
520 * Returns an immutable substring starting at the location of the
521 * first occurrence of the specified immutable string.
522 *
523 * If the string does not contain the other string, an empty string is returned.
524 *
525 * If <code>match</code> is an empty string, the complete <code>string</code> is
526 * returned.
527 *
528 * @param string the string to be scanned
529 * @param match string containing the sequence of characters to match
530 * @return a substring starting at the first occurrence of
531 * <code>match</code>, or an empty string, if the sequence is not
532 * present in <code>string</code>
533 */
534 scstr_t scstrscstr(scstr_t string, scstr_t match);
536 /**
537 * Alias for scstrscstr() which automatically converts the match string.
538 *
539 * @param string the string to be scanned
540 * @param match string containing the sequence of characters to match
541 * @return a substring starting at the first occurrence of
542 * <code>match</code>, or an empty string, if the sequence is not
543 * present in <code>string</code>
544 */
545 #define sstrscstr(string, match) scstrscstr(string, SCSTR(match))
547 /**
548 * Splits a string into parts by using a delimiter string.
549 *
550 * This function will return <code>NULL</code>, if one of the following happens:
551 * <ul>
552 * <li>the string length is zero</li>
553 * <li>the delimeter length is zero</li>
554 * <li>the string equals the delimeter</li>
555 * <li>memory allocation fails</li>
556 * </ul>
557 *
558 * The integer referenced by <code>count</code> is used as input and determines
559 * the maximum size of the resulting array, i.e. the maximum count of splits to
560 * perform + 1.
561 *
562 * The integer referenced by <code>count</code> is also used as output and is
563 * set to
564 * <ul>
565 * <li>-2, on memory allocation errors</li>
566 * <li>-1, if either the string or the delimiter is an empty string</li>
567 * <li>0, if the string equals the delimiter</li>
568 * <li>1, if the string does not contain the delimiter</li>
569 * <li>the count of array items, otherwise</li>
570 * </ul>
571 *
572 * If the string starts with the delimiter, the first item of the resulting
573 * array will be an empty string.
574 *
575 * If the string ends with the delimiter and the maximum list size is not
576 * exceeded, the last array item will be an empty string.
577 * In case the list size would be exceeded, the last array item will be the
578 * remaining string after the last split, <i>including</i> the terminating
579 * delimiter.
580 *
581 * <b>Attention:</b> The array pointer <b>AND</b> all sstr_t.ptr of the array
582 * items must be manually passed to <code>free()</code>. Use sstrsplit_a() with
583 * an allocator to managed memory, to avoid this.
584 *
585 * @param string the string to split
586 * @param delim the delimiter string
587 * @param count IN: the maximum size of the resulting array (0 = no limit),
588 * OUT: the actual size of the array
589 * @return a sstr_t array containing the split strings or
590 * <code>NULL</code> on error
591 *
592 * @see scstrsplit_a()
593 */
594 sstr_t* scstrsplit(scstr_t string, scstr_t delim, ssize_t *count);
596 /**
597 * Alias for scstrsplit() which automatically converts the arguments.
598 *
599 * @param string the string to split
600 * @param delim the delimiter string
601 * @param count IN: the maximum size of the resulting array (0 = no limit),
602 * OUT: the actual size of the array
603 * @return a sstr_t array containing the split strings or
604 * <code>NULL</code> on error
605 *
606 * @see sstrsplit_a()
607 */
608 #define sstrsplit(string, delim, count) \
609 scstrsplit(SCSTR(string), SCSTR(delim), count)
611 /**
612 * Performing scstrsplit() using a UcxAllocator.
613 *
614 * <i>Read the description of scstrsplit() for details.</i>
615 *
616 * The memory for the sstr_t.ptr pointers of the array items and the memory for
617 * the sstr_t array itself are allocated by using the UcxAllocator.malloc()
618 * function.
619 *
620 * <b>Note:</b> the allocator is not used for memory that is freed within the
621 * same call of this function (locally scoped variables).
622 *
623 * @param allocator the UcxAllocator used for allocating memory
624 * @param string the string to split
625 * @param delim the delimiter string
626 * @param count IN: the maximum size of the resulting array (0 = no limit),
627 * OUT: the actual size of the array
628 * @return a sstr_t array containing the split strings or
629 * <code>NULL</code> on error
630 *
631 * @see scstrsplit()
632 */
633 sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t string, scstr_t delim,
634 ssize_t *count);
636 /**
637 * Alias for scstrsplit_a() which automatically converts the arguments.
638 *
639 * @param allocator the UcxAllocator used for allocating memory
640 * @param string the string to split
641 * @param delim the delimiter string
642 * @param count IN: the maximum size of the resulting array (0 = no limit),
643 * OUT: the actual size of the array
644 * @return a sstr_t array containing the split strings or
645 * <code>NULL</code> on error
646 *
647 * @see sstrsplit()
648 */
649 #define sstrsplit_a(allocator, string, delim, count) \
650 scstrsplit_a(allocator, SCSTR(string), SCSTR(delim), count)
652 /**
653 * Compares two UCX strings with standard <code>memcmp()</code>.
654 *
655 * At first it compares the scstr_t.length attribute of the two strings. The
656 * <code>memcmp()</code> function is called, if and only if the lengths match.
657 *
658 * @param s1 the first string
659 * @param s2 the second string
660 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
661 * length of s1 is greater than the length of s2 or the result of
662 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
663 */
664 int scstrcmp(scstr_t s1, scstr_t s2);
666 /**
667 * Alias for scstrcmp() which automatically converts its arguments.
668 *
669 * @param s1 the first string
670 * @param s2 the second string
671 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
672 * length of s1 is greater than the length of s2 or the result of
673 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
674 */
675 #define sstrcmp(s1, s2) scstrcmp(SCSTR(s1), SCSTR(s2))
677 /**
678 * Compares two UCX strings ignoring the case.
679 *
680 * At first it compares the scstr_t.length attribute of the two strings. If and
681 * only if the lengths match, both strings are compared char by char ignoring
682 * the case.
683 *
684 * @param s1 the first string
685 * @param s2 the second string
686 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
687 * length of s1 is greater than the length of s2 or the result of the platform
688 * specific string comparison function ignoring the case.
689 */
690 int scstrcasecmp(scstr_t s1, scstr_t s2);
692 /**
693 * Alias for scstrcasecmp() which automatically converts the arguments.
694 *
695 * @param s1 the first string
696 * @param s2 the second string
697 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
698 * length of s1 is greater than the length of s2 or the result of the platform
699 * specific string comparison function ignoring the case.
700 */
701 #define sstrcasecmp(s1, s2) scstrcasecmp(SCSTR(s1), SCSTR(s2))
703 /**
704 * Creates a duplicate of the specified string.
705 *
706 * The new sstr_t will contain a copy allocated by standard
707 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
708 * <code>free()</code>.
709 *
710 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
711 * terminated and mutable, regardless of the argument.
712 *
713 * @param string the string to duplicate
714 * @return a duplicate of the string
715 * @see scstrdup_a()
716 */
717 sstr_t scstrdup(scstr_t string);
719 /**
720 * Alias for scstrdup() which automatically converts the argument.
721 *
722 * @param string the string to duplicate
723 * @return a duplicate of the string
724 * @see sstrdup_a()
725 */
726 #define sstrdup(string) scstrdup(SCSTR(string))
728 /**
729 * Creates a duplicate of the specified string using a UcxAllocator.
730 *
731 * The new sstr_t will contain a copy allocated by the allocators
732 * UcxAllocator.malloc() function. So it is implementation depended, whether the
733 * returned sstr_t.ptr pointer must be passed to the allocators
734 * UcxAllocator.free() function manually.
735 *
736 * The sstr_t.ptr of the return value will <i>always</i> be <code>NULL</code>-
737 * terminated and mutable, regardless of the argument.
738 *
739 * @param allocator a valid instance of a UcxAllocator
740 * @param string the string to duplicate
741 * @return a duplicate of the string
742 * @see scstrdup()
743 */
744 sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t string);
746 /**
747 * Alias for scstrdup_a() which automatically converts the argument.
748 *
749 * @param allocator a valid instance of a UcxAllocator
750 * @param string the string to duplicate
751 * @return a duplicate of the string
752 * @see scstrdup()
753 */
754 #define sstrdup_a(allocator, string) scstrdup_a(allocator, SCSTR(string))
757 /**
758 * Omits leading and trailing spaces.
759 *
760 * This function returns a new sstr_t containing a trimmed version of the
761 * specified string.
762 *
763 * <b>Note:</b> the new sstr_t references the same memory, thus you
764 * <b>MUST NOT</b> pass the sstr_t.ptr of the return value to
765 * <code>free()</code>. It is also highly recommended to avoid assignments like
766 * <code>mystr = sstrtrim(mystr);</code> as you lose the reference to the
767 * source string. Assignments of this type are only permitted, if the
768 * sstr_t.ptr of the source string does not need to be freed or if another
769 * reference to the source string exists.
770 *
771 * @param string the string that shall be trimmed
772 * @return a new sstr_t containing the trimmed string
773 */
774 sstr_t sstrtrim(sstr_t string);
776 /**
777 * Omits leading and trailing spaces.
778 *
779 * This function returns a new scstr_t containing a trimmed version of the
780 * specified string.
781 *
782 * <b>Note:</b> the new scstr_t references the same memory, thus you
783 * <b>MUST NOT</b> pass the scstr_t.ptr of the return value to
784 * <code>free()</code>. It is also highly recommended to avoid assignments like
785 * <code>mystr = scstrtrim(mystr);</code> as you lose the reference to the
786 * source string. Assignments of this type are only permitted, if the
787 * scstr_t.ptr of the source string does not need to be freed or if another
788 * reference to the source string exists.
789 *
790 * @param string the string that shall be trimmed
791 * @return a new scstr_t containing the trimmed string
792 */
793 scstr_t scstrtrim(scstr_t string);
795 /**
796 * Checks, if a string has a specific prefix.
797 * @param string the string to check
798 * @param prefix the prefix the string should have
799 * @return 1, if and only if the string has the specified prefix, 0 otherwise
800 */
801 int scstrprefix(scstr_t string, scstr_t prefix);
803 /**
804 * Alias for scstrprefix() which automatically converts the arguments.
805 *
806 * @param string the string to check
807 * @param prefix the prefix the string should have
808 * @return 1, if and only if the string has the specified prefix, 0 otherwise
809 */
810 #define sstrprefix(string, prefix) scstrprefix(SCSTR(string), SCSTR(prefix))
812 /**
813 * Checks, if a string has a specific suffix.
814 * @param string the string to check
815 * @param suffix the suffix the string should have
816 * @return 1, if and only if the string has the specified suffix, 0 otherwise
817 */
818 int scstrsuffix(scstr_t string, scstr_t suffix);
820 /**
821 * Alias for scstrsuffix() which automatically converts the arguments.
822 *
823 * @param string the string to check
824 * @param suffix the suffix the string should have
825 * @return 1, if and only if the string has the specified suffix, 0 otherwise
826 */
827 #define sstrsuffix(string, suffix) scstrsuffix(SCSTR(string), SCSTR(suffix))
829 /**
830 * Returns a lower case version of a string.
831 *
832 * This function creates a duplicate of the input string, first. See the
833 * documentation of scstrdup() for the implications.
834 *
835 * @param string the input string
836 * @return the resulting lower case string
837 * @see scstrdup()
838 */
839 sstr_t scstrlower(scstr_t string);
841 /**
842 * Alias for scstrlower() which automatically converts the argument.
843 *
844 * @param string the input string
845 * @return the resulting lower case string
846 */
847 #define sstrlower(string) scstrlower(SCSTR(string))
849 /**
850 * Returns a lower case version of a string.
851 *
852 * This function creates a duplicate of the input string, first. See the
853 * documentation of scstrdup_a() for the implications.
854 *
855 * @param allocator the allocator used for duplicating the string
856 * @param string the input string
857 * @return the resulting lower case string
858 * @see scstrdup_a()
859 */
860 sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string);
863 /**
864 * Alias for scstrlower_a() which automatically converts the argument.
865 *
866 * @param allocator the allocator used for duplicating the string
867 * @param string the input string
868 * @return the resulting lower case string
869 */
870 #define sstrlower_a(allocator, string) scstrlower_a(allocator, SCSTR(string))
872 /**
873 * Returns a upper case version of a string.
874 *
875 * This function creates a duplicate of the input string, first. See the
876 * documentation of scstrdup() for the implications.
877 *
878 * @param string the input string
879 * @return the resulting upper case string
880 * @see scstrdup()
881 */
882 sstr_t scstrupper(scstr_t string);
884 /**
885 * Alias for scstrupper() which automatically converts the argument.
886 *
887 * @param string the input string
888 * @return the resulting upper case string
889 */
890 #define sstrupper(string) scstrupper(SCSTR(string))
892 /**
893 * Returns a upper case version of a string.
894 *
895 * This function creates a duplicate of the input string, first. See the
896 * documentation of scstrdup_a() for the implications.
897 *
898 * @param allocator the allocator used for duplicating the string
899 * @param string the input string
900 * @return the resulting upper case string
901 * @see scstrdup_a()
902 */
903 sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string);
905 /**
906 * Alias for scstrupper_a() which automatically converts the argument.
907 *
908 * @param allocator the allocator used for duplicating the string
909 * @param string the input string
910 * @return the resulting upper case string
911 */
912 #define sstrupper_a(allocator, string) scstrupper_a(allocator, string)
914 #ifdef __cplusplus
915 }
916 #endif
918 #endif /* UCX_STRING_H */