aboutsummaryrefslogtreecommitdiff
path: root/util.h
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-04-07 16:04:40 +0200
committerMarc André Tanner <mat@brain-dump.org>2017-04-09 11:27:14 +0200
commitd2004b15f1e90efafedc367335c07ad4636d291d (patch)
tree3ab47dc219f5ad3b2f3bc9d3699aecc3b7ffd656 /util.h
parent3349b97344c6c7032bff7aaa13985406ca571c34 (diff)
downloadvis-d2004b15f1e90efafedc367335c07ad4636d291d.tar.gz
vis-d2004b15f1e90efafedc367335c07ad4636d291d.tar.xz
text: add mem{r,}chr(3) based byte search functions
These are generally implemented efficiently in libc. While memrchr(3) is non-standard, it is a common extension. If it is not available, we use a simple C implementation from musl.
Diffstat (limited to 'util.h')
-rw-r--r--util.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/util.h b/util.h
index a53deda..9048c82 100644
--- a/util.h
+++ b/util.h
@@ -23,4 +23,15 @@ static inline bool addu(size_t a, size_t b, size_t *c) {
}
#endif
+#if !HAVE_MEMRCHR
+/* MIT licensed implementation from musl libc */
+static void *memrchr(const void *m, int c, size_t n)
+{
+ const unsigned char *s = m;
+ c = (unsigned char)c;
+ while (n--) if (s[n]==c) return (void *)(s+n);
+ return 0;
+}
#endif
+
+#endif /* UTIL_H */