initial check-in, version 0.5.0
[musl] / src / string / strchr.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <limits.h>
5
6 #define ALIGN (sizeof(size_t)-1)
7 #define ONES ((size_t)-1/UCHAR_MAX)
8 #define HIGHS (ONES * (UCHAR_MAX/2+1))
9 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
10
11 char *strchr(const char *s, int c)
12 {
13         c = (char)c;
14         if (!c) return (char *)s + strlen(s);
15         for (; ((uintptr_t)s & ALIGN) && *s && *s != c; s++);
16         if (*s && *s != c) {
17                 const size_t *w;
18                 size_t k = ONES * c;
19                 for (w = (const void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
20                 for (s = (const void *)w; *s && *s != c; s++);
21         }
22         return *s ? (char *)s : 0;
23 }