Add casts to allow void * arguments.
[kopensolaris-gnu/glibc.git] / string / bits / string2.h
1 /* Machine-independant string function optimizations.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #ifndef _STRING_H
22 # error "Never use <bits/string2.h> directly; include <string.h> instead."
23 #endif
24
25 #ifndef __NO_STRING_INLINES
26
27 /* Unlike the definitions in the header <bits/string.h> the
28    definitions contained here are not optimizing down to assembler
29    level.  These optimizations are not always a good idea since this
30    means the code size increases a lot.  Instead the definitions here
31    optimize some functions in a way which does not dramatically
32    increase the code size and which does not use assembler.  The main
33    trick is to use GNU CC's `__builtin_constant_p' function.
34
35    Every function XXX which has a defined version in
36    <bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
37    to make sure we don't get redefinitions.
38
39    We must use here macros instead of inline functions since the
40    trick won't work with the later.  */
41
42 #ifdef __cplusplus
43 # define __STRING_INLINE inline
44 #else
45 # define __STRING_INLINE extern __inline
46 #endif
47
48 #if _STRING_ARCH_unaligned
49 /* If we can do unaligned memory accesses we must know the endianess.  */
50 # include <endian.h>
51 # include <bits/types.h>
52
53 # if __BYTE_ORDER == __LITTLE_ENDIAN
54 #  define __STRING2_SMALL_GET16(src, idx) \
55      (((__const unsigned char *) (src))[idx + 1] << 8                         \
56       | ((__const unsigned char *) (src))[idx])
57 #  define __STRING2_SMALL_GET32(src, idx) \
58      (((((__const unsigned char *) (src))[idx + 3] << 8                       \
59         | ((__const char *) (src))[idx + 2]) << 8                             \
60        | ((__const unsigned char *) (src))[idx + 1]) << 8                     \
61       | ((__const unsigned char *) (src))[idx])
62 # else
63 #  define __STRING2_SMALL_GET16(src, idx) \
64      (((__const unsigned char *) (src))[idx] << 8                             \
65       | ((__const unsigned char *) (src))[idx + 1])
66 #  define __STRING2_SMALL_GET32(src, idx) \
67      (((((__const unsigned char *) (src))[idx] << 8                           \
68         | ((__const unsigned char *) (src))[idx + 1]) << 8                    \
69        | ((__const unsigned char *) (src))[idx + 2]) << 8                     \
70       | ((__const unsigned char *) (src))[idx + 3])
71 # endif
72 #else
73 /* These are a few types we need for the optimizations if we cannot
74    use unaligned memory accesses.  */
75 # define __STRING2_COPY_TYPE(N) \
76   typedef struct { char __arr[N]; }                                           \
77     __STRING2_COPY_ARR##N __attribute__ ((packed))
78 __STRING2_COPY_TYPE (2);
79 __STRING2_COPY_TYPE (3);
80 __STRING2_COPY_TYPE (4);
81 __STRING2_COPY_TYPE (5);
82 __STRING2_COPY_TYPE (6);
83 __STRING2_COPY_TYPE (7);
84 __STRING2_COPY_TYPE (8);
85 # undef __STRING2_COPY_TYPE
86 #endif
87
88 /* Dereferencing a pointer arg to run sizeof on it fails for the
89    void pointer case, so we use this instead.  */
90 #define __string2_1bptr_p(x) (((size_t) ((x) + 1) - (size_t) (x)) == 1)
91
92
93 /* Set N bytes of S to C.  */
94 #ifndef _HAVE_STRING_ARCH_memset
95 # define memset(s, c, n) \
96   (__extension__ (__builtin_constant_p (c) && (c) == '\0'                     \
97                   ? ({ void *__s = (s); __bzero (__s, n); __s; })             \
98                   : memset (s, c, n)))
99 #endif
100
101
102 /* Copy SRC to DEST.  */
103 #ifndef _HAVE_STRING_ARCH_strcpy
104 # define strcpy(dest, src) \
105   (__extension__ (__builtin_constant_p (src)                                  \
106                   ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8         \
107                      ? __strcpy_small (dest, src, strlen (src) + 1)           \
108                      : (char *) memcpy (dest, src, strlen (src) + 1))         \
109                   : strcpy (dest, src)))
110
111 # if _STRING_ARCH_unaligned
112 #  define __strcpy_small(dest, src, srclen) \
113   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
114                     switch (srclen)                                           \
115                       {                                                       \
116                       case 1:                                                 \
117                         *__dest = '\0';                                       \
118                         break;                                                \
119                       case 2:                                                 \
120                         *((__uint16_t *) __dest) =                            \
121                           __STRING2_SMALL_GET16 (src, 0);                     \
122                         break;                                                \
123                       case 3:                                                 \
124                         *((__uint16_t *) __dest) =                            \
125                           __STRING2_SMALL_GET16 (src, 0);                     \
126                         *(__dest + 2) = '\0';                                 \
127                         break;                                                \
128                       case 4:                                                 \
129                         *((__uint32_t *) __dest) =                            \
130                           __STRING2_SMALL_GET32 (src, 0);                     \
131                         break;                                                \
132                       case 5:                                                 \
133                         *((__uint32_t *) __dest) =                            \
134                           __STRING2_SMALL_GET32 (src, 0);                     \
135                         *(__dest + 4) = '\0';                                 \
136                         break;                                                \
137                       case 6:                                                 \
138                         *((__uint32_t *) __dest) =                            \
139                           __STRING2_SMALL_GET32 (src, 0);                     \
140                         *((__uint16_t *) (__dest + 4)) =                      \
141                           __STRING2_SMALL_GET16 (src, 4);                     \
142                         break;                                                \
143                       case 7:                                                 \
144                         *((__uint32_t *) __dest) =                            \
145                           __STRING2_SMALL_GET32 (src, 0);                     \
146                         *((__uint16_t *) (__dest + 4)) =                      \
147                           __STRING2_SMALL_GET16 (src, 4);                     \
148                         *(__dest + 6) = '\0';                                 \
149                         break;                                                \
150                       case 8:                                                 \
151                         *((__uint32_t *) __dest) =                            \
152                           __STRING2_SMALL_GET32 (src, 0);                     \
153                         *((__uint32_t *) (__dest + 4)) =                      \
154                           __STRING2_SMALL_GET32 (src, 4);                     \
155                         break;                                                \
156                       }                                                       \
157                     (char *) __dest; }))
158 # else
159 #  define __strcpy_small(dest, src, srclen) \
160   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
161                     switch (srclen)                                           \
162                       {                                                       \
163                       case 1:                                                 \
164                         *__dest = '\0';                                       \
165                         break;                                                \
166                       case 2:                                                 \
167                         *((__STRING2_COPY_ARR2 *) __dest) =                   \
168                           ((__STRING2_COPY_ARR2)                              \
169                            { { ((__const unsigned char *) (src))[0],          \
170                                '\0' } });                                     \
171                         break;                                                \
172                       case 3:                                                 \
173                         *((__STRING2_COPY_ARR3 *) __dest) =                   \
174                           ((__STRING2_COPY_ARR3)                              \
175                            { { ((__const unsigned char *) (src))[0],          \
176                                ((__const unsigned char *) (src))[1],          \
177                                '\0' } });                                     \
178                         break;                                                \
179                       case 4:                                                 \
180                         *((__STRING2_COPY_ARR4 *) __dest) =                   \
181                           ((__STRING2_COPY_ARR4)                              \
182                            { { ((__const unsigned char *) (src))[0],          \
183                                ((__const unsigned char *) (src))[1],          \
184                                ((__const unsigned char *) (src))[2],          \
185                                '\0' } });                                     \
186                         break;                                                \
187                       case 5:                                                 \
188                         *((__STRING2_COPY_ARR5 *) __dest) =                   \
189                           ((__STRING2_COPY_ARR5)                              \
190                            { { ((__const unsigned char *) (src))[0],          \
191                                ((__const unsigned char *) (src))[1],          \
192                                ((__const unsigned char *) (src))[2],          \
193                                ((__const unsigned char *) (src))[3],          \
194                                '\0' } });                                     \
195                         break;                                                \
196                       case 6:                                                 \
197                         *((__STRING2_COPY_ARR6 *) __dest) =                   \
198                           ((__STRING2_COPY_ARR6)                              \
199                            { { ((__const unsigned char *) (src))[0],          \
200                                ((__const unsigned char *) (src))[1],          \
201                                ((__const unsigned char *) (src))[2],          \
202                                ((__const unsigned char *) (src))[3],          \
203                                ((__const unsigned char *) (src))[4],          \
204                                '\0' } });                                     \
205                         break;                                                \
206                       case 7:                                                 \
207                         *((__STRING2_COPY_ARR7 *) __dest) =                   \
208                           ((__STRING2_COPY_ARR7)                              \
209                            { { ((__const unsigned char *) (src))[0],          \
210                                ((__const unsigned char *) (src))[1],          \
211                                ((__const unsigned char *) (src))[2],          \
212                                ((__const unsigned char *) (src))[3],          \
213                                ((__const unsigned char *) (src))[4],          \
214                                ((__const unsigned char *) (src))[5],          \
215                                '\0' } });                                     \
216                         break;                                                \
217                       case 8:                                                 \
218                         *((__STRING2_COPY_ARR8 *) __dest) =                   \
219                           ((__STRING2_COPY_ARR8)                              \
220                            { { ((__const unsigned char *) (src))[0],          \
221                                ((__const unsigned char *) (src))[1],          \
222                                ((__const unsigned char *) (src))[2],          \
223                                ((__const unsigned char *) (src))[3],          \
224                                ((__const unsigned char *) (src))[4],          \
225                                ((__const unsigned char *) (src))[5],          \
226                                ((__const unsigned char *) (src))[6],          \
227                                '\0' } });                                     \
228                         break;                                                \
229                     }                                                         \
230                   (char *) __dest; }))
231 # endif
232 #endif
233
234
235 /* Copy SRC to DEST, returning pointer to final NUL byte.  */
236 #ifdef __USE_GNU
237 # ifndef _HAVE_STRING_ARCH_stpcpy
238 #  define __stpcpy(dest, src) \
239   (__extension__ (__builtin_constant_p (src)                                  \
240                   ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8         \
241                      ? __stpcpy_small (dest, src, strlen (src) + 1)           \
242                      : ((char *) __mempcpy (dest, src, strlen (src) + 1) - 1))\
243                   : __stpcpy (dest, src)))
244 /* In glibc we use this function frequently but for namespace reasons
245    we have to use the name `__stpcpy'.  */
246 #  define stpcpy(dest, src) __stpcpy (dest, src)
247
248 #  if _STRING_ARCH_unaligned
249 #   define __stpcpy_small(dest, src, srclen) \
250   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
251                     switch (srclen)                                           \
252                       {                                                       \
253                       case 1:                                                 \
254                         *__dest = '\0';                                       \
255                         break;                                                \
256                       case 2:                                                 \
257                         *((__uint16_t *) __dest) =                            \
258                           __STRING2_SMALL_GET16 (src, 0);                     \
259                         ++__dest;                                             \
260                         break;                                                \
261                       case 3:                                                 \
262                         *((__uint16_t *) __dest) =                            \
263                           __STRING2_SMALL_GET16 (src, 0);                     \
264                         __dest += sizeof (__uint16_t);                        \
265                         *__dest = '\0';                                       \
266                         break;                                                \
267                       case 4:                                                 \
268                         *((__uint32_t *) __dest) =                            \
269                           __STRING2_SMALL_GET32 (src, 0);                     \
270                         __dest += 3;                                          \
271                         break;                                                \
272                       case 5:                                                 \
273                         *((__uint32_t *) __dest) =                            \
274                           __STRING2_SMALL_GET32 (src, 0);                     \
275                         __dest += sizeof (__uint32_t);                        \
276                         *__dest = '\0';                                       \
277                         break;                                                \
278                       case 6:                                                 \
279                         *((__uint32_t *) __dest) =                            \
280                           __STRING2_SMALL_GET32 (src, 0);                     \
281                         *((__uint16_t *) (__dest + 4)) =                      \
282                           __STRING2_SMALL_GET16 (src, 4);                     \
283                         __dest += 5;                                          \
284                         break;                                                \
285                       case 7:                                                 \
286                         *((__uint32_t *) __dest) =                            \
287                           __STRING2_SMALL_GET32 (src, 0);                     \
288                         *((__uint16_t *) (__dest + 4)) =                      \
289                           __STRING2_SMALL_GET16 (src, 4);                     \
290                         __dest += 6;                                          \
291                         *__dest = '\0';                                       \
292                         break;                                                \
293                       case 8:                                                 \
294                         *((__uint32_t *) __dest) =                            \
295                           __STRING2_SMALL_GET32 (src, 0);                     \
296                         *((__uint32_t *) (__dest + 4)) =                      \
297                           __STRING2_SMALL_GET32 (src, 4);                     \
298                         __dest += 7;                                          \
299                         break;                                                \
300                       }                                                       \
301                     (char *) __dest; }))
302 #  else
303 #   define __stpcpy_small(dest, src, srclen) \
304   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
305                     switch (srclen)                                           \
306                       {                                                       \
307                       case 1:                                                 \
308                         *__dest = '\0';                                       \
309                         break;                                                \
310                       case 2:                                                 \
311                         *((__STRING2_COPY_ARR2 *) __dest) =                   \
312                           ((__STRING2_COPY_ARR2)                              \
313                            { { ((__const unsigned char *) (src))[0],          \
314                                '\0' } });                                     \
315                         break;                                                \
316                       case 3:                                                 \
317                         *((__STRING2_COPY_ARR3 *) __dest) =                   \
318                           ((__STRING2_COPY_ARR3)                              \
319                            { { ((__const unsigned char *) (src))[0],          \
320                                ((__const unsigned char *) (src))[1],          \
321                                '\0' } });                                     \
322                         break;                                                \
323                       case 4:                                                 \
324                         *((__STRING2_COPY_ARR4 *) __dest) =                   \
325                           ((__STRING2_COPY_ARR4)                              \
326                            { { ((__const unsigned char *) (src))[0],          \
327                                ((__const unsigned char *) (src))[1],          \
328                                ((__const unsigned char *) (src))[2],          \
329                                '\0' } });                                     \
330                         break;                                                \
331                       case 5:                                                 \
332                         *((__STRING2_COPY_ARR5 *) __dest) =                   \
333                           ((__STRING2_COPY_ARR5)                              \
334                            { { ((__const unsigned char *) (src))[0],          \
335                                ((__const unsigned char *) (src))[1],          \
336                                ((__const unsigned char *) (src))[2],          \
337                                ((__const unsigned char *) (src))[3],          \
338                                '\0' } });                                     \
339                         break;                                                \
340                       case 6:                                                 \
341                         *((__STRING2_COPY_ARR6 *) __dest) =                   \
342                           ((__STRING2_COPY_ARR6)                              \
343                            { { ((__const unsigned char *) (src))[0],          \
344                                ((__const unsigned char *) (src))[1],          \
345                                ((__const unsigned char *) (src))[2],          \
346                                ((__const unsigned char *) (src))[3],          \
347                                ((__const unsigned char *) (src))[4],          \
348                                '\0' } });                                     \
349                         break;                                                \
350                       case 7:                                                 \
351                         *((__STRING2_COPY_ARR7 *) __dest) =                   \
352                           ((__STRING2_COPY_ARR7)                              \
353                            { { ((__const unsigned char *) (src))[0],          \
354                                ((__const unsigned char *) (src))[1],          \
355                                ((__const unsigned char *) (src))[2],          \
356                                ((__const unsigned char *) (src))[3],          \
357                                ((__const unsigned char *) (src))[4],          \
358                                ((__const unsigned char *) (src))[5],          \
359                                '\0' } });                                     \
360                         break;                                                \
361                       case 8:                                                 \
362                         *((__STRING2_COPY_ARR8 *) __dest) =                   \
363                           ((__STRING2_COPY_ARR8)                              \
364                            { { ((__const unsigned char *) (src))[0],          \
365                                ((__const unsigned char *) (src))[1],          \
366                                ((__const unsigned char *) (src))[2],          \
367                                ((__const unsigned char *) (src))[3],          \
368                                ((__const unsigned char *) (src))[4],          \
369                                ((__const unsigned char *) (src))[5],          \
370                                ((__const unsigned char *) (src))[6],          \
371                                '\0' } });                                     \
372                         break;                                                \
373                     }                                                         \
374                   (char *) (__dest + ((srclen) - 1)); }))
375 #  endif
376 # endif
377 #endif
378
379
380 /* Copy no more than N characters of SRC to DEST.  */
381 #ifndef _HAVE_STRING_ARCH_strncpy
382 # if defined _HAVE_STRING_ARCH_memset && defined _HAVE_STRING_ARCH_mempcpy
383 #  define strncpy(dest, src, n) \
384   (__extension__ ({ char *__dest = (dest);                                    \
385                     __builtin_constant_p (src) && __builtin_constant_p (n)    \
386                     ? (strlen (src) + 1 >= ((size_t) (n))                     \
387                        ? (char *) memcpy (__dest, src, n)                     \
388                        : (memset (__mempcpy (__dest, src, strlen (src)),      \
389                                   '\0', n - strlen (src)),                    \
390                           __dest))                                            \
391                     : strncpy (__dest, src, n); }))
392 # else
393 #  define strncpy(dest, src, n) \
394   (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
395                   ? (strlen (src) + 1 >= ((size_t) (n))                       \
396                      ? (char *) memcpy (dest, src, n)                         \
397                      : strncpy (dest, src, n))                                \
398                   : strncpy (dest, src, n)))
399 # endif
400 #endif
401
402
403 /* Append no more than N characters from SRC onto DEST.  */
404 #ifndef _HAVE_STRING_ARCH_strncat
405 # ifdef _HAVE_STRING_ARCH_strchr
406 #  define strncat(dest, src, n) \
407   (__extension__ ({ char *__dest = (dest);                                    \
408                     __builtin_constant_p (src) && __builtin_constant_p (n)    \
409                     ? (strlen (src) < ((size_t) (n))                          \
410                        ? strcat (__dest, src)                                 \
411                        : (memcpy (strchr (__dest, '\0'), src, n), __dest))    \
412                     : strncat (dest, src, n); }))
413 # else
414 #  define strncat(dest, src, n) \
415   (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
416                   ? (strlen (src) < ((size_t) (n))                            \
417                      ? strcat (dest, src)                                     \
418                      : strncat (dest, src, n))                                \
419                   : strncat (dest, src, n)))
420 # endif
421 #endif
422
423
424 /* Compare characters of S1 and S2.  */
425 #ifndef _HAVE_STRING_ARCH_strcmp
426 # define strcmp(s1, s2) \
427   (__extension__ (__builtin_constant_p (s1) && __builtin_constant_p (s2)      \
428                   && (!__string2_1bptr_p (s1) || strlen (s1) >= 4)            \
429                   && (!__string2_1bptr_p (s2) || strlen (s2) >= 4)            \
430                   ? memcmp (s1, s2, (strlen (s1) < strlen (s2)                \
431                                      ? strlen (s1) : strlen (s2)) + 1)        \
432                   : (__builtin_constant_p (s1) && __string2_1bptr_p (s1)      \
433                      && __string2_1bptr_p (s2) && strlen (s1) < 4             \
434                      ? (__builtin_constant_p (s2)                             \
435                         ? __strcmp_cc (s1, s2, strlen (s1))                   \
436                         : __strcmp_cg (s1, s2, strlen (s1)))                  \
437                      : (__builtin_constant_p (s2) && __string2_1bptr_p (s1)   \
438                         && __string2_1bptr_p (s2) && strlen (s2) < 4          \
439                         ? (__builtin_constant_p (s1)                          \
440                            ? __strcmp_cc (s1, s2, strlen (s2))                \
441                            : __strcmp_gc (s1, s2, strlen (s2)))               \
442                         : strcmp (s1, s2)))))
443
444 # define __strcmp_cc(s1, s2, l) \
445   (__extension__ ({ register int __result =                                   \
446                       (((__const unsigned char *) (s1))[0]                    \
447                        - ((__const unsigned char *) (s2))[0]);                \
448                     if (l > 0 && __result == 0)                               \
449                       {                                                       \
450                         __result = (((__const unsigned char *) (s1))[1]       \
451                                     - ((__const unsigned char *) (s2))[1]);   \
452                         if (l > 1 && __result == 0)                           \
453                           {                                                   \
454                             __result =                                        \
455                               (((__const unsigned char *) (s1))[2]            \
456                                - ((__const unsigned char *) (s2))[2]);        \
457                             if (l > 2 && __result == 0)                       \
458                               __result =                                      \
459                                 (((__const unsigned char *) (s1))[3]          \
460                                  - ((__const unsigned char *) (s2))[3]);      \
461                           }                                                   \
462                       }                                                       \
463                     __result; }))
464
465 # define __strcmp_cg(s1, s2, l1) \
466   (__extension__ ({ __const unsigned char *__s2 =                             \
467                       (__const unsigned char *) (s2);                         \
468                     register int __result =                                   \
469                       (((__const unsigned char *) (s1))[0] - __s2[0]);        \
470                     if (l1 > 0 && __result == 0)                              \
471                       {                                                       \
472                         __result = (((__const unsigned char *) (s1))[1]       \
473                                     - __s2[1]);                               \
474                         if (l1 > 1 && __result == 0)                          \
475                           {                                                   \
476                             __result = (((__const unsigned char *) (s1))[2]   \
477                                         - __s2[2]);                           \
478                             if (l1 > 2 && __result == 0)                      \
479                               __result = (((__const unsigned char *) (s1))[3] \
480                                           - __s2[3]);                         \
481                           }                                                   \
482                       }                                                       \
483                     __result; }))
484
485 # define __strcmp_gc(s1, s2, l2) \
486   (__extension__ ({ __const unsigned char *__s1 =                             \
487                       (__const unsigned char *) (s1);                         \
488                     register int __result =                                   \
489                       __s1[0] - ((__const unsigned char *) (s2))[0];          \
490                     if (l2 > 0 && __result == 0)                              \
491                       {                                                       \
492                         __result = (__s1[1]                                   \
493                                     - ((__const unsigned char *) (s2))[1]);   \
494                         if (l2 > 1 && __result == 0)                          \
495                           {                                                   \
496                             __result =                                        \
497                               (__s1[2] - ((__const unsigned char *) (s2))[2]);\
498                             if (l2 > 2 && __result == 0)                      \
499                               __result =                                      \
500                                 (__s1[3]                                      \
501                                  - ((__const unsigned char *)(s2))[3]);       \
502                           }                                                   \
503                       }                                                       \
504                     __result; }))
505 #endif
506
507
508 /* Compare N characters of S1 and S2.  */
509 #ifndef _HAVE_STRING_ARCH_strncmp
510 # define strncmp(s1, s2, n) \
511   (__extension__ (__builtin_constant_p (s1) && strlen (s1) < ((size_t) (n))   \
512                   ? strcmp (s1, s2)                                           \
513                   : (__builtin_constant_p (s2) && strlen (s2) < ((size_t) (n))\
514                      ? strcmp (s1, s2)                                        \
515                      : strncmp (s1, s2, n))))
516 #endif
517
518
519 /* Return the length of the initial segment of S which
520    consists entirely of characters not in REJECT.  */
521 #ifndef _HAVE_STRING_ARCH_strcspn
522 # define strcspn(s, reject) \
523   (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
524                   ? (((__const unsigned char *) (reject))[0] == '\0'          \
525                      ? strlen (s)                                             \
526                      : (((__const unsigned char *) (reject))[1] == '\0'       \
527                         ? __strcspn_c1 (s, ((__const char *) (reject))[0])    \
528                         : strcspn (s, reject)))                               \
529                   : strcspn (s, reject)))
530
531 __STRING_INLINE size_t __strcspn_c1 (__const char *__s, char __reject);
532 __STRING_INLINE size_t
533 __strcspn_c1 (__const char *__s, char __reject)
534 {
535   register size_t __result = 0;
536   while (__s[__result] != '\0' && __s[__result] != __reject)
537     ++__result;
538   return __result;
539 }
540 #endif
541
542
543 /* Return the length of the initial segment of S which
544    consists entirely of characters in ACCEPT.  */
545 #ifndef _HAVE_STRING_ARCH_strspn
546 # define strspn(s, accept) \
547   (__extension__ (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
548                   ? (((__const unsigned char *) (accept))[0] == '\0'          \
549                      ? 0                                                      \
550                      : (((__const unsigned char *) (accept))[1] == '\0'       \
551                         ? __strspn_c1 (s, ((__const char *) (accept))[0])     \
552                         : strspn (s, accept)))                                \
553                   : strspn (s, accept)))
554
555 __STRING_INLINE size_t __strspn_c1 (__const char *__s, char __accept);
556 __STRING_INLINE size_t
557 __strspn_c1 (__const char *__s, char __accept)
558 {
559   register size_t __result = 0;
560   /* Please note that __accept never can be '\0'.  */
561   while (__s[__result] == __accept)
562     ++__result;
563   return __result;
564 }
565 #endif
566
567
568 /* Find the first occurrence in S of any character in ACCEPT.  */
569 #ifndef _HAVE_STRING_ARCH_strpbrk
570 # define strpbrk(s, accept) \
571   (__extension__ (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
572                   ? (((__const unsigned char  *) (accept))[0] == '\0'         \
573                      ? NULL                                                   \
574                      : (((__const unsigned char *) (accept))[1] == '\0'       \
575                         ? strchr (s, ((__const unsigned char *) (accept))[0]) \
576                         : strpbrk (s, accept)))                               \
577                   : strpbrk (s, accept)))
578 #endif
579
580
581 /* Find the first occurrence of NEEDLE in HAYSTACK.  */
582 #ifndef _HAVE_STRING_ARCH_strstr
583 # define strstr(haystack, needle) \
584   (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
585                   ? (((__const unsigned char *) (needle))[0] == '\0'          \
586                      ? haystack                                               \
587                      : (((__const unsigned char *) (needle))[1] == '\0'       \
588                         ? strchr (haystack,                                   \
589                                   ((__const unsigned char *) (needle))[0])    \
590                         : strstr (haystack, needle)))                         \
591                   : strstr (haystack, needle)))
592 #endif
593
594
595 #ifdef __USE_GNU
596 # ifndef _HAVE_STRING_ARCH_strnlen
597 __STRING_INLINE size_t strnlen (__const char *__string, size_t __maxlen);
598 __STRING_INLINE size_t
599 strnlen (__const char *__string, size_t __maxlen)
600 {
601   __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
602   return __end ? __end - __string : __maxlen;
603 }
604 # endif
605 #endif
606
607
608 #ifdef __USE_BSD
609 # ifndef _HAVE_STRING_ARCH_strsep
610
611 #  define strsep(s, reject) \
612   (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
613                   ? (((__const unsigned char *) (reject))[0] != '\0'          \
614                      && ((__const unsigned char *) (reject))[1] == '\0'       \
615                      ? __strsep_1c (s,                                        \
616                                     ((__const unsigned char *) (reject))[0])  \
617                      : __strsep_g (s, reject))                                \
618                   : __strsep_g (s, reject)))
619
620 __STRING_INLINE char *__strsep_1c (char **__s, char __reject);
621 __STRING_INLINE char *
622 __strsep_1c (char **__s, char __reject)
623 {
624   register char *__retval = *__s;
625   if (__retval == NULL || *__retval == '\0')
626     return NULL;
627   while (*__retval == __reject)
628     ++__retval;
629   if ((*__s = strchr (__retval, __reject)) != NULL)
630     *(*__s)++ = '\0';
631   return __retval;
632 }
633
634 __STRING_INLINE char *__strsep_g (char **__s, __const char *__reject);
635 __STRING_INLINE char *
636 __strsep_g (char **__s, __const char *__reject)
637 {
638   register char *__retval = *__s;
639   if (__retval == NULL || *__retval == '\0')
640     return NULL;
641   if ((*__s = strpbrk (__retval, __reject)) != NULL)
642     *(*__s)++ = '\0';
643   return __retval;
644 }
645 # endif
646 #endif
647
648
649 #undef __STRING_INLINE
650
651 #endif /* No string inlines.  */