X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fstring%2Fstrchrnul.c;h=ceae4d45f22000fd3ee4e5f5ce86d67a1bcf7466;hp=5e0c1a1a4487667f85525a61a59270d44de32ff7;hb=4853c1f7f7b5023aa6a409abc1e759f5f92c9c4e;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 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);