string2.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. /* Machine-independant string function optimizations.
  2. Copyright (C) 1997, 1998, 1999, 2000, 2001 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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _STRING_H
  18. # error "Never use <bits/string2.h> directly; include <string.h> instead."
  19. #endif
  20. #if !defined __NO_STRING_INLINES && !defined __BOUNDED_POINTERS__
  21. /* Unlike the definitions in the header <bits/string.h> the
  22. definitions contained here are not optimized down to assembler
  23. level. Those optimizations are not always a good idea since this
  24. means the code size increases a lot. Instead the definitions here
  25. optimize some functions in a way which do not dramatically
  26. increase the code size and which do not use assembler. The main
  27. trick is to use GNU CC's `__builtin_constant_p' function.
  28. Every function XXX which has a defined version in
  29. <bits/string.h> must be accompanied by a symbol _HAVE_STRING_ARCH_XXX
  30. to make sure we don't get redefinitions.
  31. We must use here macros instead of inline functions since the
  32. trick won't work with the latter. */
  33. #ifndef __STRING_INLINE
  34. # ifdef __cplusplus
  35. # define __STRING_INLINE inline
  36. # else
  37. # define __STRING_INLINE extern __inline
  38. # endif
  39. #endif
  40. #if _STRING_ARCH_unaligned
  41. /* If we can do unaligned memory accesses we must know the endianess. */
  42. # include <endian.h>
  43. # include <bits/types.h>
  44. # if __BYTE_ORDER == __LITTLE_ENDIAN
  45. # define __STRING2_SMALL_GET16(src, idx) \
  46. (((__const unsigned char *) (__const char *) (src))[idx + 1] << 8 \
  47. | ((__const unsigned char *) (__const char *) (src))[idx])
  48. # define __STRING2_SMALL_GET32(src, idx) \
  49. (((((__const unsigned char *) (__const char *) (src))[idx + 3] << 8 \
  50. | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8 \
  51. | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8 \
  52. | ((__const unsigned char *) (__const char *) (src))[idx])
  53. # else
  54. # define __STRING2_SMALL_GET16(src, idx) \
  55. (((__const unsigned char *) (__const char *) (src))[idx] << 8 \
  56. | ((__const unsigned char *) (__const char *) (src))[idx + 1])
  57. # define __STRING2_SMALL_GET32(src, idx) \
  58. (((((__const unsigned char *) (__const char *) (src))[idx] << 8 \
  59. | ((__const unsigned char *) (__const char *) (src))[idx + 1]) << 8 \
  60. | ((__const unsigned char *) (__const char *) (src))[idx + 2]) << 8 \
  61. | ((__const unsigned char *) (__const char *) (src))[idx + 3])
  62. # endif
  63. #else
  64. /* These are a few types we need for the optimizations if we cannot
  65. use unaligned memory accesses. */
  66. # define __STRING2_COPY_TYPE(N) \
  67. typedef struct { unsigned char __arr[N]; } \
  68. __STRING2_COPY_ARR##N __attribute__ ((packed))
  69. __STRING2_COPY_TYPE (2);
  70. __STRING2_COPY_TYPE (3);
  71. __STRING2_COPY_TYPE (4);
  72. __STRING2_COPY_TYPE (5);
  73. __STRING2_COPY_TYPE (6);
  74. __STRING2_COPY_TYPE (7);
  75. __STRING2_COPY_TYPE (8);
  76. # undef __STRING2_COPY_TYPE
  77. #endif
  78. /* Dereferencing a pointer arg to run sizeof on it fails for the void
  79. pointer case, so we use this instead.
  80. Note that __x is evaluated twice. */
  81. #define __string2_1bptr_p(__x) \
  82. ((size_t)(const void *)((__x) + 1) - (size_t)(const void *)(__x) == 1)
  83. /* Set N bytes of S to C. */
  84. #ifndef _HAVE_STRING_ARCH_memset
  85. # if _STRING_ARCH_unaligned
  86. # define memset(s, c, n) \
  87. (__extension__ (__builtin_constant_p (n) && (n) <= 16 \
  88. ? ((n) == 1 \
  89. ? __memset_1 (s, c) \
  90. : __memset_gc (s, c, n)) \
  91. : (__builtin_constant_p (c) && (c) == '\0' \
  92. ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
  93. : memset (s, c, n))))
  94. # define __memset_1(s, c) ({ void *__s = (s); \
  95. *((__uint8_t *) __s) = (__uint8_t) c; __s; })
  96. # define __memset_gc(s, c, n) \
  97. ({ void *__s = (s); \
  98. union { \
  99. unsigned int __ui; \
  100. unsigned short int __usi; \
  101. unsigned char __uc; \
  102. } *__u = __s; \
  103. __uint8_t __c = (__uint8_t) (c); \
  104. \
  105. /* This `switch' statement will be removed at compile-time. */ \
  106. switch ((unsigned int) (n)) \
  107. { \
  108. case 15: \
  109. __u->__ui = __c * 0x01010101; \
  110. __u = __extension__ (void *)((char *) __u + 4); \
  111. case 11: \
  112. __u->__ui = __c * 0x01010101; \
  113. __u = __extension__ (void *)((char *) __u + 4); \
  114. case 7: \
  115. __u->__ui = __c * 0x01010101; \
  116. __u = __extension__ (void *)((char *) __u + 4); \
  117. case 3: \
  118. __u->__usi = (unsigned short int) __c * 0x0101; \
  119. __u = __extension__ (void *)((char *) __u + 2); \
  120. __u->__uc = (unsigned char) __c; \
  121. break; \
  122. \
  123. case 14: \
  124. __u->__ui = __c * 0x01010101; \
  125. __u = __extension__ (void *)((char *) __u + 4); \
  126. case 10: \
  127. __u->__ui = __c * 0x01010101; \
  128. __u = __extension__ (void *)((char *) __u + 4); \
  129. case 6: \
  130. __u->__ui = __c * 0x01010101; \
  131. __u = __extension__ (void *)((char *) __u + 4); \
  132. case 2: \
  133. __u->__usi = (unsigned short int) __c * 0x0101; \
  134. break; \
  135. \
  136. case 13: \
  137. __u->__ui = __c * 0x01010101; \
  138. __u = __extension__ (void *)((char *) __u + 4); \
  139. case 9: \
  140. __u->__ui = __c * 0x01010101; \
  141. __u = __extension__ (void *)((char *) __u + 4); \
  142. case 5: \
  143. __u->__ui = __c * 0x01010101; \
  144. __u = __extension__ (void *)((char *) __u + 4); \
  145. case 1: \
  146. __u->__uc = (unsigned char) __c; \
  147. break; \
  148. \
  149. case 16: \
  150. __u->__ui = __c * 0x01010101; \
  151. __u = __extension__ (void *)((char *) __u + 4); \
  152. case 12: \
  153. __u->__ui = __c * 0x01010101; \
  154. __u = __extension__ (void *)((char *) __u + 4); \
  155. case 8: \
  156. __u->__ui = __c * 0x01010101; \
  157. __u = __extension__ (void *)((char *) __u + 4); \
  158. case 4: \
  159. __u->__ui = __c * 0x01010101; \
  160. case 0: \
  161. break; \
  162. } \
  163. \
  164. __s; })
  165. # else
  166. # define memset(s, c, n) \
  167. (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
  168. ? ({ void *__s = (s); __bzero (__s, n); __s; }) \
  169. : memset (s, c, n)))
  170. # endif
  171. /* GCC optimizes memset(s, 0, n) but not bzero(s, n).
  172. The optimization is broken before EGCS 1.1. */
  173. # if __GNUC_PREREQ (2, 91)
  174. # define __bzero(s, n) __builtin_memset (s, '\0', n)
  175. # endif
  176. #endif
  177. /* Copy N bytes from SRC to DEST, returning pointer to byte following the
  178. last copied. */
  179. #ifdef __USE_GNU
  180. # if !defined _HAVE_STRING_ARCH_mempcpy || defined _FORCE_INLINES
  181. # ifndef _HAVE_STRING_ARCH_mempcpy
  182. # define __mempcpy(dest, src, n) \
  183. (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
  184. && __string2_1bptr_p (src) && n <= 8 \
  185. ? __mempcpy_small (dest, __mempcpy_args (src), n) \
  186. : __mempcpy (dest, src, n)))
  187. /* In glibc we use this function frequently but for namespace reasons
  188. we have to use the name `__mempcpy'. */
  189. # define mempcpy(dest, src, n) __mempcpy (dest, src, n)
  190. # endif
  191. # if _STRING_ARCH_unaligned
  192. # ifndef _FORCE_INLINES
  193. # define __mempcpy_args(src) \
  194. ((__const char *) (src))[0], ((__const char *) (src))[2], \
  195. ((__const char *) (src))[4], ((__const char *) (src))[6], \
  196. __extension__ __STRING2_SMALL_GET16 (src, 0), \
  197. __extension__ __STRING2_SMALL_GET16 (src, 4), \
  198. __extension__ __STRING2_SMALL_GET32 (src, 0), \
  199. __extension__ __STRING2_SMALL_GET32 (src, 4)
  200. # endif
  201. __STRING_INLINE void *__mempcpy_small (void *, char, char, char, char,
  202. __uint16_t, __uint16_t, __uint32_t,
  203. __uint32_t, size_t);
  204. __STRING_INLINE void *
  205. __mempcpy_small (void *__dest1,
  206. char __src0_1, char __src2_1, char __src4_1, char __src6_1,
  207. __uint16_t __src0_2, __uint16_t __src4_2,
  208. __uint32_t __src0_4, __uint32_t __src4_4,
  209. size_t __srclen)
  210. {
  211. union {
  212. __uint32_t __ui;
  213. __uint16_t __usi;
  214. unsigned char __uc;
  215. unsigned char __c;
  216. } *__u = __dest1;
  217. switch ((unsigned int) __srclen)
  218. {
  219. case 1:
  220. __u->__c = __src0_1;
  221. __u = __extension__ (void *)((char *) __u + 1);
  222. break;
  223. case 2:
  224. __u->__usi = __src0_2;
  225. __u = __extension__ (void *)((char *) __u + 2);
  226. break;
  227. case 3:
  228. __u->__usi = __src0_2;
  229. __u = __extension__ (void *)((char *) __u + 2);
  230. __u->__c = __src2_1;
  231. __u = __extension__ (void *)((char *) __u + 1);
  232. break;
  233. case 4:
  234. __u->__ui = __src0_4;
  235. __u = __extension__ (void *)((char *) __u + 4);
  236. break;
  237. case 5:
  238. __u->__ui = __src0_4;
  239. __u = __extension__ (void *)((char *) __u + 4);
  240. __u->__c = __src4_1;
  241. __u = __extension__ (void *)((char *) __u + 1);
  242. break;
  243. case 6:
  244. __u->__ui = __src0_4;
  245. __u = __extension__ (void *)((char *) __u + 4);
  246. __u->__usi = __src4_2;
  247. __u = __extension__ (void *)((char *) __u + 2);
  248. break;
  249. case 7:
  250. __u->__ui = __src0_4;
  251. __u = __extension__ (void *)((char *) __u + 4);
  252. __u->__usi = __src4_2;
  253. __u = __extension__ (void *)((char *) __u + 2);
  254. __u->__c = __src6_1;
  255. __u = __extension__ (void *)((char *) __u + 1);
  256. break;
  257. case 8:
  258. __u->__ui = __src0_4;
  259. __u = __extension__ (void *)((char *) __u + 4);
  260. __u->__ui = __src4_4;
  261. __u = __extension__ (void *)((char *) __u + 4);
  262. break;
  263. }
  264. return (void *) __u;
  265. }
  266. # else
  267. # ifndef _FORCE_INLINES
  268. # define __mempcpy_args(src) \
  269. ((__const char *) (src))[0], \
  270. __extension__ ((__STRING2_COPY_ARR2) \
  271. { { ((__const char *) (src))[0], ((__const char *) (src))[1] } }), \
  272. __extension__ ((__STRING2_COPY_ARR3) \
  273. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  274. ((__const char *) (src))[2] } }), \
  275. __extension__ ((__STRING2_COPY_ARR4) \
  276. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  277. ((__const char *) (src))[2], ((__const char *) (src))[3] } }), \
  278. __extension__ ((__STRING2_COPY_ARR5) \
  279. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  280. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  281. ((__const char *) (src))[4] } }), \
  282. __extension__ ((__STRING2_COPY_ARR6) \
  283. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  284. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  285. ((__const char *) (src))[4], ((__const char *) (src))[5] } }), \
  286. __extension__ ((__STRING2_COPY_ARR7) \
  287. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  288. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  289. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  290. ((__const char *) (src))[6] } }), \
  291. __extension__ ((__STRING2_COPY_ARR8) \
  292. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  293. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  294. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  295. ((__const char *) (src))[6], ((__const char *) (src))[7] } })
  296. # endif
  297. __STRING_INLINE void *__mempcpy_small (void *, char, __STRING2_COPY_ARR2,
  298. __STRING2_COPY_ARR3,
  299. __STRING2_COPY_ARR4,
  300. __STRING2_COPY_ARR5,
  301. __STRING2_COPY_ARR6,
  302. __STRING2_COPY_ARR7,
  303. __STRING2_COPY_ARR8, size_t);
  304. __STRING_INLINE void *
  305. __mempcpy_small (void *__dest, char __src1,
  306. __STRING2_COPY_ARR2 __src2, __STRING2_COPY_ARR3 __src3,
  307. __STRING2_COPY_ARR4 __src4, __STRING2_COPY_ARR5 __src5,
  308. __STRING2_COPY_ARR6 __src6, __STRING2_COPY_ARR7 __src7,
  309. __STRING2_COPY_ARR8 __src8, size_t __srclen)
  310. {
  311. union {
  312. char __c;
  313. __STRING2_COPY_ARR2 __sca2;
  314. __STRING2_COPY_ARR3 __sca3;
  315. __STRING2_COPY_ARR4 __sca4;
  316. __STRING2_COPY_ARR5 __sca5;
  317. __STRING2_COPY_ARR6 __sca6;
  318. __STRING2_COPY_ARR7 __sca7;
  319. __STRING2_COPY_ARR8 __sca8;
  320. } *__u = __dest;
  321. switch ((unsigned int) __srclen)
  322. {
  323. case 1:
  324. __u->__c = __src1;
  325. break;
  326. case 2:
  327. __extension__ __u->__sca2 = __src2;
  328. break;
  329. case 3:
  330. __extension__ __u->__sca3 = __src3;
  331. break;
  332. case 4:
  333. __extension__ __u->__sca4 = __src4;
  334. break;
  335. case 5:
  336. __extension__ __u->__sca5 = __src5;
  337. break;
  338. case 6:
  339. __extension__ __u->__sca6 = __src6;
  340. break;
  341. case 7:
  342. __extension__ __u->__sca7 = __src7;
  343. break;
  344. case 8:
  345. __extension__ __u->__sca8 = __src8;
  346. break;
  347. }
  348. return __extension__ (void *)((char *) __u + __srclen);
  349. }
  350. # endif
  351. # endif
  352. #endif
  353. /* Return pointer to C in S. */
  354. #ifndef _HAVE_STRING_ARCH_strchr
  355. extern void *__rawmemchr (const void *__s, int __c);
  356. # define strchr(s, c) \
  357. (__extension__ (__builtin_constant_p (c) && (c) == '\0' \
  358. ? (char *) __rawmemchr (s, c) \
  359. : strchr (s, c)))
  360. #endif
  361. /* Copy SRC to DEST. */
  362. #if !defined _HAVE_STRING_ARCH_strcpy || defined _FORCE_INLINES
  363. # ifndef _HAVE_STRING_ARCH_strcpy
  364. # define strcpy(dest, src) \
  365. (__extension__ (__builtin_constant_p (src) \
  366. ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
  367. ? __strcpy_small (dest, __strcpy_args (src), \
  368. strlen (src) + 1) \
  369. : (char *) memcpy (dest, src, strlen (src) + 1)) \
  370. : strcpy (dest, src)))
  371. # endif
  372. # if _STRING_ARCH_unaligned
  373. # ifndef _FORCE_INLINES
  374. # define __strcpy_args(src) \
  375. __extension__ __STRING2_SMALL_GET16 (src, 0), \
  376. __extension__ __STRING2_SMALL_GET16 (src, 4), \
  377. __extension__ __STRING2_SMALL_GET32 (src, 0), \
  378. __extension__ __STRING2_SMALL_GET32 (src, 4)
  379. # endif
  380. __STRING_INLINE char *__strcpy_small (char *, __uint16_t, __uint16_t,
  381. __uint32_t, __uint32_t, size_t);
  382. __STRING_INLINE char *
  383. __strcpy_small (char *__dest,
  384. __uint16_t __src0_2, __uint16_t __src4_2,
  385. __uint32_t __src0_4, __uint32_t __src4_4,
  386. size_t __srclen)
  387. {
  388. union {
  389. __uint32_t __ui;
  390. __uint16_t __usi;
  391. unsigned char __uc;
  392. } *__u = (void *) __dest;
  393. switch ((unsigned int) __srclen)
  394. {
  395. case 1:
  396. __u->__uc = '\0';
  397. break;
  398. case 2:
  399. __u->__usi = __src0_2;
  400. break;
  401. case 3:
  402. __u->__usi = __src0_2;
  403. __u = __extension__ (void *)((char *) __u + 2);
  404. __u->__uc = '\0';
  405. break;
  406. case 4:
  407. __u->__ui = __src0_4;
  408. break;
  409. case 5:
  410. __u->__ui = __src0_4;
  411. __u = __extension__ (void *)((char *) __u + 4);
  412. __u->__uc = '\0';
  413. break;
  414. case 6:
  415. __u->__ui = __src0_4;
  416. __u = __extension__ (void *)((char *) __u + 4);
  417. __u->__usi = __src4_2;
  418. break;
  419. case 7:
  420. __u->__ui = __src0_4;
  421. __u = __extension__ (void *)((char *) __u + 4);
  422. __u->__usi = __src4_2;
  423. __u = __extension__ (void *)((char *) __u + 2);
  424. __u->__uc = '\0';
  425. break;
  426. case 8:
  427. __u->__ui = __src0_4;
  428. __u = __extension__ (void *)((char *) __u + 4);
  429. __u->__ui = __src4_4;
  430. break;
  431. }
  432. return __dest;
  433. }
  434. # else
  435. # ifndef _FORCE_INLINES
  436. # define __strcpy_args(src) \
  437. __extension__ ((__STRING2_COPY_ARR2) \
  438. { { ((__const char *) (src))[0], '\0' } }), \
  439. __extension__ ((__STRING2_COPY_ARR3) \
  440. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  441. '\0' } }), \
  442. __extension__ ((__STRING2_COPY_ARR4) \
  443. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  444. ((__const char *) (src))[2], '\0' } }), \
  445. __extension__ ((__STRING2_COPY_ARR5) \
  446. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  447. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  448. '\0' } }), \
  449. __extension__ ((__STRING2_COPY_ARR6) \
  450. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  451. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  452. ((__const char *) (src))[4], '\0' } }), \
  453. __extension__ ((__STRING2_COPY_ARR7) \
  454. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  455. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  456. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  457. '\0' } }), \
  458. __extension__ ((__STRING2_COPY_ARR8) \
  459. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  460. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  461. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  462. ((__const char *) (src))[6], '\0' } })
  463. # endif
  464. __STRING_INLINE char *__strcpy_small (char *, __STRING2_COPY_ARR2,
  465. __STRING2_COPY_ARR3,
  466. __STRING2_COPY_ARR4,
  467. __STRING2_COPY_ARR5,
  468. __STRING2_COPY_ARR6,
  469. __STRING2_COPY_ARR7,
  470. __STRING2_COPY_ARR8, size_t);
  471. __STRING_INLINE char *
  472. __strcpy_small (char *__dest,
  473. __STRING2_COPY_ARR2 __src2, __STRING2_COPY_ARR3 __src3,
  474. __STRING2_COPY_ARR4 __src4, __STRING2_COPY_ARR5 __src5,
  475. __STRING2_COPY_ARR6 __src6, __STRING2_COPY_ARR7 __src7,
  476. __STRING2_COPY_ARR8 __src8, size_t __srclen)
  477. {
  478. union {
  479. char __c;
  480. __STRING2_COPY_ARR2 __sca2;
  481. __STRING2_COPY_ARR3 __sca3;
  482. __STRING2_COPY_ARR4 __sca4;
  483. __STRING2_COPY_ARR5 __sca5;
  484. __STRING2_COPY_ARR6 __sca6;
  485. __STRING2_COPY_ARR7 __sca7;
  486. __STRING2_COPY_ARR8 __sca8;
  487. } *__u = (void *) __dest;
  488. switch ((unsigned int) __srclen)
  489. {
  490. case 1:
  491. __u->__c = '\0';
  492. break;
  493. case 2:
  494. __extension__ __u->__sca2 = __src2;
  495. break;
  496. case 3:
  497. __extension__ __u->__sca3 = __src3;
  498. break;
  499. case 4:
  500. __extension__ __u->__sca4 = __src4;
  501. break;
  502. case 5:
  503. __extension__ __u->__sca5 = __src5;
  504. break;
  505. case 6:
  506. __extension__ __u->__sca6 = __src6;
  507. break;
  508. case 7:
  509. __extension__ __u->__sca7 = __src7;
  510. break;
  511. case 8:
  512. __extension__ __u->__sca8 = __src8;
  513. break;
  514. }
  515. return __dest;
  516. }
  517. # endif
  518. #endif
  519. /* Copy SRC to DEST, returning pointer to final NUL byte. */
  520. #ifdef __USE_GNU
  521. # if !defined _HAVE_STRING_ARCH_stpcpy || defined _FORCE_INLINES
  522. # ifndef _HAVE_STRING_ARCH_stpcpy
  523. # define __stpcpy(dest, src) \
  524. (__extension__ (__builtin_constant_p (src) \
  525. ? (__string2_1bptr_p (src) && strlen (src) + 1 <= 8 \
  526. ? __stpcpy_small (dest, __stpcpy_args (src), \
  527. strlen (src) + 1) \
  528. : ((char *) __mempcpy (dest, src, strlen (src) + 1) - 1))\
  529. : __stpcpy (dest, src)))
  530. /* In glibc we use this function frequently but for namespace reasons
  531. we have to use the name `__stpcpy'. */
  532. # define stpcpy(dest, src) __stpcpy (dest, src)
  533. # endif
  534. # if _STRING_ARCH_unaligned
  535. # ifndef _FORCE_INLINES
  536. # define __stpcpy_args(src) \
  537. __extension__ __STRING2_SMALL_GET16 (src, 0), \
  538. __extension__ __STRING2_SMALL_GET16 (src, 4), \
  539. __extension__ __STRING2_SMALL_GET32 (src, 0), \
  540. __extension__ __STRING2_SMALL_GET32 (src, 4)
  541. # endif
  542. __STRING_INLINE char *__stpcpy_small (char *, __uint16_t, __uint16_t,
  543. __uint32_t, __uint32_t, size_t);
  544. __STRING_INLINE char *
  545. __stpcpy_small (char *__dest,
  546. __uint16_t __src0_2, __uint16_t __src4_2,
  547. __uint32_t __src0_4, __uint32_t __src4_4,
  548. size_t __srclen)
  549. {
  550. union {
  551. unsigned int __ui;
  552. unsigned short int __usi;
  553. unsigned char __uc;
  554. char __c;
  555. } *__u = (void *) __dest;
  556. switch ((unsigned int) __srclen)
  557. {
  558. case 1:
  559. __u->__uc = '\0';
  560. break;
  561. case 2:
  562. __u->__usi = __src0_2;
  563. __u = __extension__ (void *)((char *) __u + 1);
  564. break;
  565. case 3:
  566. __u->__usi = __src0_2;
  567. __u = __extension__ (void *)((char *) __u + 2);
  568. __u->__uc = '\0';
  569. break;
  570. case 4:
  571. __u->__ui = __src0_4;
  572. __u = __extension__ (void *)((char *) __u + 3);
  573. break;
  574. case 5:
  575. __u->__ui = __src0_4;
  576. __u = __extension__ (void *)((char *) __u + 4);
  577. __u->__uc = '\0';
  578. break;
  579. case 6:
  580. __u->__ui = __src0_4;
  581. __u = __extension__ (void *)((char *) __u + 4);
  582. __u->__usi = __src4_2;
  583. __u = __extension__ (void *)((char *) __u + 1);
  584. break;
  585. case 7:
  586. __u->__ui = __src0_4;
  587. __u = __extension__ (void *)((char *) __u + 4);
  588. __u->__usi = __src4_2;
  589. __u = __extension__ (void *)((char *) __u + 2);
  590. __u->__uc = '\0';
  591. break;
  592. case 8:
  593. __u->__ui = __src0_4;
  594. __u = __extension__ (void *)((char *) __u + 4);
  595. __u->__ui = __src4_4;
  596. __u = __extension__ (void *)((char *) __u + 3);
  597. break;
  598. }
  599. return &__u->__c;
  600. }
  601. # else
  602. # ifndef _FORCE_INLINES
  603. # define __stpcpy_args(src) \
  604. __extension__ ((__STRING2_COPY_ARR2) \
  605. { { ((__const char *) (src))[0], '\0' } }), \
  606. __extension__ ((__STRING2_COPY_ARR3) \
  607. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  608. '\0' } }), \
  609. __extension__ ((__STRING2_COPY_ARR4) \
  610. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  611. ((__const char *) (src))[2], '\0' } }), \
  612. __extension__ ((__STRING2_COPY_ARR5) \
  613. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  614. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  615. '\0' } }), \
  616. __extension__ ((__STRING2_COPY_ARR6) \
  617. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  618. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  619. ((__const char *) (src))[4], '\0' } }), \
  620. __extension__ ((__STRING2_COPY_ARR7) \
  621. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  622. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  623. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  624. '\0' } }), \
  625. __extension__ ((__STRING2_COPY_ARR8) \
  626. { { ((__const char *) (src))[0], ((__const char *) (src))[1], \
  627. ((__const char *) (src))[2], ((__const char *) (src))[3], \
  628. ((__const char *) (src))[4], ((__const char *) (src))[5], \
  629. ((__const char *) (src))[6], '\0' } })
  630. # endif
  631. __STRING_INLINE char *__stpcpy_small (char *, __STRING2_COPY_ARR2,
  632. __STRING2_COPY_ARR3,
  633. __STRING2_COPY_ARR4,
  634. __STRING2_COPY_ARR5,
  635. __STRING2_COPY_ARR6,
  636. __STRING2_COPY_ARR7,
  637. __STRING2_COPY_ARR8, size_t);
  638. __STRING_INLINE char *
  639. __stpcpy_small (char *__dest,
  640. __STRING2_COPY_ARR2 __src2, __STRING2_COPY_ARR3 __src3,
  641. __STRING2_COPY_ARR4 __src4, __STRING2_COPY_ARR5 __src5,
  642. __STRING2_COPY_ARR6 __src6, __STRING2_COPY_ARR7 __src7,
  643. __STRING2_COPY_ARR8 __src8, size_t __srclen)
  644. {
  645. union {
  646. char __c;
  647. __STRING2_COPY_ARR2 __sca2;
  648. __STRING2_COPY_ARR3 __sca3;
  649. __STRING2_COPY_ARR4 __sca4;
  650. __STRING2_COPY_ARR5 __sca5;
  651. __STRING2_COPY_ARR6 __sca6;
  652. __STRING2_COPY_ARR7 __sca7;
  653. __STRING2_COPY_ARR8 __sca8;
  654. } *__u = (void *) __dest;
  655. switch ((unsigned int) __srclen)
  656. {
  657. case 1:
  658. __u->__c = '\0';
  659. break;
  660. case 2:
  661. __extension__ __u->__sca2 = __src2;
  662. break;
  663. case 3:
  664. __extension__ __u->__sca3 = __src3;
  665. break;
  666. case 4:
  667. __extension__ __u->__sca4 = __src4;
  668. break;
  669. case 5:
  670. __extension__ __u->__sca5 = __src5;
  671. break;
  672. case 6:
  673. __extension__ __u->__sca6 = __src6;
  674. break;
  675. case 7:
  676. __extension__ __u->__sca7 = __src7;
  677. break;
  678. case 8:
  679. __extension__ __u->__sca8 = __src8;
  680. break;
  681. }
  682. return __dest + __srclen - 1;
  683. }
  684. # endif
  685. # endif
  686. #endif
  687. /* Copy no more than N characters of SRC to DEST. */
  688. #ifndef _HAVE_STRING_ARCH_strncpy
  689. # if defined _USE_STRING_ARCH_memset && defined _USE_STRING_ARCH_mempcpy
  690. # define strncpy(dest, src, n) \
  691. (__extension__ ({ char *__dest = (dest); \
  692. __builtin_constant_p (src) && __builtin_constant_p (n) \
  693. ? (strlen (src) + 1 >= ((size_t) (n)) \
  694. ? (char *) memcpy (__dest, src, n) \
  695. : (memset (__mempcpy (__dest, src, strlen (src)), \
  696. '\0', n - strlen (src)), \
  697. __dest)) \
  698. : strncpy (__dest, src, n); }))
  699. # else
  700. # define strncpy(dest, src, n) \
  701. (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
  702. ? (strlen (src) + 1 >= ((size_t) (n)) \
  703. ? (char *) memcpy (dest, src, n) \
  704. : strncpy (dest, src, n)) \
  705. : strncpy (dest, src, n)))
  706. # endif
  707. #endif
  708. /* Append no more than N characters from SRC onto DEST. */
  709. #ifndef _HAVE_STRING_ARCH_strncat
  710. # ifdef _USE_STRING_ARCH_strchr
  711. # define strncat(dest, src, n) \
  712. (__extension__ ({ char *__dest = (dest); \
  713. __builtin_constant_p (src) && __builtin_constant_p (n) \
  714. ? (strlen (src) < ((size_t) (n)) \
  715. ? strcat (__dest, src) \
  716. : (*((char *) __mempcpy (strchr (__dest, '\0'), \
  717. src, n)) = '\0', __dest)) \
  718. : strncat (dest, src, n); }))
  719. # else
  720. # define strncat(dest, src, n) \
  721. (__extension__ (__builtin_constant_p (src) && __builtin_constant_p (n) \
  722. ? (strlen (src) < ((size_t) (n)) \
  723. ? strcat (dest, src) \
  724. : strncat (dest, src, n)) \
  725. : strncat (dest, src, n)))
  726. # endif
  727. #endif
  728. /* Compare characters of S1 and S2. */
  729. #ifndef _HAVE_STRING_ARCH_strcmp
  730. # define strcmp(s1, s2) \
  731. __extension__ \
  732. ({ size_t __s1_len, __s2_len; \
  733. (__builtin_constant_p (s1) && __builtin_constant_p (s2) \
  734. && (__s1_len = strlen (s1), __s2_len = strlen (s2), \
  735. (!__string2_1bptr_p (s1) || __s1_len >= 4) \
  736. && (!__string2_1bptr_p (s2) || __s2_len >= 4)) \
  737. ? memcmp ((__const char *) (s1), (__const char *) (s2), \
  738. (__s1_len < __s2_len ? __s1_len : __s2_len) + 1) \
  739. : (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
  740. && (__s1_len = strlen (s1), __s1_len < 4) \
  741. ? (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
  742. ? __strcmp_cc (s1, s2, __s1_len) \
  743. : __strcmp_cg (s1, s2, __s1_len)) \
  744. : (__builtin_constant_p (s2) && __string2_1bptr_p (s2) \
  745. && (__s2_len = strlen (s2), __s2_len < 4) \
  746. ? (__builtin_constant_p (s1) && __string2_1bptr_p (s1) \
  747. ? __strcmp_cc (s1, s2, __s2_len) \
  748. : __strcmp_gc (s1, s2, __s2_len)) \
  749. : strcmp (s1, s2)))); })
  750. # define __strcmp_cc(s1, s2, l) \
  751. (__extension__ ({ register int __result = \
  752. (((__const unsigned char *) (__const char *) (s1))[0] \
  753. - ((__const unsigned char *) (__const char *)(s2))[0]);\
  754. if (l > 0 && __result == 0) \
  755. { \
  756. __result = (((__const unsigned char *) \
  757. (__const char *) (s1))[1] \
  758. - ((__const unsigned char *) \
  759. (__const char *) (s2))[1]); \
  760. if (l > 1 && __result == 0) \
  761. { \
  762. __result = \
  763. (((__const unsigned char *) \
  764. (__const char *) (s1))[2] \
  765. - ((__const unsigned char *) \
  766. (__const char *) (s2))[2]); \
  767. if (l > 2 && __result == 0) \
  768. __result = \
  769. (((__const unsigned char *) \
  770. (__const char *) (s1))[3] \
  771. - ((__const unsigned char *) \
  772. (__const char *) (s2))[3]); \
  773. } \
  774. } \
  775. __result; }))
  776. # define __strcmp_cg(s1, s2, l1) \
  777. (__extension__ ({ __const unsigned char *__s2 = \
  778. (__const unsigned char *) (__const char *) (s2); \
  779. register int __result = \
  780. (((__const unsigned char *) (__const char *) (s1))[0] \
  781. - __s2[0]); \
  782. if (l1 > 0 && __result == 0) \
  783. { \
  784. __result = (((__const unsigned char *) \
  785. (__const char *) (s1))[1] - __s2[1]); \
  786. if (l1 > 1 && __result == 0) \
  787. { \
  788. __result = (((__const unsigned char *) \
  789. (__const char *) (s1))[2] - __s2[2]);\
  790. if (l1 > 2 && __result == 0) \
  791. __result = (((__const unsigned char *) \
  792. (__const char *) (s1))[3] \
  793. - __s2[3]); \
  794. } \
  795. } \
  796. __result; }))
  797. # define __strcmp_gc(s1, s2, l2) \
  798. (__extension__ ({ __const unsigned char *__s1 = \
  799. (__const unsigned char *) (__const char *) (s1); \
  800. register int __result = \
  801. __s1[0] - ((__const unsigned char *) \
  802. (__const char *) (s2))[0]; \
  803. if (l2 > 0 && __result == 0) \
  804. { \
  805. __result = (__s1[1] \
  806. - ((__const unsigned char *) \
  807. (__const char *) (s2))[1]); \
  808. if (l2 > 1 && __result == 0) \
  809. { \
  810. __result = \
  811. (__s1[2] - ((__const unsigned char *) \
  812. (__const char *) (s2))[2]); \
  813. if (l2 > 2 && __result == 0) \
  814. __result = \
  815. (__s1[3] \
  816. - ((__const unsigned char *) \
  817. (__const char *) (s2))[3]); \
  818. } \
  819. } \
  820. __result; }))
  821. #endif
  822. /* Compare N characters of S1 and S2. */
  823. #ifndef _HAVE_STRING_ARCH_strncmp
  824. # define strncmp(s1, s2, n) \
  825. (__extension__ (__builtin_constant_p (n) \
  826. && ((__builtin_constant_p (s1) \
  827. && strlen (s1) < ((size_t) (n))) \
  828. || (__builtin_constant_p (s2) \
  829. && strlen (s2) < ((size_t) (n)))) \
  830. ? strcmp (s1, s2) : strncmp (s1, s2, n)))
  831. #endif
  832. /* Return the length of the initial segment of S which
  833. consists entirely of characters not in REJECT. */
  834. #if !defined _HAVE_STRING_ARCH_strcspn || defined _FORCE_INLINES
  835. # ifndef _HAVE_STRING_ARCH_strcspn
  836. # define strcspn(s, reject) \
  837. __extension__ \
  838. ({ char __r0, __r1, __r2; \
  839. (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
  840. ? ((__r0 = ((__const char *) (reject))[0], __r0 == '\0') \
  841. ? strlen (s) \
  842. : ((__r1 = ((__const char *) (reject))[1], __r1 == '\0') \
  843. ? __strcspn_c1 (s, __r0) \
  844. : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
  845. ? __strcspn_c2 (s, __r0, __r1) \
  846. : (((__const char *) (reject))[3] == '\0' \
  847. ? __strcspn_c3 (s, __r0, __r1, __r2) \
  848. : strcspn (s, reject))))) \
  849. : strcspn (s, reject)); })
  850. # endif
  851. __STRING_INLINE size_t __strcspn_c1 (__const char *__s, int __reject);
  852. __STRING_INLINE size_t
  853. __strcspn_c1 (__const char *__s, int __reject)
  854. {
  855. register size_t __result = 0;
  856. while (__s[__result] != '\0' && __s[__result] != __reject)
  857. ++__result;
  858. return __result;
  859. }
  860. __STRING_INLINE size_t __strcspn_c2 (__const char *__s, int __reject1,
  861. int __reject2);
  862. __STRING_INLINE size_t
  863. __strcspn_c2 (__const char *__s, int __reject1, int __reject2)
  864. {
  865. register size_t __result = 0;
  866. while (__s[__result] != '\0' && __s[__result] != __reject1
  867. && __s[__result] != __reject2)
  868. ++__result;
  869. return __result;
  870. }
  871. __STRING_INLINE size_t __strcspn_c3 (__const char *__s, int __reject1,
  872. int __reject2, int __reject3);
  873. __STRING_INLINE size_t
  874. __strcspn_c3 (__const char *__s, int __reject1, int __reject2,
  875. int __reject3)
  876. {
  877. register size_t __result = 0;
  878. while (__s[__result] != '\0' && __s[__result] != __reject1
  879. && __s[__result] != __reject2 && __s[__result] != __reject3)
  880. ++__result;
  881. return __result;
  882. }
  883. #endif
  884. /* Return the length of the initial segment of S which
  885. consists entirely of characters in ACCEPT. */
  886. #if !defined _HAVE_STRING_ARCH_strspn || defined _FORCE_INLINES
  887. # ifndef _HAVE_STRING_ARCH_strspn
  888. # define strspn(s, accept) \
  889. __extension__ \
  890. ({ char __a0, __a1, __a2; \
  891. (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
  892. ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
  893. ? ((void) (s), 0) \
  894. : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
  895. ? __strspn_c1 (s, __a0) \
  896. : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
  897. ? __strspn_c2 (s, __a0, __a1) \
  898. : (((__const char *) (accept))[3] == '\0' \
  899. ? __strspn_c3 (s, __a0, __a1, __a2) \
  900. : strspn (s, accept))))) \
  901. : strspn (s, accept)); })
  902. # endif
  903. __STRING_INLINE size_t __strspn_c1 (__const char *__s, int __accept);
  904. __STRING_INLINE size_t
  905. __strspn_c1 (__const char *__s, int __accept)
  906. {
  907. register size_t __result = 0;
  908. /* Please note that __accept never can be '\0'. */
  909. while (__s[__result] == __accept)
  910. ++__result;
  911. return __result;
  912. }
  913. __STRING_INLINE size_t __strspn_c2 (__const char *__s, int __accept1,
  914. int __accept2);
  915. __STRING_INLINE size_t
  916. __strspn_c2 (__const char *__s, int __accept1, int __accept2)
  917. {
  918. register size_t __result = 0;
  919. /* Please note that __accept1 and __accept2 never can be '\0'. */
  920. while (__s[__result] == __accept1 || __s[__result] == __accept2)
  921. ++__result;
  922. return __result;
  923. }
  924. __STRING_INLINE size_t __strspn_c3 (__const char *__s, int __accept1,
  925. int __accept2, int __accept3);
  926. __STRING_INLINE size_t
  927. __strspn_c3 (__const char *__s, int __accept1, int __accept2, int __accept3)
  928. {
  929. register size_t __result = 0;
  930. /* Please note that __accept1 to __accept3 never can be '\0'. */
  931. while (__s[__result] == __accept1 || __s[__result] == __accept2
  932. || __s[__result] == __accept3)
  933. ++__result;
  934. return __result;
  935. }
  936. #endif
  937. /* Find the first occurrence in S of any character in ACCEPT. */
  938. #if !defined _HAVE_STRING_ARCH_strpbrk || defined _FORCE_INLINES
  939. # ifndef _HAVE_STRING_ARCH_strpbrk
  940. # define strpbrk(s, accept) \
  941. __extension__ \
  942. ({ char __a0, __a1, __a2; \
  943. (__builtin_constant_p (accept) && __string2_1bptr_p (accept) \
  944. ? ((__a0 = ((__const char *) (accept))[0], __a0 == '\0') \
  945. ? ((void) (s), NULL) \
  946. : ((__a1 = ((__const char *) (accept))[1], __a1 == '\0') \
  947. ? strchr (s, __a0) \
  948. : ((__a2 = ((__const char *) (accept))[2], __a2 == '\0') \
  949. ? __strpbrk_c2 (s, __a0, __a1) \
  950. : (((__const char *) (accept))[3] == '\0' \
  951. ? __strpbrk_c3 (s, __a0, __a1, __a2) \
  952. : strpbrk (s, accept))))) \
  953. : strpbrk (s, accept)); })
  954. # endif
  955. __STRING_INLINE char *__strpbrk_c2 (__const char *__s, int __accept1,
  956. int __accept2);
  957. __STRING_INLINE char *
  958. __strpbrk_c2 (__const char *__s, int __accept1, int __accept2)
  959. {
  960. /* Please note that __accept1 and __accept2 never can be '\0'. */
  961. while (*__s != '\0' && *__s != __accept1 && *__s != __accept2)
  962. ++__s;
  963. return *__s == '\0' ? NULL : (char *) (size_t) __s;
  964. }
  965. __STRING_INLINE char *__strpbrk_c3 (__const char *__s, int __accept1,
  966. int __accept2, int __accept3);
  967. __STRING_INLINE char *
  968. __strpbrk_c3 (__const char *__s, int __accept1, int __accept2,
  969. int __accept3)
  970. {
  971. /* Please note that __accept1 to __accept3 never can be '\0'. */
  972. while (*__s != '\0' && *__s != __accept1 && *__s != __accept2
  973. && *__s != __accept3)
  974. ++__s;
  975. return *__s == '\0' ? NULL : (char *) (size_t) __s;
  976. }
  977. #endif
  978. /* Find the first occurrence of NEEDLE in HAYSTACK. Newer gcc versions
  979. do this itself. */
  980. #if !defined _HAVE_STRING_ARCH_strstr && !__GNUC_PREREQ (2, 97)
  981. # define strstr(haystack, needle) \
  982. (__extension__ (__builtin_constant_p (needle) && __string2_1bptr_p (needle) \
  983. ? (((__const char *) (needle))[0] == '\0' \
  984. ? (char *) (size_t) (haystack) \
  985. : (((__const char *) (needle))[1] == '\0' \
  986. ? strchr (haystack, \
  987. ((__const char *) (needle))[0]) \
  988. : strstr (haystack, needle))) \
  989. : strstr (haystack, needle)))
  990. #endif
  991. #if !defined _HAVE_STRING_ARCH_strtok_r || defined _FORCE_INLINES
  992. # ifndef _HAVE_STRING_ARCH_strtok_r
  993. # define __strtok_r(s, sep, nextp) \
  994. (__extension__ (__builtin_constant_p (sep) && __string2_1bptr_p (sep) \
  995. ? (((__const char *) (sep))[0] != '\0' \
  996. && ((__const char *) (sep))[1] == '\0' \
  997. ? __strtok_r_1c (s, ((__const char *) (sep))[0], nextp) \
  998. : __strtok_r (s, sep, nextp)) \
  999. : __strtok_r (s, sep, nextp)))
  1000. # endif
  1001. __STRING_INLINE char *__strtok_r_1c (char *__s, char __sep, char **__nextp);
  1002. __STRING_INLINE char *
  1003. __strtok_r_1c (char *__s, char __sep, char **__nextp)
  1004. {
  1005. char *__result;
  1006. if (__s == NULL)
  1007. __s = *__nextp;
  1008. while (*__s == __sep)
  1009. ++__s;
  1010. __result = NULL;
  1011. if (*__s != '\0')
  1012. {
  1013. __result = __s++;
  1014. while (*__s != '\0')
  1015. if (*__s++ == __sep)
  1016. {
  1017. __s[-1] = '\0';
  1018. break;
  1019. }
  1020. *__nextp = __s;
  1021. }
  1022. return __result;
  1023. }
  1024. # if defined __USE_POSIX || defined __USE_MISC
  1025. # define strtok_r(s, sep, nextp) __strtok_r (s, sep, nextp)
  1026. # endif
  1027. #endif
  1028. #if !defined _HAVE_STRING_ARCH_strsep || defined _FORCE_INLINES
  1029. # ifndef _HAVE_STRING_ARCH_strsep
  1030. # define __strsep(s, reject) \
  1031. __extension__ \
  1032. ({ char __r0, __r1, __r2; \
  1033. (__builtin_constant_p (reject) && __string2_1bptr_p (reject) \
  1034. && (__r0 = ((__const char *) (reject))[0], \
  1035. ((__const char *) (reject))[0] != '\0') \
  1036. ? ((__r1 = ((__const char *) (reject))[1], \
  1037. ((__const char *) (reject))[1] == '\0') \
  1038. ? __strsep_1c (s, __r0) \
  1039. : ((__r2 = ((__const char *) (reject))[2], __r2 == '\0') \
  1040. ? __strsep_2c (s, __r0, __r1) \
  1041. : (((__const char *) (reject))[3] == '\0' \
  1042. ? __strsep_3c (s, __r0, __r1, __r2) \
  1043. : __strsep (s, reject)))) \
  1044. : __strsep (s, reject)); })
  1045. # endif
  1046. __STRING_INLINE char *__strsep_1c (char **__s, char __reject);
  1047. __STRING_INLINE char *
  1048. __strsep_1c (char **__s, char __reject)
  1049. {
  1050. register char *__retval = *__s;
  1051. if (__retval != NULL && (*__s = strchr (__retval, __reject)) != NULL)
  1052. *(*__s)++ = '\0';
  1053. return __retval;
  1054. }
  1055. __STRING_INLINE char *__strsep_2c (char **__s, char __reject1, char __reject2);
  1056. __STRING_INLINE char *
  1057. __strsep_2c (char **__s, char __reject1, char __reject2)
  1058. {
  1059. register char *__retval = *__s;
  1060. if (__retval != NULL)
  1061. {
  1062. register char *__cp = __retval;
  1063. while (1)
  1064. {
  1065. if (*__cp == '\0')
  1066. {
  1067. __cp = NULL;
  1068. break;
  1069. }
  1070. if (*__cp == __reject1 || *__cp == __reject2)
  1071. {
  1072. *__cp++ = '\0';
  1073. break;
  1074. }
  1075. ++__cp;
  1076. }
  1077. *__s = __cp;
  1078. }
  1079. return __retval;
  1080. }
  1081. __STRING_INLINE char *__strsep_3c (char **__s, char __reject1, char __reject2,
  1082. char __reject3);
  1083. __STRING_INLINE char *
  1084. __strsep_3c (char **__s, char __reject1, char __reject2, char __reject3)
  1085. {
  1086. register char *__retval = *__s;
  1087. if (__retval != NULL)
  1088. {
  1089. register char *__cp = __retval;
  1090. while (1)
  1091. {
  1092. if (*__cp == '\0')
  1093. {
  1094. __cp = NULL;
  1095. break;
  1096. }
  1097. if (*__cp == __reject1 || *__cp == __reject2 || *__cp == __reject3)
  1098. {
  1099. *__cp++ = '\0';
  1100. break;
  1101. }
  1102. ++__cp;
  1103. }
  1104. *__s = __cp;
  1105. }
  1106. return __retval;
  1107. }
  1108. # ifdef __USE_BSD
  1109. # define strsep(s, reject) __strsep (s, reject)
  1110. # endif
  1111. #endif
  1112. /* We need the memory allocation functions for inline strdup().
  1113. Referring to stdlib.h (even minimally) is not allowed
  1114. in any of the tight standards compliant modes. */
  1115. #ifdef __USE_MISC
  1116. # if !defined _HAVE_STRING_ARCH_strdup || !defined _HAVE_STRING_ARCH_strndup
  1117. # define __need_malloc_and_calloc
  1118. # include <stdlib.h>
  1119. # endif
  1120. # ifndef _HAVE_STRING_ARCH_strdup
  1121. extern char *__strdup (__const char *__string) __THROW __attribute_malloc__;
  1122. # define __strdup(s) \
  1123. (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
  1124. ? (((__const char *) (s))[0] == '\0' \
  1125. ? (char *) calloc (1, 1) \
  1126. : ({ size_t __len = strlen (s) + 1; \
  1127. char *__retval = (char *) malloc (__len); \
  1128. if (__retval != NULL) \
  1129. __retval = (char *) memcpy (__retval, s, __len); \
  1130. __retval; })) \
  1131. : __strdup (s)))
  1132. # if defined __USE_SVID || defined __USE_BSD || defined __USE_XOPEN_EXTENDED
  1133. # define strdup(s) __strdup (s)
  1134. # endif
  1135. # endif
  1136. # ifndef _HAVE_STRING_ARCH_strndup
  1137. extern char *__strndup (__const char *__string, size_t __n)
  1138. __THROW __attribute_malloc__;
  1139. # define __strndup(s, n) \
  1140. (__extension__ (__builtin_constant_p (s) && __string2_1bptr_p (s) \
  1141. ? (((__const char *) (s))[0] == '\0' \
  1142. ? (char *) calloc (1, 1) \
  1143. : ({ size_t __len = strlen (s) + 1; \
  1144. size_t __n = (n); \
  1145. char *__retval; \
  1146. if (__n < __len) \
  1147. __len = __n; \
  1148. __retval = (char *) malloc (__len); \
  1149. if (__retval != NULL) \
  1150. { \
  1151. __retval[__len - 1] = '\0'; \
  1152. __retval = (char *) memcpy (__retval, s, \
  1153. __len - 1); \
  1154. } \
  1155. __retval; })) \
  1156. : __strndup (s, n)))
  1157. # ifdef __USE_GNU
  1158. # define strndup(s, n) __strndup (s, n)
  1159. # endif
  1160. # endif
  1161. #endif /* Use misc. or use GNU. */
  1162. #ifndef _FORCE_INLINES
  1163. # undef __STRING_INLINE
  1164. #endif
  1165. #endif /* No string inlines. */