about summary refs log tree commit diff
path: root/string/strpbrk.c
blob: 01cd55afdf0d3f065338d061404a99198f307ee0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Copyright (C) 1991-2015 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   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.

   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.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <string.h>
#include <stdint.h>
#undef strpbrk
#undef strcspn


#ifdef AS_STRCSPN
# ifndef STRPBRK
#  define STRPBRK strcspn
# endif
# define RETURN_TYPE size_t
# define RETURN(c) return c
#else
# define RETURN_TYPE char *
# define RETURN(c) return (char *) (s[c] != '\0' ? s + c : NULL)
#endif

#ifndef STRPBRK
#define STRPBRK strpbrk
#endif


/* Find the first occurrence in S of any character in ACCEPT.  */
RETURN_TYPE
STRPBRK (const char *_s, const char *_accept)
{
  unsigned char *s = (unsigned char *) _s;
  unsigned char *a = (unsigned char *) _accept;

#ifndef LATE_CHECK
  /* We need to align s to 4 bytes. We do check now to avoid expensive table
     construction.  */
  do
    {
      if (s[0] == *a)
        RETURN(0);
    }
  while (*a++);
  a = (unsigned char *) _accept;

  /* We couldn't do these checks in one loop as gcc
     messes up register allocation.  */
  do
    {
      if (s[1] == *a)
        RETURN(1);
    }
  while (*a++);
  a = (unsigned char *) _accept;

  do
    {
      if (s[2] == *a)
        RETURN(2);
    }
  while (*a++);
  a = (unsigned char *) _accept;

  do
    {
      if (s[3] == *a)
        RETURN(3);
    }
  while (*a++);
  a = (unsigned char *) _accept;

#endif

  unsigned char table[256];
  memset (table, 0, 256);
  do
    {
      table[*a] = 1;
    }
  while (*a++);
  unsigned char s0, s1, s2, s3;
  size_t count = 0;
#ifdef LATE_CHECK
  s0 = s[count + 0];
  s1 = s[count + 1];
  s2 = s[count + 2];
  s3 = s[count + 3];
  if (table[s0])
    goto ret0;
  if (table[s1])
    goto ret1;
  if (table[s2])
    goto ret2;
  if (table[s3])
    goto ret3;

#endif

  count = 4 - ((uintptr_t) s) % 4;

  while (1)
    {
      s0 = s[count + 0];
      s1 = s[count + 1];
      s2 = s[count + 2];
      s3 = s[count + 3];
      if (table[s0])
        goto ret0;
      if (table[s1])
        goto ret1;
      if (table[s2])
        goto ret2;
      if (table[s3])
        goto ret3;
      count += 4;
    }
  ret3:
  count++;
  ret2:
  count++;
  ret1:
  count++;
  ret0:
  RETURN(count);
}

libc_hidden_builtin_def (STRPBRK)