PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
strchrnul.c
1#define SYSCLIB_DISABLE_BUILTINS
2#include <sysclib.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
11char *__strchrnul(const char *s, int c)
12{
13 c = (unsigned char)c;
14 if (!c) return (char *)s + strlen(s);
15
16#ifdef __GNUC__
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}