f2b9ae11b605278f523397112f16bf4fe67ef720
[musl] / src / string / strchrnul.c
1 #include <string.h>
2 #include <stdint.h>
3 #include <limits.h>
4
5 #define ALIGN (sizeof(size_t))
6 #define ONES ((size_t)-1/UCHAR_MAX)
7 #define HIGHS (ONES * (UCHAR_MAX/2+1))
8 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9
10 char *__strchrnul(const char *s, int c)
11 {
12         size_t *w, k;
13
14         c = (unsigned char)c;
15         if (!c) return (char *)s + strlen(s);
16
17         for (; (uintptr_t)s % ALIGN; s++)
18                 if (!*s || *(unsigned char *)s == c) return (char *)s;
19         k = ONES * c;
20         for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
21         for (s = (void *)w; *s && *(unsigned char *)s != c; s++);
22         return (char *)s;
23 }
24
25 weak_alias(__strchrnul, strchrnul);