1 /* Copyright (C) 1991-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
20 #include <libc-pointer-arith.h>
25 # define STRCSPN strcspn
28 /* Return the length of the maximum initial segment of S
29 which contains no characters from REJECT. */
31 STRCSPN (const char *str
, const char *reject
)
33 if (__glibc_unlikely (reject
[0] == '\0') ||
34 __glibc_unlikely (reject
[1] == '\0'))
35 return __strchrnul (str
, reject
[0]) - str
;
37 /* Use multiple small memsets to enable inlining on most targets. */
38 unsigned char table
[256];
39 unsigned char *p
= memset (table
, 0, 64);
40 memset (p
+ 64, 0, 64);
41 memset (p
+ 128, 0, 64);
42 memset (p
+ 192, 0, 64);
44 unsigned char *s
= (unsigned char*) reject
;
50 s
= (unsigned char*) str
;
51 if (p
[s
[0]]) return 0;
52 if (p
[s
[1]]) return 1;
53 if (p
[s
[2]]) return 2;
54 if (p
[s
[3]]) return 3;
56 s
= (unsigned char *) PTR_ALIGN_DOWN (s
, 4);
58 unsigned int c0
, c1
, c2
, c3
;
67 while ((c0
| c1
| c2
| c3
) == 0);
69 size_t count
= s
- (unsigned char *) str
;
70 return (c0
| c1
) != 0 ? count
- c0
+ 1 : count
- c2
+ 3;
72 libc_hidden_builtin_def (strcspn
)