Example of code
Source: https://github.com/c3lang/c3c/blob/master/src/utils/lib.h
#define ELEMENTLEN(x) (sizeof(x) / sizeof(x[0]))
#define CONCAT_INNER(a, b) a ## b
#define CONCAT(a, b) CONCAT_INNER(a, b)
#define FOREACH(type__, name__, vec__) \
type__* CONCAT(foreach_vec_, __LINE__) = (vec__); type__* CONCAT(foreach_vecend_, __LINE__) = CONCAT(foreach_vec_, __LINE__) ? CONCAT(foreach_vec_, __LINE__) + vec_size(CONCAT(foreach_vec_, __LINE__)) : NULL; \
type__* CONCAT(foreach_it_, __LINE__) = CONCAT(foreach_vec_, __LINE__); \
for (type__ name__ ; CONCAT(foreach_it_, __LINE__) < CONCAT(foreach_vecend_, __LINE__) ? (name__ = *CONCAT(foreach_it_, __LINE__), true) : false; CONCAT(foreach_it_, __LINE__)++)
#if IS_GCC || IS_CLANG
#define MAX(_a, _b) ({ \
__auto_type __a__ = (_a); \
__auto_type __b__ = (_b); \
__a__ > __b__ ? __a__ : __b__; })
#define MIN(_a, _b) ({ \
__auto_type __a__ = (_a); \
__auto_type __b__ = (_b); \
__a__ < __b__ ? __a__ : __b__; })
#else
#define MAX(a_, b_) ((a_) > (b_) ? (a_) : (b_))
#define MIN(a_, b_) ((a_) < (b_) ? (a_) : (b_))
#endif
static inline bool slice_strcmp(StringSlice slice, const char *other)
{
if (strlen(other) != slice.len) return false;
return strncmp(slice.ptr, other, slice.len) == 0;
}
static inline StringSlice slice_from_string(const char *data)
{
return (StringSlice) { data, strlen(data) };
}
static inline bool char_is_lower(char c)
{
return c >= 'a' && c <= 'z';
}
static inline bool char_is_lower_(char c)
{
return c == '_' || (c >= 'a' && c <= 'z');
}
static inline bool char_is_upper(char c)
{
return c >= 'A' && c <= 'Z';
}
static inline bool char_is_oct(char c)
{
return c >= '0' && c <= '7';
}
static inline bool char_is_oct_or_(char c)
{
return c == '_' || (c >= '0' && c <= '7');
}
static inline bool char_is_binary(char c)
{
return c == '0' || c == '1';
}
static inline bool char_is_binary_or_(char c)
{
return c == '0' || c == '1' || c == '_';
}
static inline bool char_is_digit_or_(char c)
{
return c == '_' || (c >= '0' && c <= '9');
}
static inline bool char_is_digit(char c)
{
return c >= '0' && c <= '9';
}
static inline bool char_is_base64(char c)
{
return (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9')
|| c == '+' || c == '/';
}
#define NUMBER_CHAR_CASE '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9'
#define UPPER_CHAR_CASE 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': \
case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': \
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z'
#define LOWER_CHAR_CASE 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': \
case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': \
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z'
#define HEX_CHAR_CASE 'a': case 'b': case 'c': case 'd': case 'e': case 'f': \
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F'
static inline bool char_is_whitespace(char c)
{
switch (c)
{
case ' ':
case '\t':
case '\n':
return true;
case '\r':
UNREACHABLE
default:
return false;
}
}
static inline bool char_is_alphanum_(char c)
{
switch (c)
{
case LOWER_CHAR_CASE:
case UPPER_CHAR_CASE:
case NUMBER_CHAR_CASE:
case '_':
return true;
default:
return false;
}
}