Files
glibc/string/bits/string2.h
T

247 lines
8.5 KiB
C
Raw Normal View History

1997-09-11 12:09:10 +00:00
/* Machine-independant string function optimizations.
Copyright (C) 1997-2016 Free Software Foundation, Inc.
1997-09-11 12:09:10 +00:00
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1997-09-11 12:09:10 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1997-09-11 12:09:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
1997-09-11 12:09:10 +00:00
1997-09-16 00:42:43 +00:00
#ifndef _STRING_H
# error "Never use <bits/string2.h> directly; include <string.h> instead."
#endif
#ifndef __NO_STRING_INLINES
1997-09-11 12:09:10 +00:00
/* Unlike the definitions in the header <bits/string.h> the
1997-12-05 00:40:29 +00:00
definitions contained here are not optimized down to assembler
level. Those optimizations are not always a good idea since this
1997-09-11 12:09:10 +00:00
means the code size increases a lot. Instead the definitions here
1997-12-05 00:40:29 +00:00
optimize some functions in a way which do not dramatically
increase the code size and which do not use assembler. The main
2004-05-26 17:27:20 +00:00
trick is to use GCC's `__builtin_constant_p' function.
1997-09-11 12:09:10 +00:00
Every function XXX which has a defined version in
1997-09-16 00:42:43 +00:00
<bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
1997-09-11 12:09:10 +00:00
to make sure we don't get redefinitions.
We must use here macros instead of inline functions since the
2000-02-22 09:00:35 +00:00
trick won't work with the latter. */
1997-09-11 12:09:10 +00:00
1999-05-17 17:25:32 +00:00
#ifndef __STRING_INLINE
# ifdef __cplusplus
# define __STRING_INLINE inline
# else
# define __STRING_INLINE __extern_inline
1999-05-17 17:25:32 +00:00
# endif
1997-09-11 12:09:10 +00:00
#endif
1998-09-14 11:43:26 +00:00
/* Dereferencing a pointer arg to run sizeof on it fails for the void
pointer case, so we use this instead.
Note that __x is evaluated twice. */
#define __string2_1bptr_p(__x) \
1998-10-01 00:07:16 +00:00
((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
1997-09-16 00:42:43 +00:00
2016-11-14 12:35:10 -05:00
/* Set N bytes of S to 0. */
2004-05-28 06:56:51 +00:00
#if !defined _HAVE_STRING_ARCH_memset
# define __bzero(s, n) __builtin_memset (s, '\0', n)
1997-09-16 00:42:43 +00:00
#endif
1997-09-11 12:09:10 +00:00
1998-09-21 14:34:13 +00:00
1999-01-16 17:09:04 +00:00
#ifndef _HAVE_STRING_ARCH_strchr
1999-10-09 21:56:43 +00:00
extern void *__rawmemchr (const void *__s, int __c);
2004-05-26 17:27:20 +00:00
# define strchr(s, c) \
(__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \
&& (c) == '\0' \
? (char *) __rawmemchr (s, c) \
: __builtin_strchr (s, c)))
1999-01-16 17:09:04 +00:00
#endif
1997-09-11 12:09:10 +00:00
/* Copy SRC to DEST, returning pointer to final NUL byte. */
#ifdef __USE_GNU
# ifndef _HAVE_STRING_ARCH_stpcpy
# define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
1997-09-11 12:09:10 +00:00
/* In glibc we use this function frequently but for namespace reasons
we have to use the name `__stpcpy'. */
# define stpcpy(dest, src) __stpcpy (dest, src)
# endif
1997-09-11 12:09:10 +00:00
#endif
/* Copy no more than N characters of SRC to DEST. */
#ifndef _HAVE_STRING_ARCH_strncpy
# define strncpy(dest, src, n) __builtin_strncpy (dest, src, n)
1997-09-11 12:09:10 +00:00
#endif
/* Append no more than N characters from SRC onto DEST. */
#ifndef _HAVE_STRING_ARCH_strncat
2000-11-08 21:20:06 +00:00
# ifdef _USE_STRING_ARCH_strchr
1997-09-11 12:09:10 +00:00
# define strncat(dest, src, n) \
1997-09-24 23:03:42 +00:00
(__extension__ ({ char *__dest = (dest); \
__builtin_constant_p (src) && __builtin_constant_p (n) \
? (strlen (src) < ((size_t) (n)) \
? strcat (__dest, src) \
2000-10-30 04:08:58 +00:00
: (*((char *) __mempcpy (strchr (__dest, '\0'), \
src, n)) = '\0', __dest)) \
1997-09-24 23:03:42 +00:00
: strncat (dest, src, n); }))
1997-09-11 12:09:10 +00:00
# else
# define strncat(dest, src, n) __builtin_strncat (dest, src, n)
1997-09-11 12:09:10 +00:00
# endif
#endif
1997-09-24 23:03:42 +00:00
/* Compare characters of S1 and S2. */
#ifndef _HAVE_STRING_ARCH_strcmp
# define strcmp(s1, s2) \
2004-05-26 17:27:20 +00:00
__extension__ \
({ size_t __s1_len, __s2_len; \
(__builtin_constant_p (s1) && __builtin_constant_p (s2) \
&& (__s1_len = strlen (s1), __s2_len = strlen (s2), \
(!__string2_1bptr_p (s1) || __s1_len >= 4) \
&& (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
? __builtin_strcmp (s1, s2) \
: (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
&& (__s1_len = strlen (s1), __s1_len < 4) \
? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
? __builtin_strcmp (s1, s2) \
: __strcmp_cg (s1, s2, __s1_len)) \
: (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
&& (__s2_len = strlen (s2), __s2_len < 4) \
? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
? __builtin_strcmp (s1, s2) \
: -__strcmp_cg (s2, s1, __s2_len)) \
: __builtin_strcmp (s1, s2)))); })
1997-09-30 17:10:40 +00:00
1997-09-24 23:03:42 +00:00
# define __strcmp_cg(s1, s2, l1) \
2012-01-07 23:57:22 -05:00
(__extension__ ({ const unsigned char *__s2 = \
(const unsigned char *) (const char *) (s2); \
int __result = \
2012-01-07 23:57:22 -05:00
(((const unsigned char *) (const char *) (s1))[0] \
1998-05-08 12:32:47 +00:00
- __s2[0]); \
1997-09-24 23:03:42 +00:00
if (l1 > 0 && __result == 0) \
{ \
2012-01-07 23:57:22 -05:00
__result = (((const unsigned char *) \
(const char *) (s1))[1] - __s2[1]); \
1997-09-24 23:03:42 +00:00
if (l1 > 1 && __result == 0) \
{ \
2012-01-07 23:57:22 -05:00
__result = (((const unsigned char *) \
(const char *) (s1))[2] - __s2[2]); \
1997-09-24 23:03:42 +00:00
if (l1 > 2 && __result == 0) \
2012-01-07 23:57:22 -05:00
__result = (((const unsigned char *) \
(const char *) (s1))[3] \
1997-10-26 20:13:00 +00:00
- __s2[3]); \
1997-09-24 23:03:42 +00:00
} \
} \
__result; }))
#endif
1997-09-11 12:09:10 +00:00
/* Compare N characters of S1 and S2. */
#ifndef _HAVE_STRING_ARCH_strncmp
# define strncmp(s1, s2, n) \
(__extension__ (__builtin_constant_p (n) \
&& ((__builtin_constant_p (s1) \
&& strlen (s1) < ((size_t) (n))) \
|| (__builtin_constant_p (s2) \
&& strlen (s2) < ((size_t) (n)))) \
? strcmp (s1, s2) : strncmp (s1, s2, n)))
1997-09-11 12:09:10 +00:00
#endif
/* Return the length of the initial segment of S which
consists entirely of characters not in REJECT. */
2016-03-25 16:44:26 -03:00
#ifndef _HAVE_STRING_ARCH_strcspn
# define strcspn(s, reject) __builtin_strcspn (s, reject)
1997-09-11 12:09:10 +00:00
#endif
/* Return the length of the initial segment of S which
consists entirely of characters in ACCEPT. */
2016-03-26 19:09:07 +00:00
#ifndef _HAVE_STRING_ARCH_strspn
# define strspn(s, accept) __builtin_strspn (s, accept)
1997-09-11 12:09:10 +00:00
#endif
/* Find the first occurrence in S of any character in ACCEPT. */
2016-03-27 13:33:56 +00:00
#ifndef _HAVE_STRING_ARCH_strpbrk
# define strpbrk(s, accept) __builtin_strpbrk (s, accept)
1997-09-11 12:09:10 +00:00
#endif
1998-09-14 11:43:26 +00:00
/* We need the memory allocation functions for inline strdup().
1998-11-12 18:06:31 +00:00
Referring to stdlib.h (even minimally) is not allowed
1998-10-25 09:11:53 +00:00
in any of the tight standards compliant modes. */
1998-10-28 14:41:59 +00:00
#ifdef __USE_MISC
1998-03-18 14:42:25 +00:00
1998-10-25 09:11:53 +00:00
# if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
# define __need_malloc_and_calloc
# include <stdlib.h>
# endif
1998-09-14 11:43:26 +00:00
1998-10-25 09:11:53 +00:00
# ifndef _HAVE_STRING_ARCH_strdup
1998-03-18 14:42:25 +00:00
2012-01-07 23:57:22 -05:00
extern char *__strdup (const char *__string) __THROW __attribute_malloc__;
1998-10-25 09:11:53 +00:00
# define __strdup(s) \
1998-03-18 17:57:13 +00:00
(__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
2012-01-07 23:57:22 -05:00
? (((const char *) (s))[0] == '\0' \
2007-10-05 21:21:08 +00:00
? (char *) calloc ((size_t) 1, (size_t) 1) \
1998-03-18 17:57:13 +00:00
: ({ size_t __len = strlen (s) + 1; \
char *__retval = (char *) malloc (__len); \
if (__retval != NULL) \
__retval = (char *) memcpy (__retval, s, __len); \
__retval; })) \
: __strdup (s)))
1998-03-18 14:42:25 +00:00
# if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
1998-10-25 09:11:53 +00:00
# define strdup(s) __strdup (s)
# endif
1997-09-16 00:42:43 +00:00
# endif
1998-10-25 09:11:53 +00:00
# ifndef _HAVE_STRING_ARCH_strndup
1998-03-18 17:57:13 +00:00
2012-01-07 23:57:22 -05:00
extern char *__strndup (const char *__string, size_t __n)
2000-05-04 00:56:16 +00:00
__THROW __attribute_malloc__;
1998-10-25 09:11:53 +00:00
# define __strndup(s, n) \
1998-03-18 17:57:13 +00:00
(__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
2012-01-07 23:57:22 -05:00
? (((const char *) (s))[0] == '\0' \
2007-10-05 21:21:08 +00:00
? (char *) calloc ((size_t) 1, (size_t) 1) \
1998-03-18 17:57:13 +00:00
: ({ size_t __len = strlen (s) + 1; \
size_t __n = (n); \
char *__retval; \
if (__n < __len) \
2001-11-03 09:05:11 +00:00
__len = __n + 1; \
1998-03-18 17:57:13 +00:00
__retval = (char *) malloc (__len); \
if (__retval != NULL) \
{ \
__retval[__len - 1] = '\0'; \
__retval = (char *) memcpy (__retval, s, \
__len - 1); \
} \
__retval; })) \
2001-09-25 03:04:41 +00:00
: __strndup (s, n)))
1998-03-18 17:57:13 +00:00
# ifdef __USE_XOPEN2K8
2001-09-25 03:04:41 +00:00
# define strndup(s, n) __strndup (s, n)
1998-10-25 09:11:53 +00:00
# endif
1998-03-18 17:57:13 +00:00
# endif
1998-10-25 09:11:53 +00:00
#endif /* Use misc. or use GNU. */
1998-03-18 17:57:13 +00:00
1999-05-17 17:25:32 +00:00
#ifndef _FORCE_INLINES
# undef __STRING_INLINE
#endif
1997-09-11 12:09:10 +00:00
1997-09-16 00:42:43 +00:00
#endif /* No string inlines. */