1
0

strlen.c 457 B

12345678910111213141516171819
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <limits.h>
  5. #define ALIGN (sizeof(size_t))
  6. #define ONES ((size_t)-1/UCHAR_MAX)
  7. #define HIGHS (ONES * (UCHAR_MAX/2+1))
  8. #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
  9. size_t strlen(const char *s)
  10. {
  11. const char *a = s;
  12. const size_t *w;
  13. for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
  14. for (w = (const void *)s; !HASZERO(*w); w++);
  15. for (s = (const void *)w; *s; s++);
  16. return s-a;
  17. }