Link to C API documentation of master branch
[vis.git] / util.h
blob9048c821a283da71cf06417e9776d21ab4516d2f
1 #ifndef UTIL_H
2 #define UTIL_H
4 #include <stdint.h>
5 #include <stdbool.h>
7 #define LENGTH(x) ((int)(sizeof (x) / sizeof *(x)))
8 #define MIN(a, b) ((a) > (b) ? (b) : (a))
9 #define MAX(a, b) ((a) < (b) ? (b) : (a))
11 /* is c the start of a utf8 sequence? */
12 #define ISUTF8(c) (((c)&0xC0)!=0x80)
13 #define ISASCII(ch) ((unsigned char)ch < 0x80)
15 #if GCC_VERSION>=5004000 || CLANG_VERSION>=4000000
16 #define addu __builtin_add_overflow
17 #else
18 static inline bool addu(size_t a, size_t b, size_t *c) {
19 if (SIZE_MAX - a < b)
20 return false;
21 *c = a + b;
22 return true;
24 #endif
26 #if !HAVE_MEMRCHR
27 /* MIT licensed implementation from musl libc */
28 static void *memrchr(const void *m, int c, size_t n)
30 const unsigned char *s = m;
31 c = (unsigned char)c;
32 while (n--) if (s[n]==c) return (void *)(s+n);
33 return 0;
35 #endif
37 #endif /* UTIL_H */