src/cx/string.h

changeset 1676
f889ffd07c86
parent 1675
36c0fb2b60b2
child 1677
1d73c7302fbc
--- a/src/cx/string.h	Sun Dec 28 17:31:20 2025 +0100
+++ b/src/cx/string.h	Sun Dec 28 18:30:25 2025 +0100
@@ -674,19 +674,59 @@
 #define cx_strat(str, index) cx_strat_(cx_strcast(str), index)
 
 /**
- * Returns a substring starting at the location of the first occurrence of the
- * specified character.
- *
- * If the string does not contain the character, an empty string is returned.
- *
- * @param string the string where to locate the character
- * @param chr    the character to locate
- * @return       a substring starting at the first location of @p chr
- *
- * @see cx_strchr_m()
+ * Searches for a character in a string.
+ * Internal function - do not use.
+ * @param string
+ * @param chr
+ * @return
+ * @see cx_strchr()
+ */
+CX_EXTERN CX_NODISCARD
+cxstring cx_strchr_(cxstring string, int chr);
+
+/**
+ * Searches for a character in a string.
+ * Internal function - do not use.
+ * @param string
+ * @param chr
+ * @return
+ * @see cx_strrchr()
  */
 CX_EXTERN CX_NODISCARD
-cxstring cx_strchr(cxstring string, int chr);
+cxstring cx_strrchr_(cxstring string, int chr);
+
+#ifdef __cplusplus
+CX_CPPDECL cxstring cx_strchr(cxstring string, int chr) {
+    return cx_strchr_(string, chr);
+}
+CX_CPPDECL cxmutstr cx_strchr(cxmutstr string, int chr) {
+    return cx_mutstrcast(cx_strchr_(cx_strcast(string), chr));
+}
+CX_CPPDECL cxstring cx_strrchr(cxstring string, int chr) {
+    return cx_strrchr_(string, chr);
+}
+CX_CPPDECL cxmutstr cx_strrchr(cxmutstr string, int chr) {
+    return cx_mutstrcast(cx_strrchr_(cx_strcast(string), chr));
+}
+#else
+/**
+ * Internal conversion function - do not use.
+ * @param string
+ * @param chr
+ * @return
+ */
+CX_INLINE cxmutstr cx_strchr_m_(cxmutstr string, int chr) {
+    return cx_mutstrcast(cx_strchr_(cx_strcast(string), chr));
+}
+/**
+ * Internal conversion function - do not use.
+ * @param string
+ * @param chr
+ * @return
+ */
+CX_INLINE  cxmutstr cx_strrchr_m_(cxmutstr string, int chr) {
+    return cx_mutstrcast(cx_strrchr_(cx_strcast(string), chr));
+}
 
 /**
  * Returns a substring starting at the location of the first occurrence of the
@@ -697,11 +737,10 @@
  * @param string the string where to locate the character
  * @param chr    the character to locate
  * @return       a substring starting at the first location of @p chr
- *
- * @see cx_strchr()
  */
-CX_EXTERN CX_NODISCARD
-cxmutstr cx_strchr_m(cxmutstr string, int chr);
+#define cx_strchr(string, chr) _Generic(cx_strcast_m(string), \
+        cxstring: cx_strchr_, \
+        cxmutstr: cx_strchr_m_)(cx_strcast_m(string), chr)
 
 /**
  * Returns a substring starting at the location of the last occurrence of the
@@ -712,26 +751,11 @@
  * @param string the string where to locate the character
  * @param chr    the character to locate
  * @return       a substring starting at the last location of @p chr
- *
- * @see cx_strrchr_m()
  */
-CX_EXTERN CX_NODISCARD
-cxstring cx_strrchr(cxstring string, int chr);
-
-/**
- * Returns a substring starting at the location of the last occurrence of the
- * specified character.
- *
- * If the string does not contain the character, an empty string is returned.
- *
- * @param string the string where to locate the character
- * @param chr    the character to locate
- * @return       a substring starting at the last location of @p chr
- *
- * @see cx_strrchr()
- */
-CX_EXTERN CX_NODISCARD
-cxmutstr cx_strrchr_m(cxmutstr string, int chr);
+#define cx_strrchr(string, chr) _Generic(cx_strcast_m(string), \
+        cxstring: cx_strrchr_, \
+        cxmutstr: cx_strrchr_m_)(cx_strcast_m(string), chr)
+#endif
 
 /**
  * Searches for a specific substring.

mercurial