Files
glibc/string/bits/string2.h
T

120 lines
4.0 KiB
C
Raw Normal View History

1997-09-11 12:09:10 +00:00
/* Machine-independant string function optimizations.
Copyright (C) 1997-2017 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
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
/* 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
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. */