1 /* Copyright (C) 1991-2017 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>
24 # define STRSPN strspn
27 /* Return the length of the maximum initial segment
28 of S which contains only characters in ACCEPT. */
30 STRSPN (const char *str
, const char *accept
)
32 if (accept
[0] == '\0')
34 if (__glibc_unlikely (accept
[1] == '\0'))
37 for (; *str
== *accept
; str
++);
41 /* Use multiple small memsets to enable inlining on most targets. */
42 unsigned char table
[256];
43 unsigned char *p
= memset (table
, 0, 64);
44 memset (p
+ 64, 0, 64);
45 memset (p
+ 128, 0, 64);
46 memset (p
+ 192, 0, 64);
48 unsigned char *s
= (unsigned char*) accept
;
49 /* Different from strcspn it does not add the NULL on the table
50 so can avoid check if str[i] is NULL, since table['\0'] will
51 be 0 and thus stopping the loop check. */
56 s
= (unsigned char*) str
;
57 if (!p
[s
[0]]) return 0;
58 if (!p
[s
[1]]) return 1;
59 if (!p
[s
[2]]) return 2;
60 if (!p
[s
[3]]) return 3;
62 s
= (unsigned char *) PTR_ALIGN_DOWN (s
, 4);
64 unsigned int c0
, c1
, c2
, c3
;
71 } while ((c0
& c1
& c2
& c3
) != 0);
73 size_t count
= s
- (unsigned char *) str
;
74 return (c0
& c1
) == 0 ? count
+ c0
: count
+ c2
+ 2;
76 libc_hidden_builtin_def (strspn
)