1 #include <string.h>
2 
3 #include <limits.h>
4 #include <zircon/compiler.h>
5 #include <stdint.h>
6 
7 #define ALIGN (sizeof(size_t))
8 #define ONES ((size_t)-1 / UCHAR_MAX)
9 #define HIGHS (ONES * (UCHAR_MAX / 2 + 1))
10 #define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS)
11 
strlen(const char * s)12 size_t strlen(const char* s) {
13     const char* a = s;
14     for (; (uintptr_t)s % ALIGN; s++)
15         if (!*s)
16             return s - a;
17     const size_t* w = (const void*)s;
18 #if !__has_feature(address_sanitizer)
19     // This reads past the end of the string, which is usually OK since it
20     // won't cross a page boundary.  But under ASan, even one byte past the
21     // actual end is diagnosed.
22     while (!HASZERO(*w))
23         ++w;
24 #endif
25     for (s = (const void*)w; *s; s++)
26         ;
27     return s - a;
28 }
29