src/ucx/cx/string.h

changeset 582
82b60a8dd55c
parent 579
e10457d74fe1
child 613
b649d66c433e
equal deleted inserted replaced
581:4a049e416021 582:82b60a8dd55c
36 #ifndef UCX_STRING_H 36 #ifndef UCX_STRING_H
37 #define UCX_STRING_H 37 #define UCX_STRING_H
38 38
39 #include "common.h" 39 #include "common.h"
40 #include "allocator.h" 40 #include "allocator.h"
41
42 /** Expands a UCX string as printf arguments. */
43 #define CX_SFMT(s) (int) (s).length, (s).ptr
44
45 /** Format specifier for a UCX string */
46 #define CX_PRIstr ".*s"
41 47
42 /** 48 /**
43 * The maximum length of the "needle" in cx_strstr() that can use SBO. 49 * The maximum length of the "needle" in cx_strstr() that can use SBO.
44 */ 50 */
45 cx_attr_export 51 cx_attr_export
297 cxstring: cx_strcast_c) \ 303 cxstring: cx_strcast_c) \
298 (str) 304 (str)
299 #endif 305 #endif
300 306
301 /** 307 /**
302 * Passes the pointer in this string to @c free(). 308 * Passes the pointer in this string to the cxDefaultAllocator's @c free() function.
303 * 309 *
304 * The pointer in the struct is set to @c NULL and the length is set to zero 310 * 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. 311 * which means that this function protects you against double-free.
306 * 312 *
307 * @note There is no implementation for cxstring, because it is unlikely that 313 * @note There is no implementation for cxstring, because it is unlikely that
330 cx_attr_export 336 cx_attr_export
331 void cx_strfree_a( 337 void cx_strfree_a(
332 const CxAllocator *alloc, 338 const CxAllocator *alloc,
333 cxmutstr *str 339 cxmutstr *str
334 ); 340 );
341
342 /**
343 * Copies a string.
344 *
345 * The memory in the @p dest structure is either allocated or re-allocated to fit the entire
346 * source string, including a zero-terminator.
347 *
348 * The string in @p dest is guaranteed to be zero-terminated, regardless of whether @p src is.
349 *
350 * @param alloc the allocator
351 * @param dest a pointer to the structure where to copy the contents to
352 * @param src the source string
353 *
354 * @retval zero success
355 * @retval non-zero if re-allocation failed
356 */
357 cx_attr_nonnull_arg(1)
358 cx_attr_export
359 int cx_strcpy_a(
360 const CxAllocator *alloc,
361 cxmutstr *dest,
362 cxstring src
363 );
364
365
366 /**
367 * Copies a string.
368 *
369 * The memory in the @p dest structure is either allocated or re-allocated to fit the entire
370 * source string, including a zero-terminator.
371 *
372 * The string in @p dest is guaranteed to be zero-terminated, regardless of whether @p src is.
373 *
374 * @param dest (@c cxmutstr*) a pointer to the structure where to copy the contents to
375 * @param src (@c cxstring) the source string
376 *
377 * @retval zero success
378 * @retval non-zero if re-allocation failed
379 */
380 #define cx_strcpy(dest, src) cx_strcpy_a(cxDefaultAllocator, dest, src)
335 381
336 /** 382 /**
337 * Returns the accumulated length of all specified strings. 383 * Returns the accumulated length of all specified strings.
338 * 384 *
339 * If this sum overflows, errno is set to EOVERFLOW. 385 * If this sum overflows, errno is set to EOVERFLOW.
406 cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__) 452 cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
407 453
408 /** 454 /**
409 * Concatenates strings and returns a new string. 455 * Concatenates strings and returns a new string.
410 * 456 *
411 * The resulting string will be allocated by standard @c malloc(). 457 * The resulting string will be allocated by the cxDefaultAllocator.
412 * So developers @em must pass the return value to cx_strfree() eventually. 458 * So developers @em must pass the return value to cx_strfree() eventually.
413 * 459 *
414 * If memory allocation fails, the pointer in the returned string will 460 * If memory allocation fails, the pointer in the returned string will
415 * be @c NULL and @c errno might be set. 461 * be @c NULL and @c errno might be set.
416 * 462 *
426 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__) 472 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
427 473
428 /** 474 /**
429 * Concatenates strings. 475 * Concatenates strings.
430 * 476 *
431 * The resulting string will be allocated by standard @c malloc(). 477 * The resulting string will be allocated by the cxDefaultAllocator.
432 * So developers @em must pass the return value to cx_strfree() eventually. 478 * So developers @em must pass the return value to cx_strfree() eventually.
433 * 479 *
434 * If @p str already contains a string, the memory will be reallocated and 480 * If @p str already contains a string, the memory will be reallocated and
435 * the other strings are appended. Otherwise, new memory is allocated. 481 * the other strings are appended. Otherwise, new memory is allocated.
436 * 482 *
882 cx_strdup_a_((allocator), cx_strcast(string)) 928 cx_strdup_a_((allocator), cx_strcast(string))
883 929
884 /** 930 /**
885 * Creates a duplicate of the specified string. 931 * Creates a duplicate of the specified string.
886 * 932 *
887 * The new string will contain a copy allocated by standard 933 * 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(). 934 * So developers @em must pass the return value to cx_strfree().
889 * 935 *
890 * @note The returned string is guaranteed to be zero-terminated. 936 * @note The returned string is guaranteed to be zero-terminated.
891 * 937 *
892 * @param string the string to duplicate 938 * @param string the string to duplicate
893 * @return (@c cxmutstr) a duplicate of the string 939 * @return (@c cxmutstr) a duplicate of the string
1014 /** 1060 /**
1015 * Replaces a string with another string. 1061 * Replaces a string with another string.
1016 * 1062 *
1017 * Replaces at most @p replmax occurrences. 1063 * Replaces at most @p replmax occurrences.
1018 * 1064 *
1019 * The returned string will be allocated by @c malloc() and is guaranteed 1065 * The returned string will be allocated by the cxDefaultAllocator and is guaranteed
1020 * to be zero-terminated. 1066 * to be zero-terminated.
1021 * 1067 *
1022 * If allocation fails, or the input string is empty, 1068 * If allocation fails, or the input string is empty,
1023 * the returned string will be empty. 1069 * the returned string will be empty.
1024 * 1070 *
1050 cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX) 1096 cx_strreplacen_a(allocator, str, search, replacement, SIZE_MAX)
1051 1097
1052 /** 1098 /**
1053 * Replaces a string with another string. 1099 * Replaces a string with another string.
1054 * 1100 *
1055 * The returned string will be allocated by @c malloc() and is guaranteed 1101 * The returned string will be allocated by the cxDefaultAllocator and is guaranteed
1056 * to be zero-terminated. 1102 * to be zero-terminated.
1057 * 1103 *
1058 * If allocation fails, or the input string is empty, 1104 * If allocation fails, or the input string is empty,
1059 * the returned string will be empty. 1105 * the returned string will be empty.
1060 * 1106 *

mercurial