X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstring%2Fstrchrnul.c;h=39e2635b3064df1e2b15cb45d60c654238ad8f79;hb=0c277ff156749628c678257f878d3a6edb5868de;hp=5e0c1a1a4487667f85525a61a59270d44de32ff7;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01;p=musl diff --git a/src/string/strchrnul.c b/src/string/strchrnul.c index 5e0c1a1a..39e2635b 100644 --- a/src/string/strchrnul.c +++ b/src/string/strchrnul.c @@ -1,7 +1,28 @@ #include +#include +#include -char *strchrnul(const char *s, int c) +#define ALIGN (sizeof(size_t)) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +char *__strchrnul(const char *s, int c) { - char *p = strchr(s, c); - return p ? p : (char *)s + strlen(s); + c = (unsigned char)c; + if (!c) return (char *)s + strlen(s); + +#ifdef __GNUC__ + typedef size_t __attribute__((__may_alias__)) word; + const word *w; + for (; (uintptr_t)s % ALIGN; s++) + if (!*s || *(unsigned char *)s == c) return (char *)s; + size_t k = ONES * c; + for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); + s = (void *)w; +#endif + for (; *s && *(unsigned char *)s != c; s++); + return (char *)s; } + +weak_alias(__strchrnul, strchrnul);