1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| | #include <string.h>
#include <stdint.h>
#define aliases __attribute__((__may_alias__))
#define byte_repeat(x) ((size_t)~0 / 0xff * (x))
#define word_has_zero(x) (((x) - byte_repeat(0x01)) & ~(x) & byte_repeat(0x80))
#define weak_alias(o, n) extern __typeof__(o) n __attribute__((weak, alias(#o)))
char *__strchrnul(const char *_s, int i)
{
i = (unsigned char)i;
const unsigned char *s = (const void *)_s;
const size_t aliases *ws, wi = byte_repeat(i);
if (!i) return (char *)s + strlen((char *)s);
for (; (uintptr_t)s & sizeof(size_t) - 1 && *s && *s != i; s++);
if ((uintptr_t)s & sizeof(size_t) - 1) return (char *)s;
ws = (const void *)s;
for (; !word_has_zero(*ws) && !word_has_zero(*ws ^ wi); ws++);
s = (const void *)ws;
for (; *s && *s != i; s++);
return (char *)s;
}
weak_alias(__strchrnul, strchrnul);
|