xref: /haiku/src/system/libroot/posix/musl/string/strchrnul.c (revision fc7456e9b1ec38c941134ed6d01c438cf289381e)
1 #define _DEFAULT_SOURCE
2 #include <string.h>
3 #include <stdint.h>
4 #include <limits.h>
5 
6 #define ALIGN (sizeof(size_t))
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 *strchrnul(const char *s, int c)
12 {
13 	c = (unsigned char)c;
14 	if (!c) return (char *)s + strlen(s);
15 
16 #if 0
17 	typedef size_t __attribute__((__may_alias__)) word;
18 	const word *w;
19 	for (; (uintptr_t)s % ALIGN; s++)
20 		if (!*s || *(unsigned char *)s == c) return (char *)s;
21 	size_t k = ONES * c;
22 	for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
23 	s = (void *)w;
24 #endif
25 	for (; *s && *(unsigned char *)s != c; s++);
26 	return (char *)s;
27 }
28