]> git.ipfire.org Git - thirdparty/glibc.git/blob - string/strchrnul.c
3e09964d32eee82bd6977d6d30ce407c28222d4f
[thirdparty/glibc.git] / string / strchrnul.c
1 /* Copyright (C) 1991-2025 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <libc-pointer-arith.h>
19 #include <string-fzb.h>
20 #include <string-fzc.h>
21 #include <string-fzi.h>
22 #include <string-shift.h>
23 #include <string.h>
24
25 #undef __strchrnul
26 #undef strchrnul
27
28 #ifdef STRCHRNUL
29 # define __strchrnul STRCHRNUL
30 #endif
31
32 /* Find the first occurrence of C in S or the final NUL byte. */
33 char *
34 __strchrnul (const char *str, int c_in)
35 {
36 /* Align pointer to sizeof op_t. */
37 uintptr_t s_int = (uintptr_t) str;
38 const op_t *word_ptr = (const op_t *) PTR_ALIGN_DOWN (str, sizeof (op_t));
39
40 op_t repeated_c = repeat_bytes (c_in);
41
42 op_t word = *word_ptr;
43 find_t mask = shift_find (find_zero_eq_all (word, repeated_c), s_int);
44 if (mask != 0)
45 return (char *) str + index_first (mask);
46
47 do
48 word = *++word_ptr;
49 while (! has_zero_eq (word, repeated_c));
50
51 return (char *) word_ptr + index_first_zero_eq (word, repeated_c);
52 }
53 #ifndef STRCHRNUL
54 libc_hidden_def (__strchrnul)
55 weak_alias (__strchrnul, strchrnul)
56 #endif