1 /* Machine-independant string function optimizations.
2 Copyright (C) 1997-2015 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, see
18 <http://www.gnu.org/licenses/>. */
21 # error "Never use <bits/string2.h> directly; include <string.h> instead."
24 #ifndef __NO_STRING_INLINES
26 /* Unlike the definitions in the header <bits/string.h> the
27 definitions contained here are not optimized down to assembler
28 level. Those optimizations are not always a good idea since this
29 means the code size increases a lot. Instead the definitions here
30 optimize some functions in a way which do not dramatically
31 increase the code size and which do not use assembler. The main
32 trick is to use GCC's `__builtin_constant_p' function.
34 Every function XXX which has a defined version in
35 <bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
36 to make sure we don't get redefinitions.
38 We must use here macros instead of inline functions since the
39 trick won't work with the latter. */
41 #ifndef __STRING_INLINE
43 # define __STRING_INLINE inline
45 # define __STRING_INLINE __extern_inline
49 #if _STRING_ARCH_unaligned
50 /* If we can do unaligned memory accesses we must know the endianess. */
52 # include <bits/types.h>
54 # if __BYTE_ORDER == __LITTLE_ENDIAN
55 # define __STRING2_SMALL_GET16(src, idx) \
56 (((const unsigned char *) (const char *) (src))[idx + 1] << 8 \
57 | ((const unsigned char *) (const char *) (src))[idx])
58 # define __STRING2_SMALL_GET32(src, idx) \
59 (((((const unsigned char *) (const char *) (src))[idx + 3] << 8 \
60 | ((const unsigned char *) (const char *) (src))[idx + 2]) << 8 \
61 | ((const unsigned char *) (const char *) (src))[idx + 1]) << 8 \
62 | ((const unsigned char *) (const char *) (src))[idx])
64 # define __STRING2_SMALL_GET16(src, idx) \
65 (((const unsigned char *) (const char *) (src))[idx] << 8 \
66 | ((const unsigned char *) (const char *) (src))[idx + 1])
67 # define __STRING2_SMALL_GET32(src, idx) \
68 (((((const unsigned char *) (const char *) (src))[idx] << 8 \
69 | ((const unsigned char *) (const char *) (src))[idx + 1]) << 8 \
70 | ((const unsigned char *) (const char *) (src))[idx + 2]) << 8 \
71 | ((const unsigned char *) (const char *) (src))[idx + 3])
74 /* These are a few types we need for the optimizations if we cannot
75 use unaligned memory accesses. */
76 # define __STRING2_COPY_TYPE(N) \
77 typedef struct { unsigned char __arr[N]; } \
78 __attribute__ ((__packed__)) __STRING2_COPY_ARR##N
79 __STRING2_COPY_TYPE (2);
80 __STRING2_COPY_TYPE (3);
81 __STRING2_COPY_TYPE (4);
82 __STRING2_COPY_TYPE (5);
83 __STRING2_COPY_TYPE (6);
84 __STRING2_COPY_TYPE (7);
85 __STRING2_COPY_TYPE (8);
86 # undef __STRING2_COPY_TYPE
89 /* Dereferencing a pointer arg to run sizeof on it fails for the void
90 pointer case, so we use this instead.
91 Note that __x is evaluated twice. */
92 #define __string2_1bptr_p(__x) \
93 ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
95 /* Set N bytes of S to C. */
96 #if !defined _HAVE_STRING_ARCH_memset
97 # if !__GNUC_PREREQ (3, 0)
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; }) \
188 /* GCC < 3.0 optimizes memset(s, 0, n) but not bzero(s, n).
189 The optimization is broken before EGCS 1.1.
190 GCC 3.0+ has __builtin_bzero as well, but at least till GCC 3.4
191 if it decides to call the library function, it calls memset
193 # if __GNUC_PREREQ (2, 91)
194 # define __bzero(s, n) __builtin_memset (s, '\0', n)
200 /* Copy N bytes from SRC to DEST, returning pointer to byte following the
203 # if !defined _HAVE_STRING_ARCH_mempcpy || defined _FORCE_INLINES
204 # ifndef _HAVE_STRING_ARCH_mempcpy
205 # if __GNUC_PREREQ (3, 4)
206 # define __mempcpy(dest, src, n) __builtin_mempcpy (dest, src, n)
207 # elif __GNUC_PREREQ (3, 0)
208 # define __mempcpy(dest, src, n) \
209 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
210 && __string2_1bptr_p (src) && n <= 8 \
211 ? __builtin_memcpy (dest, src, n) + (n) \
212 : __mempcpy (dest, src, n)))
214 # define __mempcpy(dest, src, n) \
215 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
216 && __string2_1bptr_p (src) && n <= 8 \
217 ? __mempcpy_small (dest, __mempcpy_args (src), n) \
218 : __mempcpy (dest, src, n)))
220 /* In glibc we use this function frequently but for namespace reasons
221 we have to use the name `__mempcpy'. */
222 # define mempcpy(dest, src, n) __mempcpy (dest, src, n)
225 # if !__GNUC_PREREQ (3, 0) || defined _FORCE_INLINES
226 # if _STRING_ARCH_unaligned
227 # ifndef _FORCE_INLINES
228 # define __mempcpy_args(src) \
229 ((const char *) (src))[0], ((const char *) (src))[2], \
230 ((const char *) (src))[4], ((const char *) (src))[6], \
231 __extension__ __STRING2_SMALL_GET16 (src, 0), \
232 __extension__ __STRING2_SMALL_GET16 (src, 4), \
233 __extension__ __STRING2_SMALL_GET32 (src, 0), \
234 __extension__ __STRING2_SMALL_GET32 (src, 4)
236 __STRING_INLINE
void *__mempcpy_small (void *, char, char, char, char,
237 __uint16_t
, __uint16_t
, __uint32_t
,
239 __STRING_INLINE
void *
240 __mempcpy_small (void *__dest1
,
241 char __src0_1
, char __src2_1
, char __src4_1
, char __src6_1
,
242 __uint16_t __src0_2
, __uint16_t __src4_2
,
243 __uint32_t __src0_4
, __uint32_t __src4_4
,
252 switch ((unsigned int) __srclen
)
256 __u
= __extension__ ((void *) __u
+ 1);
259 __u
->__usi
= __src0_2
;
260 __u
= __extension__ ((void *) __u
+ 2);
263 __u
->__usi
= __src0_2
;
264 __u
= __extension__ ((void *) __u
+ 2);
266 __u
= __extension__ ((void *) __u
+ 1);
269 __u
->__ui
= __src0_4
;
270 __u
= __extension__ ((void *) __u
+ 4);
273 __u
->__ui
= __src0_4
;
274 __u
= __extension__ ((void *) __u
+ 4);
276 __u
= __extension__ ((void *) __u
+ 1);
279 __u
->__ui
= __src0_4
;
280 __u
= __extension__ ((void *) __u
+ 4);
281 __u
->__usi
= __src4_2
;
282 __u
= __extension__ ((void *) __u
+ 2);
285 __u
->__ui
= __src0_4
;
286 __u
= __extension__ ((void *) __u
+ 4);
287 __u
->__usi
= __src4_2
;
288 __u
= __extension__ ((void *) __u
+ 2);
290 __u
= __extension__ ((void *) __u
+ 1);
293 __u
->__ui
= __src0_4
;
294 __u
= __extension__ ((void *) __u
+ 4);
295 __u
->__ui
= __src4_4
;
296 __u
= __extension__ ((void *) __u
+ 4);
302 # ifndef _FORCE_INLINES
303 # define __mempcpy_args(src) \
304 ((const char *) (src))[0], \
305 __extension__ ((__STRING2_COPY_ARR2) \
306 { { ((const char *) (src))[0], ((const char *) (src))[1] } }), \
307 __extension__ ((__STRING2_COPY_ARR3) \
308 { { ((const char *) (src))[0], ((const char *) (src))[1], \
309 ((const char *) (src))[2] } }), \
310 __extension__ ((__STRING2_COPY_ARR4) \
311 { { ((const char *) (src))[0], ((const char *) (src))[1], \
312 ((const char *) (src))[2], ((const char *) (src))[3] } }), \
313 __extension__ ((__STRING2_COPY_ARR5) \
314 { { ((const char *) (src))[0], ((const char *) (src))[1], \
315 ((const char *) (src))[2], ((const char *) (src))[3], \
316 ((const char *) (src))[4] } }), \
317 __extension__ ((__STRING2_COPY_ARR6) \
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 __extension__ ((__STRING2_COPY_ARR7) \
322 { { ((const char *) (src))[0], ((const char *) (src))[1], \
323 ((const char *) (src))[2], ((const char *) (src))[3], \
324 ((const char *) (src))[4], ((const char *) (src))[5], \
325 ((const char *) (src))[6] } }), \
326 __extension__ ((__STRING2_COPY_ARR8) \
327 { { ((const char *) (src))[0], ((const char *) (src))[1], \
328 ((const char *) (src))[2], ((const char *) (src))[3], \
329 ((const char *) (src))[4], ((const char *) (src))[5], \
330 ((const char *) (src))[6], ((const char *) (src))[7] } })
332 __STRING_INLINE
void *__mempcpy_small (void *, char, __STRING2_COPY_ARR2
,
338 __STRING2_COPY_ARR8
, size_t);
339 __STRING_INLINE
void *
340 __mempcpy_small (void *__dest
, char __src1
,
341 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
342 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
343 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
344 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
348 __STRING2_COPY_ARR2 __sca2
;
349 __STRING2_COPY_ARR3 __sca3
;
350 __STRING2_COPY_ARR4 __sca4
;
351 __STRING2_COPY_ARR5 __sca5
;
352 __STRING2_COPY_ARR6 __sca6
;
353 __STRING2_COPY_ARR7 __sca7
;
354 __STRING2_COPY_ARR8 __sca8
;
356 switch ((unsigned int) __srclen
)
362 __extension__ __u
->__sca2
= __src2
;
365 __extension__ __u
->__sca3
= __src3
;
368 __extension__ __u
->__sca4
= __src4
;
371 __extension__ __u
->__sca5
= __src5
;
374 __extension__ __u
->__sca6
= __src6
;
377 __extension__ __u
->__sca7
= __src7
;
380 __extension__ __u
->__sca8
= __src8
;
383 return __extension__ ((void *) __u
+ __srclen
);
391 /* Return pointer to C in S. */
392 #ifndef _HAVE_STRING_ARCH_strchr
393 extern void *__rawmemchr (const void *__s
, int __c
);
394 # if __GNUC_PREREQ (3, 2)
395 # define strchr(s, c) \
396 (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \
398 ? (char *) __rawmemchr (s, c) \
399 : __builtin_strchr (s, c)))
401 # define strchr(s, c) \
402 (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
403 ? (char *) __rawmemchr (s, c) \
409 /* Copy SRC to DEST. */
410 #if (!defined _HAVE_STRING_ARCH_strcpy && !__GNUC_PREREQ (3, 0)) \
411 || defined _FORCE_INLINES
412 # if !defined _HAVE_STRING_ARCH_strcpy && !__GNUC_PREREQ (3, 0)
413 # define strcpy(dest, src) \
414 (__extension__ (__builtin_constant_p (src) \
415 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
416 ? __strcpy_small (dest, __strcpy_args (src), \
418 : (char *) memcpy (dest, src, strlen (src) + 1)) \
419 : strcpy (dest, src)))
422 # if _STRING_ARCH_unaligned
423 # ifndef _FORCE_INLINES
424 # define __strcpy_args(src) \
425 __extension__ __STRING2_SMALL_GET16 (src, 0), \
426 __extension__ __STRING2_SMALL_GET16 (src, 4), \
427 __extension__ __STRING2_SMALL_GET32 (src, 0), \
428 __extension__ __STRING2_SMALL_GET32 (src, 4)
430 __STRING_INLINE
char *__strcpy_small (char *, __uint16_t
, __uint16_t
,
431 __uint32_t
, __uint32_t
, size_t);
432 __STRING_INLINE
char *
433 __strcpy_small (char *__dest
,
434 __uint16_t __src0_2
, __uint16_t __src4_2
,
435 __uint32_t __src0_4
, __uint32_t __src4_4
,
442 } *__u
= (void *) __dest
;
443 switch ((unsigned int) __srclen
)
449 __u
->__usi
= __src0_2
;
452 __u
->__usi
= __src0_2
;
453 __u
= __extension__ ((void *) __u
+ 2);
457 __u
->__ui
= __src0_4
;
460 __u
->__ui
= __src0_4
;
461 __u
= __extension__ ((void *) __u
+ 4);
465 __u
->__ui
= __src0_4
;
466 __u
= __extension__ ((void *) __u
+ 4);
467 __u
->__usi
= __src4_2
;
470 __u
->__ui
= __src0_4
;
471 __u
= __extension__ ((void *) __u
+ 4);
472 __u
->__usi
= __src4_2
;
473 __u
= __extension__ ((void *) __u
+ 2);
477 __u
->__ui
= __src0_4
;
478 __u
= __extension__ ((void *) __u
+ 4);
479 __u
->__ui
= __src4_4
;
485 # ifndef _FORCE_INLINES
486 # define __strcpy_args(src) \
487 __extension__ ((__STRING2_COPY_ARR2) \
488 { { ((const char *) (src))[0], '\0' } }), \
489 __extension__ ((__STRING2_COPY_ARR3) \
490 { { ((const char *) (src))[0], ((const char *) (src))[1], \
492 __extension__ ((__STRING2_COPY_ARR4) \
493 { { ((const char *) (src))[0], ((const char *) (src))[1], \
494 ((const char *) (src))[2], '\0' } }), \
495 __extension__ ((__STRING2_COPY_ARR5) \
496 { { ((const char *) (src))[0], ((const char *) (src))[1], \
497 ((const char *) (src))[2], ((const char *) (src))[3], \
499 __extension__ ((__STRING2_COPY_ARR6) \
500 { { ((const char *) (src))[0], ((const char *) (src))[1], \
501 ((const char *) (src))[2], ((const char *) (src))[3], \
502 ((const char *) (src))[4], '\0' } }), \
503 __extension__ ((__STRING2_COPY_ARR7) \
504 { { ((const char *) (src))[0], ((const char *) (src))[1], \
505 ((const char *) (src))[2], ((const char *) (src))[3], \
506 ((const char *) (src))[4], ((const char *) (src))[5], \
508 __extension__ ((__STRING2_COPY_ARR8) \
509 { { ((const char *) (src))[0], ((const char *) (src))[1], \
510 ((const char *) (src))[2], ((const char *) (src))[3], \
511 ((const char *) (src))[4], ((const char *) (src))[5], \
512 ((const char *) (src))[6], '\0' } })
514 __STRING_INLINE
char *__strcpy_small (char *, __STRING2_COPY_ARR2
,
520 __STRING2_COPY_ARR8
, size_t);
521 __STRING_INLINE
char *
522 __strcpy_small (char *__dest
,
523 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
524 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
525 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
526 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
530 __STRING2_COPY_ARR2 __sca2
;
531 __STRING2_COPY_ARR3 __sca3
;
532 __STRING2_COPY_ARR4 __sca4
;
533 __STRING2_COPY_ARR5 __sca5
;
534 __STRING2_COPY_ARR6 __sca6
;
535 __STRING2_COPY_ARR7 __sca7
;
536 __STRING2_COPY_ARR8 __sca8
;
537 } *__u
= (void *) __dest
;
538 switch ((unsigned int) __srclen
)
544 __extension__ __u
->__sca2
= __src2
;
547 __extension__ __u
->__sca3
= __src3
;
550 __extension__ __u
->__sca4
= __src4
;
553 __extension__ __u
->__sca5
= __src5
;
556 __extension__ __u
->__sca6
= __src6
;
559 __extension__ __u
->__sca7
= __src7
;
562 __extension__ __u
->__sca8
= __src8
;
571 /* Copy SRC to DEST, returning pointer to final NUL byte. */
573 # if !defined _HAVE_STRING_ARCH_stpcpy || defined _FORCE_INLINES
574 # ifndef _HAVE_STRING_ARCH_stpcpy
575 # if __GNUC_PREREQ (3, 4)
576 # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
577 # elif __GNUC_PREREQ (3, 0)
578 # define __stpcpy(dest, src) \
579 (__extension__ (__builtin_constant_p (src) \
580 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
581 ? __builtin_strcpy (dest, src) + strlen (src) \
582 : ((char *) (__mempcpy) (dest, src, strlen (src) + 1) \
584 : __stpcpy (dest, src)))
586 # define __stpcpy(dest, src) \
587 (__extension__ (__builtin_constant_p (src) \
588 ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
589 ? __stpcpy_small (dest, __stpcpy_args (src), \
591 : ((char *) (__mempcpy) (dest, src, strlen (src) + 1) \
593 : __stpcpy (dest, src)))
595 /* In glibc we use this function frequently but for namespace reasons
596 we have to use the name `__stpcpy'. */
597 # define stpcpy(dest, src) __stpcpy (dest, src)
600 # if !__GNUC_PREREQ (3, 0) || defined _FORCE_INLINES
601 # if _STRING_ARCH_unaligned
602 # ifndef _FORCE_INLINES
603 # define __stpcpy_args(src) \
604 __extension__ __STRING2_SMALL_GET16 (src, 0), \
605 __extension__ __STRING2_SMALL_GET16 (src, 4), \
606 __extension__ __STRING2_SMALL_GET32 (src, 0), \
607 __extension__ __STRING2_SMALL_GET32 (src, 4)
609 __STRING_INLINE
char *__stpcpy_small (char *, __uint16_t
, __uint16_t
,
610 __uint32_t
, __uint32_t
, size_t);
611 __STRING_INLINE
char *
612 __stpcpy_small (char *__dest
,
613 __uint16_t __src0_2
, __uint16_t __src4_2
,
614 __uint32_t __src0_4
, __uint32_t __src4_4
,
619 unsigned short int __usi
;
622 } *__u
= (void *) __dest
;
623 switch ((unsigned int) __srclen
)
629 __u
->__usi
= __src0_2
;
630 __u
= __extension__ ((void *) __u
+ 1);
633 __u
->__usi
= __src0_2
;
634 __u
= __extension__ ((void *) __u
+ 2);
638 __u
->__ui
= __src0_4
;
639 __u
= __extension__ ((void *) __u
+ 3);
642 __u
->__ui
= __src0_4
;
643 __u
= __extension__ ((void *) __u
+ 4);
647 __u
->__ui
= __src0_4
;
648 __u
= __extension__ ((void *) __u
+ 4);
649 __u
->__usi
= __src4_2
;
650 __u
= __extension__ ((void *) __u
+ 1);
653 __u
->__ui
= __src0_4
;
654 __u
= __extension__ ((void *) __u
+ 4);
655 __u
->__usi
= __src4_2
;
656 __u
= __extension__ ((void *) __u
+ 2);
660 __u
->__ui
= __src0_4
;
661 __u
= __extension__ ((void *) __u
+ 4);
662 __u
->__ui
= __src4_4
;
663 __u
= __extension__ ((void *) __u
+ 3);
669 # ifndef _FORCE_INLINES
670 # define __stpcpy_args(src) \
671 __extension__ ((__STRING2_COPY_ARR2) \
672 { { ((const char *) (src))[0], '\0' } }), \
673 __extension__ ((__STRING2_COPY_ARR3) \
674 { { ((const char *) (src))[0], ((const char *) (src))[1], \
676 __extension__ ((__STRING2_COPY_ARR4) \
677 { { ((const char *) (src))[0], ((const char *) (src))[1], \
678 ((const char *) (src))[2], '\0' } }), \
679 __extension__ ((__STRING2_COPY_ARR5) \
680 { { ((const char *) (src))[0], ((const char *) (src))[1], \
681 ((const char *) (src))[2], ((const char *) (src))[3], \
683 __extension__ ((__STRING2_COPY_ARR6) \
684 { { ((const char *) (src))[0], ((const char *) (src))[1], \
685 ((const char *) (src))[2], ((const char *) (src))[3], \
686 ((const char *) (src))[4], '\0' } }), \
687 __extension__ ((__STRING2_COPY_ARR7) \
688 { { ((const char *) (src))[0], ((const char *) (src))[1], \
689 ((const char *) (src))[2], ((const char *) (src))[3], \
690 ((const char *) (src))[4], ((const char *) (src))[5], \
692 __extension__ ((__STRING2_COPY_ARR8) \
693 { { ((const char *) (src))[0], ((const char *) (src))[1], \
694 ((const char *) (src))[2], ((const char *) (src))[3], \
695 ((const char *) (src))[4], ((const char *) (src))[5], \
696 ((const char *) (src))[6], '\0' } })
698 __STRING_INLINE
char *__stpcpy_small (char *, __STRING2_COPY_ARR2
,
704 __STRING2_COPY_ARR8
, size_t);
705 __STRING_INLINE
char *
706 __stpcpy_small (char *__dest
,
707 __STRING2_COPY_ARR2 __src2
, __STRING2_COPY_ARR3 __src3
,
708 __STRING2_COPY_ARR4 __src4
, __STRING2_COPY_ARR5 __src5
,
709 __STRING2_COPY_ARR6 __src6
, __STRING2_COPY_ARR7 __src7
,
710 __STRING2_COPY_ARR8 __src8
, size_t __srclen
)
714 __STRING2_COPY_ARR2 __sca2
;
715 __STRING2_COPY_ARR3 __sca3
;
716 __STRING2_COPY_ARR4 __sca4
;
717 __STRING2_COPY_ARR5 __sca5
;
718 __STRING2_COPY_ARR6 __sca6
;
719 __STRING2_COPY_ARR7 __sca7
;
720 __STRING2_COPY_ARR8 __sca8
;
721 } *__u
= (void *) __dest
;
722 switch ((unsigned int) __srclen
)
728 __extension__ __u
->__sca2
= __src2
;
731 __extension__ __u
->__sca3
= __src3
;
734 __extension__ __u
->__sca4
= __src4
;
737 __extension__ __u
->__sca5
= __src5
;
740 __extension__ __u
->__sca6
= __src6
;
743 __extension__ __u
->__sca7
= __src7
;
746 __extension__ __u
->__sca8
= __src8
;
749 return __dest
+ __srclen
- 1;
757 /* Copy no more than N characters of SRC to DEST. */
758 #ifndef _HAVE_STRING_ARCH_strncpy
759 # if __GNUC_PREREQ (3, 2)
760 # define strncpy(dest, src, n) __builtin_strncpy (dest, src, n)
762 # define strncpy(dest, src, n) \
763 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
764 ? (strlen (src) + 1 >= ((size_t) (n)) \
765 ? (char *) memcpy (dest, src, n) \
766 : strncpy (dest, src, n)) \
767 : strncpy (dest, src, n)))
772 /* Append no more than N characters from SRC onto DEST. */
773 #ifndef _HAVE_STRING_ARCH_strncat
774 # ifdef _USE_STRING_ARCH_strchr
775 # define strncat(dest, src, n) \
776 (__extension__ ({ char *__dest = (dest); \
777 __builtin_constant_p (src) && __builtin_constant_p (n) \
778 ? (strlen (src) < ((size_t) (n)) \
779 ? strcat (__dest, src) \
780 : (*((char *) __mempcpy (strchr (__dest, '\0'), \
781 src, n)) = '\0', __dest)) \
782 : strncat (dest, src, n); }))
783 # elif __GNUC_PREREQ (3, 2)
784 # define strncat(dest, src, n) __builtin_strncat (dest, src, n)
786 # define strncat(dest, src, n) \
787 (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
788 ? (strlen (src) < ((size_t) (n)) \
789 ? strcat (dest, src) \
790 : strncat (dest, src, n)) \
791 : strncat (dest, src, n)))
796 /* Compare characters of S1 and S2. */
797 #ifndef _HAVE_STRING_ARCH_strcmp
798 # if __GNUC_PREREQ (3, 2)
799 # define strcmp(s1, s2) \
801 ({ size_t __s1_len, __s2_len; \
802 (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
803 && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
804 (!__string2_1bptr_p (s1) || __s1_len >= 4) \
805 && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
806 ? __builtin_strcmp (s1, s2) \
807 : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
808 && (__s1_len = strlen (s1), __s1_len < 4) \
809 ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
810 ? __builtin_strcmp (s1, s2) \
811 : __strcmp_cg (s1, s2, __s1_len)) \
812 : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
813 && (__s2_len = strlen (s2), __s2_len < 4) \
814 ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
815 ? __builtin_strcmp (s1, s2) \
816 : __strcmp_gc (s1, s2, __s2_len)) \
817 : __builtin_strcmp (s1, s2)))); })
819 # define strcmp(s1, s2) \
821 ({ size_t __s1_len, __s2_len; \
822 (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
823 && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
824 (!__string2_1bptr_p (s1) || __s1_len >= 4) \
825 && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
826 ? memcmp ((const char *) (s1), (const char *) (s2), \
827 (__s1_len < __s2_len ? __s1_len : __s2_len) + 1) \
828 : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
829 && (__s1_len = strlen (s1), __s1_len < 4) \
830 ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
831 ? __strcmp_cc (s1, s2, __s1_len) \
832 : __strcmp_cg (s1, s2, __s1_len)) \
833 : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
834 && (__s2_len = strlen (s2), __s2_len < 4) \
835 ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
836 ? __strcmp_cc (s1, s2, __s2_len) \
837 : __strcmp_gc (s1, s2, __s2_len)) \
838 : strcmp (s1, s2)))); })
841 # define __strcmp_cc(s1, s2, l) \
842 (__extension__ ({ int __result = \
843 (((const unsigned char *) (const char *) (s1))[0] \
844 - ((const unsigned char *) (const char *)(s2))[0]); \
845 if (l > 0 && __result == 0) \
847 __result = (((const unsigned char *) \
848 (const char *) (s1))[1] \
849 - ((const unsigned char *) \
850 (const char *) (s2))[1]); \
851 if (l > 1 && __result == 0) \
854 (((const unsigned char *) \
855 (const char *) (s1))[2] \
856 - ((const unsigned char *) \
857 (const char *) (s2))[2]); \
858 if (l > 2 && __result == 0) \
860 (((const unsigned char *) \
861 (const char *) (s1))[3] \
862 - ((const unsigned char *) \
863 (const char *) (s2))[3]); \
868 # define __strcmp_cg(s1, s2, l1) \
869 (__extension__ ({ const unsigned char *__s2 = \
870 (const unsigned char *) (const char *) (s2); \
872 (((const unsigned char *) (const char *) (s1))[0] \
874 if (l1 > 0 && __result == 0) \
876 __result = (((const unsigned char *) \
877 (const char *) (s1))[1] - __s2[1]); \
878 if (l1 > 1 && __result == 0) \
880 __result = (((const unsigned char *) \
881 (const char *) (s1))[2] - __s2[2]); \
882 if (l1 > 2 && __result == 0) \
883 __result = (((const unsigned char *) \
884 (const char *) (s1))[3] \
890 # define __strcmp_gc(s1, s2, l2) (- __strcmp_cg (s2, s1, l2))
894 /* Compare N characters of S1 and S2. */
895 #ifndef _HAVE_STRING_ARCH_strncmp
896 # define strncmp(s1, s2, n) \
897 (__extension__ (__builtin_constant_p (n) \
898 && ((__builtin_constant_p (s1) \
899 && strlen (s1) < ((size_t) (n))) \
900 || (__builtin_constant_p (s2) \
901 && strlen (s2) < ((size_t) (n)))) \
902 ? strcmp (s1, s2) : strncmp (s1, s2, n)))
906 /* Return the length of the initial segment of S which
907 consists entirely of characters not in REJECT. */
908 #if !defined _HAVE_STRING_ARCH_strcspn || defined _FORCE_INLINES
909 # ifndef _HAVE_STRING_ARCH_strcspn
910 # if __GNUC_PREREQ (3, 2)
911 # define strcspn(s, reject) \
913 ({ char __r0, __r1, __r2; \
914 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
915 ? ((__builtin_constant_p (s) && __string2_1bptr_p (s)) \
916 ? __builtin_strcspn (s, reject) \
917 : ((__r0 = ((const char *) (reject))[0], __r0 == '\0') \
919 : ((__r1 = ((const char *) (reject))[1], __r1 == '\0') \
920 ? __strcspn_c1 (s, __r0) \
921 : ((__r2 = ((const char *) (reject))[2], __r2 == '\0') \
922 ? __strcspn_c2 (s, __r0, __r1) \
923 : (((const char *) (reject))[3] == '\0' \
924 ? __strcspn_c3 (s, __r0, __r1, __r2) \
925 : __builtin_strcspn (s, reject)))))) \
926 : __builtin_strcspn (s, reject)); })
928 # define strcspn(s, reject) \
930 ({ char __r0, __r1, __r2; \
931 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
932 ? ((__r0 = ((const char *) (reject))[0], __r0 == '\0') \
934 : ((__r1 = ((const char *) (reject))[1], __r1 == '\0') \
935 ? __strcspn_c1 (s, __r0) \
936 : ((__r2 = ((const char *) (reject))[2], __r2 == '\0') \
937 ? __strcspn_c2 (s, __r0, __r1) \
938 : (((const char *) (reject))[3] == '\0' \
939 ? __strcspn_c3 (s, __r0, __r1, __r2) \
940 : strcspn (s, reject))))) \
941 : strcspn (s, reject)); })
945 __STRING_INLINE
size_t __strcspn_c1 (const char *__s
, int __reject
);
946 __STRING_INLINE
size_t
947 __strcspn_c1 (const char *__s
, int __reject
)
950 while (__s
[__result
] != '\0' && __s
[__result
] != __reject
)
955 __STRING_INLINE
size_t __strcspn_c2 (const char *__s
, int __reject1
,
957 __STRING_INLINE
size_t
958 __strcspn_c2 (const char *__s
, int __reject1
, int __reject2
)
961 while (__s
[__result
] != '\0' && __s
[__result
] != __reject1
962 && __s
[__result
] != __reject2
)
967 __STRING_INLINE
size_t __strcspn_c3 (const char *__s
, int __reject1
,
968 int __reject2
, int __reject3
);
969 __STRING_INLINE
size_t
970 __strcspn_c3 (const char *__s
, int __reject1
, int __reject2
,
974 while (__s
[__result
] != '\0' && __s
[__result
] != __reject1
975 && __s
[__result
] != __reject2
&& __s
[__result
] != __reject3
)
982 /* Return the length of the initial segment of S which
983 consists entirely of characters in ACCEPT. */
984 #if !defined _HAVE_STRING_ARCH_strspn || defined _FORCE_INLINES
985 # ifndef _HAVE_STRING_ARCH_strspn
986 # if __GNUC_PREREQ (3, 2)
987 # define strspn(s, accept) \
989 ({ char __a0, __a1, __a2; \
990 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
991 ? ((__builtin_constant_p (s) && __string2_1bptr_p (s)) \
992 ? __builtin_strspn (s, accept) \
993 : ((__a0 = ((const char *) (accept))[0], __a0 == '\0') \
994 ? ((void) (s), (size_t) 0) \
995 : ((__a1 = ((const char *) (accept))[1], __a1 == '\0') \
996 ? __strspn_c1 (s, __a0) \
997 : ((__a2 = ((const char *) (accept))[2], __a2 == '\0') \
998 ? __strspn_c2 (s, __a0, __a1) \
999 : (((const char *) (accept))[3] == '\0' \
1000 ? __strspn_c3 (s, __a0, __a1, __a2) \
1001 : __builtin_strspn (s, accept)))))) \
1002 : __builtin_strspn (s, accept)); })
1004 # define strspn(s, accept) \
1006 ({ char __a0, __a1, __a2; \
1007 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
1008 ? ((__a0 = ((const char *) (accept))[0], __a0 == '\0') \
1009 ? ((void) (s), (size_t) 0) \
1010 : ((__a1 = ((const char *) (accept))[1], __a1 == '\0') \
1011 ? __strspn_c1 (s, __a0) \
1012 : ((__a2 = ((const char *) (accept))[2], __a2 == '\0') \
1013 ? __strspn_c2 (s, __a0, __a1) \
1014 : (((const char *) (accept))[3] == '\0' \
1015 ? __strspn_c3 (s, __a0, __a1, __a2) \
1016 : strspn (s, accept))))) \
1017 : strspn (s, accept)); })
1021 __STRING_INLINE
size_t __strspn_c1 (const char *__s
, int __accept
);
1022 __STRING_INLINE
size_t
1023 __strspn_c1 (const char *__s
, int __accept
)
1025 size_t __result
= 0;
1026 /* Please note that __accept never can be '\0'. */
1027 while (__s
[__result
] == __accept
)
1032 __STRING_INLINE
size_t __strspn_c2 (const char *__s
, int __accept1
,
1034 __STRING_INLINE
size_t
1035 __strspn_c2 (const char *__s
, int __accept1
, int __accept2
)
1037 size_t __result
= 0;
1038 /* Please note that __accept1 and __accept2 never can be '\0'. */
1039 while (__s
[__result
] == __accept1
|| __s
[__result
] == __accept2
)
1044 __STRING_INLINE
size_t __strspn_c3 (const char *__s
, int __accept1
,
1045 int __accept2
, int __accept3
);
1046 __STRING_INLINE
size_t
1047 __strspn_c3 (const char *__s
, int __accept1
, int __accept2
, int __accept3
)
1049 size_t __result
= 0;
1050 /* Please note that __accept1 to __accept3 never can be '\0'. */
1051 while (__s
[__result
] == __accept1
|| __s
[__result
] == __accept2
1052 || __s
[__result
] == __accept3
)
1059 /* Find the first occurrence in S of any character in ACCEPT. */
1060 #if !defined _HAVE_STRING_ARCH_strpbrk || defined _FORCE_INLINES
1061 # ifndef _HAVE_STRING_ARCH_strpbrk
1062 # if __GNUC_PREREQ (3, 2)
1063 # define strpbrk(s, accept) \
1065 ({ char __a0, __a1, __a2; \
1066 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
1067 ? ((__builtin_constant_p (s) && __string2_1bptr_p (s)) \
1068 ? __builtin_strpbrk (s, accept) \
1069 : ((__a0 = ((const char *) (accept))[0], __a0 == '\0') \
1070 ? ((void) (s), (char *) NULL) \
1071 : ((__a1 = ((const char *) (accept))[1], __a1 == '\0') \
1072 ? __builtin_strchr (s, __a0) \
1073 : ((__a2 = ((const char *) (accept))[2], __a2 == '\0') \
1074 ? __strpbrk_c2 (s, __a0, __a1) \
1075 : (((const char *) (accept))[3] == '\0' \
1076 ? __strpbrk_c3 (s, __a0, __a1, __a2) \
1077 : __builtin_strpbrk (s, accept)))))) \
1078 : __builtin_strpbrk (s, accept)); })
1080 # define strpbrk(s, accept) \
1082 ({ char __a0, __a1, __a2; \
1083 (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
1084 ? ((__a0 = ((const char *) (accept))[0], __a0 == '\0') \
1085 ? ((void) (s), (char *) NULL) \
1086 : ((__a1 = ((const char *) (accept))[1], __a1 == '\0') \
1087 ? strchr (s, __a0) \
1088 : ((__a2 = ((const char *) (accept))[2], __a2 == '\0') \
1089 ? __strpbrk_c2 (s, __a0, __a1) \
1090 : (((const char *) (accept))[3] == '\0' \
1091 ? __strpbrk_c3 (s, __a0, __a1, __a2) \
1092 : strpbrk (s, accept))))) \
1093 : strpbrk (s, accept)); })
1097 __STRING_INLINE
char *__strpbrk_c2 (const char *__s
, int __accept1
,
1099 __STRING_INLINE
char *
1100 __strpbrk_c2 (const char *__s
, int __accept1
, int __accept2
)
1102 /* Please note that __accept1 and __accept2 never can be '\0'. */
1103 while (*__s
!= '\0' && *__s
!= __accept1
&& *__s
!= __accept2
)
1105 return *__s
== '\0' ? NULL
: (char *) (size_t) __s
;
1108 __STRING_INLINE
char *__strpbrk_c3 (const char *__s
, int __accept1
,
1109 int __accept2
, int __accept3
);
1110 __STRING_INLINE
char *
1111 __strpbrk_c3 (const char *__s
, int __accept1
, int __accept2
, int __accept3
)
1113 /* Please note that __accept1 to __accept3 never can be '\0'. */
1114 while (*__s
!= '\0' && *__s
!= __accept1
&& *__s
!= __accept2
1115 && *__s
!= __accept3
)
1117 return *__s
== '\0' ? NULL
: (char *) (size_t) __s
;
1122 /* Find the first occurrence of NEEDLE in HAYSTACK. Newer gcc versions
1124 #if !defined _HAVE_STRING_ARCH_strstr && !__GNUC_PREREQ (2, 97)
1125 # define strstr(haystack, needle) \
1126 (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
1127 ? (((const char *) (needle))[0] == '\0' \
1128 ? (char *) (size_t) (haystack) \
1129 : (((const char *) (needle))[1] == '\0' \
1130 ? strchr (haystack, \
1131 ((const char *) (needle))[0]) \
1132 : strstr (haystack, needle))) \
1133 : strstr (haystack, needle)))
1137 #if !defined _HAVE_STRING_ARCH_strtok_r || defined _FORCE_INLINES
1138 # ifndef _HAVE_STRING_ARCH_strtok_r
1139 # define __strtok_r(s, sep, nextp) \
1140 (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \
1141 && ((const char *) (sep))[0] != '\0' \
1142 && ((const char *) (sep))[1] == '\0' \
1143 ? __strtok_r_1c (s, ((const char *) (sep))[0], nextp) \
1144 : __strtok_r (s, sep, nextp)))
1147 __STRING_INLINE
char *__strtok_r_1c (char *__s
, char __sep
, char **__nextp
);
1148 __STRING_INLINE
char *
1149 __strtok_r_1c (char *__s
, char __sep
, char **__nextp
)
1154 while (*__s
== __sep
)
1160 while (*__s
!= '\0')
1161 if (*__s
++ == __sep
)
1171 # define strtok_r(s, sep, nextp) __strtok_r (s, sep, nextp)
1176 #if !defined _HAVE_STRING_ARCH_strsep || defined _FORCE_INLINES
1177 # ifndef _HAVE_STRING_ARCH_strsep
1179 extern char *__strsep_g (char **__stringp
, const char *__delim
);
1180 # define __strsep(s, reject) \
1182 ({ char __r0, __r1, __r2; \
1183 (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
1184 && (__r0 = ((const char *) (reject))[0], \
1185 ((const char *) (reject))[0] != '\0') \
1186 ? ((__r1 = ((const char *) (reject))[1], \
1187 ((const char *) (reject))[1] == '\0') \
1188 ? __strsep_1c (s, __r0) \
1189 : ((__r2 = ((const char *) (reject))[2], __r2 == '\0') \
1190 ? __strsep_2c (s, __r0, __r1) \
1191 : (((const char *) (reject))[3] == '\0' \
1192 ? __strsep_3c (s, __r0, __r1, __r2) \
1193 : __strsep_g (s, reject)))) \
1194 : __strsep_g (s, reject)); })
1197 __STRING_INLINE
char *__strsep_1c (char **__s
, char __reject
);
1198 __STRING_INLINE
char *
1199 __strsep_1c (char **__s
, char __reject
)
1201 char *__retval
= *__s
;
1202 if (__retval
!= NULL
&& (*__s
= strchr (__retval
, __reject
)) != NULL
)
1207 __STRING_INLINE
char *__strsep_2c (char **__s
, char __reject1
, char __reject2
);
1208 __STRING_INLINE
char *
1209 __strsep_2c (char **__s
, char __reject1
, char __reject2
)
1211 char *__retval
= *__s
;
1212 if (__retval
!= NULL
)
1214 char *__cp
= __retval
;
1222 if (*__cp
== __reject1
|| *__cp
== __reject2
)
1234 __STRING_INLINE
char *__strsep_3c (char **__s
, char __reject1
, char __reject2
,
1236 __STRING_INLINE
char *
1237 __strsep_3c (char **__s
, char __reject1
, char __reject2
, char __reject3
)
1239 char *__retval
= *__s
;
1240 if (__retval
!= NULL
)
1242 char *__cp
= __retval
;
1250 if (*__cp
== __reject1
|| *__cp
== __reject2
|| *__cp
== __reject3
)
1262 # define strsep(s, reject) __strsep (s, reject)
1266 /* We need the memory allocation functions for inline strdup().
1267 Referring to stdlib.h (even minimally) is not allowed
1268 in any of the tight standards compliant modes. */
1271 # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
1272 # define __need_malloc_and_calloc
1273 # include <stdlib.h>
1276 # ifndef _HAVE_STRING_ARCH_strdup
1278 extern char *__strdup (const char *__string
) __THROW __attribute_malloc__
;
1279 # define __strdup(s) \
1280 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
1281 ? (((const char *) (s))[0] == '\0' \
1282 ? (char *) calloc ((size_t) 1, (size_t) 1) \
1283 : ({ size_t __len = strlen (s) + 1; \
1284 char *__retval = (char *) malloc (__len); \
1285 if (__retval != NULL) \
1286 __retval = (char *) memcpy (__retval, s, __len); \
1290 # if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
1291 # define strdup(s) __strdup (s)
1295 # ifndef _HAVE_STRING_ARCH_strndup
1297 extern char *__strndup (const char *__string
, size_t __n
)
1298 __THROW __attribute_malloc__
;
1299 # define __strndup(s, n) \
1300 (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
1301 ? (((const char *) (s))[0] == '\0' \
1302 ? (char *) calloc ((size_t) 1, (size_t) 1) \
1303 : ({ size_t __len = strlen (s) + 1; \
1308 __retval = (char *) malloc (__len); \
1309 if (__retval != NULL) \
1311 __retval[__len - 1] = '\0'; \
1312 __retval = (char *) memcpy (__retval, s, \
1316 : __strndup (s, n)))
1318 # ifdef __USE_XOPEN2K8
1319 # define strndup(s, n) __strndup (s, n)
1323 #endif /* Use misc. or use GNU. */
1325 #ifndef _FORCE_INLINES
1326 # undef __STRING_INLINE
1329 #endif /* No string inlines. */