ucx/ucx/string.h

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

mercurial