]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 1991-2018 Free Software Foundation, Inc. |
c84142e8 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
c84142e8 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
c84142e8 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
28f540f4 | 18 | #include <string.h> |
91f3b75f | 19 | #include <stdint.h> |
9090848d | 20 | #include <libc-pointer-arith.h> |
28f540f4 | 21 | |
9a0a462c | 22 | #undef strspn |
e65caf1f | 23 | #ifndef STRSPN |
91f3b75f | 24 | # define STRSPN strspn |
e65caf1f | 25 | #endif |
28f540f4 RM |
26 | |
27 | /* Return the length of the maximum initial segment | |
28 | of S which contains only characters in ACCEPT. */ | |
29 | size_t | |
91f3b75f | 30 | STRSPN (const char *str, const char *accept) |
28f540f4 | 31 | { |
91f3b75f AZ |
32 | if (accept[0] == '\0') |
33 | return 0; | |
34 | if (__glibc_unlikely (accept[1] == '\0')) | |
28f540f4 | 35 | { |
91f3b75f AZ |
36 | const char *a = str; |
37 | for (; *str == *accept; str++); | |
38 | return str - a; | |
28f540f4 RM |
39 | } |
40 | ||
91f3b75f AZ |
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); | |
47 | ||
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. */ | |
52 | do | |
53 | p[*s++] = 1; | |
54 | while (*s); | |
55 | ||
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; | |
61 | ||
2e51bc38 AZ |
62 | s = (unsigned char *) PTR_ALIGN_DOWN (s, 4); |
63 | ||
91f3b75f AZ |
64 | unsigned int c0, c1, c2, c3; |
65 | do { | |
66 | s += 4; | |
67 | c0 = p[s[0]]; | |
68 | c1 = p[s[1]]; | |
69 | c2 = p[s[2]]; | |
70 | c3 = p[s[3]]; | |
71 | } while ((c0 & c1 & c2 & c3) != 0); | |
72 | ||
73 | size_t count = s - (unsigned char *) str; | |
74 | return (c0 & c1) == 0 ? count + c0 : count + c2 + 2; | |
28f540f4 | 75 | } |
85dd1003 | 76 | libc_hidden_builtin_def (strspn) |