src/cx/string.h

changeset 1050
3df63e95921a
parent 1046
fe2c842fce4b
--- a/src/cx/string.h	Sun Dec 22 22:53:53 2024 +0100
+++ b/src/cx/string.h	Sun Dec 22 23:10:07 2024 +0100
@@ -244,18 +244,56 @@
         size_t length
 );
 
+#ifdef __cplusplus
+} // extern "C"
+cx_attr_nodiscard
+static inline cxstring cx_strcast(cxmutstr str) {
+    return cx_strn(str.ptr, str.length);
+}
+cx_attr_nodiscard
+static inline cxstring cx_strcast(cxstring str) {
+    return str;
+}
+extern "C" {
+#else
+/**
+ * Internal function, do not use.
+ * @param str
+ * @return
+ * @see cx_strcast()
+ */
+cx_attr_nodiscard
+static inline cxstring cx_strcast_m(cxmutstr str) {
+    return (cxstring) {str.ptr, str.length};
+}
+/**
+ * Internal function, do not use.
+ * @param str
+ * @return
+ * @see cx_strcast()
+ */
+cx_attr_nodiscard
+static inline cxstring cx_strcast_c(cxstring str) {
+    return str;
+}
+
 /**
 * Casts a mutable string to an immutable string.
 *
-* \note This is not seriously a cast. Instead you get a copy
+* Does nothing for already immutable strings.
+*
+* \note This is not seriously a cast. Instead, you get a copy
 * of the struct with the desired pointer type. Both structs still
 * point to the same location, though!
 *
 * @param str the mutable string to cast
 * @return an immutable copy of the string pointer
 */
-cx_attr_nodiscard
-cxstring cx_strcast(cxmutstr str);
+#define cx_strcast(str) _Generic((str), \
+        cxmutstr: cx_strcast_m, \
+        cxstring: cx_strcast_c) \
+        (str)
+#endif
 
 /**
  * Passes the pointer in this string to \c free().

mercurial