From 0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 12 Feb 2011 00:22:29 -0500 Subject: initial check-in, version 0.5.0 --- src/string/strlen.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/string/strlen.c (limited to 'src/string/strlen.c') diff --git a/src/string/strlen.c b/src/string/strlen.c new file mode 100644 index 00000000..936fb5cf --- /dev/null +++ b/src/string/strlen.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +#define ALIGN (sizeof(size_t)-1) +#define ONES ((size_t)-1/UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX/2+1)) +#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) + +size_t strlen(const char *s) +{ + const char *a = s; + const size_t *w; + for (; ((uintptr_t)s & ALIGN) && *s; s++); + if (*s) { + for (w = (const void *)s; !HASZERO(*w); w++); + for (s = (const void *)w; *s; s++); + } + return s-a; +} -- cgit v1.2.1