Add fallback min/max for compilers that don't have statement expressions

Statement expressions are a GNU C extension and are not available
in ISO C.

On compilers that don't have them, define these macros as plain
conditional expressions, since they are only ever used with expressions
that have no side-effects.

The statement-expression version is still retained as an added
safety measure on GNU-compatible compilers.

Signed-off-by: Michael Forney <mforney@mforney.org>
This commit is contained in:
Michael Forney 2019-06-16 17:05:30 -07:00
parent e74ba891f7
commit 06ef34c86b

View file

@ -34,6 +34,7 @@
#undef min
#undef max
#ifdef __GNUC__
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
@ -44,6 +45,10 @@
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
#else
#define min(a,b) ((a) > (b) ? (b) : (a))
#define max(a,b) ((a) > (b) ? (a) : (b))
#endif
static inline bool
startswith(const char *str, size_t len, const char *prefix, size_t plen)