1 /* Machine-independant string function optimizations.
2 Copyright (C) 1997,1998,1999,2000,2001,2002 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # error "Never use <bits/string2.h> directly; include <string.h> instead."
25 #if !defined __NO_STRING_INLINES && !defined __BOUNDED_POINTERS__
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 latter. */
42 #ifndef __STRING_INLINE
44 # define __STRING_INLINE inline
46 # define __STRING_INLINE extern __inline
50 #if _STRING_ARCH_unaligned
51 /* If we can do unaligned memory accesses we must know the endianess. */
53 # include <bits/types.h>
55 # if __BYTE_ORDER == __LITTLE_ENDIAN
56 # define __STRING2_SMALL_GET16(src, idx) \
57 (((__const unsigned char *) (__const char *) (src))[idx + 1] << 8 \
58 | ((__const unsigned char *) (__const char *) (src))[idx])
59 # define __STRING2_SMALL_GET32(src, idx) \
60 (((((__const unsigned char *) (__const char *) (src))[idx + 3] << 8 \
61 | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8 \
62 | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8 \
63 | ((__const unsigned char *) (__const char *) (src))[idx])
65 # define __STRING2_SMALL_GET16(src, idx) \
66 (((__const unsigned char *) (__const char *) (src))[idx] << 8 \
67 | ((__const unsigned char *) (__const char *) (src))[idx + 1])
68 # define __STRING2_SMALL_GET32(src, idx) \
69 (((((__const unsigned char *) (__const char *) (src))[idx] << 8 \
70 | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8 \
71 | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8 \
72 | ((__const unsigned char *) (__const char *) (src))[idx + 3])
75 /* These are a few types we need for the optimizations if we cannot
76 use unaligned memory accesses. */
77 # define __STRING2_COPY_TYPE(N) \
78 typedef struct { unsigned char __arr[N]; } \
79 __STRING2_COPY_ARR##N __attribute__ ((packed))
80 __STRING2_COPY_TYPE (2);
81 __STRING2_COPY_TYPE (3);
82 __STRING2_COPY_TYPE (4);
83 __STRING2_COPY_TYPE (5);
84 __STRING2_COPY_TYPE (6);
85 __STRING2_COPY_TYPE (7);
86 __STRING2_COPY_TYPE (8);
87 # undef __STRING2_COPY_TYPE
90 /* Dereferencing a pointer arg to run sizeof on it fails for the void
91 pointer case, so we use this instead.
92 Note that __x is evaluated twice. */
93 #define __string2_1bptr_p(__x) \
94 ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
96 /* Set N bytes of S to C. */
97 #ifndef _HAVE_STRING_ARCH_memset
98 # if _STRING_ARCH_unaligned
99 # define memset(s, c, n) \
100 (__extension__ (__builtin_constant_p (n) && (n) <= 16 \
102 ? __memset_1 (s, c) \
103 : __memset_gc (s, c, n)) \
104 : (__builtin_constant_p (c) && (c) == '\0' \
105 ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
106 : memset (s, c, n))))
108 # define __memset_1(s, c) ({ void *__s = (s); \
109 *((__uint8_t *) __s) = (__uint8_t) c; __s; })
111 # define __memset_gc(s, c, n) \
112 ({ void *__s = (s); \
115 unsigned short int __usi; \
116 unsigned char __uc; \
118 __uint8_t __c = (__uint8_t) (c); \
120 /* This `switch' statement will be removed at compile-time. */ \
121 switch ((unsigned int) (n)) \
124 __u->__ui = __c * 0x01010101; \
125 __u = __extension__ ((void *) __u + 4); \
127 __u->__ui = __c * 0x01010101; \
128 __u = __extension__ ((void *) __u + 4); \
130 __u->__ui = __c * 0x01010101; \
131 __u = __extension__ ((void *) __u + 4); \
133 __u->__usi = (unsigned short int) __c * 0x0101; \
134 __u = __extension__ ((void *) __u + 2); \
135 __u->__uc = (unsigned char) __c; \
139 __u->__ui = __c * 0x01010101; \
140 __u = __extension__ ((void *) __u + 4); \
142 __u->__ui = __c * 0x01010101; \
143 __u = __extension__ ((void *) __u + 4); \
145 __u->__ui = __c * 0x01010101; \
146 __u = __extension__ ((void *) __u + 4); \
148 __u->__usi = (unsigned short int) __c * 0x0101; \
152 __u->__ui = __c * 0x01010101; \
153 __u = __extension__ ((void *) __u + 4); \
155 __u->__ui = __c * 0x01010101; \
156 __u = __extension__ ((void *) __u + 4); \
158 __u->__ui = __c * 0x01010101; \
159 __u = __extension__ ((void *) __u + 4); \
161 __u->__uc = (unsigned char) __c; \
165 __u->__ui = __c * 0x01010101; \
166 __u = __extension__ ((void *) __u + 4); \
168 __u->__ui = __c * 0x01010101; \
169 __u = __extension__ ((void *) __u + 4); \
171 __u->__ui = __c * 0x01010101; \
172 __u = __extension__ ((void *) __u + 4); \
174 __u->__ui = __c * 0x01010101; \
181 # if ! __GNUC_PREREQ (3, 0)
182 # define memset(s, c, n) \
183 (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
184 ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
189 /* GCC optimizes memset(s, 0, n) but not bzero(s, n).
190 The optimization is broken before EGCS 1.1. */
191 # if __GNUC_PREREQ (2, 91)
192 # define __bzero(s, n) __builtin_memset (s, '\0', n)
198 /* Copy N bytes from SRC to DEST, returning pointer to byte following the
201 # if !defined _HAVE_STRING_ARCH_mempcpy || defined _FORCE_INLINES
202 # ifndef _HAVE_STRING_ARCH_mempcpy
203 # if __GNUC_PREREQ (3, 0)
204 # define __mempcpy(dest, src, n) \
205 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
206 && __string2_1bptr_p (src) && n <= 8 \
207 ? __builtin_memcpy (dest, src, n) + (n) \
208 : __mempcpy (dest, src, n)))
210 # define __mempcpy(dest, src, n) \
211 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
212 && __string2_1bptr_p (src) && n <= 8 \
213 ? __mempcpy_small (dest, __mempcpy_args (src), n) \
214 : __mempcpy (dest, src, n)))
216 /* In glibc we use this function frequently but for namespace reasons
217 we have to use the name `__mempcpy'. */
218 # define mempcpy(dest, src, n) __mempcpy (dest, src, n)
221 # if !__GNUC_PREREQ (3, 0) || defined _FORCE_INLINES
222 # if _STRING_ARCH_unaligned
223 # ifndef _FORCE_INLINES
224 # define __mempcpy_args(src) \
225 ((__const char *) (src))[0], ((__const char *) (src))[2], \
226 ((__const char *) (src))[4], ((__const char *) (src))[6], \
227 __extension__ __STRING2_SMALL_GET16 (src, 0), \
228 __extension__ __STRING2_SMALL_GET16 (src, 4), \
229 __extension__ __STRING2_SMALL_GET32 (src, 0), \
230 __extension__ __STRING2_SMALL_GET32 (src, 4)
232 __STRING_INLINE
void *__mempcpy_small (void *, char, char, char, char,
233 __uint16_t
, __uint16_t
, __uint32_t
,
235 __STRING_INLINE
void *
236 __mempcpy_small (void *__dest1
,
237 char __src0_1
, char __src2_1
, char __src4_1
, char __src6_1
,
238 __uint16_t __src0_2
, __uint16_t __src4_2
,
239 __uint32_t __src0_4
, __uint32_t __src4_4
,
248 switch ((unsigned int) __srclen
)
252 __u
= __extension__ ((void *) __u
+ 1);
255 __u
->__usi
= __src0_2
;
256 __u
= __extension__ ((void *) __u
+ 2);
259 __u
->__usi
= __src0_2
;
260 __u
= __extension__ ((void *) __u
+ 2);
262 __u
= __extension__ ((void *) __u
+ 1);
265 __u
->__ui
= __src0_4
;
266 __u
= __extension__ ((void *) __u
+ 4);
269 __u
->__ui
= __src0_4
;
270 __u
= __extension__ ((void *) __u
+ 4);
272 __u
= __extension__ ((void *) __u
+ 1);
275 __u
->__ui
= __src0_4
;
276 __u
= __extension__ ((void *) __u
+ 4);
277 __u
->__usi
= __src4_2
;
278 __u
= __extension__ ((void *) __u
+ 2);
281 __u
->__ui
= __src0_4
;
282 __u
= __extension__ ((void *) __u
+ 4);
283 __u
->__usi
= __src4_2
;
284 __u
= __extension__ ((void *) __u
+ 2);
286 __u
= __extension__ ((void *) __u
+ 1);
289 __u
->__ui
= __src0_4
;
290 __u
= __extension__ ((void *) __u
+ 4);
291 __u
->__ui
= __src4_4
;
292 __u
= __extension__ ((void *) __u
+ 4);
298 # ifndef _FORCE_INLINES
299 # define __mempcpy_args(src) \
300 ((__const char *) (src))[0], \
301 __extension__ ((__STRING2_COPY_ARR2) \
302 { { ((__const char *) (src))[0], ((__const char *) (src))[1] } }), \
303 __extension__ ((__STRING2_COPY_ARR3) \
304 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
305 ((__const char *) (src))[2] } }), \
306 __extension__ ((__STRING2_COPY_ARR4) \
307 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
308 ((__const char *) (src))[2], ((__const char *) (src))[3] } }), \
309 __extension__ ((__STRING2_COPY_ARR5) \
310 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
311 ((__const char *) (src))[2], ((__const char *) (src))[3], \
312 ((__const char *) (src))[4] } }), \
313 __extension__ ((__STRING2_COPY_ARR6) \
314 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
315 ((__const char *) (src))[2], ((__const char *) (src))[3], \
316 ((__const char *) (src))[4], ((__const char *) (src))[5] } }), \
317 __extension__ ((__STRING2_COPY_ARR7) \
318 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
319 ((__const char *) (src))[2], ((__const char *) (src))[3], \
320 ((__const char *) (src))[4], ((__const char *) (src))[5], \
321 ((__const char *) (src))[6] } }), \
322 __extension__ ((__STRING2_COPY_ARR8) \
323 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
324 ((__const char *) (src))[2], ((__const char *) (src))[3], \
325 ((__const char *) (src))[4], ((__const char *) (src))[5], \
326 ((__const char *) (src))[6], ((__const char *) (src))[7] } })
328 __STRING_INLINE
void *__mempcpy_small (void *, char, __STRING2_COPY_ARR2
,
334 __STRING2_COPY_ARR8
, size_t);
335 __STRING_INLINE
void *
336 __mempcpy_small (void *__dest
, char __src1
,
337 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
338 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
339 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
340 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
344 __STRING2_COPY_ARR2 __sca2
;
345 __STRING2_COPY_ARR3 __sca3
;
346 __STRING2_COPY_ARR4 __sca4
;
347 __STRING2_COPY_ARR5 __sca5
;
348 __STRING2_COPY_ARR6 __sca6
;
349 __STRING2_COPY_ARR7 __sca7
;
350 __STRING2_COPY_ARR8 __sca8
;
352 switch ((unsigned int) __srclen
)
358 __extension__ __u
->__sca2
= __src2
;
361 __extension__ __u
->__sca3
= __src3
;
364 __extension__ __u
->__sca4
= __src4
;
367 __extension__ __u
->__sca5
= __src5
;
370 __extension__ __u
->__sca6
= __src6
;
373 __extension__ __u
->__sca7
= __src7
;
376 __extension__ __u
->__sca8
= __src8
;
379 return __extension__ ((void *) __u
+ __srclen
);
387 /* Return pointer to C in S. */
388 #ifndef _HAVE_STRING_ARCH_strchr
389 extern void *__rawmemchr (const void *__s
, int __c
);
390 # define strchr(s, c) \
391 (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
392 ? (char *) __rawmemchr (s, c) \
397 /* Copy SRC to DEST. */
398 #if (!defined _HAVE_STRING_ARCH_strcpy && !__GNUC_PREREQ (3, 0)) \
399 || defined _FORCE_INLINES
400 # if !defined _HAVE_STRING_ARCH_strcpy && !__GNUC_PREREQ (3, 0)
401 # define strcpy(dest, src) \
402 (__extension__ (__builtin_constant_p (src) \
403 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
404 ? __strcpy_small (dest, __strcpy_args (src), \
406 : (char *) memcpy (dest, src, strlen (src) + 1)) \
407 : strcpy (dest, src)))
410 # if _STRING_ARCH_unaligned
411 # ifndef _FORCE_INLINES
412 # define __strcpy_args(src) \
413 __extension__ __STRING2_SMALL_GET16 (src, 0), \
414 __extension__ __STRING2_SMALL_GET16 (src, 4), \
415 __extension__ __STRING2_SMALL_GET32 (src, 0), \
416 __extension__ __STRING2_SMALL_GET32 (src, 4)
418 __STRING_INLINE
char *__strcpy_small (char *, __uint16_t
, __uint16_t
,
419 __uint32_t
, __uint32_t
, size_t);
420 __STRING_INLINE
char *
421 __strcpy_small (char *__dest
,
422 __uint16_t __src0_2
, __uint16_t __src4_2
,
423 __uint32_t __src0_4
, __uint32_t __src4_4
,
430 } *__u
= (void *) __dest
;
431 switch ((unsigned int) __srclen
)
437 __u
->__usi
= __src0_2
;
440 __u
->__usi
= __src0_2
;
441 __u
= __extension__ ((void *) __u
+ 2);
445 __u
->__ui
= __src0_4
;
448 __u
->__ui
= __src0_4
;
449 __u
= __extension__ ((void *) __u
+ 4);
453 __u
->__ui
= __src0_4
;
454 __u
= __extension__ ((void *) __u
+ 4);
455 __u
->__usi
= __src4_2
;
458 __u
->__ui
= __src0_4
;
459 __u
= __extension__ ((void *) __u
+ 4);
460 __u
->__usi
= __src4_2
;
461 __u
= __extension__ ((void *) __u
+ 2);
465 __u
->__ui
= __src0_4
;
466 __u
= __extension__ ((void *) __u
+ 4);
467 __u
->__ui
= __src4_4
;
473 # ifndef _FORCE_INLINES
474 # define __strcpy_args(src) \
475 __extension__ ((__STRING2_COPY_ARR2) \
476 { { ((__const char *) (src))[0], '\0' } }), \
477 __extension__ ((__STRING2_COPY_ARR3) \
478 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
480 __extension__ ((__STRING2_COPY_ARR4) \
481 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
482 ((__const char *) (src))[2], '\0' } }), \
483 __extension__ ((__STRING2_COPY_ARR5) \
484 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
485 ((__const char *) (src))[2], ((__const char *) (src))[3], \
487 __extension__ ((__STRING2_COPY_ARR6) \
488 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
489 ((__const char *) (src))[2], ((__const char *) (src))[3], \
490 ((__const char *) (src))[4], '\0' } }), \
491 __extension__ ((__STRING2_COPY_ARR7) \
492 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
493 ((__const char *) (src))[2], ((__const char *) (src))[3], \
494 ((__const char *) (src))[4], ((__const char *) (src))[5], \
496 __extension__ ((__STRING2_COPY_ARR8) \
497 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
498 ((__const char *) (src))[2], ((__const char *) (src))[3], \
499 ((__const char *) (src))[4], ((__const char *) (src))[5], \
500 ((__const char *) (src))[6], '\0' } })
502 __STRING_INLINE
char *__strcpy_small (char *, __STRING2_COPY_ARR2
,
508 __STRING2_COPY_ARR8
, size_t);
509 __STRING_INLINE
char *
510 __strcpy_small (char *__dest
,
511 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
512 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
513 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
514 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
518 __STRING2_COPY_ARR2 __sca2
;
519 __STRING2_COPY_ARR3 __sca3
;
520 __STRING2_COPY_ARR4 __sca4
;
521 __STRING2_COPY_ARR5 __sca5
;
522 __STRING2_COPY_ARR6 __sca6
;
523 __STRING2_COPY_ARR7 __sca7
;
524 __STRING2_COPY_ARR8 __sca8
;
525 } *__u
= (void *) __dest
;
526 switch ((unsigned int) __srclen
)
532 __extension__ __u
->__sca2
= __src2
;
535 __extension__ __u
->__sca3
= __src3
;
538 __extension__ __u
->__sca4
= __src4
;
541 __extension__ __u
->__sca5
= __src5
;
544 __extension__ __u
->__sca6
= __src6
;
547 __extension__ __u
->__sca7
= __src7
;
550 __extension__ __u
->__sca8
= __src8
;
559 /* Copy SRC to DEST, returning pointer to final NUL byte. */
561 # if !defined _HAVE_STRING_ARCH_stpcpy || defined _FORCE_INLINES
562 # ifndef _HAVE_STRING_ARCH_stpcpy
563 # if __GNUC_PREREQ (3, 0)
564 # define __stpcpy(dest, src) \
565 (__extension__ (__builtin_constant_p (src) \
566 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
567 ? __builtin_strcpy (dest, src) + strlen (src) \
568 : ((char *) (__mempcpy) (dest, src, strlen (src) + 1) \
570 : __stpcpy (dest, src)))
572 # define __stpcpy(dest, src) \
573 (__extension__ (__builtin_constant_p (src) \
574 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
575 ? __stpcpy_small (dest, __stpcpy_args (src), \
577 : ((char *) (__mempcpy) (dest, src, strlen (src) + 1) \
579 : __stpcpy (dest, src)))
581 /* In glibc we use this function frequently but for namespace reasons
582 we have to use the name `__stpcpy'. */
583 # define stpcpy(dest, src) __stpcpy (dest, src)
586 # if !__GNUC_PREREQ (3, 0) || defined _FORCE_INLINES
587 # if _STRING_ARCH_unaligned
588 # ifndef _FORCE_INLINES
589 # define __stpcpy_args(src) \
590 __extension__ __STRING2_SMALL_GET16 (src, 0), \
591 __extension__ __STRING2_SMALL_GET16 (src, 4), \
592 __extension__ __STRING2_SMALL_GET32 (src, 0), \
593 __extension__ __STRING2_SMALL_GET32 (src, 4)
595 __STRING_INLINE
char *__stpcpy_small (char *, __uint16_t
, __uint16_t
,
596 __uint32_t
, __uint32_t
, size_t);
597 __STRING_INLINE
char *
598 __stpcpy_small (char *__dest
,
599 __uint16_t __src0_2
, __uint16_t __src4_2
,
600 __uint32_t __src0_4
, __uint32_t __src4_4
,
605 unsigned short int __usi
;
608 } *__u
= (void *) __dest
;
609 switch ((unsigned int) __srclen
)
615 __u
->__usi
= __src0_2
;
616 __u
= __extension__ ((void *) __u
+ 1);
619 __u
->__usi
= __src0_2
;
620 __u
= __extension__ ((void *) __u
+ 2);
624 __u
->__ui
= __src0_4
;
625 __u
= __extension__ ((void *) __u
+ 3);
628 __u
->__ui
= __src0_4
;
629 __u
= __extension__ ((void *) __u
+ 4);
633 __u
->__ui
= __src0_4
;
634 __u
= __extension__ ((void *) __u
+ 4);
635 __u
->__usi
= __src4_2
;
636 __u
= __extension__ ((void *) __u
+ 1);
639 __u
->__ui
= __src0_4
;
640 __u
= __extension__ ((void *) __u
+ 4);
641 __u
->__usi
= __src4_2
;
642 __u
= __extension__ ((void *) __u
+ 2);
646 __u
->__ui
= __src0_4
;
647 __u
= __extension__ ((void *) __u
+ 4);
648 __u
->__ui
= __src4_4
;
649 __u
= __extension__ ((void *) __u
+ 3);
655 # ifndef _FORCE_INLINES
656 # define __stpcpy_args(src) \
657 __extension__ ((__STRING2_COPY_ARR2) \
658 { { ((__const char *) (src))[0], '\0' } }), \
659 __extension__ ((__STRING2_COPY_ARR3) \
660 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
662 __extension__ ((__STRING2_COPY_ARR4) \
663 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
664 ((__const char *) (src))[2], '\0' } }), \
665 __extension__ ((__STRING2_COPY_ARR5) \
666 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
667 ((__const char *) (src))[2], ((__const char *) (src))[3], \
669 __extension__ ((__STRING2_COPY_ARR6) \
670 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
671 ((__const char *) (src))[2], ((__const char *) (src))[3], \
672 ((__const char *) (src))[4], '\0' } }), \
673 __extension__ ((__STRING2_COPY_ARR7) \
674 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
675 ((__const char *) (src))[2], ((__const char *) (src))[3], \
676 ((__const char *) (src))[4], ((__const char *) (src))[5], \
678 __extension__ ((__STRING2_COPY_ARR8) \
679 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
680 ((__const char *) (src))[2], ((__const char *) (src))[3], \
681 ((__const char *) (src))[4], ((__const char *) (src))[5], \
682 ((__const char *) (src))[6], '\0' } })
684 __STRING_INLINE
char *__stpcpy_small (char *, __STRING2_COPY_ARR2
,
690 __STRING2_COPY_ARR8
, size_t);
691 __STRING_INLINE
char *
692 __stpcpy_small (char *__dest
,
693 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
694 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
695 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
696 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
700 __STRING2_COPY_ARR2 __sca2
;
701 __STRING2_COPY_ARR3 __sca3
;
702 __STRING2_COPY_ARR4 __sca4
;
703 __STRING2_COPY_ARR5 __sca5
;
704 __STRING2_COPY_ARR6 __sca6
;
705 __STRING2_COPY_ARR7 __sca7
;
706 __STRING2_COPY_ARR8 __sca8
;
707 } *__u
= (void *) __dest
;
708 switch ((unsigned int) __srclen
)
714 __extension__ __u
->__sca2
= __src2
;
717 __extension__ __u
->__sca3
= __src3
;
720 __extension__ __u
->__sca4
= __src4
;
723 __extension__ __u
->__sca5
= __src5
;
726 __extension__ __u
->__sca6
= __src6
;
729 __extension__ __u
->__sca7
= __src7
;
732 __extension__ __u
->__sca8
= __src8
;
735 return __dest
+ __srclen
- 1;
743 /* Copy no more than N characters of SRC to DEST. */
744 #ifndef _HAVE_STRING_ARCH_strncpy
745 # if defined _USE_STRING_ARCH_memset && defined _USE_STRING_ARCH_mempcpy
746 # define strncpy(dest, src, n) \
747 (__extension__ ({ char *__dest = (dest); \
748 __builtin_constant_p (src) && __builtin_constant_p (n) \
749 ? (strlen (src) + 1 >= ((size_t) (n)) \
750 ? (char *) memcpy (__dest, src, n) \
751 : (memset (__mempcpy (__dest, src, strlen (src)), \
752 '\0', n - strlen (src)), \
754 : strncpy (__dest, src, n); }))
756 # define strncpy(dest, src, n) \
757 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
758 ? (strlen (src) + 1 >= ((size_t) (n)) \
759 ? (char *) memcpy (dest, src, n) \
760 : strncpy (dest, src, n)) \
761 : strncpy (dest, src, n)))
766 /* Append no more than N characters from SRC onto DEST. */
767 #ifndef _HAVE_STRING_ARCH_strncat
768 # ifdef _USE_STRING_ARCH_strchr
769 # define strncat(dest, src, n) \
770 (__extension__ ({ char *__dest = (dest); \
771 __builtin_constant_p (src) && __builtin_constant_p (n) \
772 ? (strlen (src) < ((size_t) (n)) \
773 ? strcat (__dest, src) \
774 : (*((char *) __mempcpy (strchr (__dest, '\0'), \
775 src, n)) = '\0', __dest)) \
776 : strncat (dest, src, n); }))
778 # define strncat(dest, src, n) \
779 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
780 ? (strlen (src) < ((size_t) (n)) \
781 ? strcat (dest, src) \
782 : strncat (dest, src, n)) \
783 : strncat (dest, src, n)))
788 /* Compare characters of S1 and S2. */
789 #ifndef _HAVE_STRING_ARCH_strcmp
790 # define strcmp(s1, s2) \
792 ({ size_t __s1_len, __s2_len; \
793 (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
794 && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
795 (!__string2_1bptr_p (s1) || __s1_len >= 4) \
796 && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
797 ? memcmp ((__const char *) (s1), (__const char *) (s2), \
798 (__s1_len < __s2_len ? __s1_len : __s2_len) + 1) \
799 : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
800 && (__s1_len = strlen (s1), __s1_len < 4) \
801 ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
802 ? __strcmp_cc (s1, s2, __s1_len) \
803 : __strcmp_cg (s1, s2, __s1_len)) \
804 : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
805 && (__s2_len = strlen (s2), __s2_len < 4) \
806 ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
807 ? __strcmp_cc (s1, s2, __s2_len) \
808 : __strcmp_gc (s1, s2, __s2_len)) \
809 : strcmp (s1, s2)))); })
811 # define __strcmp_cc(s1, s2, l) \
812 (__extension__ ({ register int __result = \
813 (((__const unsigned char *) (__const char *) (s1))[0] \
814 - ((__const unsigned char *) (__const char *)(s2))[0]);\
815 if (l > 0 && __result == 0) \
817 __result = (((__const unsigned char *) \
818 (__const char *) (s1))[1] \
819 - ((__const unsigned char *) \
820 (__const char *) (s2))[1]); \
821 if (l > 1 && __result == 0) \
824 (((__const unsigned char *) \
825 (__const char *) (s1))[2] \
826 - ((__const unsigned char *) \
827 (__const char *) (s2))[2]); \
828 if (l > 2 && __result == 0) \
830 (((__const unsigned char *) \
831 (__const char *) (s1))[3] \
832 - ((__const unsigned char *) \
833 (__const char *) (s2))[3]); \
838 # define __strcmp_cg(s1, s2, l1) \
839 (__extension__ ({ __const unsigned char *__s2 = \
840 (__const unsigned char *) (__const char *) (s2); \
841 register int __result = \
842 (((__const unsigned char *) (__const char *) (s1))[0] \
844 if (l1 > 0 && __result == 0) \
846 __result = (((__const unsigned char *) \
847 (__const char *) (s1))[1] - __s2[1]); \
848 if (l1 > 1 && __result == 0) \
850 __result = (((__const unsigned char *) \
851 (__const char *) (s1))[2] - __s2[2]);\
852 if (l1 > 2 && __result == 0) \
853 __result = (((__const unsigned char *) \
854 (__const char *) (s1))[3] \
860 # define __strcmp_gc(s1, s2, l2) \
861 (__extension__ ({ __const unsigned char *__s1 = \
862 (__const unsigned char *) (__const char *) (s1); \
863 register int __result = \
864 __s1[0] - ((__const unsigned char *) \
865 (__const char *) (s2))[0]; \
866 if (l2 > 0 && __result == 0) \
868 __result = (__s1[1] \
869 - ((__const unsigned char *) \
870 (__const char *) (s2))[1]); \
871 if (l2 > 1 && __result == 0) \
874 (__s1[2] - ((__const unsigned char *) \
875 (__const char *) (s2))[2]); \
876 if (l2 > 2 && __result == 0) \
879 - ((__const unsigned char *) \
880 (__const char *) (s2))[3]); \
887 /* Compare N characters of S1 and S2. */
888 #ifndef _HAVE_STRING_ARCH_strncmp
889 # define strncmp(s1, s2, n) \
890 (__extension__ (__builtin_constant_p (n) \
891 && ((__builtin_constant_p (s1) \
892 && strlen (s1) < ((size_t) (n))) \
893 || (__builtin_constant_p (s2) \
894 && strlen (s2) < ((size_t) (n)))) \
895 ? strcmp (s1, s2) : strncmp (s1, s2, n)))
899 /* Return the length of the initial segment of S which
900 consists entirely of characters not in REJECT. */
901 #if !defined _HAVE_STRING_ARCH_strcspn || defined _FORCE_INLINES
902 # ifndef _HAVE_STRING_ARCH_strcspn
903 # define strcspn(s, reject) \
905 ({ char __r0, __r1, __r2; \
906 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
907 ? ((__r0 = ((__const char *) (reject))[0], __r0 == '\0') \
909 : ((__r1 = ((__const char *) (reject))[1], __r1 == '\0') \
910 ? __strcspn_c1 (s, __r0) \
911 : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
912 ? __strcspn_c2 (s, __r0, __r1) \
913 : (((__const char *) (reject))[3] == '\0' \
914 ? __strcspn_c3 (s, __r0, __r1, __r2) \
915 : strcspn (s, reject))))) \
916 : strcspn (s, reject)); })
919 __STRING_INLINE
size_t __strcspn_c1 (__const
char *__s
, int __reject
);
920 __STRING_INLINE
size_t
921 __strcspn_c1 (__const
char *__s
, int __reject
)
923 register size_t __result
= 0;
924 while (__s
[__result
] != '\0' && __s
[__result
] != __reject
)
929 __STRING_INLINE
size_t __strcspn_c2 (__const
char *__s
, int __reject1
,
931 __STRING_INLINE
size_t
932 __strcspn_c2 (__const
char *__s
, int __reject1
, int __reject2
)
934 register size_t __result
= 0;
935 while (__s
[__result
] != '\0' && __s
[__result
] != __reject1
936 && __s
[__result
] != __reject2
)
941 __STRING_INLINE
size_t __strcspn_c3 (__const
char *__s
, int __reject1
,
942 int __reject2
, int __reject3
);
943 __STRING_INLINE
size_t
944 __strcspn_c3 (__const
char *__s
, int __reject1
, int __reject2
,
947 register size_t __result
= 0;
948 while (__s
[__result
] != '\0' && __s
[__result
] != __reject1
949 && __s
[__result
] != __reject2
&& __s
[__result
] != __reject3
)
956 /* Return the length of the initial segment of S which
957 consists entirely of characters in ACCEPT. */
958 #if !defined _HAVE_STRING_ARCH_strspn || defined _FORCE_INLINES
959 # ifndef _HAVE_STRING_ARCH_strspn
960 # define strspn(s, accept) \
962 ({ char __a0, __a1, __a2; \
963 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
964 ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
966 : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
967 ? __strspn_c1 (s, __a0) \
968 : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
969 ? __strspn_c2 (s, __a0, __a1) \
970 : (((__const char *) (accept))[3] == '\0' \
971 ? __strspn_c3 (s, __a0, __a1, __a2) \
972 : strspn (s, accept))))) \
973 : strspn (s, accept)); })
976 __STRING_INLINE
size_t __strspn_c1 (__const
char *__s
, int __accept
);
977 __STRING_INLINE
size_t
978 __strspn_c1 (__const
char *__s
, int __accept
)
980 register size_t __result
= 0;
981 /* Please note that __accept never can be '\0'. */
982 while (__s
[__result
] == __accept
)
987 __STRING_INLINE
size_t __strspn_c2 (__const
char *__s
, int __accept1
,
989 __STRING_INLINE
size_t
990 __strspn_c2 (__const
char *__s
, int __accept1
, int __accept2
)
992 register size_t __result
= 0;
993 /* Please note that __accept1 and __accept2 never can be '\0'. */
994 while (__s
[__result
] == __accept1
|| __s
[__result
] == __accept2
)
999 __STRING_INLINE
size_t __strspn_c3 (__const
char *__s
, int __accept1
,
1000 int __accept2
, int __accept3
);
1001 __STRING_INLINE
size_t
1002 __strspn_c3 (__const
char *__s
, int __accept1
, int __accept2
, int __accept3
)
1004 register size_t __result
= 0;
1005 /* Please note that __accept1 to __accept3 never can be '\0'. */
1006 while (__s
[__result
] == __accept1
|| __s
[__result
] == __accept2
1007 || __s
[__result
] == __accept3
)
1014 /* Find the first occurrence in S of any character in ACCEPT. */
1015 #if !defined _HAVE_STRING_ARCH_strpbrk || defined _FORCE_INLINES
1016 # ifndef _HAVE_STRING_ARCH_strpbrk
1017 # define strpbrk(s, accept) \
1019 ({ char __a0, __a1, __a2; \
1020 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
1021 ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
1022 ? ((void) (s), NULL) \
1023 : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
1024 ? strchr (s, __a0) \
1025 : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
1026 ? __strpbrk_c2 (s, __a0, __a1) \
1027 : (((__const char *) (accept))[3] == '\0' \
1028 ? __strpbrk_c3 (s, __a0, __a1, __a2) \
1029 : strpbrk (s, accept))))) \
1030 : strpbrk (s, accept)); })
1033 __STRING_INLINE
char *__strpbrk_c2 (__const
char *__s
, int __accept1
,
1035 __STRING_INLINE
char *
1036 __strpbrk_c2 (__const
char *__s
, int __accept1
, int __accept2
)
1038 /* Please note that __accept1 and __accept2 never can be '\0'. */
1039 while (*__s
!= '\0' && *__s
!= __accept1
&& *__s
!= __accept2
)
1041 return *__s
== '\0' ? NULL
: (char *) (size_t) __s
;
1044 __STRING_INLINE
char *__strpbrk_c3 (__const
char *__s
, int __accept1
,
1045 int __accept2
, int __accept3
);
1046 __STRING_INLINE
char *
1047 __strpbrk_c3 (__const
char *__s
, int __accept1
, int __accept2
,
1050 /* Please note that __accept1 to __accept3 never can be '\0'. */
1051 while (*__s
!= '\0' && *__s
!= __accept1
&& *__s
!= __accept2
1052 && *__s
!= __accept3
)
1054 return *__s
== '\0' ? NULL
: (char *) (size_t) __s
;
1059 /* Find the first occurrence of NEEDLE in HAYSTACK. Newer gcc versions
1061 #if !defined _HAVE_STRING_ARCH_strstr && !__GNUC_PREREQ (2, 97)
1062 # define strstr(haystack, needle) \
1063 (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
1064 ? (((__const char *) (needle))[0] == '\0' \
1065 ? (char *) (size_t) (haystack) \
1066 : (((__const char *) (needle))[1] == '\0' \
1067 ? strchr (haystack, \
1068 ((__const char *) (needle))[0]) \
1069 : strstr (haystack, needle))) \
1070 : strstr (haystack, needle)))
1074 #if !defined _HAVE_STRING_ARCH_strtok_r || defined _FORCE_INLINES
1075 # ifndef _HAVE_STRING_ARCH_strtok_r
1076 # define __strtok_r(s, sep, nextp) \
1077 (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \
1078 ? (((__const char *) (sep))[0] != '\0' \
1079 && ((__const char *) (sep))[1] == '\0' \
1080 ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp) \
1081 : __strtok_r (s, sep, nextp)) \
1082 : __strtok_r (s, sep, nextp)))
1085 __STRING_INLINE
char *__strtok_r_1c (char *__s
, char __sep
, char **__nextp
);
1086 __STRING_INLINE
char *
1087 __strtok_r_1c (char *__s
, char __sep
, char **__nextp
)
1092 while (*__s
== __sep
)
1098 while (*__s
!= '\0')
1099 if (*__s
++ == __sep
)
1108 # if defined __USE_POSIX || defined __USE_MISC
1109 # define strtok_r(s, sep, nextp) __strtok_r (s, sep, nextp)
1114 #if !defined _HAVE_STRING_ARCH_strsep || defined _FORCE_INLINES
1115 # ifndef _HAVE_STRING_ARCH_strsep
1117 extern char *__strsep_g (char **__stringp
, __const
char *__delim
);
1118 # define __strsep(s, reject) \
1120 ({ char __r0, __r1, __r2; \
1121 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
1122 && (__r0 = ((__const char *) (reject))[0], \
1123 ((__const char *) (reject))[0] != '\0') \
1124 ? ((__r1 = ((__const char *) (reject))[1], \
1125 ((__const char *) (reject))[1] == '\0') \
1126 ? __strsep_1c (s, __r0) \
1127 : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
1128 ? __strsep_2c (s, __r0, __r1) \
1129 : (((__const char *) (reject))[3] == '\0' \
1130 ? __strsep_3c (s, __r0, __r1, __r2) \
1131 : __strsep_g (s, reject)))) \
1132 : __strsep_g (s, reject)); })
1135 __STRING_INLINE
char *__strsep_1c (char **__s
, char __reject
);
1136 __STRING_INLINE
char *
1137 __strsep_1c (char **__s
, char __reject
)
1139 register char *__retval
= *__s
;
1140 if (__retval
!= NULL
&& (*__s
= strchr (__retval
, __reject
)) != NULL
)
1145 __STRING_INLINE
char *__strsep_2c (char **__s
, char __reject1
, char __reject2
);
1146 __STRING_INLINE
char *
1147 __strsep_2c (char **__s
, char __reject1
, char __reject2
)
1149 register char *__retval
= *__s
;
1150 if (__retval
!= NULL
)
1152 register char *__cp
= __retval
;
1160 if (*__cp
== __reject1
|| *__cp
== __reject2
)
1172 __STRING_INLINE
char *__strsep_3c (char **__s
, char __reject1
, char __reject2
,
1174 __STRING_INLINE
char *
1175 __strsep_3c (char **__s
, char __reject1
, char __reject2
, char __reject3
)
1177 register char *__retval
= *__s
;
1178 if (__retval
!= NULL
)
1180 register char *__cp
= __retval
;
1188 if (*__cp
== __reject1
|| *__cp
== __reject2
|| *__cp
== __reject3
)
1200 # define strsep(s, reject) __strsep (s, reject)
1204 /* We need the memory allocation functions for inline strdup().
1205 Referring to stdlib.h (even minimally) is not allowed
1206 in any of the tight standards compliant modes. */
1209 # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
1210 # define __need_malloc_and_calloc
1211 # include <stdlib.h>
1214 # ifndef _HAVE_STRING_ARCH_strdup
1216 extern char *__strdup (__const
char *__string
) __THROW __attribute_malloc__
;
1217 # define __strdup(s) \
1218 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
1219 ? (((__const char *) (s))[0] == '\0' \
1220 ? (char *) calloc (1, 1) \
1221 : ({ size_t __len = strlen (s) + 1; \
1222 char *__retval = (char *) malloc (__len); \
1223 if (__retval != NULL) \
1224 __retval = (char *) memcpy (__retval, s, __len); \
1228 # if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
1229 # define strdup(s) __strdup (s)
1233 # ifndef _HAVE_STRING_ARCH_strndup
1235 extern char *__strndup (__const
char *__string
, size_t __n
)
1236 __THROW __attribute_malloc__
;
1237 # define __strndup(s, n) \
1238 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
1239 ? (((__const char *) (s))[0] == '\0' \
1240 ? (char *) calloc (1, 1) \
1241 : ({ size_t __len = strlen (s) + 1; \
1246 __retval = (char *) malloc (__len); \
1247 if (__retval != NULL) \
1249 __retval[__len - 1] = '\0'; \
1250 __retval = (char *) memcpy (__retval, s, \
1254 : __strndup (s, n)))
1257 # define strndup(s, n) __strndup (s, n)
1261 #endif /* Use misc. or use GNU. */
1263 #ifndef _FORCE_INLINES
1264 # undef __STRING_INLINE
1267 #endif /* No string inlines. */