From 0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 12 Feb 2011 00:22:29 -0500 Subject: initial check-in, version 0.5.0 --- src/string/strchr.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/string/strchr.c (limited to 'src/string/strchr.c') diff --git a/src/string/strchr.c b/src/string/strchr.c new file mode 100644 index 00000000..e606f4fe --- /dev/null +++ b/src/string/strchr.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *strchr(const char *s, int c) +{ + c = (char)c; + if (!c) return (char *)s + strlen(s); + for (; ((uintptr_t)s & ALIGN) && *s && *s != c; s++); + if (*s && *s != c) { + const size_t *w; + size_t k = ONES * c; + for (w = (const void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); + for (s = (const void *)w; *s && *s != c; s++); + } + return *s ? (char *)s : 0; +} -- cgit v1.2.1