xref: /freebsd/lib/libc/string/strchrnul.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d902844bSNiclas Zeising /*-
2*7f72497eSEd Maste  * SPDX-License-Identifier: MIT
3d915a14eSPedro F. Giffuni  *
4*7f72497eSEd Maste  * Copyright (c) 2005-2014 Rich Felker, et al.
5d902844bSNiclas Zeising  *
6*7f72497eSEd Maste  * Permission is hereby granted, free of charge, to any person obtaining
7*7f72497eSEd Maste  * a copy of this software and associated documentation files (the
8*7f72497eSEd Maste  * "Software"), to deal in the Software without restriction, including
9*7f72497eSEd Maste  * without limitation the rights to use, copy, modify, merge, publish,
10*7f72497eSEd Maste  * distribute, sublicense, and/or sell copies of the Software, and to
11*7f72497eSEd Maste  * permit persons to whom the Software is furnished to do so, subject to
12*7f72497eSEd Maste  * the following conditions:
13d902844bSNiclas Zeising  *
14*7f72497eSEd Maste  * The above copyright notice and this permission notice shall be
15*7f72497eSEd Maste  * included in all copies or substantial portions of the Software.
16d902844bSNiclas Zeising  *
17*7f72497eSEd Maste  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18*7f72497eSEd Maste  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19*7f72497eSEd Maste  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20*7f72497eSEd Maste  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21*7f72497eSEd Maste  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22*7f72497eSEd Maste  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23*7f72497eSEd Maste  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24d902844bSNiclas Zeising  */
25*7f72497eSEd Maste #include <limits.h>
26*7f72497eSEd Maste #include <stdint.h>
27d902844bSNiclas Zeising #include <string.h>
28d902844bSNiclas Zeising 
29*7f72497eSEd Maste #define ALIGN (sizeof(size_t))
30*7f72497eSEd Maste #define ONES ((size_t)-1 / UCHAR_MAX)
31*7f72497eSEd Maste #define HIGHS (ONES * (UCHAR_MAX / 2 + 1))
32*7f72497eSEd Maste #define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS)
33d902844bSNiclas Zeising 
34d5cc6f73SCraig Rodrigues char *__strchrnul(const char *, int);
35d5cc6f73SCraig Rodrigues 
36d902844bSNiclas Zeising char *
__strchrnul(const char * s,int c)37*7f72497eSEd Maste __strchrnul(const char *s, int c)
38d902844bSNiclas Zeising {
39*7f72497eSEd Maste 	c = (unsigned char)c;
40*7f72497eSEd Maste 	if (!c)
41*7f72497eSEd Maste 		return (char *)s + strlen(s);
42d902844bSNiclas Zeising 
43*7f72497eSEd Maste #ifdef __GNUC__
44*7f72497eSEd Maste 	typedef size_t __attribute__((__may_alias__)) word;
45*7f72497eSEd Maste 	const word *w;
46*7f72497eSEd Maste 	for (; (uintptr_t)s % ALIGN; s++)
47*7f72497eSEd Maste 		if (!*s || *(unsigned char *)s == c)
48*7f72497eSEd Maste 			return (char *)s;
49*7f72497eSEd Maste 	size_t k = ONES * c;
50*7f72497eSEd Maste 	for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w ^ k); w++)
51*7f72497eSEd Maste 		;
52*7f72497eSEd Maste 	s = (void *)w;
53*7f72497eSEd Maste #endif
54*7f72497eSEd Maste 	for (; *s && *(unsigned char *)s != c; s++)
55*7f72497eSEd Maste 		;
56*7f72497eSEd Maste 	return (char *)s;
57d902844bSNiclas Zeising }
58d902844bSNiclas Zeising 
59*7f72497eSEd Maste __weak_reference(__strchrnul, strchrnul);
60