src/ucx/cx/string.h

changeset 582
82b60a8dd55c
parent 579
e10457d74fe1
child 613
b649d66c433e
--- a/src/ucx/cx/string.h	Mon May 26 21:02:30 2025 +0200
+++ b/src/ucx/cx/string.h	Mon May 26 21:06:17 2025 +0200
@@ -39,6 +39,12 @@
 #include "common.h"
 #include "allocator.h"
 
+/** Expands a UCX string as printf arguments. */
+#define CX_SFMT(s) (int) (s).length, (s).ptr
+
+/** Format specifier for a UCX string */
+#define CX_PRIstr ".*s"
+
 /**
  * The maximum length of the "needle" in cx_strstr() that can use SBO.
  */
@@ -299,7 +305,7 @@
 #endif
 
 /**
- * Passes the pointer in this string to @c free().
+ * Passes the pointer in this string to the cxDefaultAllocator's @c free() function.
  *
  * The pointer in the struct is set to @c NULL and the length is set to zero
  * which means that this function protects you against double-free.
@@ -334,6 +340,46 @@
 );
 
 /**
+ * Copies a string.
+ *
+ * The memory in the @p dest structure is either allocated or re-allocated to fit the entire
+ * source string, including a zero-terminator.
+ *
+ * The string in @p dest is guaranteed to be zero-terminated, regardless of whether @p src is.
+ *
+ * @param alloc the allocator
+ * @param dest a pointer to the structure where to copy the contents to
+ * @param src the source string
+ *
+ * @retval zero success
+ * @retval non-zero if re-allocation failed
+ */
+cx_attr_nonnull_arg(1)
+cx_attr_export
+int cx_strcpy_a(
+        const CxAllocator *alloc,
+        cxmutstr *dest,
+        cxstring src
+);
+
+
+/**
+ * Copies a string.
+ *
+ * The memory in the @p dest structure is either allocated or re-allocated to fit the entire
+ * source string, including a zero-terminator.
+ *
+ * The string in @p dest is guaranteed to be zero-terminated, regardless of whether @p src is.
+ *
+ * @param dest (@c cxmutstr*) a pointer to the structure where to copy the contents to
+ * @param src (@c cxstring) the source string
+ *
+ * @retval zero success
+ * @retval non-zero if re-allocation failed
+ */
+#define cx_strcpy(dest, src) cx_strcpy_a(cxDefaultAllocator, dest, src)
+
+/**
  * Returns the accumulated length of all specified strings.
  * 
  * If this sum overflows, errno is set to EOVERFLOW.
@@ -408,7 +454,7 @@
 /**
  * Concatenates strings and returns a new string.
  *
- * The resulting string will be allocated by standard @c malloc().
+ * The resulting string will be allocated by the cxDefaultAllocator.
  * So developers @em must pass the return value to cx_strfree() eventually.
  *
 * If memory allocation fails, the pointer in the returned string will
@@ -428,7 +474,7 @@
 /**
  * Concatenates strings.
  *
- * The resulting string will be allocated by standard @c malloc().
+ * The resulting string will be allocated by the cxDefaultAllocator.
  * So developers @em must pass the return value to cx_strfree() eventually.
  *
  * If @p str already contains a string, the memory will be reallocated and
@@ -884,8 +930,8 @@
 /**
  * Creates a duplicate of the specified string.
  *
- * The new string will contain a copy allocated by standard
- * @c malloc(). So developers @em must pass the return value to cx_strfree().
+ * The new string will contain a copy allocated by the cxDefaultAllocator.
+ * So developers @em must pass the return value to cx_strfree().
  *
  * @note The returned string is guaranteed to be zero-terminated.
  *
@@ -1016,7 +1062,7 @@
  *
  * Replaces at most @p replmax occurrences.
  *
- * The returned string will be allocated by @c malloc() and is guaranteed
+ * The returned string will be allocated by the cxDefaultAllocator and is guaranteed
  * to be zero-terminated.
  *
  * If allocation fails, or the input string is empty,
@@ -1052,7 +1098,7 @@
 /**
  * Replaces a string with another string.
  *
- * The returned string will be allocated by @c malloc() and is guaranteed
+ * The returned string will be allocated by the cxDefaultAllocator and is guaranteed
  * to be zero-terminated.
  *
  * If allocation fails, or the input string is empty,

mercurial