# HG changeset patch # User Mike Becker # Date 1748786416 -7200 # Node ID 7763892ed8018e1fdf9f250a33f26a8e1032c675 # Parent 2fc1c25441f1b114c197afdc64ae6fdaa17893c0 allow NULL for creating UCX strings - resolves #683 diff -r 2fc1c25441f1 -r 7763892ed801 CHANGELOG --- a/CHANGELOG Tue May 27 22:31:06 2025 +0200 +++ b/CHANGELOG Sun Jun 01 16:00:16 2025 +0200 @@ -15,6 +15,7 @@ * adds cx_strcpy() and cx_strcpy_a() * adds cxStdlibAllocator and allows changes of cxDefaultAllocator * improves performance of the CxList array list implementation + * changes cx_str() and cx_mutstr() to allow NULL strings * changes cx_strcast() to also accept C-strings as input * changes grow strategy for the mempory pool to reduce reallocations * changes grow strategy for CxBuffer, which does now take the page size into account diff -r 2fc1c25441f1 -r 7763892ed801 docs/Writerside/topics/about.md --- a/docs/Writerside/topics/about.md Tue May 27 22:31:06 2025 +0200 +++ b/docs/Writerside/topics/about.md Sun Jun 01 16:00:16 2025 +0200 @@ -42,6 +42,7 @@ * adds cx_strcpy() and cx_strcpy_a() * adds cxStdlibAllocator and allows changes of cxDefaultAllocator * improves performance of the CxList array list implementation +* changes cx_str() and cx_mutstr() to allow NULL strings * changes cx_strcast() to also accept C-strings as input * changes grow strategy for the memory pool to reduce reallocations * changes grow strategy for CxBuffer, which does now take the page size into account diff -r 2fc1c25441f1 -r 7763892ed801 docs/Writerside/topics/string.h.md --- a/docs/Writerside/topics/string.h.md Tue May 27 22:31:06 2025 +0200 +++ b/docs/Writerside/topics/string.h.md Sun Jun 01 16:00:16 2025 +0200 @@ -53,7 +53,7 @@ ``` The functions `cx_str()` and `cx_mutstr()` create a UCX string from a `const char*` or a `char*` -and compute the length with a call to stdlib `strlen()`. +and compute the length with a call to stdlib `strlen()` (except for `NULL` in which case the length is set to zero). In case you already know the length, or the string is not zero-terminated, you can use `cx_strn()` or `cx_mutstrn()`. The function `cx_strdup_a()` allocates new memory with the given `allocator` and copies the given `string` diff -r 2fc1c25441f1 -r 7763892ed801 src/cx/string.h --- a/src/cx/string.h Tue May 27 22:31:06 2025 +0200 +++ b/src/cx/string.h Sun Jun 01 16:00:16 2025 +0200 @@ -167,6 +167,8 @@ * * The length is implicitly inferred by using a call to @c strlen(). * + * When @c NULL is passed, the length will be set to zero. + * * @note the wrapped string will share the specified pointer to the string. * If you do want a copy, use cx_strdup() on the return value of this function. * @@ -177,7 +179,6 @@ * * @see cx_mutstrn() */ -cx_attr_nonnull cx_attr_nodiscard cx_attr_cstr_arg(1) cx_attr_export @@ -212,6 +213,8 @@ * * The length is implicitly inferred by using a call to @c strlen(). * + * When @c NULL is passed, the length will be set to zero. + * * @note the wrapped string will share the specified pointer to the string. * If you do want a copy, use cx_strdup() on the return value of this function. * @@ -222,7 +225,6 @@ * * @see cx_strn() */ -cx_attr_nonnull cx_attr_nodiscard cx_attr_cstr_arg(1) cx_attr_export diff -r 2fc1c25441f1 -r 7763892ed801 src/string.c --- a/src/string.c Tue May 27 22:31:06 2025 +0200 +++ b/src/string.c Sun Jun 01 16:00:16 2025 +0200 @@ -42,7 +42,7 @@ #endif cxmutstr cx_mutstr(char *cstring) { - return (cxmutstr) {cstring, strlen(cstring)}; + return (cxmutstr) {cstring, cstring == NULL ? 0 : strlen(cstring)}; } cxmutstr cx_mutstrn( @@ -53,7 +53,7 @@ } cxstring cx_str(const char *cstring) { - return (cxstring) {cstring, strlen(cstring)}; + return (cxstring) {cstring, cstring == NULL ? 0 : strlen(cstring)}; } cxstring cx_strn(