strlen.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <libc-pointer-arith.h>
  15. #include <string-fzb.h>
  16. #include <string-fzc.h>
  17. #include <string-fzi.h>
  18. #include <string-shift.h>
  19. #include <string.h>
  20. #ifdef STRLEN
  21. # define __strlen STRLEN
  22. #endif
  23. /* Return the length of the null-terminated string STR. Scan for
  24. the null terminator quickly by testing four bytes at a time. */
  25. size_t
  26. __strlen (const char *str)
  27. {
  28. /* Align pointer to sizeof op_t. */
  29. const uintptr_t s_int = (uintptr_t) str;
  30. const op_t *word_ptr = (const op_t*) PTR_ALIGN_DOWN (str, sizeof (op_t));
  31. op_t word = *word_ptr;
  32. find_t mask = shift_find (find_zero_all (word), s_int);
  33. if (mask != 0)
  34. return index_first (mask);
  35. do
  36. word = *++word_ptr;
  37. while (! has_zero (word));
  38. return ((const char *) word_ptr) + index_first_zero (word) - str;
  39. }
  40. #ifndef STRLEN
  41. weak_alias (__strlen, strlen)
  42. libc_hidden_builtin_def (strlen)
  43. #endif