(__strsep_g): Little cleanup.
[kopensolaris-gnu/glibc.git] / string / bits / string2.h
1 /* Machine-independant string function optimizations.
2    Copyright (C) 1997, 1998 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 optimized down to assembler
29    level.  Those 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 do not dramatically
32    increase the code size and which do 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.  Note that the argument
90    must not contain any side effects.  */
91 #define __string2_1bptr_p(x) (((size_t) ((x) + 1) - (size_t) (x)) == 1)
92
93
94 /* Set N bytes of S to C.  */
95 #ifndef _HAVE_STRING_ARCH_memset
96 # define memset(s, c, n) \
97   (__extension__ (__builtin_constant_p (c) && (c) == '\0'                     \
98                   ? ({ void *__s = (s); __bzero (__s, n); __s; })             \
99                   : memset (s, c, n)))
100 #endif
101
102
103 /* Copy SRC to DEST.  */
104 #ifndef _HAVE_STRING_ARCH_strcpy
105 # define strcpy(dest, src) \
106   (__extension__ (__builtin_constant_p (src)                                  \
107                   ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8         \
108                      ? __strcpy_small (dest, src, strlen (src) + 1)           \
109                      : (char *) memcpy (dest, src, strlen (src) + 1))         \
110                   : strcpy (dest, src)))
111
112 # if _STRING_ARCH_unaligned
113 #  define __strcpy_small(dest, src, srclen) \
114   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
115                     switch (srclen)                                           \
116                       {                                                       \
117                       case 1:                                                 \
118                         *__dest = '\0';                                       \
119                         break;                                                \
120                       case 2:                                                 \
121                         *((__uint16_t *) __dest) =                            \
122                           __STRING2_SMALL_GET16 (src, 0);                     \
123                         break;                                                \
124                       case 3:                                                 \
125                         *((__uint16_t *) __dest) =                            \
126                           __STRING2_SMALL_GET16 (src, 0);                     \
127                         *(__dest + 2) = '\0';                                 \
128                         break;                                                \
129                       case 4:                                                 \
130                         *((__uint32_t *) __dest) =                            \
131                           __STRING2_SMALL_GET32 (src, 0);                     \
132                         break;                                                \
133                       case 5:                                                 \
134                         *((__uint32_t *) __dest) =                            \
135                           __STRING2_SMALL_GET32 (src, 0);                     \
136                         *(__dest + 4) = '\0';                                 \
137                         break;                                                \
138                       case 6:                                                 \
139                         *((__uint32_t *) __dest) =                            \
140                           __STRING2_SMALL_GET32 (src, 0);                     \
141                         *((__uint16_t *) (__dest + 4)) =                      \
142                           __STRING2_SMALL_GET16 (src, 4);                     \
143                         break;                                                \
144                       case 7:                                                 \
145                         *((__uint32_t *) __dest) =                            \
146                           __STRING2_SMALL_GET32 (src, 0);                     \
147                         *((__uint16_t *) (__dest + 4)) =                      \
148                           __STRING2_SMALL_GET16 (src, 4);                     \
149                         *(__dest + 6) = '\0';                                 \
150                         break;                                                \
151                       case 8:                                                 \
152                         *((__uint32_t *) __dest) =                            \
153                           __STRING2_SMALL_GET32 (src, 0);                     \
154                         *((__uint32_t *) (__dest + 4)) =                      \
155                           __STRING2_SMALL_GET32 (src, 4);                     \
156                         break;                                                \
157                       }                                                       \
158                     (char *) __dest; }))
159 # else
160 #  define __strcpy_small(dest, src, srclen) \
161   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
162                     switch (srclen)                                           \
163                       {                                                       \
164                       case 1:                                                 \
165                         *__dest = '\0';                                       \
166                         break;                                                \
167                       case 2:                                                 \
168                         *((__STRING2_COPY_ARR2 *) __dest) =                   \
169                           ((__STRING2_COPY_ARR2)                              \
170                            { { ((__const unsigned char *) (src))[0],          \
171                                '\0' } });                                     \
172                         break;                                                \
173                       case 3:                                                 \
174                         *((__STRING2_COPY_ARR3 *) __dest) =                   \
175                           ((__STRING2_COPY_ARR3)                              \
176                            { { ((__const unsigned char *) (src))[0],          \
177                                ((__const unsigned char *) (src))[1],          \
178                                '\0' } });                                     \
179                         break;                                                \
180                       case 4:                                                 \
181                         *((__STRING2_COPY_ARR4 *) __dest) =                   \
182                           ((__STRING2_COPY_ARR4)                              \
183                            { { ((__const unsigned char *) (src))[0],          \
184                                ((__const unsigned char *) (src))[1],          \
185                                ((__const unsigned char *) (src))[2],          \
186                                '\0' } });                                     \
187                         break;                                                \
188                       case 5:                                                 \
189                         *((__STRING2_COPY_ARR5 *) __dest) =                   \
190                           ((__STRING2_COPY_ARR5)                              \
191                            { { ((__const unsigned char *) (src))[0],          \
192                                ((__const unsigned char *) (src))[1],          \
193                                ((__const unsigned char *) (src))[2],          \
194                                ((__const unsigned char *) (src))[3],          \
195                                '\0' } });                                     \
196                         break;                                                \
197                       case 6:                                                 \
198                         *((__STRING2_COPY_ARR6 *) __dest) =                   \
199                           ((__STRING2_COPY_ARR6)                              \
200                            { { ((__const unsigned char *) (src))[0],          \
201                                ((__const unsigned char *) (src))[1],          \
202                                ((__const unsigned char *) (src))[2],          \
203                                ((__const unsigned char *) (src))[3],          \
204                                ((__const unsigned char *) (src))[4],          \
205                                '\0' } });                                     \
206                         break;                                                \
207                       case 7:                                                 \
208                         *((__STRING2_COPY_ARR7 *) __dest) =                   \
209                           ((__STRING2_COPY_ARR7)                              \
210                            { { ((__const unsigned char *) (src))[0],          \
211                                ((__const unsigned char *) (src))[1],          \
212                                ((__const unsigned char *) (src))[2],          \
213                                ((__const unsigned char *) (src))[3],          \
214                                ((__const unsigned char *) (src))[4],          \
215                                ((__const unsigned char *) (src))[5],          \
216                                '\0' } });                                     \
217                         break;                                                \
218                       case 8:                                                 \
219                         *((__STRING2_COPY_ARR8 *) __dest) =                   \
220                           ((__STRING2_COPY_ARR8)                              \
221                            { { ((__const unsigned char *) (src))[0],          \
222                                ((__const unsigned char *) (src))[1],          \
223                                ((__const unsigned char *) (src))[2],          \
224                                ((__const unsigned char *) (src))[3],          \
225                                ((__const unsigned char *) (src))[4],          \
226                                ((__const unsigned char *) (src))[5],          \
227                                ((__const unsigned char *) (src))[6],          \
228                                '\0' } });                                     \
229                         break;                                                \
230                     }                                                         \
231                   (char *) __dest; }))
232 # endif
233 #endif
234
235
236 /* Copy SRC to DEST, returning pointer to final NUL byte.  */
237 #ifdef __USE_GNU
238 # ifndef _HAVE_STRING_ARCH_stpcpy
239 #  define __stpcpy(dest, src) \
240   (__extension__ (__builtin_constant_p (src)                                  \
241                   ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8         \
242                      ? __stpcpy_small (dest, src, strlen (src) + 1)           \
243                      : ((char *) __mempcpy (dest, src, strlen (src) + 1) - 1))\
244                   : __stpcpy (dest, src)))
245 /* In glibc we use this function frequently but for namespace reasons
246    we have to use the name `__stpcpy'.  */
247 #  define stpcpy(dest, src) __stpcpy (dest, src)
248
249 #  if _STRING_ARCH_unaligned
250 #   define __stpcpy_small(dest, src, srclen) \
251   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
252                     switch (srclen)                                           \
253                       {                                                       \
254                       case 1:                                                 \
255                         *__dest = '\0';                                       \
256                         break;                                                \
257                       case 2:                                                 \
258                         *((__uint16_t *) __dest) =                            \
259                           __STRING2_SMALL_GET16 (src, 0);                     \
260                         ++__dest;                                             \
261                         break;                                                \
262                       case 3:                                                 \
263                         *((__uint16_t *) __dest) =                            \
264                           __STRING2_SMALL_GET16 (src, 0);                     \
265                         __dest += sizeof (__uint16_t);                        \
266                         *__dest = '\0';                                       \
267                         break;                                                \
268                       case 4:                                                 \
269                         *((__uint32_t *) __dest) =                            \
270                           __STRING2_SMALL_GET32 (src, 0);                     \
271                         __dest += 3;                                          \
272                         break;                                                \
273                       case 5:                                                 \
274                         *((__uint32_t *) __dest) =                            \
275                           __STRING2_SMALL_GET32 (src, 0);                     \
276                         __dest += sizeof (__uint32_t);                        \
277                         *__dest = '\0';                                       \
278                         break;                                                \
279                       case 6:                                                 \
280                         *((__uint32_t *) __dest) =                            \
281                           __STRING2_SMALL_GET32 (src, 0);                     \
282                         *((__uint16_t *) (__dest + 4)) =                      \
283                           __STRING2_SMALL_GET16 (src, 4);                     \
284                         __dest += 5;                                          \
285                         break;                                                \
286                       case 7:                                                 \
287                         *((__uint32_t *) __dest) =                            \
288                           __STRING2_SMALL_GET32 (src, 0);                     \
289                         *((__uint16_t *) (__dest + 4)) =                      \
290                           __STRING2_SMALL_GET16 (src, 4);                     \
291                         __dest += 6;                                          \
292                         *__dest = '\0';                                       \
293                         break;                                                \
294                       case 8:                                                 \
295                         *((__uint32_t *) __dest) =                            \
296                           __STRING2_SMALL_GET32 (src, 0);                     \
297                         *((__uint32_t *) (__dest + 4)) =                      \
298                           __STRING2_SMALL_GET32 (src, 4);                     \
299                         __dest += 7;                                          \
300                         break;                                                \
301                       }                                                       \
302                     (char *) __dest; }))
303 #  else
304 #   define __stpcpy_small(dest, src, srclen) \
305   (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest);         \
306                     switch (srclen)                                           \
307                       {                                                       \
308                       case 1:                                                 \
309                         *__dest = '\0';                                       \
310                         break;                                                \
311                       case 2:                                                 \
312                         *((__STRING2_COPY_ARR2 *) __dest) =                   \
313                           ((__STRING2_COPY_ARR2)                              \
314                            { { ((__const unsigned char *) (src))[0],          \
315                                '\0' } });                                     \
316                         break;                                                \
317                       case 3:                                                 \
318                         *((__STRING2_COPY_ARR3 *) __dest) =                   \
319                           ((__STRING2_COPY_ARR3)                              \
320                            { { ((__const unsigned char *) (src))[0],          \
321                                ((__const unsigned char *) (src))[1],          \
322                                '\0' } });                                     \
323                         break;                                                \
324                       case 4:                                                 \
325                         *((__STRING2_COPY_ARR4 *) __dest) =                   \
326                           ((__STRING2_COPY_ARR4)                              \
327                            { { ((__const unsigned char *) (src))[0],          \
328                                ((__const unsigned char *) (src))[1],          \
329                                ((__const unsigned char *) (src))[2],          \
330                                '\0' } });                                     \
331                         break;                                                \
332                       case 5:                                                 \
333                         *((__STRING2_COPY_ARR5 *) __dest) =                   \
334                           ((__STRING2_COPY_ARR5)                              \
335                            { { ((__const unsigned char *) (src))[0],          \
336                                ((__const unsigned char *) (src))[1],          \
337                                ((__const unsigned char *) (src))[2],          \
338                                ((__const unsigned char *) (src))[3],          \
339                                '\0' } });                                     \
340                         break;                                                \
341                       case 6:                                                 \
342                         *((__STRING2_COPY_ARR6 *) __dest) =                   \
343                           ((__STRING2_COPY_ARR6)                              \
344                            { { ((__const unsigned char *) (src))[0],          \
345                                ((__const unsigned char *) (src))[1],          \
346                                ((__const unsigned char *) (src))[2],          \
347                                ((__const unsigned char *) (src))[3],          \
348                                ((__const unsigned char *) (src))[4],          \
349                                '\0' } });                                     \
350                         break;                                                \
351                       case 7:                                                 \
352                         *((__STRING2_COPY_ARR7 *) __dest) =                   \
353                           ((__STRING2_COPY_ARR7)                              \
354                            { { ((__const unsigned char *) (src))[0],          \
355                                ((__const unsigned char *) (src))[1],          \
356                                ((__const unsigned char *) (src))[2],          \
357                                ((__const unsigned char *) (src))[3],          \
358                                ((__const unsigned char *) (src))[4],          \
359                                ((__const unsigned char *) (src))[5],          \
360                                '\0' } });                                     \
361                         break;                                                \
362                       case 8:                                                 \
363                         *((__STRING2_COPY_ARR8 *) __dest) =                   \
364                           ((__STRING2_COPY_ARR8)                              \
365                            { { ((__const unsigned char *) (src))[0],          \
366                                ((__const unsigned char *) (src))[1],          \
367                                ((__const unsigned char *) (src))[2],          \
368                                ((__const unsigned char *) (src))[3],          \
369                                ((__const unsigned char *) (src))[4],          \
370                                ((__const unsigned char *) (src))[5],          \
371                                ((__const unsigned char *) (src))[6],          \
372                                '\0' } });                                     \
373                         break;                                                \
374                     }                                                         \
375                   (char *) (__dest + ((srclen) - 1)); }))
376 #  endif
377 # endif
378 #endif
379
380
381 /* Copy no more than N characters of SRC to DEST.  */
382 #ifndef _HAVE_STRING_ARCH_strncpy
383 # if defined _HAVE_STRING_ARCH_memset && defined _HAVE_STRING_ARCH_mempcpy
384 #  define strncpy(dest, src, n) \
385   (__extension__ ({ char *__dest = (dest);                                    \
386                     __builtin_constant_p (src) && __builtin_constant_p (n)    \
387                     ? (strlen (src) + 1 >= ((size_t) (n))                     \
388                        ? (char *) memcpy (__dest, src, n)                     \
389                        : (memset (__mempcpy (__dest, src, strlen (src)),      \
390                                   '\0', n - strlen (src)),                    \
391                           __dest))                                            \
392                     : strncpy (__dest, src, n); }))
393 # else
394 #  define strncpy(dest, src, n) \
395   (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
396                   ? (strlen (src) + 1 >= ((size_t) (n))                       \
397                      ? (char *) memcpy (dest, src, n)                         \
398                      : strncpy (dest, src, n))                                \
399                   : strncpy (dest, src, n)))
400 # endif
401 #endif
402
403
404 /* Append no more than N characters from SRC onto DEST.  */
405 #ifndef _HAVE_STRING_ARCH_strncat
406 # ifdef _HAVE_STRING_ARCH_strchr
407 #  define strncat(dest, src, n) \
408   (__extension__ ({ char *__dest = (dest);                                    \
409                     __builtin_constant_p (src) && __builtin_constant_p (n)    \
410                     ? (strlen (src) < ((size_t) (n))                          \
411                        ? strcat (__dest, src)                                 \
412                        : (memcpy (strchr (__dest, '\0'), src, n), __dest))    \
413                     : strncat (dest, src, n); }))
414 # else
415 #  define strncat(dest, src, n) \
416   (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n)      \
417                   ? (strlen (src) < ((size_t) (n))                            \
418                      ? strcat (dest, src)                                     \
419                      : strncat (dest, src, n))                                \
420                   : strncat (dest, src, n)))
421 # endif
422 #endif
423
424
425 /* Compare characters of S1 and S2.  */
426 #ifndef _HAVE_STRING_ARCH_strcmp
427 # define strcmp(s1, s2) \
428   (__extension__ (__builtin_constant_p (s1) && __builtin_constant_p (s2)      \
429                   && (!__string2_1bptr_p (s1) || strlen (s1) >= 4)            \
430                   && (!__string2_1bptr_p (s2) || strlen (s2) >= 4)            \
431                   ? memcmp (s1, s2, (strlen (s1) < strlen (s2)                \
432                                      ? strlen (s1) : strlen (s2)) + 1)        \
433                   : (__builtin_constant_p (s1) && __string2_1bptr_p (s1)      \
434                      && strlen (s1) < 4                                       \
435                      ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2)   \
436                         ? __strcmp_cc (s1, s2, strlen (s1))                   \
437                         : __strcmp_cg (s1, s2, strlen (s1)))                  \
438                      : (__builtin_constant_p (s2) && __string2_1bptr_p (s2)   \
439                         && strlen (s2) < 4                                    \
440                         ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1)\
441                            ? __strcmp_cc (s1, s2, strlen (s2))                \
442                            : __strcmp_gc (s1, s2, strlen (s2)))               \
443                         : strcmp (s1, s2)))))
444
445 # define __strcmp_cc(s1, s2, l) \
446   (__extension__ ({ register int __result =                                   \
447                       (((__const unsigned char *) (s1))[0]                    \
448                        - ((__const unsigned char *) (s2))[0]);                \
449                     if (l > 0 && __result == 0)                               \
450                       {                                                       \
451                         __result = (((__const unsigned char *) (s1))[1]       \
452                                     - ((__const unsigned char *) (s2))[1]);   \
453                         if (l > 1 && __result == 0)                           \
454                           {                                                   \
455                             __result =                                        \
456                               (((__const unsigned char *) (s1))[2]            \
457                                - ((__const unsigned char *) (s2))[2]);        \
458                             if (l > 2 && __result == 0)                       \
459                               __result =                                      \
460                                 (((__const unsigned char *) (s1))[3]          \
461                                  - ((__const unsigned char *) (s2))[3]);      \
462                           }                                                   \
463                       }                                                       \
464                     __result; }))
465
466 # define __strcmp_cg(s1, s2, l1) \
467   (__extension__ ({ __const unsigned char *__s2 =                             \
468                       (__const unsigned char *) (s2);                         \
469                     register int __result =                                   \
470                       (((__const unsigned char *) (s1))[0] - __s2[0]);        \
471                     if (l1 > 0 && __result == 0)                              \
472                       {                                                       \
473                         __result = (((__const unsigned char *) (s1))[1]       \
474                                     - __s2[1]);                               \
475                         if (l1 > 1 && __result == 0)                          \
476                           {                                                   \
477                             __result = (((__const unsigned char *) (s1))[2]   \
478                                         - __s2[2]);                           \
479                             if (l1 > 2 && __result == 0)                      \
480                               __result = (((__const unsigned char *) (s1))[3] \
481                                           - __s2[3]);                         \
482                           }                                                   \
483                       }                                                       \
484                     __result; }))
485
486 # define __strcmp_gc(s1, s2, l2) \
487   (__extension__ ({ __const unsigned char *__s1 =                             \
488                       (__const unsigned char *) (s1);                         \
489                     register int __result =                                   \
490                       __s1[0] - ((__const unsigned char *) (s2))[0];          \
491                     if (l2 > 0 && __result == 0)                              \
492                       {                                                       \
493                         __result = (__s1[1]                                   \
494                                     - ((__const unsigned char *) (s2))[1]);   \
495                         if (l2 > 1 && __result == 0)                          \
496                           {                                                   \
497                             __result =                                        \
498                               (__s1[2] - ((__const unsigned char *) (s2))[2]);\
499                             if (l2 > 2 && __result == 0)                      \
500                               __result =                                      \
501                                 (__s1[3]                                      \
502                                  - ((__const unsigned char *)(s2))[3]);       \
503                           }                                                   \
504                       }                                                       \
505                     __result; }))
506 #endif
507
508
509 /* Compare N characters of S1 and S2.  */
510 #ifndef _HAVE_STRING_ARCH_strncmp
511 # define strncmp(s1, s2, n) \
512   (__extension__ (__builtin_constant_p (s1) && strlen (s1) < ((size_t) (n))   \
513                   ? strcmp (s1, s2)                                           \
514                   : (__builtin_constant_p (s2) && strlen (s2) < ((size_t) (n))\
515                      ? strcmp (s1, s2)                                        \
516                      : strncmp (s1, s2, n))))
517 #endif
518
519
520 /* Return the length of the initial segment of S which
521    consists entirely of characters not in REJECT.  */
522 #ifndef _HAVE_STRING_ARCH_strcspn
523 # define strcspn(s, reject) \
524   (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
525                   ? (((__const unsigned char *) (reject))[0] == '\0'          \
526                      ? strlen (s)                                             \
527                      : (((__const unsigned char *) (reject))[1] == '\0'       \
528                         ? __strcspn_c1 (s, ((__const char *) (reject))[0])    \
529                         : (((__const unsigned char *) (reject))[2] == '\0'    \
530                            ? __strcspn_c2 (s, ((__const char *) (reject))[0], \
531                                            ((__const char *) (reject))[1])    \
532                            : (((__const unsigned char *) (reject))[3] == '\0' \
533                               ? __strcspn_c3 (s,                              \
534                                               ((__const char *) (reject))[0], \
535                                               ((__const char *) (reject))[1], \
536                                               ((__const char *) (reject))[2]) \
537                               : strcspn (s, reject)))))                       \
538                   : strcspn (s, reject)))
539
540 __STRING_INLINE size_t __strcspn_c1 (__const char *__s, char __reject);
541 __STRING_INLINE size_t
542 __strcspn_c1 (__const char *__s, char __reject)
543 {
544   register size_t __result = 0;
545   while (__s[__result] != '\0' && __s[__result] != __reject)
546     ++__result;
547   return __result;
548 }
549
550 __STRING_INLINE size_t __strcspn_c2 (__const char *__s, char __reject1,
551                                      char __reject2);
552 __STRING_INLINE size_t
553 __strcspn_c2 (__const char *__s, char __reject1, char __reject2)
554 {
555   register size_t __result = 0;
556   while (__s[__result] != '\0' && __s[__result] != __reject1
557          && __s[__result] != __reject2)
558     ++__result;
559   return __result;
560 }
561
562 __STRING_INLINE size_t __strcspn_c3 (__const char *__s, char __reject1,
563                                      char __reject2, char __reject3);
564 __STRING_INLINE size_t
565 __strcspn_c3 (__const char *__s, char __reject1, char __reject2,
566               char __reject3)
567 {
568   register size_t __result = 0;
569   while (__s[__result] != '\0' && __s[__result] != __reject1
570          && __s[__result] != __reject2 && __s[__result] != __reject3)
571     ++__result;
572   return __result;
573 }
574 #endif
575
576
577 /* Return the length of the initial segment of S which
578    consists entirely of characters in ACCEPT.  */
579 #ifndef _HAVE_STRING_ARCH_strspn
580 # define strspn(s, accept) \
581   (__extension__ (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
582                   ? (((__const unsigned char *) (accept))[0] == '\0'          \
583                      ? 0                                                      \
584                      : (((__const unsigned char *) (accept))[1] == '\0'       \
585                         ? __strspn_c1 (s, ((__const char *) (accept))[0])     \
586                         : (((__const unsigned char *) (accept))[2] == '\0'    \
587                            ? __strspn_c2 (s, ((__const char *) (accept))[0],  \
588                                           ((__const char *) (accept))[1])     \
589                            : (((__const unsigned char *) (accept))[3] == '\0' \
590                               ? __strspn_c3 (s,                               \
591                                              ((__const char *) (accept))[0],  \
592                                              ((__const char *) (accept))[1],  \
593                                              ((__const char *) (accept))[2])  \
594                               : strspn (s, accept)))))                        \
595                   : strspn (s, accept)))
596
597 __STRING_INLINE size_t __strspn_c1 (__const char *__s, char __accept);
598 __STRING_INLINE size_t
599 __strspn_c1 (__const char *__s, char __accept)
600 {
601   register size_t __result = 0;
602   /* Please note that __accept never can be '\0'.  */
603   while (__s[__result] == __accept)
604     ++__result;
605   return __result;
606 }
607
608 __STRING_INLINE size_t __strspn_c2 (__const char *__s, char __accept1,
609                                     char __accept2);
610 __STRING_INLINE size_t
611 __strspn_c2 (__const char *__s, char __accept1, char __accept2)
612 {
613   register size_t __result = 0;
614   /* Please note that __accept1 and __accept2 never can be '\0'.  */
615   while (__s[__result] == __accept1 || __s[__result] == __accept2)
616     ++__result;
617   return __result;
618 }
619
620 __STRING_INLINE size_t __strspn_c3 (__const char *__s, char __accept1,
621                                     char __accept2, char __accept3);
622 __STRING_INLINE size_t
623 __strspn_c3 (__const char *__s, char __accept1, char __accept2, char __accept3)
624 {
625   register size_t __result = 0;
626   /* Please note that __accept1 to __accept3 never can be '\0'.  */
627   while (__s[__result] == __accept1 || __s[__result] == __accept2
628          || __s[__result] == __accept3)
629     ++__result;
630   return __result;
631 }
632 #endif
633
634
635 /* Find the first occurrence in S of any character in ACCEPT.  */
636 #ifndef _HAVE_STRING_ARCH_strpbrk
637 # define strpbrk(s, accept) \
638   (__extension__ (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
639                   ? (((__const unsigned char  *) (accept))[0] == '\0'         \
640                      ? NULL                                                   \
641                      : (((__const unsigned char *) (accept))[1] == '\0'       \
642                         ? strchr (s, ((__const unsigned char *) (accept))[0]) \
643                         : (((__const unsigned char *) (accept))[2] == '\0'    \
644                            ? __strpbrk_c2 (s, ((__const char *) (accept))[0], \
645                                            ((__const char *) (accept))[1])    \
646                            : (((__const unsigned char *) (accept))[3] == '\0' \
647                               ? __strpbrk_c3 (s,                              \
648                                               ((__const char *) (accept))[0], \
649                                               ((__const char *) (accept))[1], \
650                                               ((__const char *) (accept))[2]) \
651                               : strpbrk (s, accept)))))                       \
652                   : strpbrk (s, accept)))
653
654 __STRING_INLINE char *__strpbrk_c2 (__const char *__s, char __accept1,
655                                      char __accept2);
656 __STRING_INLINE char *
657 __strpbrk_c2 (__const char *__s, char __accept1, char __accept2)
658 {
659   /* Please note that __accept1 and __accept2 never can be '\0'.  */
660   while (*__s != '\0' && *__s != __accept1 && *__s != __accept2)
661     ++__s;
662   return *__s == '\0' ? NULL : (char *) __s;
663 }
664
665 __STRING_INLINE char *__strpbrk_c3 (__const char *__s, char __accept1,
666                                      char __accept2, char __accept3);
667 __STRING_INLINE char *
668 __strpbrk_c3 (__const char *__s, char __accept1, char __accept2,
669               char __accept3)
670 {
671   /* Please note that __accept1 to __accept3 never can be '\0'.  */
672   while (*__s != '\0' && *__s != __accept1 && *__s != __accept2
673          && *__s != __accept3)
674     ++__s;
675   return *__s == '\0' ? NULL : (char *) __s;
676 }
677 #endif
678
679
680 /* Find the first occurrence of NEEDLE in HAYSTACK.  */
681 #ifndef _HAVE_STRING_ARCH_strstr
682 # define strstr(haystack, needle) \
683   (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
684                   ? (((__const unsigned char *) (needle))[0] == '\0'          \
685                      ? (char *) (haystack)                                    \
686                      : (((__const unsigned char *) (needle))[1] == '\0'       \
687                         ? strchr (haystack,                                   \
688                                   ((__const unsigned char *) (needle))[0])    \
689                         : strstr (haystack, needle)))                         \
690                   : strstr (haystack, needle)))
691 #endif
692
693
694 #ifdef __USE_GNU
695 # ifndef _HAVE_STRING_ARCH_strnlen
696 __STRING_INLINE size_t strnlen (__const char *__string, size_t __maxlen);
697 __STRING_INLINE size_t
698 strnlen (__const char *__string, size_t __maxlen)
699 {
700   __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
701   return __end ? __end - __string : __maxlen;
702 }
703 # endif
704 #endif
705
706
707 #if defined __USE_POSIX || defined __USE_MISC
708 # ifndef _HAVE_STRING_ARCH_strtok_r
709 #  define strtok_r(s, sep, nextp) \
710   (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep)       \
711                   ? (((__const unsigned char *) (sep))[0] != '\0'             \
712                      && ((__const unsigned char *) (sep))[1] == '\0'          \
713                      ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp)  \
714                      : strtok_r (s, sep, nextp))                              \
715                   : strtok_r (s, sep, nextp)))
716
717 __STRING_INLINE char *__strtok_r_1c (char *__s, char __sep, char **__nextp);
718 __STRING_INLINE char *
719 __strtok_r_1c (char *__s, char __sep, char **__nextp)
720 {
721   char *__result;
722   if (__s == NULL)
723     __s = *__nextp;
724   while (*__s == __sep)
725     ++__s;
726   if (*__s == '\0')
727     __result = NULL;
728   else
729     {
730       __result = __s;
731       while (*__s != '\0' && *__s != __sep)
732         ++__s;
733       if (*__s == '\0')
734         *__nextp = __s;
735       else
736         {
737           *__s = '\0';
738           *__nextp = __s + 1;
739         }
740     }
741   return __result;
742 }
743 # endif
744 #endif
745
746
747 #ifdef __USE_BSD
748 # ifndef _HAVE_STRING_ARCH_strsep
749
750 #  define strsep(s, reject) \
751   (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
752                   && ((__const unsigned char *) (reject))[0] != '\0'          \
753                   ? (((__const unsigned char *) (reject))[1] == '\0'          \
754                      ? __strsep_1c (s,                                        \
755                                     ((__const char *) (reject))[0])           \
756                      : (((__const unsigned char *) (reject))[2] == '\0'       \
757                         ? __strsep_2c (s, ((__const char *) (reject))[0],     \
758                                        ((__const char *) (reject))[1])        \
759                         : (((__const unsigned char *) (reject))[3] == '\0'    \
760                            ? __strsep_3c (s, ((__const char *) (reject))[0],  \
761                                           ((__const char *) (reject))[1],     \
762                                           ((__const char *) (reject))[2])     \
763                            : __strsep_g (s, reject))))                        \
764                   : __strsep_g (s, reject)))
765
766 __STRING_INLINE char *__strsep_1c (char **__s, char __reject);
767 __STRING_INLINE char *
768 __strsep_1c (char **__s, char __reject)
769 {
770   register char *__retval = *__s;
771   if (__retval == NULL)
772     return *__s = NULL;
773   if (*__retval == __reject)
774     *(*__s)++ = '\0';
775   else
776     if ((*__s = strchr (__retval, __reject)) != NULL)
777       *(*__s)++ = '\0';
778     else
779       *__s = NULL;
780   return __retval;
781 }
782
783 __STRING_INLINE char *__strsep_2c (char **__s, char __reject1, char __reject2);
784 __STRING_INLINE char *
785 __strsep_2c (char **__s, char __reject1, char __reject2)
786 {
787   register char *__retval = *__s;
788   if (__retval == NULL)
789     return *__s = NULL;
790   if (*__retval == __reject1 || *__retval == __reject2)
791     *(*__s)++ = '\0';
792   else
793     {
794       register char *__cp = __retval;
795       while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2)
796         ++__cp;
797       if (*__cp != '\0')
798         {
799           *__s = __cp;
800           *(*__s)++ = '\0';
801         }
802       else
803         *__s = NULL;
804     }
805   return __retval;
806 }
807
808 __STRING_INLINE char *__strsep_3c (char **__s, char __reject1, char __reject2,
809                                    char __reject3);
810 __STRING_INLINE char *
811 __strsep_3c (char **__s, char __reject1, char __reject2, char __reject3)
812 {
813   register char *__retval = *__s;
814   if (__retval == NULL)
815     return *__s = NULL;
816   if (*__retval == __reject1 || *__retval == __reject2
817       || *__retval == __reject3)
818     *(*__s)++ = '\0';
819   else
820     {
821       register char *__cp = __retval;
822       while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2
823              && *__cp != __reject3)
824         ++__cp;
825       if (*__cp != '\0')
826         {
827           *__s = __cp;
828           *(*__s)++ = '\0';
829         }
830       else
831         *__s = NULL;
832     }
833   return __retval;
834 }
835
836 __STRING_INLINE char *__strsep_g (char **__s, __const char *__reject);
837 __STRING_INLINE char *
838 __strsep_g (char **__s, __const char *__reject)
839 {
840   register char *__retval = *__s;
841   if (__retval == NULL || *__retval == '\0')
842     return NULL;
843   if ((*__s = strpbrk (__retval, __reject)) != NULL)
844     *(*__s)++ = '\0';
845   return __retval;
846 }
847 # endif
848 #endif
849
850
851 #undef __STRING_INLINE
852
853 #endif /* No string inlines.  */