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.
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.
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.
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. */
22 # error "Never use <bits/string2.h> directly; include <string.h> instead."
25 #ifndef __NO_STRING_INLINES
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.
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.
39 We must use here macros instead of inline functions since the
40 trick won't work with the later. */
43 # define __STRING_INLINE inline
45 # define __STRING_INLINE extern __inline
48 #if _STRING_ARCH_unaligned
49 /* If we can do unaligned memory accesses we must know the endianess. */
51 # include <bits/types.h>
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])
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])
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
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)
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; }) \
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)))
112 # if _STRING_ARCH_unaligned
113 # define __strcpy_small(dest, src, srclen) \
114 (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest); \
121 *((__uint16_t *) __dest) = \
122 __STRING2_SMALL_GET16 (src, 0); \
125 *((__uint16_t *) __dest) = \
126 __STRING2_SMALL_GET16 (src, 0); \
127 *(__dest + 2) = '\0'; \
130 *((__uint32_t *) __dest) = \
131 __STRING2_SMALL_GET32 (src, 0); \
134 *((__uint32_t *) __dest) = \
135 __STRING2_SMALL_GET32 (src, 0); \
136 *(__dest + 4) = '\0'; \
139 *((__uint32_t *) __dest) = \
140 __STRING2_SMALL_GET32 (src, 0); \
141 *((__uint16_t *) (__dest + 4)) = \
142 __STRING2_SMALL_GET16 (src, 4); \
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'; \
152 *((__uint32_t *) __dest) = \
153 __STRING2_SMALL_GET32 (src, 0); \
154 *((__uint32_t *) (__dest + 4)) = \
155 __STRING2_SMALL_GET32 (src, 4); \
160 # define __strcpy_small(dest, src, srclen) \
161 (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest); \
168 *((__STRING2_COPY_ARR2 *) __dest) = \
169 ((__STRING2_COPY_ARR2) \
170 { { ((__const unsigned char *) (src))[0], \
174 *((__STRING2_COPY_ARR3 *) __dest) = \
175 ((__STRING2_COPY_ARR3) \
176 { { ((__const unsigned char *) (src))[0], \
177 ((__const unsigned char *) (src))[1], \
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], \
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], \
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], \
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], \
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], \
236 /* Copy SRC to DEST, returning pointer to final NUL byte. */
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)
249 # if _STRING_ARCH_unaligned
250 # define __stpcpy_small(dest, src, srclen) \
251 (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest); \
258 *((__uint16_t *) __dest) = \
259 __STRING2_SMALL_GET16 (src, 0); \
263 *((__uint16_t *) __dest) = \
264 __STRING2_SMALL_GET16 (src, 0); \
265 __dest += sizeof (__uint16_t); \
269 *((__uint32_t *) __dest) = \
270 __STRING2_SMALL_GET32 (src, 0); \
274 *((__uint32_t *) __dest) = \
275 __STRING2_SMALL_GET32 (src, 0); \
276 __dest += sizeof (__uint32_t); \
280 *((__uint32_t *) __dest) = \
281 __STRING2_SMALL_GET32 (src, 0); \
282 *((__uint16_t *) (__dest + 4)) = \
283 __STRING2_SMALL_GET16 (src, 4); \
287 *((__uint32_t *) __dest) = \
288 __STRING2_SMALL_GET32 (src, 0); \
289 *((__uint16_t *) (__dest + 4)) = \
290 __STRING2_SMALL_GET16 (src, 4); \
295 *((__uint32_t *) __dest) = \
296 __STRING2_SMALL_GET32 (src, 0); \
297 *((__uint32_t *) (__dest + 4)) = \
298 __STRING2_SMALL_GET32 (src, 4); \
304 # define __stpcpy_small(dest, src, srclen) \
305 (__extension__ ({ unsigned char *__dest = (unsigned char *) (dest); \
312 *((__STRING2_COPY_ARR2 *) __dest) = \
313 ((__STRING2_COPY_ARR2) \
314 { { ((__const unsigned char *) (src))[0], \
318 *((__STRING2_COPY_ARR3 *) __dest) = \
319 ((__STRING2_COPY_ARR3) \
320 { { ((__const unsigned char *) (src))[0], \
321 ((__const unsigned char *) (src))[1], \
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], \
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], \
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], \
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], \
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], \
375 (char *) (__dest + ((srclen) - 1)); }))
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)), \
392 : strncpy (__dest, src, n); }))
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)))
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); }))
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)))
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) \
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) \
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)))))
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) \
451 __result = (((__const unsigned char *) (s1))[1] \
452 - ((__const unsigned char *) (s2))[1]); \
453 if (l > 1 && __result == 0) \
456 (((__const unsigned char *) (s1))[2] \
457 - ((__const unsigned char *) (s2))[2]); \
458 if (l > 2 && __result == 0) \
460 (((__const unsigned char *) (s1))[3] \
461 - ((__const unsigned char *) (s2))[3]); \
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) \
473 __result = (((__const unsigned char *) (s1))[1] \
475 if (l1 > 1 && __result == 0) \
477 __result = (((__const unsigned char *) (s1))[2] \
479 if (l1 > 2 && __result == 0) \
480 __result = (((__const unsigned char *) (s1))[3] \
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) \
493 __result = (__s1[1] \
494 - ((__const unsigned char *) (s2))[1]); \
495 if (l2 > 1 && __result == 0) \
498 (__s1[2] - ((__const unsigned char *) (s2))[2]);\
499 if (l2 > 2 && __result == 0) \
502 - ((__const unsigned char *)(s2))[3]); \
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)) \
514 : (__builtin_constant_p (s2) && strlen (s2) < ((size_t) (n))\
516 : strncmp (s1, s2, n))))
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' \
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' \
534 ((__const char *) (reject))[0], \
535 ((__const char *) (reject))[1], \
536 ((__const char *) (reject))[2]) \
537 : strcspn (s, reject))))) \
538 : strcspn (s, reject)))
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)
544 register size_t __result = 0;
545 while (__s[__result] != '\0' && __s[__result] != __reject)
550 __STRING_INLINE size_t __strcspn_c2 (__const char *__s, char __reject1,
552 __STRING_INLINE size_t
553 __strcspn_c2 (__const char *__s, char __reject1, char __reject2)
555 register size_t __result = 0;
556 while (__s[__result] != '\0' && __s[__result] != __reject1
557 && __s[__result] != __reject2)
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,
568 register size_t __result = 0;
569 while (__s[__result] != '\0' && __s[__result] != __reject1
570 && __s[__result] != __reject2 && __s[__result] != __reject3)
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' \
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' \
591 ((__const char *) (accept))[0], \
592 ((__const char *) (accept))[1], \
593 ((__const char *) (accept))[2]) \
594 : strspn (s, accept))))) \
595 : strspn (s, accept)))
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)
601 register size_t __result = 0;
602 /* Please note that __accept never can be '\0'. */
603 while (__s[__result] == __accept)
608 __STRING_INLINE size_t __strspn_c2 (__const char *__s, char __accept1,
610 __STRING_INLINE size_t
611 __strspn_c2 (__const char *__s, char __accept1, char __accept2)
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)
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)
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)
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' \
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' \
648 ((__const char *) (accept))[0], \
649 ((__const char *) (accept))[1], \
650 ((__const char *) (accept))[2]) \
651 : strpbrk (s, accept))))) \
652 : strpbrk (s, accept)))
654 __STRING_INLINE char *__strpbrk_c2 (__const char *__s, char __accept1,
656 __STRING_INLINE char *
657 __strpbrk_c2 (__const char *__s, char __accept1, char __accept2)
659 /* Please note that __accept1 and __accept2 never can be '\0'. */
660 while (*__s != '\0' && *__s != __accept1 && *__s != __accept2)
662 return *__s == '\0' ? NULL : (char *) __s;
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,
671 /* Please note that __accept1 to __accept3 never can be '\0'. */
672 while (*__s != '\0' && *__s != __accept1 && *__s != __accept2
673 && *__s != __accept3)
675 return *__s == '\0' ? NULL : (char *) __s;
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)))
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)
700 __const char *__end = (__const char *) memchr (__string, '\0', __maxlen);
701 return __end ? __end - __string : __maxlen;
707 #ifndef _HAVE_STRING_ARCH_strtok_r
708 # define __strtok_r(s, sep, nextp) \
709 (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \
710 ? (((__const unsigned char *) (sep))[0] != '\0' \
711 && ((__const unsigned char *) (sep))[1] == '\0' \
712 ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp) \
713 : __strtok_r (s, sep, nextp)) \
714 : __strtok_r (s, sep, nextp)))
716 __STRING_INLINE char *__strtok_r_1c (char *__s, char __sep, char **__nextp);
717 __STRING_INLINE char *
718 __strtok_r_1c (char *__s, char __sep, char **__nextp)
723 while (*__s == __sep)
730 while (*__s != '\0' && *__s != __sep)
742 # if defined __USE_POSIX || defined __USE_MISC
743 # define strtok_r(s, sep, nextp) __strtok_r ((s), (sep), (nextp))
748 #ifndef _HAVE_STRING_ARCH_strsep
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' \
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)))
766 __STRING_INLINE char *__strsep_1c (char **__s, char __reject);
767 __STRING_INLINE char *
768 __strsep_1c (char **__s, char __reject)
770 register char *__retval = *__s;
771 if (__retval == NULL)
773 if (*__retval == __reject)
776 if ((*__s = strchr (__retval, __reject)) != NULL)
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)
787 register char *__retval = *__s;
788 if (__retval == NULL)
790 if (*__retval == __reject1 || *__retval == __reject2)
794 register char *__cp = __retval;
795 while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2)
808 __STRING_INLINE char *__strsep_3c (char **__s, char __reject1, char __reject2,
810 __STRING_INLINE char *
811 __strsep_3c (char **__s, char __reject1, char __reject2, char __reject3)
813 register char *__retval = *__s;
814 if (__retval == NULL)
816 if (*__retval == __reject1 || *__retval == __reject2
817 || *__retval == __reject3)
821 register char *__cp = __retval;
822 while (*__cp != '\0' && *__cp != __reject1 && *__cp != __reject2
823 && *__cp != __reject3)
836 __STRING_INLINE char *__strsep_g (char **__s, __const char *__reject);
837 __STRING_INLINE char *
838 __strsep_g (char **__s, __const char *__reject)
840 register char *__retval = *__s;
841 if (__retval == NULL || *__retval == '\0')
843 if ((*__s = strpbrk (__retval, __reject)) != NULL)
848 # define strsep(s, reject) __strsep ((s), (reject))
853 #if !defined _HAVE_STRING_ARCH_strdup && !defined __STRICT_ANSI__
855 /* We need the memory allocation functions. Including this header is
859 # define __strdup(s) \
860 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
861 ? (((__const unsigned char *) (s))[0] == '\0' \
862 ? (char *) calloc (1, 1) \
863 : ({ size_t __len = strlen (s) + 1; \
864 char *__retval = (char *) malloc (__len); \
865 if (__retval != NULL) \
866 __retval = (char *) memcpy (__retval, s, __len); \
870 # if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
871 # define strdup(s) __strdup (s)
876 #if !defined _HAVE_STRING_ARCH_strndup && !defined __STRICT_ANSI__
878 /* We need the memory allocation functions. Including this header is
882 # define __strndup(s, n) \
883 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
884 ? (((__const unsigned char *) (s))[0] == '\0' \
885 ? (char *) calloc (1, 1) \
886 : ({ size_t __len = strlen (s) + 1; \
891 __retval = (char *) malloc (__len); \
892 if (__retval != NULL) \
894 __retval[__len - 1] = '\0'; \
895 __retval = (char *) memcpy (__retval, s, \
899 : __strndup ((s), (n))))
902 # define strndup(s, n) __strndup ((s), (n))
907 #undef __STRING_INLINE
909 #endif /* No string inlines. */