d3563f18c932d139b7b78f5e37a473518671a5a0
[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         size_t *w, k;
14
15         c = (unsigned char)c;
16         if (!c) return (char *)s + strlen(s);
17
18         for (; ((uintptr_t)s & ALIGN); s++)
19                 if (*(unsigned char *)s == c) return (char *)s;
20                 else if (!*s) return 0;
21         k = ONES * c;
22         for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
23         for (s = (void *)w; *s; s++)
24                 if (*(unsigned char *)s == c) return (char *)s;
25         return 0;
26 }