src/cx/string.h

changeset 1673
0c338b80e7dd
parent 1672
94360453bce4
child 1674
8b0f162ac88e
--- a/src/cx/string.h	Sun Dec 28 14:10:14 2025 +0100
+++ b/src/cx/string.h	Sun Dec 28 14:47:36 2025 +0100
@@ -243,41 +243,60 @@
 
 #ifdef __cplusplus
 cx_attr_nodiscard
-CX_CPPDECL cxstring cx_strcast(cxmutstr str) {
-    return cx_strn(str.ptr, str.length);
+CX_CPPDECL cxmutstr cx_strcast_m(cxmutstr str) {
+    return str;
 }
 cx_attr_nodiscard
-CX_CPPDECL cxstring cx_strcast(cxstring str) {
+CX_CPPDECL cxstring cx_strcast_m(cxstring str) {
     return str;
 }
 cx_attr_nodiscard
-CX_CPPDECL cxstring cx_strcast(const char *str) {
+CX_CPPDECL cxmutstr cx_strcast_m(char *str) {
+    return cx_mutstr(str);
+}
+cx_attr_nodiscard
+CX_CPPDECL cxmutstr cx_strcast_m(unsigned char *str) {
+    return cx_mutstr(reinterpret_cast<char*>(str));
+}
+cx_attr_nodiscard
+CX_CPPDECL cxstring cx_strcast_m(const char *str) {
     return cx_str(str);
 }
 cx_attr_nodiscard
-CX_CPPDECL cxstring cx_strcast(const unsigned char *str) {
+CX_CPPDECL cxstring cx_strcast_m(const unsigned char *str) {
     return cx_str(reinterpret_cast<const char*>(str));
 }
+cx_attr_nodiscard
+CX_CPPDECL cxstring cx_strcast_(cxmutstr str) {
+    return cx_strn(str.ptr, str.length);
+}
+cx_attr_nodiscard
+CX_CPPDECL cxstring cx_strcast_(cxstring str) {
+    return str;
+}
+#define cx_strcast(s) cx_strcast_(cx_strcast_m(s))
 extern "C" {
 #else
 /**
  * Internal function, do not use.
  * @param str
  * @return
+ * @see cx_strcast_m()
  * @see cx_strcast()
  */
 cx_attr_nodiscard
-CX_INLINE cxstring cx_strcast_m(cxmutstr str) {
-    return (cxstring) {str.ptr, str.length};
+CX_INLINE cxmutstr cx_strcast_cxms(cxmutstr str) {
+    return str;
 }
 /**
  * Internal function, do not use.
  * @param str
  * @return
+ * @see cx_strcast_m()
  * @see cx_strcast()
  */
 cx_attr_nodiscard
-CX_INLINE cxstring cx_strcast_c(cxstring str) {
+CX_INLINE cxstring cx_strcast_cxs(cxstring str) {
     return str;
 }
 
@@ -285,10 +304,35 @@
  * Internal function, do not use.
  * @param str
  * @return
+ * @see cx_strcast_m()
  * @see cx_strcast()
  */
 cx_attr_nodiscard
-CX_INLINE cxstring cx_strcast_u(const unsigned char *str) {
+CX_INLINE cxmutstr cx_strcast_uc(unsigned char *str) {
+    return cx_mutstr((char*)str);
+}
+
+/**
+ * Internal function, do not use.
+ * @param str
+ * @return
+ * @see cx_strcast_m()
+ * @see cx_strcast()
+ */
+cx_attr_nodiscard
+CX_INLINE cxmutstr cx_strcast_c(char *str) {
+    return cx_mutstr(str);
+}
+
+/**
+ * Internal function, do not use.
+ * @param str
+ * @return
+ * @see cx_strcast_m()
+ * @see cx_strcast()
+ */
+cx_attr_nodiscard
+CX_INLINE cxstring cx_strcast_ucc(const unsigned char *str) {
     return cx_str((const char*)str);
 }
 
@@ -296,10 +340,11 @@
  * Internal function, do not use.
  * @param str
  * @return
+ * @see cx_strcast_m()
  * @see cx_strcast()
  */
 cx_attr_nodiscard
-CX_INLINE cxstring cx_strcast_z(const char *str) {
+CX_INLINE cxstring cx_strcast_cc(const char *str) {
     return cx_str(str);
 }
 
@@ -307,15 +352,46 @@
  * Wraps any string into an UCX string.
  *
  * @param str (any supported string type) the string to cast
- * @return (@c cxstring) the string wrapped as UCX string
+ * @return (@c cxstring) or (@c cxmutstr) the string wrapped as UCX string
+ */
+#define cx_strcast_m(str) _Generic((str), \
+        cxmutstr: cx_strcast_cxms, \
+        cxstring: cx_strcast_cxs, \
+        const unsigned char*: cx_strcast_ucc, \
+        unsigned char *: cx_strcast_uc, \
+        const char*: cx_strcast_cc, \
+        char *: cx_strcast_c) (str)
+
+/**
+ * Internal function, do not use.
+ * @param str
+ * @return
  */
-#define cx_strcast(str) _Generic((str), \
-        cxmutstr: cx_strcast_m, \
-        cxstring: cx_strcast_c, \
-        const unsigned char*: cx_strcast_u, \
-        unsigned char *: cx_strcast_u, \
-        const char*: cx_strcast_z, \
-        char *: cx_strcast_z) (str)
+CX_INLINE cxstring cx_strcast_1(cxmutstr str) {
+    return (cxstring){str.ptr, str.length};
+}
+
+/**
+ * Internal function, do not use.
+ * @param str
+ * @return
+ */
+CX_INLINE cxstring cx_strcast_2(cxstring str) {
+    return str;
+}
+
+/** internal conversion macro */
+#define cx_strcast_(str) _Generic((str), \
+        cxmutstr: cx_strcast_1, \
+        cxstring: cx_strcast_2)(str)
+
+/**
+ * Converts any string to a cxstring.
+ *
+ * @param str (any supported string type) the string to cast
+ * @return he string converted to a (@c cxstring)
+ */
+#define cx_strcast(str) cx_strcast_(cx_strcast_m(str))
 #endif
 
 /**

mercurial