diff options
author | Haru <haru@dotalux.com> | 2018-08-25 17:48:38 +0200 |
---|---|---|
committer | Haru <haru@dotalux.com> | 2018-08-25 18:50:20 +0200 |
commit | 8776be77524450e321064f0a65c8f941284e1c34 (patch) | |
tree | 6b8cbe272169a74d8915985ec137406df2d7a680 /src | |
parent | 919f004106a705947a52ead1b6b23238cbe10218 (diff) | |
download | hercules-8776be77524450e321064f0a65c8f941284e1c34.tar.gz hercules-8776be77524450e321064f0a65c8f941284e1c34.tar.bz2 hercules-8776be77524450e321064f0a65c8f941284e1c34.tar.xz hercules-8776be77524450e321064f0a65c8f941284e1c34.zip |
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 <haru@dotalux.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/common/strlib.h | 21 |
1 files changed, 21 insertions, 0 deletions
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 { |