0d378039fea6bfadcd72707823e2d92cbe67ccd7
[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 *) (__const char *) (src))[idx + 1] << 8        \
56       | ((__const unsigned char *) (__const char *) (src))[idx])
57 #  define __STRING2_SMALL_GET32(src, idx) \
58      (((((__const unsigned char *) (__const char *) (src))[idx + 3] << 8      \
59         | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8   \
60        | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8    \
61       | ((__const unsigned char *) (__const char *) (src))[idx])
62 # else
63 #  define __STRING2_SMALL_GET16(src, idx) \
64      (((__const unsigned char *) (__const char *) (src))[idx] << 8            \
65       | ((__const unsigned char *) (__const char *) (src))[idx + 1])
66 #  define __STRING2_SMALL_GET32(src, idx) \
67      (((((__const unsigned char *) (__const char *) (src))[idx] << 8          \
68         | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8   \
69        | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8    \
70       | ((__const unsigned char *) (__const 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 { unsigned 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__ ({ char *__dest = (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                     __dest; }))
159 # else
160 #  define __strcpy_small(dest, src, srclen) \
161   (__extension__ ({ char *__dest = (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 char *) (src))[0],                   \
171                                '\0' } });                                     \
172                         break;                                                \
173                       case 3:                                                 \
174                         *((__STRING2_COPY_ARR3 *) __dest) =                   \
175                           ((__STRING2_COPY_ARR3)                              \
176                            { { ((__const char *) (src))[0],                   \
177                                ((__const char *) (src))[1],                   \
178                                '\0' } });                                     \
179                         break;                                                \
180                       case 4:                                                 \
181                         *((__STRING2_COPY_ARR4 *) __dest) =                   \
182                           ((__STRING2_COPY_ARR4)                              \
183                            { { ((__const char *) (src))[0],                   \
184                                ((__const char *) (src))[1],                   \
185                                ((__const char *) (src))[2],                   \
186                                '\0' } });                                     \
187                         break;                                                \
188                       case 5:                                                 \
189                         *((__STRING2_COPY_ARR5 *) __dest) =                   \
190                           ((__STRING2_COPY_ARR5)                              \
191                            { { ((__const char *) (src))[0],                   \
192                                ((__const char *) (src))[1],                   \
193                                ((__const char *) (src))[2],                   \
194                                ((__const char *) (src))[3],                   \
195                                '\0' } });                                     \
196                         break;                                                \
197                       case 6:                                                 \
198                         *((__STRING2_COPY_ARR6 *) __dest) =                   \
199                           ((__STRING2_COPY_ARR6)                              \
200                            { { ((__const char *) (src))[0],                   \
201                                ((__const char *) (src))[1],                   \
202                                ((__const char *) (src))[2],                   \
203                                ((__const char *) (src))[3],                   \
204                                ((__const char *) (src))[4],                   \
205                                '\0' } });                                     \
206                         break;                                                \
207                       case 7:                                                 \
208                         *((__STRING2_COPY_ARR7 *) __dest) =                   \
209                           ((__STRING2_COPY_ARR7)                              \
210                            { { ((__const char *) (src))[0],                   \
211                                ((__const char *) (src))[1],                   \
212                                ((__const char *) (src))[2],                   \
213                                ((__const char *) (src))[3],                   \
214                                ((__const char *) (src))[4],                   \
215                                ((__const char *) (src))[5],                   \
216                                '\0' } });                                     \
217                         break;                                                \
218                       case 8:                                                 \
219                         *((__STRING2_COPY_ARR8 *) __dest) =                   \
220                           ((__STRING2_COPY_ARR8)                              \
221                            { { ((__const char *) (src))[0],                   \
222                                ((__const char *) (src))[1],                   \
223                                ((__const char *) (src))[2],                   \
224                                ((__const char *) (src))[3],                   \
225                                ((__const char *) (src))[4],                   \
226                                ((__const char *) (src))[5],                   \
227                                ((__const char *) (src))[6],                   \
228                                '\0' } });                                     \
229                         break;                                                \
230                     }                                                         \
231                   __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__ ({ char *__dest = (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                     __dest; }))
303 #  else
304 #   define __stpcpy_small(dest, src, srclen) \
305   (__extension__ ({ char *__dest = (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 char *) (src))[0],                   \
315                                '\0' } });                                     \
316                         break;                                                \
317                       case 3:                                                 \
318                         *((__STRING2_COPY_ARR3 *) __dest) =                   \
319                           ((__STRING2_COPY_ARR3)                              \
320                            { { ((__const char *) (src))[0],                   \
321                                ((__const char *) (src))[1],                   \
322                                '\0' } });                                     \
323                         break;                                                \
324                       case 4:                                                 \
325                         *((__STRING2_COPY_ARR4 *) __dest) =                   \
326                           ((__STRING2_COPY_ARR4)                              \
327                            { { ((__const char *) (src))[0],                   \
328                                ((__const char *) (src))[1],                   \
329                                ((__const char *) (src))[2],                   \
330                                '\0' } });                                     \
331                         break;                                                \
332                       case 5:                                                 \
333                         *((__STRING2_COPY_ARR5 *) __dest) =                   \
334                           ((__STRING2_COPY_ARR5)                              \
335                            { { ((__const char *) (src))[0],                   \
336                                ((__const char *) (src))[1],                   \
337                                ((__const char *) (src))[2],                   \
338                                ((__const char *) (src))[3],                   \
339                                '\0' } });                                     \
340                         break;                                                \
341                       case 6:                                                 \
342                         *((__STRING2_COPY_ARR6 *) __dest) =                   \
343                           ((__STRING2_COPY_ARR6)                              \
344                            { { ((__const char *) (src))[0],                   \
345                                ((__const char *) (src))[1],                   \
346                                ((__const char *) (src))[2],                   \
347                                ((__const char *) (src))[3],                   \
348                                ((__const char *) (src))[4],                   \
349                                '\0' } });                                     \
350                         break;                                                \
351                       case 7:                                                 \
352                         *((__STRING2_COPY_ARR7 *) __dest) =                   \
353                           ((__STRING2_COPY_ARR7)                              \
354                            { { ((__const char *) (src))[0],                   \
355                                ((__const char *) (src))[1],                   \
356                                ((__const char *) (src))[2],                   \
357                                ((__const char *) (src))[3],                   \
358                                ((__const char *) (src))[4],                   \
359                                ((__const char *) (src))[5],                   \
360                                '\0' } });                                     \
361                         break;                                                \
362                       case 8:                                                 \
363                         *((__STRING2_COPY_ARR8 *) __dest) =                   \
364                           ((__STRING2_COPY_ARR8)                              \
365                            { { ((__const char *) (src))[0],                   \
366                                ((__const char *) (src))[1],                   \
367                                ((__const char *) (src))[2],                   \
368                                ((__const char *) (src))[3],                   \
369                                ((__const char *) (src))[4],                   \
370                                ((__const char *) (src))[5],                   \
371                                ((__const char *) (src))[6],                   \
372                                '\0' } });                                     \
373                         break;                                                \
374                     }                                                         \
375                   __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 ((__const char *) (s1), (__const char *) (s2),     \
432                             (strlen (s1) < strlen (s2)                        \
433                              ? strlen (s1) : strlen (s2)) + 1)                \
434                   : (__builtin_constant_p (s1) && __string2_1bptr_p (s1)      \
435                      && strlen (s1) < 4                                       \
436                      ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2)   \
437                         ? __strcmp_cc (s1, s2, strlen (s1))                   \
438                         : __strcmp_cg (s1, s2, strlen (s1)))                  \
439                      : (__builtin_constant_p (s2) && __string2_1bptr_p (s2)   \
440                         && strlen (s2) < 4                                    \
441                         ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1)\
442                            ? __strcmp_cc (s1, s2, strlen (s2))                \
443                            : __strcmp_gc (s1, s2, strlen (s2)))               \
444                         : strcmp (s1, s2)))))
445
446 # define __strcmp_cc(s1, s2, l) \
447   (__extension__ ({ register int __result =                                   \
448                       (((__const unsigned char *) (__const char *) (s1))[0]   \
449                        - ((__const unsigned char *) (__const char *)(s2))[0]);\
450                     if (l > 0 && __result == 0)                               \
451                       {                                                       \
452                         __result = (((__const unsigned char *)                \
453                                      (__const char *) (s1))[1]                \
454                                     - ((__const unsigned char *)              \
455                                        (__const char *) (s2))[1]);            \
456                         if (l > 1 && __result == 0)                           \
457                           {                                                   \
458                             __result =                                        \
459                               (((__const unsigned char *)                     \
460                                 (__const char *) (s1))[2]                     \
461                                - ((__const unsigned char *)                   \
462                                   (__const char *) (s2))[2]);                 \
463                             if (l > 2 && __result == 0)                       \
464                               __result =                                      \
465                                 (((__const unsigned char *)                   \
466                                   (__const char *) (s1))[3]                   \
467                                  - ((__const unsigned char *)                 \
468                                     (__const char *) (s2))[3]);               \
469                           }                                                   \
470                       }                                                       \
471                     __result; }))
472
473 # define __strcmp_cg(s1, s2, l1) \
474   (__extension__ ({ __const unsigned char *__s2 =                             \
475                       (__const unsigned char *) (__const char *) (s2);        \
476                     register int __result =                                   \
477                       (((__const unsigned char *) (__const char *) (s1))[0]   \
478                        - __s2[0]);                                            \
479                     if (l1 > 0 && __result == 0)                              \
480                       {                                                       \
481                         __result = (((__const unsigned char *)                \
482                                      (__const char *) (s1))[1] - __s2[1]);    \
483                         if (l1 > 1 && __result == 0)                          \
484                           {                                                   \
485                             __result = (((__const unsigned char *)            \
486                                          (__const char *) (s1))[2] - __s2[2]);\
487                             if (l1 > 2 && __result == 0)                      \
488                               __result = (((__const unsigned char *)          \
489                                           (__const char *)  (s1))[3]          \
490                                           - __s2[3]);                         \
491                           }                                                   \
492                       }                                                       \
493                     __result; }))
494
495 # define __strcmp_gc(s1, s2, l2) \
496   (__extension__ ({ __const unsigned char *__s1 =                             \
497                       (__const unsigned char *) (__const char *) (s1);        \
498                     register int __result =                                   \
499                       __s1[0] - ((__const unsigned char *)                    \
500                                  (__const char *) (s2))[0];                   \
501                     if (l2 > 0 && __result == 0)                              \
502                       {                                                       \
503                         __result = (__s1[1]                                   \
504                                     - ((__const unsigned char *)              \
505                                        (__const char *) (s2))[1]);            \
506                         if (l2 > 1 && __result == 0)                          \
507                           {                                                   \
508                             __result =                                        \
509                               (__s1[2] - ((__const unsigned char *)           \
510                                           (__const char *) (s2))[2]);         \
511                             if (l2 > 2 && __result == 0)                      \
512                               __result =                                      \
513                                 (__s1[3]                                      \
514                                  - ((__const unsigned char *)                 \
515                                     (__const char *) (s2))[3]);               \
516                           }                                                   \
517                       }                                                       \
518                     __result; }))
519 #endif
520
521
522 /* Compare N characters of S1 and S2.  */
523 #ifndef _HAVE_STRING_ARCH_strncmp
524 # define strncmp(s1, s2, n) \
525   (__extension__ (__builtin_constant_p (s1) && strlen (s1) < ((size_t) (n))   \
526                   ? strcmp (s1, s2)                                           \
527                   : (__builtin_constant_p (s2) && strlen (s2) < ((size_t) (n))\
528                      ? strcmp (s1, s2)                                        \
529                      : strncmp (s1, s2, n))))
530 #endif
531
532
533 /* Return the length of the initial segment of S which
534    consists entirely of characters not in REJECT.  */
535 #ifndef _HAVE_STRING_ARCH_strcspn
536 # define strcspn(s, reject) \
537   (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
538                   ? (((__const char *) (reject))[0] == '\0'                   \
539                      ? strlen (s)                                             \
540                      : (((__const char *) (reject))[1] == '\0'                \
541                         ? __strcspn_c1 (s, ((__const char *) (reject))[0])    \
542                         : (((__const char *) (reject))[2] == '\0'             \
543                            ? __strcspn_c2 (s, ((__const char *) (reject))[0], \
544                                            ((__const char *) (reject))[1])    \
545                            : (((__const char *) (reject))[3] == '\0'          \
546                               ? __strcspn_c3 (s,                              \
547                                               ((__const char *) (reject))[0], \
548                                               ((__const char *) (reject))[1], \
549                                               ((__const char *) (reject))[2]) \
550                               : strcspn (s, reject)))))                       \
551                   : strcspn (s, reject)))
552
553 __STRING_INLINE size_t __strcspn_c1 (__const char *__s, char __reject);
554 __STRING_INLINE size_t
555 __strcspn_c1 (__const char *__s, char __reject)
556 {
557   register size_t __result = 0;
558   while (__s[__result] != '\0' && __s[__result] != __reject)
559     ++__result;
560   return __result;
561 }
562
563 __STRING_INLINE size_t __strcspn_c2 (__const char *__s, char __reject1,
564                                      char __reject2);
565 __STRING_INLINE size_t
566 __strcspn_c2 (__const char *__s, char __reject1, char __reject2)
567 {
568   register size_t __result = 0;
569   while (__s[__result] != '\0' && __s[__result] != __reject1
570          && __s[__result] != __reject2)
571     ++__result;
572   return __result;
573 }
574
575 __STRING_INLINE size_t __strcspn_c3 (__const char *__s, char __reject1,
576                                      char __reject2, char __reject3);
577 __STRING_INLINE size_t
578 __strcspn_c3 (__const char *__s, char __reject1, char __reject2,
579               char __reject3)
580 {
581   register size_t __result = 0;
582   while (__s[__result] != '\0' && __s[__result] != __reject1
583          && __s[__result] != __reject2 && __s[__result] != __reject3)
584     ++__result;
585   return __result;
586 }
587 #endif
588
589
590 /* Return the length of the initial segment of S which
591    consists entirely of characters in ACCEPT.  */
592 #ifndef _HAVE_STRING_ARCH_strspn
593 # define strspn(s, accept) \
594   (__extension__ (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
595                   ? (((__const char *) (accept))[0] == '\0'                   \
596                      ? 0                                                      \
597                      : (((__const char *) (accept))[1] == '\0'                \
598                         ? __strspn_c1 (s, ((__const char *) (accept))[0])     \
599                         : (((__const char *) (accept))[2] == '\0'             \
600                            ? __strspn_c2 (s, ((__const char *) (accept))[0],  \
601                                           ((__const char *) (accept))[1])     \
602                            : (((__const char *) (accept))[3] == '\0'          \
603                               ? __strspn_c3 (s,                               \
604                                              ((__const char *) (accept))[0],  \
605                                              ((__const char *) (accept))[1],  \
606                                              ((__const char *) (accept))[2])  \
607                               : strspn (s, accept)))))                        \
608                   : strspn (s, accept)))
609
610 __STRING_INLINE size_t __strspn_c1 (__const char *__s, char __accept);
611 __STRING_INLINE size_t
612 __strspn_c1 (__const char *__s, char __accept)
613 {
614   register size_t __result = 0;
615   /* Please note that __accept never can be '\0'.  */
616   while (__s[__result] == __accept)
617     ++__result;
618   return __result;
619 }
620
621 __STRING_INLINE size_t __strspn_c2 (__const char *__s, char __accept1,
622                                     char __accept2);
623 __STRING_INLINE size_t
624 __strspn_c2 (__const char *__s, char __accept1, char __accept2)
625 {
626   register size_t __result = 0;
627   /* Please note that __accept1 and __accept2 never can be '\0'.  */
628   while (__s[__result] == __accept1 || __s[__result] == __accept2)
629     ++__result;
630   return __result;
631 }
632
633 __STRING_INLINE size_t __strspn_c3 (__const char *__s, char __accept1,
634                                     char __accept2, char __accept3);
635 __STRING_INLINE size_t
636 __strspn_c3 (__const char *__s, char __accept1, char __accept2, char __accept3)
637 {
638   register size_t __result = 0;
639   /* Please note that __accept1 to __accept3 never can be '\0'.  */
640   while (__s[__result] == __accept1 || __s[__result] == __accept2
641          || __s[__result] == __accept3)
642     ++__result;
643   return __result;
644 }
645 #endif
646
647
648 /* Find the first occurrence in S of any character in ACCEPT.  */
649 #ifndef _HAVE_STRING_ARCH_strpbrk
650 # define strpbrk(s, accept) \
651   (__extension__ (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
652                   ? (((__const char  *) (accept))[0] == '\0'                  \
653                      ? NULL                                                   \
654                      : (((__const char *) (accept))[1] == '\0'                \
655                         ? strchr (s, ((__const char *) (accept))[0])          \
656                         : (((__const char *) (accept))[2] == '\0'             \
657                            ? __strpbrk_c2 (s, ((__const char *) (accept))[0], \
658                                            ((__const char *) (accept))[1])    \
659                            : (((__const char *) (accept))[3] == '\0'          \
660                               ? __strpbrk_c3 (s,                              \
661                                               ((__const char *) (accept))[0], \
662                                               ((__const char *) (accept))[1], \
663                                               ((__const char *) (accept))[2]) \
664                               : strpbrk (s, accept)))))                       \
665                   : strpbrk (s, accept)))
666
667 __STRING_INLINE char *__strpbrk_c2 (__const char *__s, char __accept1,
668                                      char __accept2);
669 __STRING_INLINE char *
670 __strpbrk_c2 (__const char *__s, char __accept1, char __accept2)
671 {
672   /* Please note that __accept1 and __accept2 never can be '\0'.  */
673   while (*__s != '\0' && *__s != __accept1 && *__s != __accept2)
674     ++__s;
675   return *__s == '\0' ? NULL : (char *) __s;
676 }
677
678 __STRING_INLINE char *__strpbrk_c3 (__const char *__s, char __accept1,
679                                      char __accept2, char __accept3);
680 __STRING_INLINE char *
681 __strpbrk_c3 (__const char *__s, char __accept1, char __accept2,
682               char __accept3)
683 {
684   /* Please note that __accept1 to __accept3 never can be '\0'.  */
685   while (*__s != '\0' && *__s != __accept1 && *__s != __accept2
686          && *__s != __accept3)
687     ++__s;
688   return *__s == '\0' ? NULL : (char *) __s;
689 }
690 #endif
691
692
693 /* Find the first occurrence of NEEDLE in HAYSTACK.  */
694 #ifndef _HAVE_STRING_ARCH_strstr
695 # define strstr(haystack, needle) \
696   (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
697                   ? (((__const char *) (needle))[0] == '\0'                   \
698                      ? (char *) (haystack)                                    \
699                      : (((__const char *) (needle))[1] == '\0'                \
700                         ? strchr (haystack,                                   \
701                                   ((__const char *) (needle))[0])             \
702                         : strstr (haystack, needle)))                         \
703                   : strstr (haystack, needle)))
704 #endif
705
706
707 #ifdef __USE_GNU
708 # ifndef _HAVE_STRING_ARCH_strnlen
709 __STRING_INLINE size_t strnlen (__const char *__string, size_t __maxlen);
710 __STRING_INLINE size_t
711 strnlen (__const char *__string, size_t __maxlen)
712 {
713   __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
714   return __end ? __end - __string : __maxlen;
715 }
716 # endif
717 #endif
718
719
720 #ifndef _HAVE_STRING_ARCH_strtok_r
721 # define __strtok_r(s, sep, nextp) \
722   (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep)       \
723                   ? (((__const char *) (sep))[0] != '\0'                      \
724                      && ((__const char *) (sep))[1] == '\0'                   \
725                      ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp)  \
726                      : __strtok_r (s, sep, nextp))                            \
727                   : __strtok_r (s, sep, nextp)))
728
729 __STRING_INLINE char *__strtok_r_1c (char *__s, char __sep, char **__nextp);
730 __STRING_INLINE char *
731 __strtok_r_1c (char *__s, char __sep, char **__nextp)
732 {
733   char *__result;
734   if (__s == NULL)
735     __s = *__nextp;
736   while (*__s == __sep)
737     ++__s;
738   if (*__s == '\0')
739     __result = NULL;
740   else
741     {
742       __result = __s;
743       while (*__s != '\0' && *__s != __sep)
744         ++__s;
745       if (*__s == '\0')
746         *__nextp = __s;
747       else
748         {
749           *__s = '\0';
750           *__nextp = __s + 1;
751         }
752     }
753   return __result;
754 }
755 # if defined __USE_POSIX || defined __USE_MISC
756 #  define strtok_r(s, sep, nextp) __strtok_r ((s), (sep), (nextp))
757 # endif
758 #endif
759
760
761 #ifndef _HAVE_STRING_ARCH_strsep
762
763 # define __strsep(s, reject) \
764   (__extension__ (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
765                   && ((__const char *) (reject))[0] != '\0'                   \
766                   ? (((__const char *) (reject))[1] == '\0'                   \
767                      ? __strsep_1c (s,                                        \
768                                     ((__const char *) (reject))[0])           \
769                      : (((__const char *) (reject))[2] == '\0'                \
770                         ? __strsep_2c (s, ((__const char *) (reject))[0],     \
771                                        ((__const char *) (reject))[1])        \
772                         : (((__const char *) (reject))[3] == '\0'             \
773                            ? __strsep_3c (s, ((__const char *) (reject))[0],  \
774                                           ((__const char *) (reject))[1],     \
775                                           ((__const char *) (reject))[2])     \
776                            : __strsep_g (s, reject))))                        \
777                   : __strsep_g (s, reject)))
778
779 __STRING_INLINE char *__strsep_1c (char **__s, char __reject);
780 __STRING_INLINE char *
781 __strsep_1c (char **__s, char __reject)
782 {
783   register char *__retval = *__s;
784   if (__retval == NULL)
785     return *__s = NULL;
786   if (*__retval == __reject)
787     *(*__s)++ = '\0';
788   else
789     if ((*__s = strchr (__retval, __reject)) != NULL)
790       *(*__s)++ = '\0';
791     else
792       *__s = NULL;
793   return __retval;
794 }
795
796 __STRING_INLINE char *__strsep_2c (char **__s, char __reject1, char __reject2);
797 __STRING_INLINE char *
798 __strsep_2c (char **__s, char __reject1, char __reject2)
799 {
800   register char *__retval = *__s;
801   if (__retval == NULL)
802     return *__s = NULL;
803   if (*__retval == __reject1 || *__retval == __reject2)
804     *(*__s)++ = '\0';
805   else
806     {
807       register char *__cp = __retval;
808       while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2)
809         ++__cp;
810       if (*__cp != '\0')
811         {
812           *__s = __cp;
813           *(*__s)++ = '\0';
814         }
815       else
816         *__s = NULL;
817     }
818   return __retval;
819 }
820
821 __STRING_INLINE char *__strsep_3c (char **__s, char __reject1, char __reject2,
822                                    char __reject3);
823 __STRING_INLINE char *
824 __strsep_3c (char **__s, char __reject1, char __reject2, char __reject3)
825 {
826   register char *__retval = *__s;
827   if (__retval == NULL)
828     return *__s = NULL;
829   if (*__retval == __reject1 || *__retval == __reject2
830       || *__retval == __reject3)
831     *(*__s)++ = '\0';
832   else
833     {
834       register char *__cp = __retval;
835       while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2
836              && *__cp != __reject3)
837         ++__cp;
838       if (*__cp != '\0')
839         {
840           *__s = __cp;
841           *(*__s)++ = '\0';
842         }
843       else
844         *__s = NULL;
845     }
846   return __retval;
847 }
848
849 __STRING_INLINE char *__strsep_g (char **__s, __const char *__reject);
850 __STRING_INLINE char *
851 __strsep_g (char **__s, __const char *__reject)
852 {
853   register char *__retval = *__s;
854   if (__retval == NULL || *__retval == '\0')
855     return NULL;
856   if ((*__s = strpbrk (__retval, __reject)) != NULL)
857     *(*__s)++ = '\0';
858   return __retval;
859 }
860 # ifdef __USE_BSD
861 #  define strsep(s, reject) __strsep ((s), (reject))
862 # endif
863 #endif
864
865
866 #if !defined _HAVE_STRING_ARCH_strdup && !defined __STRICT_ANSI__
867
868 /* We need the memory allocation functions.  Including this header is
869    not allowed. */
870 # include <stdlib.h>
871
872 # define __strdup(s) \
873   (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s)           \
874                   ? (((__const char *) (s))[0] == '\0'                        \
875                      ? (char *) calloc (1, 1)                                 \
876                      : ({ size_t __len = strlen (s) + 1;                      \
877                           char *__retval = (char *) malloc (__len);           \
878                           if (__retval != NULL)                               \
879                             __retval = (char *) memcpy (__retval, s, __len);  \
880                           __retval; }))                                       \
881                   : __strdup (s)))
882
883 # if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
884 #  define strdup(s) __strdup (s)
885 # endif
886 #endif
887
888
889 #if !defined _HAVE_STRING_ARCH_strndup && !defined __STRICT_ANSI__
890
891 /* We need the memory allocation functions.  Including this header is
892    not allowed. */
893 # include <stdlib.h>
894
895 # define __strndup(s, n) \
896   (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s)           \
897                   ? (((__const char *) (s))[0] == '\0'                        \
898                      ? (char *) calloc (1, 1)                                 \
899                      : ({ size_t __len = strlen (s) + 1;                      \
900                           size_t __n = (n);                                   \
901                           char *__retval;                                     \
902                           if (__n < __len)                                    \
903                             __len = __n;                                      \
904                           __retval = (char *) malloc (__len);                 \
905                           if (__retval != NULL)                               \
906                             {                                                 \
907                               __retval[__len - 1] = '\0';                     \
908                               __retval = (char *) memcpy (__retval, s,        \
909                                                           __len - 1);         \
910                             }                                                 \
911                           __retval; }))                                       \
912                   : __strndup ((s), (n))))
913
914 # ifdef __GNU_SOURCE
915 #  define strndup(s, n) __strndup ((s), (n))
916 # endif
917 #endif
918
919
920 #undef __STRING_INLINE
921
922 #endif /* No string inlines.  */