# HG changeset patch # User Mike Becker # Date 1734905407 -3600 # Node ID 3df63e95921a4a27e6471572f7e55fb45caa4fd7 # Parent 415bf2ce6bab97e7430f5a3e6022f1d3d4af7d9d make cx_strcast() also support cxstring also makes the solution for issue #536 a lot nicer diff -r 415bf2ce6bab -r 3df63e95921a CHANGELOG --- a/CHANGELOG Sun Dec 22 22:53:53 2024 +0100 +++ b/CHANGELOG Sun Dec 22 23:10:07 2024 +0100 @@ -20,6 +20,7 @@ to accept NULL as allocator argument (in which case a default allocator will be used) * changes the name of destroy functions that actually free the memory to better indicate their behavior * change cx_strcat variants to allow handling of ENOMEM + * change cx_strcast() to also accept cxstring (and doing nothing in that case) * change the behavior of cxBufferSeek() to allow offset zero for SEEK_END * moves cx_compare_func typedef to compare.h * moves cx_szmul() to common.h @@ -30,7 +31,7 @@ * removes CMake * removes GTest dependency * removes flags to disable SBO in tests - * fixes cx_hash_key_cxstr() being a macro, evaluating the argument twice + * fixes cx_hash_key_cxstr() evaluating the argument twice * fixes wrong link from UCX 2 documentation to UCX 3 documentation * fixes critical bug that produced wrong results when comparing lists of different type but same size diff -r 415bf2ce6bab -r 3df63e95921a src/Makefile --- a/src/Makefile Sun Dec 22 22:53:53 2024 +0100 +++ b/src/Makefile Sun Dec 22 23:10:07 2024 +0100 @@ -85,7 +85,8 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(build_dir)/hash_key$(OBJ_EXT): hash_key.c cx/hash_key.h cx/common.h +$(build_dir)/hash_key$(OBJ_EXT): hash_key.c cx/hash_key.h cx/common.h \ + cx/string.h cx/allocator.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< diff -r 415bf2ce6bab -r 3df63e95921a src/cx/hash_key.h --- a/src/cx/hash_key.h Sun Dec 22 22:53:53 2024 +0100 +++ b/src/cx/hash_key.h Sun Dec 22 23:10:07 2024 +0100 @@ -118,46 +118,27 @@ size_t len ); -#ifdef __cplusplus -} // extern "C" - -cx_attr_nodiscard -static inline CxHashKey cx_hash_key_cxstr(cxstring str) { - return cx_hash_key(str.ptr, str.length); -} - -cx_attr_nodiscard -static inline CxHashKey cx_hash_key_cxstr(cxmutstr str) { - return cx_hash_key(str.ptr, str.length); -} - -#else /** * Computes a hash key from a UCX string. * * @param str the string * @return the hash key */ -#define cx_hash_key_cxstr(str) _Generic((str), \ - cxstring: cx_hash_key_cxstring, \ - cxmutstr: cx_hash_key_cxmutstr) \ - (str) - -/** - * @copydoc cx_hash_key_cxstr() - */ cx_attr_nodiscard -static inline CxHashKey cx_hash_key_cxstring(cxstring str) { +static inline CxHashKey cx_hash_key_cxstr(cxstring str) { return cx_hash_key(str.ptr, str.length); } /** - * @copydoc cx_hash_key_cxstr() + * Computes a hash key from a UCX string. + * + * @param str the string + * @return the hash key */ -cx_attr_nodiscard -static inline CxHashKey cx_hash_key_cxmutstr(cxmutstr str) { - return cx_hash_key(str.ptr, str.length); -} +#define cx_hash_key_cxstr(str) cx_hash_key_cxstr(cx_strcast(str)) + +#ifdef __cplusplus +} // extern "C" #endif #endif // UCX_HASH_KEY_H diff -r 415bf2ce6bab -r 3df63e95921a src/cx/string.h --- 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(). diff -r 415bf2ce6bab -r 3df63e95921a src/string.c --- a/src/string.c Sun Dec 22 22:53:53 2024 +0100 +++ b/src/string.c Sun Dec 22 23:10:07 2024 +0100 @@ -63,10 +63,6 @@ return (cxstring) {cstring, length}; } -cxstring cx_strcast(cxmutstr str) { - return (cxstring) {str.ptr, str.length}; -} - void cx_strfree(cxmutstr *str) { if (str == NULL) return; free(str->ptr); diff -r 415bf2ce6bab -r 3df63e95921a tests/Makefile --- a/tests/Makefile Sun Dec 22 22:53:53 2024 +0100 +++ b/tests/Makefile Sun Dec 22 23:10:07 2024 +0100 @@ -70,7 +70,7 @@ $(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \ ../src/cx/common.h ../src/cx/hash_key.h ../src/cx/string.h \ - ../src/cx/allocator.h + ../src/cx/allocator.h ../src/cx/string.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -I../src -c $<