strchrnul.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. #undef __strchrnul
  21. #undef strchrnul
  22. #ifdef STRCHRNUL
  23. # define __strchrnul STRCHRNUL
  24. #endif
  25. /* Find the first occurrence of C in S or the final NUL byte. */
  26. char *
  27. __strchrnul (const char *str, int c_in)
  28. {
  29. /* Align pointer to sizeof op_t. */
  30. uintptr_t s_int = (uintptr_t) str;
  31. const op_t *word_ptr = (const op_t *) PTR_ALIGN_DOWN (str, sizeof (op_t));
  32. op_t repeated_c = repeat_bytes (c_in);
  33. op_t word = *word_ptr;
  34. find_t mask = shift_find (find_zero_eq_all (word, repeated_c), s_int);
  35. if (mask != 0)
  36. return (char *) str + index_first (mask);
  37. do
  38. word = *++word_ptr;
  39. while (! has_zero_eq (word, repeated_c));
  40. return (char *) word_ptr + index_first_zero_eq (word, repeated_c);
  41. }
  42. #ifndef STRCHRNUL
  43. libc_hidden_def (__strchrnul)
  44. weak_alias (__strchrnul, strchrnul)
  45. #endif