1 /* Machine-independant string function optimizations.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 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 # define memset(s, c, n) \
182 (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
183 ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
187 /* GCC optimizes memset(s, 0, n) but not bzero(s, n).
188 The optimization is broken before EGCS 1.1. */
189 # if __GNUC_PREREQ (2, 91)
190 # define __bzero(s, n) __builtin_memset (s, '\0', n)
196 /* Copy N bytes from SRC to DEST, returning pointer to byte following the
199 # if !defined _HAVE_STRING_ARCH_mempcpy || defined _FORCE_INLINES
200 # ifndef _HAVE_STRING_ARCH_mempcpy
201 # define __mempcpy(dest, src, n) \
202 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
203 && __string2_1bptr_p (src) && n <= 8 \
204 ? __mempcpy_small (dest, __mempcpy_args (src), n) \
205 : __mempcpy (dest, src, n)))
206 /* In glibc we use this function frequently but for namespace reasons
207 we have to use the name `__mempcpy'. */
208 # define mempcpy(dest, src, n) __mempcpy (dest, src, n)
211 # if _STRING_ARCH_unaligned
212 # ifndef _FORCE_INLINES
213 # define __mempcpy_args(src) \
214 ((__const char *) (src))[0], ((__const char *) (src))[2], \
215 ((__const char *) (src))[4], ((__const char *) (src))[6], \
216 __extension__ __STRING2_SMALL_GET16 (src, 0), \
217 __extension__ __STRING2_SMALL_GET16 (src, 4), \
218 __extension__ __STRING2_SMALL_GET32 (src, 0), \
219 __extension__ __STRING2_SMALL_GET32 (src, 4)
221 __STRING_INLINE
void *__mempcpy_small (void *, char, char, char, char,
222 __uint16_t
, __uint16_t
, __uint32_t
,
224 __STRING_INLINE
void *
225 __mempcpy_small (void *__dest1
,
226 char __src0_1
, char __src2_1
, char __src4_1
, char __src6_1
,
227 __uint16_t __src0_2
, __uint16_t __src4_2
,
228 __uint32_t __src0_4
, __uint32_t __src4_4
,
237 switch ((unsigned int) __srclen
)
241 __u
= __extension__ ((void *) __u
+ 1);
244 __u
->__usi
= __src0_2
;
245 __u
= __extension__ ((void *) __u
+ 2);
248 __u
->__usi
= __src0_2
;
249 __u
= __extension__ ((void *) __u
+ 2);
251 __u
= __extension__ ((void *) __u
+ 1);
254 __u
->__ui
= __src0_4
;
255 __u
= __extension__ ((void *) __u
+ 4);
258 __u
->__ui
= __src0_4
;
259 __u
= __extension__ ((void *) __u
+ 4);
261 __u
= __extension__ ((void *) __u
+ 1);
264 __u
->__ui
= __src0_4
;
265 __u
= __extension__ ((void *) __u
+ 4);
266 __u
->__usi
= __src4_2
;
267 __u
= __extension__ ((void *) __u
+ 2);
270 __u
->__ui
= __src0_4
;
271 __u
= __extension__ ((void *) __u
+ 4);
272 __u
->__usi
= __src4_2
;
273 __u
= __extension__ ((void *) __u
+ 2);
275 __u
= __extension__ ((void *) __u
+ 1);
278 __u
->__ui
= __src0_4
;
279 __u
= __extension__ ((void *) __u
+ 4);
280 __u
->__ui
= __src4_4
;
281 __u
= __extension__ ((void *) __u
+ 4);
287 # ifndef _FORCE_INLINES
288 # define __mempcpy_args(src) \
289 ((__const char *) (src))[0], \
290 __extension__ ((__STRING2_COPY_ARR2) \
291 { { ((__const char *) (src))[0], ((__const char *) (src))[1] } }), \
292 __extension__ ((__STRING2_COPY_ARR3) \
293 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
294 ((__const char *) (src))[2] } }), \
295 __extension__ ((__STRING2_COPY_ARR4) \
296 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
297 ((__const char *) (src))[2], ((__const char *) (src))[3] } }), \
298 __extension__ ((__STRING2_COPY_ARR5) \
299 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
300 ((__const char *) (src))[2], ((__const char *) (src))[3], \
301 ((__const char *) (src))[4] } }), \
302 __extension__ ((__STRING2_COPY_ARR6) \
303 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
304 ((__const char *) (src))[2], ((__const char *) (src))[3], \
305 ((__const char *) (src))[4], ((__const char *) (src))[5] } }), \
306 __extension__ ((__STRING2_COPY_ARR7) \
307 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
308 ((__const char *) (src))[2], ((__const char *) (src))[3], \
309 ((__const char *) (src))[4], ((__const char *) (src))[5], \
310 ((__const char *) (src))[6] } }), \
311 __extension__ ((__STRING2_COPY_ARR8) \
312 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
313 ((__const char *) (src))[2], ((__const char *) (src))[3], \
314 ((__const char *) (src))[4], ((__const char *) (src))[5], \
315 ((__const char *) (src))[6], ((__const char *) (src))[7] } })
317 __STRING_INLINE
void *__mempcpy_small (void *, char, __STRING2_COPY_ARR2
,
323 __STRING2_COPY_ARR8
, size_t);
324 __STRING_INLINE
void *
325 __mempcpy_small (void *__dest
, char __src1
,
326 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
327 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
328 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
329 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
333 __STRING2_COPY_ARR2 __sca2
;
334 __STRING2_COPY_ARR3 __sca3
;
335 __STRING2_COPY_ARR4 __sca4
;
336 __STRING2_COPY_ARR5 __sca5
;
337 __STRING2_COPY_ARR6 __sca6
;
338 __STRING2_COPY_ARR7 __sca7
;
339 __STRING2_COPY_ARR8 __sca8
;
341 switch ((unsigned int) __srclen
)
347 __extension__ __u
->__sca2
= __src2
;
350 __extension__ __u
->__sca3
= __src3
;
353 __extension__ __u
->__sca4
= __src4
;
356 __extension__ __u
->__sca5
= __src5
;
359 __extension__ __u
->__sca6
= __src6
;
362 __extension__ __u
->__sca7
= __src7
;
365 __extension__ __u
->__sca8
= __src8
;
368 return __extension__ ((void *) __u
+ __srclen
);
375 /* Return pointer to C in S. */
376 #ifndef _HAVE_STRING_ARCH_strchr
377 extern void *__rawmemchr (const void *__s
, int __c
);
378 # define strchr(s, c) \
379 (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
380 ? (char *) __rawmemchr (s, c) \
385 /* Copy SRC to DEST. */
386 #if !defined _HAVE_STRING_ARCH_strcpy || defined _FORCE_INLINES
387 # ifndef _HAVE_STRING_ARCH_strcpy
388 # define strcpy(dest, src) \
389 (__extension__ (__builtin_constant_p (src) \
390 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
391 ? __strcpy_small (dest, __strcpy_args (src), \
393 : (char *) memcpy (dest, src, strlen (src) + 1)) \
394 : strcpy (dest, src)))
397 # if _STRING_ARCH_unaligned
398 # ifndef _FORCE_INLINES
399 # define __strcpy_args(src) \
400 __extension__ __STRING2_SMALL_GET16 (src, 0), \
401 __extension__ __STRING2_SMALL_GET16 (src, 4), \
402 __extension__ __STRING2_SMALL_GET32 (src, 0), \
403 __extension__ __STRING2_SMALL_GET32 (src, 4)
405 __STRING_INLINE
char *__strcpy_small (char *, __uint16_t
, __uint16_t
,
406 __uint32_t
, __uint32_t
, size_t);
407 __STRING_INLINE
char *
408 __strcpy_small (char *__dest
,
409 __uint16_t __src0_2
, __uint16_t __src4_2
,
410 __uint32_t __src0_4
, __uint32_t __src4_4
,
417 } *__u
= (void *) __dest
;
418 switch ((unsigned int) __srclen
)
424 __u
->__usi
= __src0_2
;
427 __u
->__usi
= __src0_2
;
428 __u
= __extension__ ((void *) __u
+ 2);
432 __u
->__ui
= __src0_4
;
435 __u
->__ui
= __src0_4
;
436 __u
= __extension__ ((void *) __u
+ 4);
440 __u
->__ui
= __src0_4
;
441 __u
= __extension__ ((void *) __u
+ 4);
442 __u
->__usi
= __src4_2
;
445 __u
->__ui
= __src0_4
;
446 __u
= __extension__ ((void *) __u
+ 4);
447 __u
->__usi
= __src4_2
;
448 __u
= __extension__ ((void *) __u
+ 2);
452 __u
->__ui
= __src0_4
;
453 __u
= __extension__ ((void *) __u
+ 4);
454 __u
->__ui
= __src4_4
;
460 # ifndef _FORCE_INLINES
461 # define __strcpy_args(src) \
462 __extension__ ((__STRING2_COPY_ARR2) \
463 { { ((__const char *) (src))[0], '\0' } }), \
464 __extension__ ((__STRING2_COPY_ARR3) \
465 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
467 __extension__ ((__STRING2_COPY_ARR4) \
468 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
469 ((__const char *) (src))[2], '\0' } }), \
470 __extension__ ((__STRING2_COPY_ARR5) \
471 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
472 ((__const char *) (src))[2], ((__const char *) (src))[3], \
474 __extension__ ((__STRING2_COPY_ARR6) \
475 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
476 ((__const char *) (src))[2], ((__const char *) (src))[3], \
477 ((__const char *) (src))[4], '\0' } }), \
478 __extension__ ((__STRING2_COPY_ARR7) \
479 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
480 ((__const char *) (src))[2], ((__const char *) (src))[3], \
481 ((__const char *) (src))[4], ((__const char *) (src))[5], \
483 __extension__ ((__STRING2_COPY_ARR8) \
484 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
485 ((__const char *) (src))[2], ((__const char *) (src))[3], \
486 ((__const char *) (src))[4], ((__const char *) (src))[5], \
487 ((__const char *) (src))[6], '\0' } })
489 __STRING_INLINE
char *__strcpy_small (char *, __STRING2_COPY_ARR2
,
495 __STRING2_COPY_ARR8
, size_t);
496 __STRING_INLINE
char *
497 __strcpy_small (char *__dest
,
498 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
499 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
500 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
501 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
505 __STRING2_COPY_ARR2 __sca2
;
506 __STRING2_COPY_ARR3 __sca3
;
507 __STRING2_COPY_ARR4 __sca4
;
508 __STRING2_COPY_ARR5 __sca5
;
509 __STRING2_COPY_ARR6 __sca6
;
510 __STRING2_COPY_ARR7 __sca7
;
511 __STRING2_COPY_ARR8 __sca8
;
512 } *__u
= (void *) __dest
;
513 switch ((unsigned int) __srclen
)
519 __extension__ __u
->__sca2
= __src2
;
522 __extension__ __u
->__sca3
= __src3
;
525 __extension__ __u
->__sca4
= __src4
;
528 __extension__ __u
->__sca5
= __src5
;
531 __extension__ __u
->__sca6
= __src6
;
534 __extension__ __u
->__sca7
= __src7
;
537 __extension__ __u
->__sca8
= __src8
;
546 /* Copy SRC to DEST, returning pointer to final NUL byte. */
548 # if !defined _HAVE_STRING_ARCH_stpcpy || defined _FORCE_INLINES
549 # ifndef _HAVE_STRING_ARCH_stpcpy
550 # define __stpcpy(dest, src) \
551 (__extension__ (__builtin_constant_p (src) \
552 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
553 ? __stpcpy_small (dest, __stpcpy_args (src), \
555 : ((char *) __mempcpy (dest, src, strlen (src) + 1) - 1))\
556 : __stpcpy (dest, src)))
557 /* In glibc we use this function frequently but for namespace reasons
558 we have to use the name `__stpcpy'. */
559 # define stpcpy(dest, src) __stpcpy (dest, src)
562 # if _STRING_ARCH_unaligned
563 # ifndef _FORCE_INLINES
564 # define __stpcpy_args(src) \
565 __extension__ __STRING2_SMALL_GET16 (src, 0), \
566 __extension__ __STRING2_SMALL_GET16 (src, 4), \
567 __extension__ __STRING2_SMALL_GET32 (src, 0), \
568 __extension__ __STRING2_SMALL_GET32 (src, 4)
570 __STRING_INLINE
char *__stpcpy_small (char *, __uint16_t
, __uint16_t
,
571 __uint32_t
, __uint32_t
, size_t);
572 __STRING_INLINE
char *
573 __stpcpy_small (char *__dest
,
574 __uint16_t __src0_2
, __uint16_t __src4_2
,
575 __uint32_t __src0_4
, __uint32_t __src4_4
,
580 unsigned short int __usi
;
583 } *__u
= (void *) __dest
;
584 switch ((unsigned int) __srclen
)
590 __u
->__usi
= __src0_2
;
591 __u
= __extension__ ((void *) __u
+ 1);
594 __u
->__usi
= __src0_2
;
595 __u
= __extension__ ((void *) __u
+ 2);
599 __u
->__ui
= __src0_4
;
600 __u
= __extension__ ((void *) __u
+ 3);
603 __u
->__ui
= __src0_4
;
604 __u
= __extension__ ((void *) __u
+ 4);
608 __u
->__ui
= __src0_4
;
609 __u
= __extension__ ((void *) __u
+ 4);
610 __u
->__usi
= __src4_2
;
611 __u
= __extension__ ((void *) __u
+ 1);
614 __u
->__ui
= __src0_4
;
615 __u
= __extension__ ((void *) __u
+ 4);
616 __u
->__usi
= __src4_2
;
617 __u
= __extension__ ((void *) __u
+ 2);
621 __u
->__ui
= __src0_4
;
622 __u
= __extension__ ((void *) __u
+ 4);
623 __u
->__ui
= __src4_4
;
624 __u
= __extension__ ((void *) __u
+ 3);
630 # ifndef _FORCE_INLINES
631 # define __stpcpy_args(src) \
632 __extension__ ((__STRING2_COPY_ARR2) \
633 { { ((__const char *) (src))[0], '\0' } }), \
634 __extension__ ((__STRING2_COPY_ARR3) \
635 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
637 __extension__ ((__STRING2_COPY_ARR4) \
638 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
639 ((__const char *) (src))[2], '\0' } }), \
640 __extension__ ((__STRING2_COPY_ARR5) \
641 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
642 ((__const char *) (src))[2], ((__const char *) (src))[3], \
644 __extension__ ((__STRING2_COPY_ARR6) \
645 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
646 ((__const char *) (src))[2], ((__const char *) (src))[3], \
647 ((__const char *) (src))[4], '\0' } }), \
648 __extension__ ((__STRING2_COPY_ARR7) \
649 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
650 ((__const char *) (src))[2], ((__const char *) (src))[3], \
651 ((__const char *) (src))[4], ((__const char *) (src))[5], \
653 __extension__ ((__STRING2_COPY_ARR8) \
654 { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
655 ((__const char *) (src))[2], ((__const char *) (src))[3], \
656 ((__const char *) (src))[4], ((__const char *) (src))[5], \
657 ((__const char *) (src))[6], '\0' } })
659 __STRING_INLINE
char *__stpcpy_small (char *, __STRING2_COPY_ARR2
,
665 __STRING2_COPY_ARR8
, size_t);
666 __STRING_INLINE
char *
667 __stpcpy_small (char *__dest
,
668 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
669 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
670 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
671 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
675 __STRING2_COPY_ARR2 __sca2
;
676 __STRING2_COPY_ARR3 __sca3
;
677 __STRING2_COPY_ARR4 __sca4
;
678 __STRING2_COPY_ARR5 __sca5
;
679 __STRING2_COPY_ARR6 __sca6
;
680 __STRING2_COPY_ARR7 __sca7
;
681 __STRING2_COPY_ARR8 __sca8
;
682 } *__u
= (void *) __dest
;
683 switch ((unsigned int) __srclen
)
689 __extension__ __u
->__sca2
= __src2
;
692 __extension__ __u
->__sca3
= __src3
;
695 __extension__ __u
->__sca4
= __src4
;
698 __extension__ __u
->__sca5
= __src5
;
701 __extension__ __u
->__sca6
= __src6
;
704 __extension__ __u
->__sca7
= __src7
;
707 __extension__ __u
->__sca8
= __src8
;
710 return __dest
+ __srclen
- 1;
717 /* Copy no more than N characters of SRC to DEST. */
718 #ifndef _HAVE_STRING_ARCH_strncpy
719 # if defined _USE_STRING_ARCH_memset && defined _USE_STRING_ARCH_mempcpy
720 # define strncpy(dest, src, n) \
721 (__extension__ ({ char *__dest = (dest); \
722 __builtin_constant_p (src) && __builtin_constant_p (n) \
723 ? (strlen (src) + 1 >= ((size_t) (n)) \
724 ? (char *) memcpy (__dest, src, n) \
725 : (memset (__mempcpy (__dest, src, strlen (src)), \
726 '\0', n - strlen (src)), \
728 : strncpy (__dest, src, n); }))
730 # define strncpy(dest, src, n) \
731 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
732 ? (strlen (src) + 1 >= ((size_t) (n)) \
733 ? (char *) memcpy (dest, src, n) \
734 : strncpy (dest, src, n)) \
735 : strncpy (dest, src, n)))
740 /* Append no more than N characters from SRC onto DEST. */
741 #ifndef _HAVE_STRING_ARCH_strncat
742 # ifdef _USE_STRING_ARCH_strchr
743 # define strncat(dest, src, n) \
744 (__extension__ ({ char *__dest = (dest); \
745 __builtin_constant_p (src) && __builtin_constant_p (n) \
746 ? (strlen (src) < ((size_t) (n)) \
747 ? strcat (__dest, src) \
748 : (*((char *) __mempcpy (strchr (__dest, '\0'), \
749 src, n)) = '\0', __dest)) \
750 : strncat (dest, src, n); }))
752 # define strncat(dest, src, n) \
753 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
754 ? (strlen (src) < ((size_t) (n)) \
755 ? strcat (dest, src) \
756 : strncat (dest, src, n)) \
757 : strncat (dest, src, n)))
762 /* Compare characters of S1 and S2. */
763 #ifndef _HAVE_STRING_ARCH_strcmp
764 # define strcmp(s1, s2) \
766 ({ size_t __s1_len, __s2_len; \
767 (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
768 && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
769 (!__string2_1bptr_p (s1) || __s1_len >= 4) \
770 && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
771 ? memcmp ((__const char *) (s1), (__const char *) (s2), \
772 (__s1_len < __s2_len ? __s1_len : __s2_len) + 1) \
773 : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
774 && (__s1_len = strlen (s1), __s1_len < 4) \
775 ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
776 ? __strcmp_cc (s1, s2, __s1_len) \
777 : __strcmp_cg (s1, s2, __s1_len)) \
778 : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
779 && (__s2_len = strlen (s2), __s2_len < 4) \
780 ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
781 ? __strcmp_cc (s1, s2, __s2_len) \
782 : __strcmp_gc (s1, s2, __s2_len)) \
783 : strcmp (s1, s2)))); })
785 # define __strcmp_cc(s1, s2, l) \
786 (__extension__ ({ register int __result = \
787 (((__const unsigned char *) (__const char *) (s1))[0] \
788 - ((__const unsigned char *) (__const char *)(s2))[0]);\
789 if (l > 0 && __result == 0) \
791 __result = (((__const unsigned char *) \
792 (__const char *) (s1))[1] \
793 - ((__const unsigned char *) \
794 (__const char *) (s2))[1]); \
795 if (l > 1 && __result == 0) \
798 (((__const unsigned char *) \
799 (__const char *) (s1))[2] \
800 - ((__const unsigned char *) \
801 (__const char *) (s2))[2]); \
802 if (l > 2 && __result == 0) \
804 (((__const unsigned char *) \
805 (__const char *) (s1))[3] \
806 - ((__const unsigned char *) \
807 (__const char *) (s2))[3]); \
812 # define __strcmp_cg(s1, s2, l1) \
813 (__extension__ ({ __const unsigned char *__s2 = \
814 (__const unsigned char *) (__const char *) (s2); \
815 register int __result = \
816 (((__const unsigned char *) (__const char *) (s1))[0] \
818 if (l1 > 0 && __result == 0) \
820 __result = (((__const unsigned char *) \
821 (__const char *) (s1))[1] - __s2[1]); \
822 if (l1 > 1 && __result == 0) \
824 __result = (((__const unsigned char *) \
825 (__const char *) (s1))[2] - __s2[2]);\
826 if (l1 > 2 && __result == 0) \
827 __result = (((__const unsigned char *) \
828 (__const char *) (s1))[3] \
834 # define __strcmp_gc(s1, s2, l2) \
835 (__extension__ ({ __const unsigned char *__s1 = \
836 (__const unsigned char *) (__const char *) (s1); \
837 register int __result = \
838 __s1[0] - ((__const unsigned char *) \
839 (__const char *) (s2))[0]; \
840 if (l2 > 0 && __result == 0) \
842 __result = (__s1[1] \
843 - ((__const unsigned char *) \
844 (__const char *) (s2))[1]); \
845 if (l2 > 1 && __result == 0) \
848 (__s1[2] - ((__const unsigned char *) \
849 (__const char *) (s2))[2]); \
850 if (l2 > 2 && __result == 0) \
853 - ((__const unsigned char *) \
854 (__const char *) (s2))[3]); \
861 /* Compare N characters of S1 and S2. */
862 #ifndef _HAVE_STRING_ARCH_strncmp
863 # define strncmp(s1, s2, n) \
864 (__extension__ (__builtin_constant_p (n) \
865 && ((__builtin_constant_p (s1) \
866 && strlen (s1) < ((size_t) (n))) \
867 || (__builtin_constant_p (s2) \
868 && strlen (s2) < ((size_t) (n)))) \
869 ? strcmp (s1, s2) : strncmp (s1, s2, n)))
873 /* Return the length of the initial segment of S which
874 consists entirely of characters not in REJECT. */
875 #if !defined _HAVE_STRING_ARCH_strcspn || defined _FORCE_INLINES
876 # ifndef _HAVE_STRING_ARCH_strcspn
877 # define strcspn(s, reject) \
879 ({ char __r0, __r1, __r2; \
880 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
881 ? ((__r0 = ((__const char *) (reject))[0], __r0 == '\0') \
883 : ((__r1 = ((__const char *) (reject))[1], __r1 == '\0') \
884 ? __strcspn_c1 (s, __r0) \
885 : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
886 ? __strcspn_c2 (s, __r0, __r1) \
887 : (((__const char *) (reject))[3] == '\0' \
888 ? __strcspn_c3 (s, __r0, __r1, __r2) \
889 : strcspn (s, reject))))) \
890 : strcspn (s, reject)); })
893 __STRING_INLINE
size_t __strcspn_c1 (__const
char *__s
, int __reject
);
894 __STRING_INLINE
size_t
895 __strcspn_c1 (__const
char *__s
, int __reject
)
897 register size_t __result
= 0;
898 while (__s
[__result
] != '\0' && __s
[__result
] != __reject
)
903 __STRING_INLINE
size_t __strcspn_c2 (__const
char *__s
, int __reject1
,
905 __STRING_INLINE
size_t
906 __strcspn_c2 (__const
char *__s
, int __reject1
, int __reject2
)
908 register size_t __result
= 0;
909 while (__s
[__result
] != '\0' && __s
[__result
] != __reject1
910 && __s
[__result
] != __reject2
)
915 __STRING_INLINE
size_t __strcspn_c3 (__const
char *__s
, int __reject1
,
916 int __reject2
, int __reject3
);
917 __STRING_INLINE
size_t
918 __strcspn_c3 (__const
char *__s
, int __reject1
, int __reject2
,
921 register size_t __result
= 0;
922 while (__s
[__result
] != '\0' && __s
[__result
] != __reject1
923 && __s
[__result
] != __reject2
&& __s
[__result
] != __reject3
)
930 /* Return the length of the initial segment of S which
931 consists entirely of characters in ACCEPT. */
932 #if !defined _HAVE_STRING_ARCH_strspn || defined _FORCE_INLINES
933 # ifndef _HAVE_STRING_ARCH_strspn
934 # define strspn(s, accept) \
936 ({ char __a0, __a1, __a2; \
937 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
938 ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
940 : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
941 ? __strspn_c1 (s, __a0) \
942 : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
943 ? __strspn_c2 (s, __a0, __a1) \
944 : (((__const char *) (accept))[3] == '\0' \
945 ? __strspn_c3 (s, __a0, __a1, __a2) \
946 : strspn (s, accept))))) \
947 : strspn (s, accept)); })
950 __STRING_INLINE
size_t __strspn_c1 (__const
char *__s
, int __accept
);
951 __STRING_INLINE
size_t
952 __strspn_c1 (__const
char *__s
, int __accept
)
954 register size_t __result
= 0;
955 /* Please note that __accept never can be '\0'. */
956 while (__s
[__result
] == __accept
)
961 __STRING_INLINE
size_t __strspn_c2 (__const
char *__s
, int __accept1
,
963 __STRING_INLINE
size_t
964 __strspn_c2 (__const
char *__s
, int __accept1
, int __accept2
)
966 register size_t __result
= 0;
967 /* Please note that __accept1 and __accept2 never can be '\0'. */
968 while (__s
[__result
] == __accept1
|| __s
[__result
] == __accept2
)
973 __STRING_INLINE
size_t __strspn_c3 (__const
char *__s
, int __accept1
,
974 int __accept2
, int __accept3
);
975 __STRING_INLINE
size_t
976 __strspn_c3 (__const
char *__s
, int __accept1
, int __accept2
, int __accept3
)
978 register size_t __result
= 0;
979 /* Please note that __accept1 to __accept3 never can be '\0'. */
980 while (__s
[__result
] == __accept1
|| __s
[__result
] == __accept2
981 || __s
[__result
] == __accept3
)
988 /* Find the first occurrence in S of any character in ACCEPT. */
989 #if !defined _HAVE_STRING_ARCH_strpbrk || defined _FORCE_INLINES
990 # ifndef _HAVE_STRING_ARCH_strpbrk
991 # define strpbrk(s, accept) \
993 ({ char __a0, __a1, __a2; \
994 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
995 ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
996 ? ((void) (s), NULL) \
997 : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
999 : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
1000 ? __strpbrk_c2 (s, __a0, __a1) \
1001 : (((__const char *) (accept))[3] == '\0' \
1002 ? __strpbrk_c3 (s, __a0, __a1, __a2) \
1003 : strpbrk (s, accept))))) \
1004 : strpbrk (s, accept)); })
1007 __STRING_INLINE
char *__strpbrk_c2 (__const
char *__s
, int __accept1
,
1009 __STRING_INLINE
char *
1010 __strpbrk_c2 (__const
char *__s
, int __accept1
, int __accept2
)
1012 /* Please note that __accept1 and __accept2 never can be '\0'. */
1013 while (*__s
!= '\0' && *__s
!= __accept1
&& *__s
!= __accept2
)
1015 return *__s
== '\0' ? NULL
: (char *) (size_t) __s
;
1018 __STRING_INLINE
char *__strpbrk_c3 (__const
char *__s
, int __accept1
,
1019 int __accept2
, int __accept3
);
1020 __STRING_INLINE
char *
1021 __strpbrk_c3 (__const
char *__s
, int __accept1
, int __accept2
,
1024 /* Please note that __accept1 to __accept3 never can be '\0'. */
1025 while (*__s
!= '\0' && *__s
!= __accept1
&& *__s
!= __accept2
1026 && *__s
!= __accept3
)
1028 return *__s
== '\0' ? NULL
: (char *) (size_t) __s
;
1033 /* Find the first occurrence of NEEDLE in HAYSTACK. Newer gcc versions
1035 #if !defined _HAVE_STRING_ARCH_strstr && !__GNUC_PREREQ (2, 97)
1036 # define strstr(haystack, needle) \
1037 (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
1038 ? (((__const char *) (needle))[0] == '\0' \
1039 ? (char *) (size_t) (haystack) \
1040 : (((__const char *) (needle))[1] == '\0' \
1041 ? strchr (haystack, \
1042 ((__const char *) (needle))[0]) \
1043 : strstr (haystack, needle))) \
1044 : strstr (haystack, needle)))
1048 #if !defined _HAVE_STRING_ARCH_strtok_r || defined _FORCE_INLINES
1049 # ifndef _HAVE_STRING_ARCH_strtok_r
1050 # define __strtok_r(s, sep, nextp) \
1051 (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \
1052 ? (((__const char *) (sep))[0] != '\0' \
1053 && ((__const char *) (sep))[1] == '\0' \
1054 ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp) \
1055 : __strtok_r (s, sep, nextp)) \
1056 : __strtok_r (s, sep, nextp)))
1059 __STRING_INLINE
char *__strtok_r_1c (char *__s
, char __sep
, char **__nextp
);
1060 __STRING_INLINE
char *
1061 __strtok_r_1c (char *__s
, char __sep
, char **__nextp
)
1066 while (*__s
== __sep
)
1072 while (*__s
!= '\0')
1073 if (*__s
++ == __sep
)
1082 # if defined __USE_POSIX || defined __USE_MISC
1083 # define strtok_r(s, sep, nextp) __strtok_r (s, sep, nextp)
1088 #if !defined _HAVE_STRING_ARCH_strsep || defined _FORCE_INLINES
1089 # ifndef _HAVE_STRING_ARCH_strsep
1091 extern char *__strsep_g (char **__stringp
, __const
char *__delim
);
1092 # define __strsep(s, reject) \
1094 ({ char __r0, __r1, __r2; \
1095 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
1096 && (__r0 = ((__const char *) (reject))[0], \
1097 ((__const char *) (reject))[0] != '\0') \
1098 ? ((__r1 = ((__const char *) (reject))[1], \
1099 ((__const char *) (reject))[1] == '\0') \
1100 ? __strsep_1c (s, __r0) \
1101 : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
1102 ? __strsep_2c (s, __r0, __r1) \
1103 : (((__const char *) (reject))[3] == '\0' \
1104 ? __strsep_3c (s, __r0, __r1, __r2) \
1105 : __strsep_g (s, reject)))) \
1106 : __strsep_g (s, reject)); })
1109 __STRING_INLINE
char *__strsep_1c (char **__s
, char __reject
);
1110 __STRING_INLINE
char *
1111 __strsep_1c (char **__s
, char __reject
)
1113 register char *__retval
= *__s
;
1114 if (__retval
!= NULL
&& (*__s
= strchr (__retval
, __reject
)) != NULL
)
1119 __STRING_INLINE
char *__strsep_2c (char **__s
, char __reject1
, char __reject2
);
1120 __STRING_INLINE
char *
1121 __strsep_2c (char **__s
, char __reject1
, char __reject2
)
1123 register char *__retval
= *__s
;
1124 if (__retval
!= NULL
)
1126 register char *__cp
= __retval
;
1134 if (*__cp
== __reject1
|| *__cp
== __reject2
)
1146 __STRING_INLINE
char *__strsep_3c (char **__s
, char __reject1
, char __reject2
,
1148 __STRING_INLINE
char *
1149 __strsep_3c (char **__s
, char __reject1
, char __reject2
, char __reject3
)
1151 register char *__retval
= *__s
;
1152 if (__retval
!= NULL
)
1154 register char *__cp
= __retval
;
1162 if (*__cp
== __reject1
|| *__cp
== __reject2
|| *__cp
== __reject3
)
1174 # define strsep(s, reject) __strsep (s, reject)
1178 /* We need the memory allocation functions for inline strdup().
1179 Referring to stdlib.h (even minimally) is not allowed
1180 in any of the tight standards compliant modes. */
1183 # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
1184 # define __need_malloc_and_calloc
1185 # include <stdlib.h>
1188 # ifndef _HAVE_STRING_ARCH_strdup
1190 extern char *__strdup (__const
char *__string
) __THROW __attribute_malloc__
;
1191 # define __strdup(s) \
1192 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
1193 ? (((__const char *) (s))[0] == '\0' \
1194 ? (char *) calloc (1, 1) \
1195 : ({ size_t __len = strlen (s) + 1; \
1196 char *__retval = (char *) malloc (__len); \
1197 if (__retval != NULL) \
1198 __retval = (char *) memcpy (__retval, s, __len); \
1202 # if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
1203 # define strdup(s) __strdup (s)
1207 # ifndef _HAVE_STRING_ARCH_strndup
1209 extern char *__strndup (__const
char *__string
, size_t __n
)
1210 __THROW __attribute_malloc__
;
1211 # define __strndup(s, n) \
1212 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
1213 ? (((__const char *) (s))[0] == '\0' \
1214 ? (char *) calloc (1, 1) \
1215 : ({ size_t __len = strlen (s) + 1; \
1220 __retval = (char *) malloc (__len); \
1221 if (__retval != NULL) \
1223 __retval[__len - 1] = '\0'; \
1224 __retval = (char *) memcpy (__retval, s, \
1228 : __strndup (s, n)))
1231 # define strndup(s, n) __strndup (s, n)
1235 #endif /* Use misc. or use GNU. */
1237 #ifndef _FORCE_INLINES
1238 # undef __STRING_INLINE
1241 #endif /* No string inlines. */