add support for C-strings in cx_strcast() - resolves #549

Tue, 27 May 2025 22:23:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 27 May 2025 22:23:33 +0200
changeset 1332
02946dc73e6a
parent 1331
caa66055b025
child 1333
2fc1c25441f1

add support for C-strings in cx_strcast() - resolves #549

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
--- a/CHANGELOG	Tue May 27 22:23:06 2025 +0200
+++ b/CHANGELOG	Tue May 27 22:23:33 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_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
  * changes the implementation of cx_strreplacen() for improved efficiency
--- a/docs/Writerside/topics/about.md	Tue May 27 22:23:06 2025 +0200
+++ b/docs/Writerside/topics/about.md	Tue May 27 22:23:33 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_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
 * changes the implementation of cx_strreplacen() for improved efficiency
--- a/docs/Writerside/topics/string.h.md	Tue May 27 22:23:06 2025 +0200
+++ b/docs/Writerside/topics/string.h.md	Tue May 27 22:23:33 2025 +0200
@@ -9,7 +9,7 @@
 
 ## Basics
 
-> To make documentation simpler, we introduce the pseudo-type `AnyStr` with the meaning that
+> To simplify documentation, we introduce the pseudo-type `AnyStr` with the meaning that
 > both `cxstring` and `cxmutstr` are accepted for that argument.
 > The implementation is actually hidden behind a macro which uses `cx_strcast()` to guarantee compatibility.
 {style="note"}
@@ -33,8 +33,6 @@
 
 cxmutstr cx_mutstrn(char *cstring, size_t length);
 
-cxstring cx_strcast(AnyStr str);
-
 cxmutstr cx_strdup(AnyStr string);
 
 cxmutstr cx_strdup_a(const CxAllocator *allocator, AnyStr string);
@@ -51,6 +49,7 @@
 
 #define CX_SFMT(s)   (int) (s).length, (s).ptr
 #define CX_PRIstr    ".*s"
+#define cx_strcast(s)  // converts any string to cxstring
 ```
 
 The functions `cx_str()` and `cx_mutstr()` create a UCX string from a `const char*` or a `char*`
--- a/src/cx/string.h	Tue May 27 22:23:06 2025 +0200
+++ b/src/cx/string.h	Tue May 27 22:23:33 2025 +0200
@@ -263,6 +263,10 @@
 static inline cxstring cx_strcast(cxstring str) {
     return str;
 }
+cx_attr_nodiscard
+static inline cxstring cx_strcast(const char *str) {
+    return cx_str(str);
+}
 extern "C" {
 #else
 /**
@@ -287,6 +291,17 @@
 }
 
 /**
+ * Internal function, do not use.
+ * @param str
+ * @return
+ * @see cx_strcast()
+ */
+cx_attr_nodiscard
+static inline cxstring cx_strcast_z(const char *str) {
+    return cx_str(str);
+}
+
+/**
 * Casts a mutable string to an immutable string.
 *
 * Does nothing for already immutable strings.
@@ -300,8 +315,9 @@
 */
 #define cx_strcast(str) _Generic((str), \
         cxmutstr: cx_strcast_m, \
-        cxstring: cx_strcast_c) \
-        (str)
+        cxstring: cx_strcast_c, \
+        const char*: cx_strcast_z, \
+        char *: cx_strcast_z) (str)
 #endif
 
 /**

mercurial