• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
__strchrnul(const char * s,int c)10 char *__strchrnul(const char *s, int c)
11 {
12 	c = (unsigned char)c;
13 	if (!c) return (char *)s + strlen(s);
14 
15 #ifdef __GNUC__
16 	typedef size_t __attribute__((__may_alias__)) word;
17 	const word *w;
18 	for (; (uintptr_t)s % ALIGN; s++)
19 		if (!*s || *(unsigned char *)s == c) return (char *)s;
20 	size_t k = ONES * c;
21 	for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
22 	s = (void *)w;
23 #endif
24 	for (; *s && *(unsigned char *)s != c; s++);
25 	return (char *)s;
26 }
27 
28 weak_alias(__strchrnul, strchrnul);
29