string2.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _STRING_H
  17. # error "Never use <bits/string2.h> directly; include <string.h> instead."
  18. #endif
  19. #ifndef __NO_STRING_INLINES
  20. /* Unlike the definitions in the header <bits/string.h> the
  21. definitions contained here are not optimized down to assembler
  22. level. Those optimizations are not always a good idea since this
  23. means the code size increases a lot. Instead the definitions here
  24. optimize some functions in a way which do not dramatically
  25. increase the code size and which do not use assembler. The main
  26. trick is to use GCC's `__builtin_constant_p' function.
  27. Every function XXX which has a defined version in
  28. <bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
  29. to make sure we don't get redefinitions.
  30. We must use here macros instead of inline functions since the
  31. trick won't work with the latter. */
  32. #ifndef __STRING_INLINE
  33. # ifdef __cplusplus
  34. # define __STRING_INLINE inline
  35. # else
  36. # define __STRING_INLINE __extern_inline
  37. # endif
  38. #endif
  39. /* Dereferencing a pointer arg to run sizeof on it fails for the void
  40. pointer case, so we use this instead.
  41. Note that __x is evaluated twice. */
  42. #define __string2_1bptr_p(__x) \
  43. ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
  44. /* Set N bytes of S to 0. */
  45. #if !defined _HAVE_STRING_ARCH_memset
  46. # define __bzero(s, n) __builtin_memset (s, '\0', n)
  47. #endif
  48. #ifndef _HAVE_STRING_ARCH_strchr
  49. extern void *__rawmemchr (const void *__s, int __c);
  50. # define strchr(s, c) \
  51. (__extension__ (__builtin_constant_p (c) && !__builtin_constant_p (s) \
  52. && (c) == '\0' \
  53. ? (char *) __rawmemchr (s, c) \
  54. : __builtin_strchr (s, c)))
  55. #endif
  56. /* Copy SRC to DEST, returning pointer to final NUL byte. */
  57. #ifdef __USE_GNU
  58. # ifndef _HAVE_STRING_ARCH_stpcpy
  59. # define __stpcpy(dest, src) __builtin_stpcpy (dest, src)
  60. /* In glibc we use this function frequently but for namespace reasons
  61. we have to use the name `__stpcpy'. */
  62. # define stpcpy(dest, src) __stpcpy (dest, src)
  63. # endif
  64. #endif
  65. /* Copy no more than N characters of SRC to DEST. */
  66. #ifndef _HAVE_STRING_ARCH_strncpy
  67. # define strncpy(dest, src, n) __builtin_strncpy (dest, src, n)
  68. #endif
  69. /* Append no more than N characters from SRC onto DEST. */
  70. #ifndef _HAVE_STRING_ARCH_strncat
  71. # ifdef _USE_STRING_ARCH_strchr
  72. # define strncat(dest, src, n) \
  73. (__extension__ ({ char *__dest = (dest); \
  74. __builtin_constant_p (src) && __builtin_constant_p (n) \
  75. ? (strlen (src) < ((size_t) (n)) \
  76. ? strcat (__dest, src) \
  77. : (*((char *) __mempcpy (strchr (__dest, '\0'), \
  78. src, n)) = '\0', __dest)) \
  79. : strncat (dest, src, n); }))
  80. # else
  81. # define strncat(dest, src, n) __builtin_strncat (dest, src, n)
  82. # endif
  83. #endif
  84. /* Compare characters of S1 and S2. */
  85. #ifndef _HAVE_STRING_ARCH_strcmp
  86. # define strcmp(s1, s2) \
  87. __extension__ \
  88. ({ size_t __s1_len, __s2_len; \
  89. (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
  90. && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
  91. (!__string2_1bptr_p (s1) || __s1_len >= 4) \
  92. && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
  93. ? __builtin_strcmp (s1, s2) \
  94. : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
  95. && (__s1_len = strlen (s1), __s1_len < 4) \
  96. ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
  97. ? __builtin_strcmp (s1, s2) \
  98. : __strcmp_cg (s1, s2, __s1_len)) \
  99. : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
  100. && (__s2_len = strlen (s2), __s2_len < 4) \
  101. ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
  102. ? __builtin_strcmp (s1, s2) \
  103. : -__strcmp_cg (s2, s1, __s2_len)) \
  104. : __builtin_strcmp (s1, s2)))); })
  105. # define __strcmp_cg(s1, s2, l1) \
  106. (__extension__ ({ const unsigned char *__s2 = \
  107. (const unsigned char *) (const char *) (s2); \
  108. int __result = \
  109. (((const unsigned char *) (const char *) (s1))[0] \
  110. - __s2[0]); \
  111. if (l1 > 0 && __result == 0) \
  112. { \
  113. __result = (((const unsigned char *) \
  114. (const char *) (s1))[1] - __s2[1]); \
  115. if (l1 > 1 && __result == 0) \
  116. { \
  117. __result = (((const unsigned char *) \
  118. (const char *) (s1))[2] - __s2[2]); \
  119. if (l1 > 2 && __result == 0) \
  120. __result = (((const unsigned char *) \
  121. (const char *) (s1))[3] \
  122. - __s2[3]); \
  123. } \
  124. } \
  125. __result; }))
  126. #endif
  127. /* Compare N characters of S1 and S2. */
  128. #ifndef _HAVE_STRING_ARCH_strncmp
  129. # define strncmp(s1, s2, n) \
  130. (__extension__ (__builtin_constant_p (n) \
  131. && ((__builtin_constant_p (s1) \
  132. && strlen (s1) < ((size_t) (n))) \
  133. || (__builtin_constant_p (s2) \
  134. && strlen (s2) < ((size_t) (n)))) \
  135. ? strcmp (s1, s2) : strncmp (s1, s2, n)))
  136. #endif
  137. /* Return the length of the initial segment of S which
  138. consists entirely of characters not in REJECT. */
  139. #ifndef _HAVE_STRING_ARCH_strcspn
  140. # define strcspn(s, reject) __builtin_strcspn (s, reject)
  141. #endif
  142. /* Return the length of the initial segment of S which
  143. consists entirely of characters in ACCEPT. */
  144. #ifndef _HAVE_STRING_ARCH_strspn
  145. # define strspn(s, accept) __builtin_strspn (s, accept)
  146. #endif
  147. /* Find the first occurrence in S of any character in ACCEPT. */
  148. #ifndef _HAVE_STRING_ARCH_strpbrk
  149. # define strpbrk(s, accept) __builtin_strpbrk (s, accept)
  150. #endif
  151. /* We need the memory allocation functions for inline strdup().
  152. Referring to stdlib.h (even minimally) is not allowed
  153. in any of the tight standards compliant modes. */
  154. #ifdef __USE_MISC
  155. # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
  156. # define __need_malloc_and_calloc
  157. # include <stdlib.h>
  158. # endif
  159. # ifndef _HAVE_STRING_ARCH_strdup
  160. extern char *__strdup (const char *__string) __THROW __attribute_malloc__;
  161. # define __strdup(s) \
  162. (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
  163. ? (((const char *) (s))[0] == '\0' \
  164. ? (char *) calloc ((size_t) 1, (size_t) 1) \
  165. : ({ size_t __len = strlen (s) + 1; \
  166. char *__retval = (char *) malloc (__len); \
  167. if (__retval != NULL) \
  168. __retval = (char *) memcpy (__retval, s, __len); \
  169. __retval; })) \
  170. : __strdup (s)))
  171. # if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8
  172. # define strdup(s) __strdup (s)
  173. # endif
  174. # endif
  175. # ifndef _HAVE_STRING_ARCH_strndup
  176. extern char *__strndup (const char *__string, size_t __n)
  177. __THROW __attribute_malloc__;
  178. # define __strndup(s, n) \
  179. (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
  180. ? (((const char *) (s))[0] == '\0' \
  181. ? (char *) calloc ((size_t) 1, (size_t) 1) \
  182. : ({ size_t __len = strlen (s) + 1; \
  183. size_t __n = (n); \
  184. char *__retval; \
  185. if (__n < __len) \
  186. __len = __n + 1; \
  187. __retval = (char *) malloc (__len); \
  188. if (__retval != NULL) \
  189. { \
  190. __retval[__len - 1] = '\0'; \
  191. __retval = (char *) memcpy (__retval, s, \
  192. __len - 1); \
  193. } \
  194. __retval; })) \
  195. : __strndup (s, n)))
  196. # ifdef __USE_XOPEN2K8
  197. # define strndup(s, n) __strndup (s, n)
  198. # endif
  199. # endif
  200. #endif /* Use misc. or use GNU. */
  201. #ifndef _FORCE_INLINES
  202. # undef __STRING_INLINE
  203. #endif
  204. #endif /* No string inlines. */