| 1 | /* Machine-independant string function optimizations. |
| 2 | Copyright (C) 1997-2017 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. */ |
| 19 | |
| 20 | #ifndef _STRING_H |
| 21 | # error "Never use <bits/string2.h> directly; include <string.h> instead." |
| 22 | #endif |
| 23 | |
| 24 | #ifndef __NO_STRING_INLINES |
| 25 | |
| 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. |
| 33 | |
| 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. |
| 37 | |
| 38 | We must use here macros instead of inline functions since the |
| 39 | trick won't work with the latter. */ |
| 40 | |
| 41 | #ifndef __STRING_INLINE |
| 42 | # ifdef __cplusplus |
| 43 | # define __STRING_INLINE inline |
| 44 | # else |
| 45 | # define __STRING_INLINE __extern_inline |
| 46 | # endif |
| 47 | #endif |
| 48 | |
| 49 | /* Dereferencing a pointer arg to run sizeof on it fails for the void |
| 50 | pointer case, so we use this instead. |
| 51 | Note that __x is evaluated twice. */ |
| 52 | #define __string2_1bptr_p(__x) \ |
| 53 | ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1) |
| 54 | |
| 55 | /* Set N bytes of S to 0. */ |
| 56 | #if !defined _HAVE_STRING_ARCH_memset |
| 57 | # define __bzero(s, n) __builtin_memset (s, '\0', n) |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | #ifndef _HAVE_STRING_ARCH_strchr |
| 62 | extern void *__rawmemchr (const void *__s, int __c); |
| 63 | # define strchr(s, c) \ |
| 64 | (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \ |
| 65 | && (c) == '\0' \ |
| 66 | ? (char *) __rawmemchr (s, c) \ |
| 67 | : __builtin_strchr (s, c))) |
| 68 | #endif |
| 69 | |
| 70 | |
| 71 | /* Copy SRC to DEST, returning pointer to final NUL byte. */ |
| 72 | #ifdef __USE_GNU |
| 73 | # ifndef _HAVE_STRING_ARCH_stpcpy |
| 74 | # define __stpcpy(dest, src) __builtin_stpcpy (dest, src) |
| 75 | /* In glibc we use this function frequently but for namespace reasons |
| 76 | we have to use the name `__stpcpy'. */ |
| 77 | # define stpcpy(dest, src) __stpcpy (dest, src) |
| 78 | # endif |
| 79 | #endif |
| 80 | |
| 81 | |
| 82 | /* Copy no more than N characters of SRC to DEST. */ |
| 83 | #ifndef _HAVE_STRING_ARCH_strncpy |
| 84 | # define strncpy(dest, src, n) __builtin_strncpy (dest, src, n) |
| 85 | #endif |
| 86 | |
| 87 | |
| 88 | /* Append no more than N characters from SRC onto DEST. */ |
| 89 | #ifndef _HAVE_STRING_ARCH_strncat |
| 90 | # ifdef _USE_STRING_ARCH_strchr |
| 91 | # define strncat(dest, src, n) \ |
| 92 | (__extension__ ({ char *__dest = (dest); \ |
| 93 | __builtin_constant_p (src) && __builtin_constant_p (n) \ |
| 94 | ? (strlen (src) < ((size_t) (n)) \ |
| 95 | ? strcat (__dest, src) \ |
| 96 | : (*((char *) __mempcpy (strchr (__dest, '\0'), \ |
| 97 | src, n)) = '\0', __dest)) \ |
| 98 | : strncat (dest, src, n); })) |
| 99 | # else |
| 100 | # define strncat(dest, src, n) __builtin_strncat (dest, src, n) |
| 101 | # endif |
| 102 | #endif |
| 103 | |
| 104 | |
| 105 | /* Compare characters of S1 and S2. */ |
| 106 | #ifndef _HAVE_STRING_ARCH_strcmp |
| 107 | # define strcmp(s1, s2) \ |
| 108 | __extension__ \ |
| 109 | ({ size_t __s1_len, __s2_len; \ |
| 110 | (__builtin_constant_p (s1) && __builtin_constant_p (s2) \ |
| 111 | && (__s1_len = strlen (s1), __s2_len = strlen (s2), \ |
| 112 | (!__string2_1bptr_p (s1) || __s1_len >= 4) \ |
| 113 | && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \ |
| 114 | ? __builtin_strcmp (s1, s2) \ |
| 115 | : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \ |
| 116 | && (__s1_len = strlen (s1), __s1_len < 4) \ |
| 117 | ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \ |
| 118 | ? __builtin_strcmp (s1, s2) \ |
| 119 | : __strcmp_cg (s1, s2, __s1_len)) \ |
| 120 | : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \ |
| 121 | && (__s2_len = strlen (s2), __s2_len < 4) \ |
| 122 | ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \ |
| 123 | ? __builtin_strcmp (s1, s2) \ |
| 124 | : -__strcmp_cg (s2, s1, __s2_len)) \ |
| 125 | : __builtin_strcmp (s1, s2)))); }) |
| 126 | |
| 127 | # define __strcmp_cg(s1, s2, l1) \ |
| 128 | (__extension__ ({ const unsigned char *__s2 = \ |
| 129 | (const unsigned char *) (const char *) (s2); \ |
| 130 | int __result = \ |
| 131 | (((const unsigned char *) (const char *) (s1))[0] \ |
| 132 | - __s2[0]); \ |
| 133 | if (l1 > 0 && __result == 0) \ |
| 134 | { \ |
| 135 | __result = (((const unsigned char *) \ |
| 136 | (const char *) (s1))[1] - __s2[1]); \ |
| 137 | if (l1 > 1 && __result == 0) \ |
| 138 | { \ |
| 139 | __result = (((const unsigned char *) \ |
| 140 | (const char *) (s1))[2] - __s2[2]); \ |
| 141 | if (l1 > 2 && __result == 0) \ |
| 142 | __result = (((const unsigned char *) \ |
| 143 | (const char *) (s1))[3] \ |
| 144 | - __s2[3]); \ |
| 145 | } \ |
| 146 | } \ |
| 147 | __result; })) |
| 148 | #endif |
| 149 | |
| 150 | |
| 151 | /* Compare N characters of S1 and S2. */ |
| 152 | #ifndef _HAVE_STRING_ARCH_strncmp |
| 153 | # define strncmp(s1, s2, n) \ |
| 154 | (__extension__ (__builtin_constant_p (n) \ |
| 155 | && ((__builtin_constant_p (s1) \ |
| 156 | && strlen (s1) < ((size_t) (n))) \ |
| 157 | || (__builtin_constant_p (s2) \ |
| 158 | && strlen (s2) < ((size_t) (n)))) \ |
| 159 | ? strcmp (s1, s2) : strncmp (s1, s2, n))) |
| 160 | #endif |
| 161 | |
| 162 | |
| 163 | /* Return the length of the initial segment of S which |
| 164 | consists entirely of characters not in REJECT. */ |
| 165 | #ifndef _HAVE_STRING_ARCH_strcspn |
| 166 | # define strcspn(s, reject) __builtin_strcspn (s, reject) |
| 167 | #endif |
| 168 | |
| 169 | |
| 170 | /* Return the length of the initial segment of S which |
| 171 | consists entirely of characters in ACCEPT. */ |
| 172 | #ifndef _HAVE_STRING_ARCH_strspn |
| 173 | # define strspn(s, accept) __builtin_strspn (s, accept) |
| 174 | #endif |
| 175 | |
| 176 | |
| 177 | /* Find the first occurrence in S of any character in ACCEPT. */ |
| 178 | #ifndef _HAVE_STRING_ARCH_strpbrk |
| 179 | # define strpbrk(s, accept) __builtin_strpbrk (s, accept) |
| 180 | #endif |
| 181 | |
| 182 | |
| 183 | /* We need the memory allocation functions for inline strdup(). |
| 184 | Referring to stdlib.h (even minimally) is not allowed |
| 185 | in any of the tight standards compliant modes. */ |
| 186 | #ifdef __USE_MISC |
| 187 | |
| 188 | # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup |
| 189 | # define __need_malloc_and_calloc |
| 190 | # include <stdlib.h> |
| 191 | # endif |
| 192 | |
| 193 | # ifndef _HAVE_STRING_ARCH_strdup |
| 194 | |
| 195 | extern char *__strdup (const char *__string) __THROW __attribute_malloc__; |
| 196 | # define __strdup(s) \ |
| 197 | (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \ |
| 198 | ? (((const char *) (s))[0] == '\0' \ |
| 199 | ? (char *) calloc ((size_t) 1, (size_t) 1) \ |
| 200 | : ({ size_t __len = strlen (s) + 1; \ |
| 201 | char *__retval = (char *) malloc (__len); \ |
| 202 | if (__retval != NULL) \ |
| 203 | __retval = (char *) memcpy (__retval, s, __len); \ |
| 204 | __retval; })) \ |
| 205 | : __strdup (s))) |
| 206 | |
| 207 | # if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8 |
| 208 | # define strdup(s) __strdup (s) |
| 209 | # endif |
| 210 | # endif |
| 211 | |
| 212 | # ifndef _HAVE_STRING_ARCH_strndup |
| 213 | |
| 214 | extern char *__strndup (const char *__string, size_t __n) |
| 215 | __THROW __attribute_malloc__; |
| 216 | # define __strndup(s, n) \ |
| 217 | (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \ |
| 218 | ? (((const char *) (s))[0] == '\0' \ |
| 219 | ? (char *) calloc ((size_t) 1, (size_t) 1) \ |
| 220 | : ({ size_t __len = strlen (s) + 1; \ |
| 221 | size_t __n = (n); \ |
| 222 | char *__retval; \ |
| 223 | if (__n < __len) \ |
| 224 | __len = __n + 1; \ |
| 225 | __retval = (char *) malloc (__len); \ |
| 226 | if (__retval != NULL) \ |
| 227 | { \ |
| 228 | __retval[__len - 1] = '\0'; \ |
| 229 | __retval = (char *) memcpy (__retval, s, \ |
| 230 | __len - 1); \ |
| 231 | } \ |
| 232 | __retval; })) \ |
| 233 | : __strndup (s, n))) |
| 234 | |
| 235 | # ifdef __USE_XOPEN2K8 |
| 236 | # define strndup(s, n) __strndup (s, n) |
| 237 | # endif |
| 238 | # endif |
| 239 | |
| 240 | #endif /* Use misc. or use GNU. */ |
| 241 | |
| 242 | #ifndef _FORCE_INLINES |
| 243 | # undef __STRING_INLINE |
| 244 | #endif |
| 245 | |
| 246 | #endif /* No string inlines. */ |
| 247 | |