allow NULL for creating UCX strings - resolves #683 default tip

Sun, 01 Jun 2025 16:00:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Jun 2025 16:00:16 +0200
changeset 1334
7763892ed801
parent 1333
2fc1c25441f1

allow NULL for creating UCX strings - resolves #683

CHANGELOG file | annotate | diff | comparison | revisions
docs/Writerside/topics/about.md file | annotate | diff | comparison | revisions
docs/Writerside/topics/string.h.md file | annotate | diff | comparison | revisions
src/cx/string.h file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
--- 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
--- 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
--- 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`
--- 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
--- 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(

mercurial