X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fstring%2Fstrchrnul.c;h=ceae4d45f22000fd3ee4e5f5ce86d67a1bcf7466;hp=5e0c1a1a4487667f85525a61a59270d44de32ff7;hb=68dbd05039f8b256f586ed9a589645fa3a1b7f5f;hpb=3f9ff1514e49b06c20a61af9ae9e52bd53b48d9a diff --git a/src/string/strchrnul.c b/src/string/strchrnul.c index 5e0c1a1a..ceae4d45 100644 --- a/src/string/strchrnul.c +++ b/src/string/strchrnul.c @@ -1,7 +1,27 @@ #include +#include +#include +#include +#include "libc.h" -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); + size_t *w, k; + + c = (unsigned char)c; + if (!c) return (char *)s + strlen(s); + + for (; (uintptr_t)s % ALIGN; s++) + if (!*s || *(unsigned char *)s == c) return (char *)s; + k = ONES * c; + for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++); + for (s = (void *)w; *s && *(unsigned char *)s != c; s++); + return (char *)s; } + +weak_alias(__strchrnul, strchrnul);