From 8776be77524450e321064f0a65c8f941284e1c34 Mon Sep 17 00:00:00 2001 From: Haru Date: Sat, 25 Aug 2018 17:48:38 +0200 Subject: Add const-safe wrappers for strchr(), strrchr() and strstr() using c11 generics C99 compilers will still use the normal, const-unsafe, version Signed-off-by: Haru --- src/common/strlib.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/common/strlib.h b/src/common/strlib.h index 5ea4f4763..006bbd14b 100644 --- a/src/common/strlib.h +++ b/src/common/strlib.h @@ -49,6 +49,27 @@ #define safesnprintf(buf,sz,fmt,...) (strlib->safesnprintf_((buf),(sz),(fmt),##__VA_ARGS__)) #define strline(str,pos) (strlib->strline_((str),(pos))) #define bin2hex(output,input,count) (strlib->bin2hex_((output),(input),(count))) +#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) +#if defined(__GNUC__) && !defined(__clang__) && GCC_VERSION < 40900 +// _Generic is only supported starting with GCC 4.9 +#else +#ifdef strchr +#undef strchr +#endif // strchr +#define strchr(src, chr) _Generic((src), \ + const char * : ((const char *)(strchr)((src), (chr))), \ + char * : ((strchr)((src), (chr))) \ + ) +#define strrchr(src, chr) _Generic((src), \ + const char * : ((const char *)(strrchr)((src), (chr))), \ + char * : ((strrchr)((src), (chr))) \ + ) +#define strstr(haystack, needle) _Generic((haystack), \ + const char * : ((const char *)(strstr)((haystack), (needle))), \ + char * : ((strstr)((haystack), (needle))) \ + ) +#endif +#endif /// Bitfield determining the behavior of sv_parse and sv_split. typedef enum e_svopt { -- cgit v1.2.3-60-g2f50