*/
typedef struct cx_strtok_ctx_s CxStrtokCtx;
+#ifdef __cplusplus
+extern "C" {
+
+/**
+ * A literal initializer for an UCX string structure.
+ *
+ * @param literal the string literal
+ */
+#define CX_STR(literal) cxstring{literal, sizeof(literal) - 1}
+
+#else // __cplusplus
+
/**
* A literal initializer for an UCX string structure.
*
*
* @param literal the string literal
*/
-#define CX_STR(literal) {literal, sizeof(literal) - 1}
+#define CX_STR(literal) (cxstring){literal, sizeof(literal) - 1}
-#ifdef __cplusplus
-extern "C" {
#endif
*/
__attribute__((__nonnull__))
void cx_strfree_a(
- CxAllocator *alloc,
+ CxAllocator const *alloc,
cxmutstr *str
);
);
/**
- * Concatenates two or more strings.
+ * Concatenates strings.
*
* The resulting string will be allocated by the specified allocator.
- * So developers \em must pass the return value to cx_strfree() eventually.
- *
- * \note It is guaranteed that there is only one allocation.
- * It is also guaranteed that the returned string is zero-terminated.
+ * So developers \em must pass the return value to cx_strfree_a() eventually.
+ *
+ * If \p str already contains a string, the memory will be reallocated and
+ * the other strings are appended. Otherwise, new memory is allocated.
+ *
+ * \note It is guaranteed that there is only one allocation.
+ * It is also guaranteed that the returned string is zero-terminated.
*
* @param alloc the allocator to use
- * @param count the total number of strings to concatenate
- * @param ... all strings
+ * @param str the string the other strings shall be concatenated to
+ * @param count the number of the other following strings to concatenate
+ * @param ... all other strings
* @return the concatenated string
*/
__attribute__((__warn_unused_result__, __nonnull__))
-cxmutstr cx_strcat_a(
- CxAllocator *alloc,
+cxmutstr cx_strcat_ma(
+ CxAllocator const *alloc,
+ cxmutstr str,
size_t count,
...
);
/**
- * Concatenates two or more strings.
+ * Concatenates strings and returns a new string.
+ *
+ * The resulting string will be allocated by the specified allocator.
+ * So developers \em must pass the return value to cx_strfree_a() eventually.
+ *
+ * \note It is guaranteed that there is only one allocation.
+ * It is also guaranteed that the returned string is zero-terminated.
+ *
+ * @param alloc the allocator to use
+ * @param count the number of the other following strings to concatenate
+ * @param ... all other strings
+ * @return the concatenated string
+ */
+#define cx_strcat_a(alloc, count, ...) \
+cx_strcat_ma(alloc, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
+
+/**
+ * Concatenates strings and returns a new string.
*
* The resulting string will be allocated by standard \c malloc().
* So developers \em must pass the return value to cx_strfree() eventually.
* \note It is guaranteed that there is only one allocation.
* It is also guaranteed that the returned string is zero-terminated.
*
- * @param count the total number of strings to concatenate
- * @param ... all strings
+ * @param count the number of the other following strings to concatenate
+ * @param ... all other strings
* @return the concatenated string
*/
#define cx_strcat(count, ...) \
-cx_strcat_a(cxDefaultAllocator, count, __VA_ARGS__)
+cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(NULL, 0), count, __VA_ARGS__)
+
+/**
+ * Concatenates strings.
+ *
+ * The resulting string will be allocated by standard \c malloc().
+ * 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
+ * the other strings are appended. Otherwise, new memory is allocated.
+ *
+ * \note It is guaranteed that there is only one allocation.
+ * It is also guaranteed that the returned string is zero-terminated.
+ *
+ * @param str the string the other strings shall be concatenated to
+ * @param count the number of the other following strings to concatenate
+ * @param ... all other strings
+ * @return the concatenated string
+ */
+#define cx_strcat_m(str, count, ...) \
+cx_strcat_ma(cxDefaultAllocator, str, count, __VA_ARGS__)
/**
* Returns a substring starting at the specified location.
*/
__attribute__((__warn_unused_result__, __nonnull__))
size_t cx_strsplit_a(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxstring string,
cxstring delim,
size_t limit,
*/
__attribute__((__warn_unused_result__, __nonnull__))
size_t cx_strsplit_ma(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxmutstr string,
cxstring delim,
size_t limit,
/**
* Compares two strings.
*
- * This function has a compatible signature for the use as a CxListComparator.
+ * This function has a compatible signature for the use as a cx_compare_func.
*
* @param s1 the first string
* @param s2 the second string
/**
* Compares two strings ignoring case.
*
- * This function has a compatible signature for the use as a CxListComparator.
+ * This function has a compatible signature for the use as a cx_compare_func.
*
* @param s1 the first string
* @param s2 the second string
*/
__attribute__((__warn_unused_result__, __nonnull__))
cxmutstr cx_strdup_a(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxstring string
);
*/
#define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
+
+/**
+ * Creates a duplicate of the specified string.
+ *
+ * The new string will contain a copy allocated by \p allocator.
+ *
+ * \note The returned string is guaranteed to be zero-terminated.
+ *
+ * @param allocator the allocator to use
+ * @param string the string to duplicate
+ * @return a duplicate of the string
+ * @see cx_strdup_m()
+ */
+#define cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string))
+
+/**
+ * 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().
+ *
+ * \note The returned string is guaranteed to be zero-terminated.
+ *
+ * @param string the string to duplicate
+ * @return a duplicate of the string
+ * @see cx_strdup_ma()
+ */
+#define cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string))
+
/**
* Omits leading and trailing spaces.
*
*/
__attribute__((__warn_unused_result__, __nonnull__))
cxmutstr cx_strreplacen_a(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxstring str,
cxstring pattern,
cxstring replacement,