+/* Round up @a so it's divisible by @b. */
+#define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
+
+#define STRINGIFY(x) #x
+#define STRINGIFY2(x) STRINGIFY(x)
+
+char
+to_lower(char c);
+
+int
+istrcmp(const char *a, const char *b);
+
+int
+istrncmp(const char *a, const char *b, size_t n);
+
+static inline bool
+streq(const char *s1, const char *s2)
+{
+ assert(s1 && s2);
+ return strcmp(s1, s2) == 0;
+}
+
+static inline bool
+streq_null(const char *s1, const char *s2)
+{
+ if (s1 == NULL || s2 == NULL)
+ return s1 == s2;
+ return streq(s1, s2);
+}
+
+static inline bool
+streq_not_null(const char *s1, const char *s2)
+{
+ if (!s1 || !s2)
+ return false;
+ return streq(s1, s2);
+}
+
+static inline bool
+istreq(const char *s1, const char *s2)
+{
+ return istrcmp(s1, s2) == 0;
+}
+
+static inline bool
+istreq_prefix(const char *s1, const char *s2)
+{
+ return istrncmp(s1, s2, strlen(s1)) == 0;
+}
+
+static inline char *
+strdup_safe(const char *s)
+{
+ return s ? strdup(s) : NULL;
+}
+
+static inline size_t
+strlen_safe(const char *s)
+{
+ return s ? strlen(s) : 0;
+}
+
+static inline bool
+isempty(const char *s)
+{
+ return s == NULL || s[0] == '\0';
+}
+
+static inline const char *
+strnull(const char *s)
+{
+ return s ? s : "(null)";
+}
+
+static inline const char *
+strempty(const char *s)
+{
+ return s ? s : "";
+}
+
+static inline void *
+memdup(const void *mem, size_t nmemb, size_t size)
+{
+ void *p = calloc(nmemb, size);
+ if (p)
+ memcpy(p, mem, nmemb * size);
+ return p;
+}
+
+#if !(defined(HAVE_STRNDUP) && HAVE_STRNDUP)
+static inline char *
+strndup(const char *s, size_t n)
+{
+ size_t slen = strlen(s);
+ size_t len = MIN(slen, n);
+ char *p = malloc(len + 1);
+ if (!p)
+ return NULL;
+ memcpy(p, s, len);
+ p[len] = '\0';
+ return p;
+}
+#endif
+
+/* ctype.h is locale-dependent and has other oddities. */
+static inline bool
+is_space(char ch)
+{
+ return ch == ' ' || (ch >= '\t' && ch <= '\r');
+}
+
+static inline bool
+is_alpha(char ch)
+{
+ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
+}
+
+static inline bool
+is_digit(char ch)
+{
+ return ch >= '0' && ch <= '9';
+}
+
+static inline bool
+is_alnum(char ch)
+{
+ return is_alpha(ch) || is_digit(ch);
+}
+
+static inline bool
+is_xdigit(char ch)
+{
+ return
+ (ch >= '0' && ch <= '9') ||
+ (ch >= 'a' && ch <= 'f') ||
+ (ch >= 'A' && ch <= 'F');
+}
+
+static inline bool
+is_graph(char ch)
+{
+ /* See table in ascii(7). */
+ return ch >= '!' && ch <= '~';
+}