1/* Copyright (C) 1991-2024 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. */
33char *
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 (word: find_zero_eq_all (x1: word, x2: repeated_c), s: s_int);
44 if (mask != 0)
45 return (char *) str + index_first (c: mask);
46
47 do
48 word = *++word_ptr;
49 while (! has_zero_eq (x1: word, x2: repeated_c));
50
51 return (char *) word_ptr + index_first_zero_eq (x1: word, x2: repeated_c);
52}
53#ifndef STRCHRNUL
54libc_hidden_def (__strchrnul)
55weak_alias (__strchrnul, strchrnul)
56#endif
57

source code of glibc/string/strchrnul.c