src/cx/string.h

changeset 1680
1aa21afb8763
parent 1679
4a08dabe5e8f
equal deleted inserted replaced
1679:4a08dabe5e8f 1680:1aa21afb8763
880 CX_EXTERN CX_NODISCARD CX_NONNULL CX_ACCESS_W(5) 880 CX_EXTERN CX_NODISCARD CX_NONNULL CX_ACCESS_W(5)
881 size_t cx_strsplit_ma_(const CxAllocator *allocator, 881 size_t cx_strsplit_ma_(const CxAllocator *allocator,
882 cxmutstr string, cxstring delim, size_t limit, 882 cxmutstr string, cxstring delim, size_t limit,
883 cxmutstr **output); 883 cxmutstr **output);
884 884
885 #ifdef __cplusplus
886 CX_CPPDECL size_t cx_strsplit_cpp_(cxstring string, cxstring delim,
887 size_t limit, cxstring *output) {
888 return cx_strsplit_(string, delim, limit, output);
889 }
890 CX_CPPDECL size_t cx_strsplit_cpp_(cxmutstr string, cxstring delim,
891 size_t limit, cxmutstr *output) {
892 return cx_strsplit_m_(string, delim, limit, output);
893 }
894 CX_CPPDECL size_t cx_strsplit_a_cpp_(const CxAllocator *allocator,
895 cxstring string, cxstring delim, size_t limit, cxstring **output) {
896 return cx_strsplit_a_(allocator, string, delim, limit, output);
897 }
898 CX_CPPDECL size_t cx_strsplit_a_cpp_(const CxAllocator *allocator,
899 cxmutstr string, cxstring delim, size_t limit, cxmutstr **output) {
900 return cx_strsplit_ma_(allocator, string, delim, limit, output);
901 }
902 #define cx_strsplit(string, delim, limit, output) \
903 cx_strsplit_cpp_(cx_strcast_m(string), cx_strcast(delim), limit, output)
904 #define cx_strsplit_a(allocator, string, delim, limit, output) \
905 cx_strsplit_a_cpp_(allocator, cx_strcast_m(string), cx_strcast(delim), limit, output)
906 #else
885 /** 907 /**
886 * Splits a given string using a delimiter string. 908 * Splits a given string using a delimiter string.
887 * 909 *
888 * @note The resulting array contains strings that point to the source 910 * @note The resulting array contains strings that point to the source
889 * @p string. Use cx_strdup() to get copies. 911 * @p string. Use cx_strdup() to get copies.
890 * 912 *
891 * @param string (@c cxstring) the string to split 913 * @param string the string to split
892 * @param delim the delimiter 914 * @param delim the delimiter
893 * @param limit (@c size_t) the maximum number of split items 915 * @param limit (@c size_t) the maximum number of split items
894 * @param output (@c cxstring*) a preallocated array of at least @p limit length 916 * @param output (@c cxstring* or @c cxmutstr*) a preallocated array of at
917 * least @p limit length
895 * @return the actual number of split items 918 * @return the actual number of split items
896 */ 919 */
897 #define cx_strsplit(string, delim, limit, output) \ 920 #define cx_strsplit(string, delim, limit, output) \
898 cx_strsplit_(string, cx_strcast(delim), limit, output) 921 _Generic(cx_strcast_m(string), \
922 cxstring: cx_strsplit_, \
923 cxmutstr: cx_strsplit_m_)\
924 (cx_strcast_m(string), cx_strcast(delim), limit, output)
899 925
900 /** 926 /**
901 * Splits a given string using a delimiter string. 927 * Splits a given string using a delimiter string.
902 * 928 *
903 * The array pointed to by @p output will be allocated by @p allocator. 929 * The array pointed to by @p output will be allocated by @p allocator.
907 * 933 *
908 * @attention If allocation fails, the @c NULL pointer will be written to 934 * @attention If allocation fails, the @c NULL pointer will be written to
909 * @p output and the number returned will be zero. 935 * @p output and the number returned will be zero.
910 * 936 *
911 * @param allocator (@c CxAllocator*) the allocator to use for allocating the resulting array 937 * @param allocator (@c CxAllocator*) the allocator to use for allocating the resulting array
912 * @param string (@c cxstring) the string to split 938 * @param string the string to split
913 * @param delim the delimiter 939 * @param delim the delimiter
914 * @param limit (@c size_t) the maximum number of split items 940 * @param limit (@c size_t) the maximum number of split items
915 * @param output (@c cxstring**) a pointer where the address of the allocated 941 * @param output (@c cxstring** or @c cxmutstr**) a pointer where the address
916 * array shall be written to 942 * of the allocated array shall be written to
917 * @return the actual number of split items 943 * @return the actual number of split items
918 */ 944 */
919 #define cx_strsplit_a(allocator, string, delim, limit, output) \ 945 #define cx_strsplit_a(allocator, string, delim, limit, output) \
920 cx_strsplit_a_(allocator, string, cx_strcast(delim), limit, output) 946 _Generic(cx_strcast_m(string), \
921 947 cxstring: cx_strsplit_a_, \
922 948 cxmutstr: cx_strsplit_ma_)\
923 /** 949 (allocator, cx_strcast_m(string), cx_strcast(delim), limit, output)
924 * Splits a given string using a delimiter string. 950 #endif
925 *
926 * @note The resulting array contains strings that point to the source
927 * @p string. Use cx_strdup() to get copies.
928 *
929 * @param string (@c cxmutstr) the string to split
930 * @param delim the delimiter
931 * @param limit (@c size_t) the maximum number of split items
932 * @param output (@c cxmutstr*) a preallocated array of at least @p limit length
933 * @return the actual number of split items
934 */
935 #define cx_strsplit_m(string, delim, limit, output) \
936 cx_strsplit_m_(string, cx_strcast(delim), limit, output)
937
938 /**
939 * Splits a given string using a delimiter string.
940 *
941 * The array pointed to by @p output will be allocated by @p allocator.
942 *
943 * @note The resulting array contains strings that point to the source
944 * @p string. Use cx_strdup() to get copies.
945 *
946 * @attention If allocation fails, the @c NULL pointer will be written to
947 * @p output and the number returned will be zero.
948 *
949 * @param allocator (@c CxAllocator*) the allocator to use for allocating the resulting array
950 * @param string (@c cxmutstr) the string to split
951 * @param delim the delimiter
952 * @param limit (@c size_t) the maximum number of split items
953 * @param output (@c cxmutstr**) a pointer where the address of the allocated
954 * array shall be written to
955 * @return the actual number of split items
956 */
957 #define cx_strsplit_ma(allocator, string, delim, limit, output) \
958 cx_strsplit_ma_(allocator, string, cx_strcast(delim), limit, output)
959 951
960 /** 952 /**
961 * Compares two strings. 953 * Compares two strings.
962 * 954 *
963 * @param s1 the first string 955 * @param s1 the first string

mercurial