Sun, 28 Dec 2025 18:43:21 +0100
add full generic support for cx_strstr()
relates to #792
| src/cx/string.h | file | annotate | diff | comparison | revisions | |
| src/string.c | file | annotate | diff | comparison | revisions | |
| tests/test_string.c | file | annotate | diff | comparison | revisions |
--- a/src/cx/string.h Sun Dec 28 18:30:25 2025 +0100 +++ b/src/cx/string.h Sun Dec 28 18:43:21 2025 +0100 @@ -696,18 +696,20 @@ cxstring cx_strrchr_(cxstring string, int chr); #ifdef __cplusplus -CX_CPPDECL cxstring cx_strchr(cxstring string, int chr) { +CX_CPPDECL cxstring cx_strchr_cpp(cxstring string, int chr) { return cx_strchr_(string, chr); } -CX_CPPDECL cxmutstr cx_strchr(cxmutstr string, int chr) { +CX_CPPDECL cxmutstr cx_strchr_cpp(cxmutstr string, int chr) { return cx_mutstrcast(cx_strchr_(cx_strcast(string), chr)); } -CX_CPPDECL cxstring cx_strrchr(cxstring string, int chr) { +#define cx_strchr(s, chr) cx_strchr_cpp(cx_strcast_m(s), chr) +CX_CPPDECL cxstring cx_strrchr_cpp(cxstring string, int chr) { return cx_strrchr_(string, chr); } -CX_CPPDECL cxmutstr cx_strrchr(cxmutstr string, int chr) { +CX_CPPDECL cxmutstr cx_strrchr_cpp(cxmutstr string, int chr) { return cx_mutstrcast(cx_strrchr_(cx_strcast(string), chr)); } +#define cx_strrchr(s, chr) cx_strrchr_cpp(cx_strcast_m(s), chr) #else /** * Internal conversion function - do not use. @@ -735,8 +737,9 @@ * 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 + * @param chr (@c int) the character to locate + * @return (@c cxstring or @c cxmutstr) a substring starting at the first + * location of @p chr */ #define cx_strchr(string, chr) _Generic(cx_strcast_m(string), \ cxstring: cx_strchr_, \ @@ -749,8 +752,9 @@ * 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 + * @param chr (@c int) the character to locate + * @return (@c cxstring or @c cxmutstr) a substring starting at the last + * location of @p chr */ #define cx_strrchr(string, chr) _Generic(cx_strcast_m(string), \ cxstring: cx_strrchr_, \ @@ -771,36 +775,24 @@ CX_EXTERN CX_NODISCARD cxstring cx_strstr_(cxstring haystack, cxstring needle); +#ifdef __cplusplus +CX_CPPDECL cxstring cx_strstr_cpp(cxstring haystack, cxstring needle) { + return cx_strstr_(haystack, needle); +} +CX_CPPDECL cxmutstr cx_strstr_cpp(cxmutstr haystack, cxstring needle) { + return cx_mutstrcast(cx_strstr_(cx_strcast(haystack), needle)); +} +#define cx_strstr(h,n) cx_strstr_cpp(cx_strcast_m(h), cx_strcast(n)) +#else /** - * Returns a substring starting at the location of the first occurrence of the - * specified string. - * - * If @p haystack does not contain @p needle, an empty string is returned. - * - * If @p needle is an empty string, the complete @p haystack is - * returned. - * - * @param haystack (@c cxstring) the string to be scanned - * @param needle string containing the sequence of characters to match - * @return (@c cxstring) a substring starting at the first occurrence of - * @p needle, or an empty string, if the sequence is not contained - * @see cx_strstr_m() + * Internal conversion - do not use. + * @param haystack + * @param needle + * @return */ -#define cx_strstr(haystack, needle) cx_strstr_(haystack, cx_strcast(needle)) - -/** - * Searches for a specific substring. - * - * Internal function - do not use. - * - * @param haystack the string to be scanned - * @param needle string containing the sequence of characters to match - * @return a substring starting at the first occurrence of @p needle, - * or an empty string, if the sequence is not contained - * @see cx_strstr_m() - */ -CX_EXTERN CX_NODISCARD -cxmutstr cx_strstr_m_(cxmutstr haystack, cxstring needle); +CX_INLINE cxmutstr cx_strstr_m_(cxmutstr haystack, cxstring needle) { + return cx_mutstrcast(cx_strstr_(cx_strcast(haystack), needle)); +} /** * Returns a substring starting at the location of the first occurrence of the @@ -811,13 +803,15 @@ * If @p needle is an empty string, the complete @p haystack is * returned. * - * @param haystack (@c cxmutstr) the string to be scanned + * @param haystack the string to be scanned * @param needle string containing the sequence of characters to match - * @return (@c cxmutstr) a substring starting at the first occurrence of - * @p needle, or an empty string, if the sequence is not contained - * @see cx_strstr() + * @return (@c cxstring or @c cxmutstr) a substring starting at the first + * occurrence of @p needle, or an empty string, if the sequence is not contained */ -#define cx_strstr_m(haystack, needle) cx_strstr_m_(haystack, cx_strcast(needle)) +#define cx_strstr(haystack, needle) _Generic(cx_strcast_m(haystack), \ + cxstring: cx_strstr_,\ + cxmutstr: cx_strstr_m_)(cx_strcast_m(haystack), cx_strcast(needle)) +#endif /** * Splits a given string using a delimiter string.
--- a/src/string.c Sun Dec 28 18:30:25 2025 +0100 +++ b/src/string.c Sun Dec 28 18:43:21 2025 +0100 @@ -283,14 +283,6 @@ return result; } -cxmutstr cx_strstr_m_( - cxmutstr haystack, - cxstring needle -) { - cxstring result = cx_strstr(cx_strcast(haystack), needle); - return (cxmutstr) {(char *) result.ptr, result.length}; -} - size_t cx_strsplit_( cxstring string, cxstring delim,
--- a/tests/test_string.c Sun Dec 28 18:30:25 2025 +0100 +++ b/tests/test_string.c Sun Dec 28 18:43:21 2025 +0100 @@ -478,7 +478,7 @@ CX_TEST_ASSERT(result.length == str.length); CX_TEST_ASSERT(0 == strcmp(result.ptr, str.ptr)); - cxmutstr resultm = cx_strstr_m(longstr, longstrpattern); + cxmutstr resultm = cx_strstr(longstr, longstrpattern); CX_TEST_ASSERT(resultm.length == longstrresult.length); CX_TEST_ASSERT(0 == strcmp(resultm.ptr, longstrresult.ptr)); }