| 292 * @param str (@c cxstring or @c cxmutstr) the string to cast |
313 * @param str (@c cxstring or @c cxmutstr) the string to cast |
| 293 * @return (@c cxstring) an immutable copy of the string pointer |
314 * @return (@c cxstring) an immutable copy of the string pointer |
| 294 */ |
315 */ |
| 295 #define cx_strcast(str) _Generic((str), \ |
316 #define cx_strcast(str) _Generic((str), \ |
| 296 cxmutstr: cx_strcast_m, \ |
317 cxmutstr: cx_strcast_m, \ |
| 297 cxstring: cx_strcast_c) \ |
318 cxstring: cx_strcast_c, \ |
| 298 (str) |
319 const char*: cx_strcast_z, \ |
| |
320 char *: cx_strcast_z) (str) |
| 299 #endif |
321 #endif |
| 300 |
322 |
| 301 /** |
323 /** |
| 302 * Passes the pointer in this string to @c free(). |
324 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function. |
| 303 * |
325 * |
| 304 * The pointer in the struct is set to @c NULL and the length is set to zero |
326 * The pointer in the struct is set to @c NULL and the length is set to zero |
| 305 * which means that this function protects you against double-free. |
327 * which means that this function protects you against double-free. |
| 306 * |
328 * |
| 307 * @note There is no implementation for cxstring, because it is unlikely that |
329 * @note There is no implementation for cxstring, because it is unlikely that |
| 330 cx_attr_export |
352 cx_attr_export |
| 331 void cx_strfree_a( |
353 void cx_strfree_a( |
| 332 const CxAllocator *alloc, |
354 const CxAllocator *alloc, |
| 333 cxmutstr *str |
355 cxmutstr *str |
| 334 ); |
356 ); |
| |
357 |
| |
358 /** |
| |
359 * Copies a string. |
| |
360 * |
| |
361 * The memory in the @p dest structure is either allocated or re-allocated to fit the entire |
| |
362 * source string, including a zero-terminator. |
| |
363 * |
| |
364 * The string in @p dest is guaranteed to be zero-terminated, regardless of whether @p src is. |
| |
365 * |
| |
366 * @param alloc the allocator |
| |
367 * @param dest a pointer to the structure where to copy the contents to |
| |
368 * @param src the source string |
| |
369 * |
| |
370 * @retval zero success |
| |
371 * @retval non-zero if re-allocation failed |
| |
372 */ |
| |
373 cx_attr_nonnull_arg(1) |
| |
374 cx_attr_export |
| |
375 int cx_strcpy_a( |
| |
376 const CxAllocator *alloc, |
| |
377 cxmutstr *dest, |
| |
378 cxstring src |
| |
379 ); |
| |
380 |
| |
381 |
| |
382 /** |
| |
383 * Copies a string. |
| |
384 * |
| |
385 * The memory in the @p dest structure is either allocated or re-allocated to fit the entire |
| |
386 * source string, including a zero-terminator. |
| |
387 * |
| |
388 * The string in @p dest is guaranteed to be zero-terminated, regardless of whether @p src is. |
| |
389 * |
| |
390 * @param dest (@c cxmutstr*) a pointer to the structure where to copy the contents to |
| |
391 * @param src (@c cxstring) the source string |
| |
392 * |
| |
393 * @retval zero success |
| |
394 * @retval non-zero if re-allocation failed |
| |
395 */ |
| |
396 #define cx_strcpy(dest, src) cx_strcpy_a(cxDefaultAllocator, dest, src) |
| 335 |
397 |
| 336 /** |
398 /** |
| 337 * Returns the accumulated length of all specified strings. |
399 * Returns the accumulated length of all specified strings. |
| 338 * |
400 * |
| 339 * If this sum overflows, errno is set to EOVERFLOW. |
401 * If this sum overflows, errno is set to EOVERFLOW. |
| 406 cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
468 cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
| 407 |
469 |
| 408 /** |
470 /** |
| 409 * Concatenates strings and returns a new string. |
471 * Concatenates strings and returns a new string. |
| 410 * |
472 * |
| 411 * The resulting string will be allocated by standard @c malloc(). |
473 * The resulting string will be allocated by the cxDefaultAllocator. |
| 412 * So developers @em must pass the return value to cx_strfree() eventually. |
474 * So developers @em must pass the return value to cx_strfree() eventually. |
| 413 * |
475 * |
| 414 * If memory allocation fails, the pointer in the returned string will |
476 * If memory allocation fails, the pointer in the returned string will |
| 415 * be @c NULL and @c errno might be set. |
477 * be @c NULL and @c errno might be set. |
| 416 * |
478 * |
| 426 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
488 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__) |
| 427 |
489 |
| 428 /** |
490 /** |
| 429 * Concatenates strings. |
491 * Concatenates strings. |
| 430 * |
492 * |
| 431 * The resulting string will be allocated by standard @c malloc(). |
493 * The resulting string will be allocated by the cxDefaultAllocator. |
| 432 * So developers @em must pass the return value to cx_strfree() eventually. |
494 * So developers @em must pass the return value to cx_strfree() eventually. |
| 433 * |
495 * |
| 434 * If @p str already contains a string, the memory will be reallocated and |
496 * If @p str already contains a string, the memory will be reallocated and |
| 435 * the other strings are appended. Otherwise, new memory is allocated. |
497 * the other strings are appended. Otherwise, new memory is allocated. |
| 436 * |
498 * |
| 877 * @return (@c cxmutstr) a duplicate of the string |
939 * @return (@c cxmutstr) a duplicate of the string |
| 878 * @see cx_strdup() |
940 * @see cx_strdup() |
| 879 * @see cx_strfree_a() |
941 * @see cx_strfree_a() |
| 880 */ |
942 */ |
| 881 #define cx_strdup_a(allocator, string) \ |
943 #define cx_strdup_a(allocator, string) \ |
| 882 cx_strdup_a_((allocator), cx_strcast((string))) |
944 cx_strdup_a_((allocator), cx_strcast(string)) |
| 883 |
945 |
| 884 /** |
946 /** |
| 885 * Creates a duplicate of the specified string. |
947 * Creates a duplicate of the specified string. |
| 886 * |
948 * |
| 887 * The new string will contain a copy allocated by standard |
949 * The new string will contain a copy allocated by the cxDefaultAllocator. |
| 888 * @c malloc(). So developers @em must pass the return value to cx_strfree(). |
950 * So developers @em must pass the return value to cx_strfree(). |
| 889 * |
951 * |
| 890 * @note The returned string is guaranteed to be zero-terminated. |
952 * @note The returned string is guaranteed to be zero-terminated. |
| 891 * |
953 * |
| 892 * @param string the string to duplicate |
954 * @param string the string to duplicate |
| 893 * @return (@c cxmutstr) a duplicate of the string |
955 * @return (@c cxmutstr) a duplicate of the string |
| 894 * @see cx_strdup_a() |
956 * @see cx_strdup_a() |
| 895 * @see cx_strfree() |
957 * @see cx_strfree() |
| 896 */ |
958 */ |
| 897 #define cx_strdup(string) cx_strdup_a_(cxDefaultAllocator, string) |
959 #define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string) |
| 898 |
960 |
| 899 /** |
961 /** |
| 900 * Omits leading and trailing spaces. |
962 * Omits leading and trailing spaces. |
| 901 * |
963 * |
| 902 * @note the returned string references the same memory, thus you |
964 * @note the returned string references the same memory, thus you |
| 1014 /** |
1076 /** |
| 1015 * Replaces a string with another string. |
1077 * Replaces a string with another string. |
| 1016 * |
1078 * |
| 1017 * Replaces at most @p replmax occurrences. |
1079 * Replaces at most @p replmax occurrences. |
| 1018 * |
1080 * |
| 1019 * The returned string will be allocated by @c malloc() and is guaranteed |
1081 * The returned string will be allocated by the cxDefaultAllocator and is guaranteed |
| 1020 * to be zero-terminated. |
1082 * to be zero-terminated. |
| 1021 * |
1083 * |
| 1022 * If allocation fails, or the input string is empty, |
1084 * If allocation fails, or the input string is empty, |
| 1023 * the returned string will be empty. |
1085 * the returned string will be empty. |
| 1024 * |
1086 * |
| 1050 cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX) |
1112 cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX) |
| 1051 |
1113 |
| 1052 /** |
1114 /** |
| 1053 * Replaces a string with another string. |
1115 * Replaces a string with another string. |
| 1054 * |
1116 * |
| 1055 * The returned string will be allocated by @c malloc() and is guaranteed |
1117 * The returned string will be allocated by the cxDefaultAllocator and is guaranteed |
| 1056 * to be zero-terminated. |
1118 * to be zero-terminated. |
| 1057 * |
1119 * |
| 1058 * If allocation fails, or the input string is empty, |
1120 * If allocation fails, or the input string is empty, |
| 1059 * the returned string will be empty. |
1121 * the returned string will be empty. |
| 1060 * |
1122 * |
| 1089 * @param delim the delimiter string (must not be empty) |
1151 * @param delim the delimiter string (must not be empty) |
| 1090 * @param limit (@c size_t) the maximum number of tokens that shall be returned |
1152 * @param limit (@c size_t) the maximum number of tokens that shall be returned |
| 1091 * @return (@c CxStrtokCtx) a new string tokenization context |
1153 * @return (@c CxStrtokCtx) a new string tokenization context |
| 1092 */ |
1154 */ |
| 1093 #define cx_strtok(str, delim, limit) \ |
1155 #define cx_strtok(str, delim, limit) \ |
| 1094 cx_strtok_(cx_strcast((str)), cx_strcast((delim)), (limit)) |
1156 cx_strtok_(cx_strcast(str), cx_strcast(delim), (limit)) |
| 1095 |
1157 |
| 1096 /** |
1158 /** |
| 1097 * Returns the next token. |
1159 * Returns the next token. |
| 1098 * |
1160 * |
| 1099 * The token will point to the source string. |
1161 * The token will point to the source string. |