diff options
Diffstat (limited to '')
-rw-r--r-- | include/slibc-human.h | 17 | ||||
-rw-r--r-- | include/slibc/features.h | 92 |
2 files changed, 101 insertions, 8 deletions
diff --git a/include/slibc-human.h b/include/slibc-human.h index 6f5a603..9aebcfd 100644 --- a/include/slibc-human.h +++ b/include/slibc-human.h @@ -160,12 +160,12 @@ enum machinesize_mode -char* humanmode(const char* restrict buffer, mode_t mode); +char* humanmode(char* restrict buffer, mode_t mode); mode_t machinemode(const char* restrict str, mode_t mode, mode_t mask); -char* humansize(const char* restrict buffer, size_t size, enum humansize_mode mode, int detail); +char* humansize(char* restrict buffer, size_t size, enum humansize_mode mode, int detail); int machinesize(size_t* restrict size, const char* restrict string, enum machinesize_mode mode); @@ -177,13 +177,22 @@ int machinedur(intmax_t* restrict sec, long int* nsec, const char* restrict str, const char* restrict space, const char* restrict comma); -int machineint(intmax_t* restrict r, const char* restrict str); +char* machineint(intmax_t* restrict r, const char* restrict str); +# ifdef __CONST_CORRECT +# define machineint(...) (__const_correct_2p(machineint, __VA_ARGS__)) +# endif -int machineuint(uintmax_t* restrict r, const char* restrict str); +char* machineuint(uintmax_t* restrict r, const char* restrict str); +# ifdef __CONST_CORRECT +# define machineuint(...) (__const_correct_2p(machineuint, __VA_ARGS__)) +# endif #endif int machinefloat(long double* restrict r, const char* restrict str, const char* restrict space, const char* restrict comma); +#ifdef __CONST_CORRECT +# define machinefloat(...) (__const_correct_2(machinefloat, __VA_ARGS__)) +#endif /** diff --git a/include/slibc/features.h b/include/slibc/features.h index 6951ea4..bb28189 100644 --- a/include/slibc/features.h +++ b/include/slibc/features.h @@ -29,6 +29,17 @@ /** + * Update existance of `__const_correct*` macros + */ +#ifdef __CONST_CORRECT +# undef __CONST_CORRECT +# undef __const_correct +# undef __const_correct2 +# undef __const_correct2p +#endif + + +/** * Macro for any function with at least 2 arguments, * that shall return with `const` qualifier if and only * if the first argument is `const`-qualifier. @@ -48,10 +59,6 @@ * @param ... The rest of the arguments. * @return The result casted to the same type as `first`. */ -#ifdef __CONST_CORRECT -# undef __CONST_CORRECT -# undef __const_correct -#endif #if defined(__GNUC__) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) # define __CONST_CORRECT # define __const_correct(function, first, ...) \ @@ -81,6 +88,83 @@ * than using a fall back. */ +/** + * Macro for any function with at least 3 arguments, + * that shall return with `const` qualifier if and only + * if the second argument is `const`-qualifier. + * + * Other qualifiers could be dropped. + * + * @param function The name of the function. + * @param first The first argument. + * @param second The second argument. + * @param ... The rest of the arguments. + * @return The result casted to the same type as `second`. + */ +#if defined(__GNUC__) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +# define __CONST_CORRECT +# define __const_correct2(function, first, second, ...) \ + (_Generic(&(second), \ + const wchar_t(*)[]: (const wchar_t*)function(first, second, __VA_ARGS__), \ + const char(*)[]: (const char*) function(first, second, __VA_ARGS__), \ + const void**: (const void*) function(first, second, __VA_ARGS__), \ + void**: function(first, second, __VA_ARGS__), \ + default: (__typeof__(&*first))function(first, second, __VA_ARGS__))) +#elif defined(__GNUC__) +# define __CONST_CORRECT +# define __const_correct2(function, first, second, ...) \ + ((__typeof__(&*second))function(first, second, __VA_ARGS__)) +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +# define __CONST_CORRECT +# define __const_correct2(function, first, second, ...) \ + (_Generic(&(second), \ + const wchar_t(*)[]: (const wchar_t*)function(first, second, __VA_ARGS__), \ + const char(*)[]: (const char*) function(first, second, __VA_ARGS__), \ + const wchar_t**: (const wchar_t*)function(first, second, __VA_ARGS__), \ + const char**: (const char*) function(first, second, __VA_ARGS__), \ + const void**: (const void*) function(first, second, __VA_ARGS__), \ + default: function(first, second, __VA_ARGS__))) +#endif + + +/** + * Macro for any function with precisely 2 arguments, + * that shall return with `const` qualifier if and only + * if the second argument is `const`-qualifier. + * + * Other qualifiers could be dropped. + * + * @param function The name of the function. + * @param first The first argument. + * @param second The second argument. + * @return The result casted to the same type as `second`. + */ +#if defined(__GNUC__) && defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +# define __CONST_CORRECT +# define __const_correct2p(function, first, second) \ + (_Generic(&(second), \ + const wchar_t(*)[]: (const wchar_t*)function(first, second), \ + const char(*)[]: (const char*) function(first, second), \ + const void**: (const void*) function(first, second), \ + void**: function(first, second), \ + default: (__typeof__(&*first))function(first, second))) +#elif defined(__GNUC__) +# define __CONST_CORRECT +# define __const_correct2p(function, first, second) \ + ((__typeof__(&*second))function(first, second)) +#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) +# define __CONST_CORRECT +# define __const_correct2p(function, first, second) \ + (_Generic(&(second), \ + const wchar_t(*)[]: (const wchar_t*)function(first, second), \ + const char(*)[]: (const char*) function(first, second), \ + const wchar_t**: (const wchar_t*)function(first, second), \ + const char**: (const char*) function(first, second), \ + const void**: (const void*) function(first, second), \ + default: function(first, second))) +#endif + + /** * _BSD_SOURCE || _SVID_SOURCE || _GNU_SOURCE implies _POSIX_C_SOURCE = 2. |