argp-standalone/strchrnul.c

57 lines
1.7 KiB
C
Raw Normal View History

2023-12-08 08:32:25 +00:00
/* Copyright (C) 1991-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
2021-02-10 19:16:04 +00:00
2023-12-08 08:32:25 +00:00
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
2021-02-10 19:16:04 +00:00
2023-12-08 08:32:25 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2021-02-10 19:16:04 +00:00
2023-12-08 08:32:25 +00:00
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <libc-pointer-arith.h>
#include <string-fzb.h>
#include <string-fzc.h>
#include <string-fzi.h>
#include <string-shift.h>
#include <string.h>
#undef __strchrnul
#undef strchrnul
#ifdef STRCHRNUL
# define __strchrnul STRCHRNUL
#endif
/* Find the first occurrence of C in S or the final NUL byte. */
char *
__strchrnul (const char *str, int c_in)
2021-02-10 19:16:04 +00:00
{
2023-12-08 08:32:25 +00:00
/* Align pointer to sizeof op_t. */
uintptr_t s_int = (uintptr_t) str;
const op_t *word_ptr = (const op_t *) PTR_ALIGN_DOWN (str, sizeof (op_t));
op_t repeated_c = repeat_bytes (c_in);
op_t word = *word_ptr;
find_t mask = shift_find (find_zero_eq_all (word, repeated_c), s_int);
if (mask != 0)
return (char *) str + index_first (mask);
do
word = *++word_ptr;
while (! has_zero_eq (word, repeated_c));
2021-02-10 19:16:04 +00:00
2023-12-08 08:32:25 +00:00
return (char *) word_ptr + index_first_zero_eq (word, repeated_c);
2021-02-10 19:16:04 +00:00
}
2023-12-08 08:32:25 +00:00
#ifndef STRCHRNUL
libc_hidden_def (__strchrnul)
weak_alias (__strchrnul, strchrnul)
#endif