string.in.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /* A GNU-like <string.h>.
  2. Copyright (C) 1995-1996, 2001-2010 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #ifndef _GL_STRING_H
  15. #if __GNUC__ >= 3
  16. @PRAGMA_SYSTEM_HEADER@
  17. #endif
  18. /* The include_next requires a split double-inclusion guard. */
  19. #@INCLUDE_NEXT@ @NEXT_STRING_H@
  20. #ifndef _GL_STRING_H
  21. #define _GL_STRING_H
  22. /* NetBSD 5.0 mis-defines NULL. */
  23. #include <stddef.h>
  24. /* MirBSD defines mbslen as a macro. */
  25. #if @GNULIB_MBSLEN@ && defined __MirBSD__
  26. # include <wchar.h>
  27. #endif
  28. #ifndef __attribute__
  29. /* This feature is available in gcc versions 2.5 and later. */
  30. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
  31. # define __attribute__(Spec) /* empty */
  32. # endif
  33. /* The attribute __pure__ was added in gcc 2.96. */
  34. # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
  35. # define __pure__ /* empty */
  36. # endif
  37. #endif
  38. /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
  39. /* The definition of _GL_ARG_NONNULL is copied here. */
  40. /* The definition of _GL_WARN_ON_USE is copied here. */
  41. /* Return the first instance of C within N bytes of S, or NULL. */
  42. #if @GNULIB_MEMCHR@
  43. # if @REPLACE_MEMCHR@
  44. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  45. # define memchr rpl_memchr
  46. # endif
  47. _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
  48. __attribute__ ((__pure__))
  49. _GL_ARG_NONNULL ((1)));
  50. _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
  51. # else
  52. # if ! @HAVE_MEMCHR@
  53. _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
  54. __attribute__ ((__pure__))
  55. _GL_ARG_NONNULL ((1)));
  56. # endif
  57. /* On some systems, this function is defined as an overloaded function:
  58. extern "C" { const void * std::memchr (const void *, int, size_t); }
  59. extern "C++" { void * std::memchr (void *, int, size_t); } */
  60. _GL_CXXALIAS_SYS_CAST2 (memchr,
  61. void *, (void const *__s, int __c, size_t __n),
  62. void const *, (void const *__s, int __c, size_t __n));
  63. # endif
  64. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  65. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  66. _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
  67. _GL_CXXALIASWARN1 (memchr, void const *,
  68. (void const *__s, int __c, size_t __n));
  69. # else
  70. _GL_CXXALIASWARN (memchr);
  71. # endif
  72. #elif defined GNULIB_POSIXCHECK
  73. # undef memchr
  74. /* Assume memchr is always declared. */
  75. _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
  76. "use gnulib module memchr for portability" );
  77. #endif
  78. /* Return the first occurrence of NEEDLE in HAYSTACK. */
  79. #if @GNULIB_MEMMEM@
  80. # if @REPLACE_MEMMEM@
  81. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  82. # define memmem rpl_memmem
  83. # endif
  84. _GL_FUNCDECL_RPL (memmem, void *,
  85. (void const *__haystack, size_t __haystack_len,
  86. void const *__needle, size_t __needle_len)
  87. __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3)));
  88. _GL_CXXALIAS_RPL (memmem, void *,
  89. (void const *__haystack, size_t __haystack_len,
  90. void const *__needle, size_t __needle_len));
  91. # else
  92. # if ! @HAVE_DECL_MEMMEM@
  93. _GL_FUNCDECL_SYS (memmem, void *,
  94. (void const *__haystack, size_t __haystack_len,
  95. void const *__needle, size_t __needle_len)
  96. __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3)));
  97. # endif
  98. _GL_CXXALIAS_SYS (memmem, void *,
  99. (void const *__haystack, size_t __haystack_len,
  100. void const *__needle, size_t __needle_len));
  101. # endif
  102. _GL_CXXALIASWARN (memmem);
  103. #elif defined GNULIB_POSIXCHECK
  104. # undef memmem
  105. # if HAVE_RAW_DECL_MEMMEM
  106. _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
  107. "use gnulib module memmem-simple for portability, "
  108. "and module memmem for speed" );
  109. # endif
  110. #endif
  111. /* Copy N bytes of SRC to DEST, return pointer to bytes after the
  112. last written byte. */
  113. #if @GNULIB_MEMPCPY@
  114. # if ! @HAVE_MEMPCPY@
  115. _GL_FUNCDECL_SYS (mempcpy, void *,
  116. (void *restrict __dest, void const *restrict __src,
  117. size_t __n)
  118. _GL_ARG_NONNULL ((1, 2)));
  119. # endif
  120. _GL_CXXALIAS_SYS (mempcpy, void *,
  121. (void *restrict __dest, void const *restrict __src,
  122. size_t __n));
  123. _GL_CXXALIASWARN (mempcpy);
  124. #elif defined GNULIB_POSIXCHECK
  125. # undef mempcpy
  126. # if HAVE_RAW_DECL_MEMPCPY
  127. _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
  128. "use gnulib module mempcpy for portability");
  129. # endif
  130. #endif
  131. /* Search backwards through a block for a byte (specified as an int). */
  132. #if @GNULIB_MEMRCHR@
  133. # if ! @HAVE_DECL_MEMRCHR@
  134. _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
  135. __attribute__ ((__pure__))
  136. _GL_ARG_NONNULL ((1)));
  137. # endif
  138. /* On some systems, this function is defined as an overloaded function:
  139. extern "C++" { const void * std::memrchr (const void *, int, size_t); }
  140. extern "C++" { void * std::memrchr (void *, int, size_t); } */
  141. _GL_CXXALIAS_SYS_CAST2 (memrchr,
  142. void *, (void const *, int, size_t),
  143. void const *, (void const *, int, size_t));
  144. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  145. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  146. _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
  147. _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
  148. # else
  149. _GL_CXXALIASWARN (memrchr);
  150. # endif
  151. #elif defined GNULIB_POSIXCHECK
  152. # undef memrchr
  153. # if HAVE_RAW_DECL_MEMRCHR
  154. _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
  155. "use gnulib module memrchr for portability");
  156. # endif
  157. #endif
  158. /* Find the first occurrence of C in S. More efficient than
  159. memchr(S,C,N), at the expense of undefined behavior if C does not
  160. occur within N bytes. */
  161. #if @GNULIB_RAWMEMCHR@
  162. # if ! @HAVE_RAWMEMCHR@
  163. _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
  164. __attribute__ ((__pure__))
  165. _GL_ARG_NONNULL ((1)));
  166. # endif
  167. /* On some systems, this function is defined as an overloaded function:
  168. extern "C++" { const void * std::rawmemchr (const void *, int); }
  169. extern "C++" { void * std::rawmemchr (void *, int); } */
  170. _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
  171. void *, (void const *__s, int __c_in),
  172. void const *, (void const *__s, int __c_in));
  173. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  174. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  175. _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
  176. _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
  177. # else
  178. _GL_CXXALIASWARN (rawmemchr);
  179. # endif
  180. #elif defined GNULIB_POSIXCHECK
  181. # undef rawmemchr
  182. # if HAVE_RAW_DECL_RAWMEMCHR
  183. _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
  184. "use gnulib module rawmemchr for portability");
  185. # endif
  186. #endif
  187. /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
  188. #if @GNULIB_STPCPY@
  189. # if ! @HAVE_STPCPY@
  190. _GL_FUNCDECL_SYS (stpcpy, char *,
  191. (char *restrict __dst, char const *restrict __src)
  192. _GL_ARG_NONNULL ((1, 2)));
  193. # endif
  194. _GL_CXXALIAS_SYS (stpcpy, char *,
  195. (char *restrict __dst, char const *restrict __src));
  196. _GL_CXXALIASWARN (stpcpy);
  197. #elif defined GNULIB_POSIXCHECK
  198. # undef stpcpy
  199. # if HAVE_RAW_DECL_STPCPY
  200. _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
  201. "use gnulib module stpcpy for portability");
  202. # endif
  203. #endif
  204. /* Copy no more than N bytes of SRC to DST, returning a pointer past the
  205. last non-NUL byte written into DST. */
  206. #if @GNULIB_STPNCPY@
  207. # if @REPLACE_STPNCPY@
  208. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  209. # define stpncpy rpl_stpncpy
  210. # endif
  211. _GL_FUNCDECL_RPL (stpncpy, char *,
  212. (char *restrict __dst, char const *restrict __src,
  213. size_t __n)
  214. _GL_ARG_NONNULL ((1, 2)));
  215. _GL_CXXALIAS_RPL (stpncpy, char *,
  216. (char *restrict __dst, char const *restrict __src,
  217. size_t __n));
  218. # else
  219. # if ! @HAVE_STPNCPY@
  220. _GL_FUNCDECL_SYS (stpncpy, char *,
  221. (char *restrict __dst, char const *restrict __src,
  222. size_t __n)
  223. _GL_ARG_NONNULL ((1, 2)));
  224. # endif
  225. _GL_CXXALIAS_SYS (stpncpy, char *,
  226. (char *restrict __dst, char const *restrict __src,
  227. size_t __n));
  228. # endif
  229. _GL_CXXALIASWARN (stpncpy);
  230. #elif defined GNULIB_POSIXCHECK
  231. # undef stpncpy
  232. # if HAVE_RAW_DECL_STPNCPY
  233. _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
  234. "use gnulib module stpncpy for portability");
  235. # endif
  236. #endif
  237. #if defined GNULIB_POSIXCHECK
  238. /* strchr() does not work with multibyte strings if the locale encoding is
  239. GB18030 and the character to be searched is a digit. */
  240. # undef strchr
  241. /* Assume strchr is always declared. */
  242. _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
  243. "in some multibyte locales - "
  244. "use mbschr if you care about internationalization");
  245. #endif
  246. /* Find the first occurrence of C in S or the final NUL byte. */
  247. #if @GNULIB_STRCHRNUL@
  248. # if ! @HAVE_STRCHRNUL@
  249. _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
  250. __attribute__ ((__pure__))
  251. _GL_ARG_NONNULL ((1)));
  252. # endif
  253. /* On some systems, this function is defined as an overloaded function:
  254. extern "C++" { const char * std::strchrnul (const char *, int); }
  255. extern "C++" { char * std::strchrnul (char *, int); } */
  256. _GL_CXXALIAS_SYS_CAST2 (strchrnul,
  257. char *, (char const *__s, int __c_in),
  258. char const *, (char const *__s, int __c_in));
  259. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  260. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  261. _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
  262. _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
  263. # else
  264. _GL_CXXALIASWARN (strchrnul);
  265. # endif
  266. #elif defined GNULIB_POSIXCHECK
  267. # undef strchrnul
  268. # if HAVE_RAW_DECL_STRCHRNUL
  269. _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
  270. "use gnulib module strchrnul for portability");
  271. # endif
  272. #endif
  273. /* Duplicate S, returning an identical malloc'd string. */
  274. #if @GNULIB_STRDUP@
  275. # if @REPLACE_STRDUP@
  276. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  277. # undef strdup
  278. # define strdup rpl_strdup
  279. # endif
  280. _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
  281. _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
  282. # else
  283. # if !(@HAVE_DECL_STRDUP@ || defined strdup)
  284. _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
  285. # endif
  286. _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
  287. # endif
  288. _GL_CXXALIASWARN (strdup);
  289. #elif defined GNULIB_POSIXCHECK
  290. # undef strdup
  291. # if HAVE_RAW_DECL_STRDUP
  292. _GL_WARN_ON_USE (strdup, "strdup is unportable - "
  293. "use gnulib module strdup for portability");
  294. # endif
  295. #endif
  296. /* Append no more than N characters from SRC onto DEST. */
  297. #if @GNULIB_STRNCAT@
  298. # if @REPLACE_STRNCAT@
  299. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  300. # undef strncat
  301. # define strncat rpl_strncat
  302. # endif
  303. _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
  304. _GL_ARG_NONNULL ((1, 2)));
  305. _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
  306. # else
  307. _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
  308. # endif
  309. _GL_CXXALIASWARN (strncat);
  310. #elif defined GNULIB_POSIXCHECK
  311. # undef strncat
  312. # if HAVE_RAW_DECL_STRNCAT
  313. _GL_WARN_ON_USE (strncat, "strncat is unportable - "
  314. "use gnulib module strncat for portability");
  315. # endif
  316. #endif
  317. /* Return a newly allocated copy of at most N bytes of STRING. */
  318. #if @GNULIB_STRNDUP@
  319. # if @REPLACE_STRNDUP@
  320. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  321. # undef strndup
  322. # define strndup rpl_strndup
  323. # endif
  324. _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
  325. _GL_ARG_NONNULL ((1)));
  326. _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
  327. # else
  328. # if ! @HAVE_DECL_STRNDUP@
  329. _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
  330. _GL_ARG_NONNULL ((1)));
  331. # endif
  332. _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
  333. # endif
  334. _GL_CXXALIASWARN (strndup);
  335. #elif defined GNULIB_POSIXCHECK
  336. # undef strndup
  337. # if HAVE_RAW_DECL_STRNDUP
  338. _GL_WARN_ON_USE (strndup, "strndup is unportable - "
  339. "use gnulib module strndup for portability");
  340. # endif
  341. #endif
  342. /* Find the length (number of bytes) of STRING, but scan at most
  343. MAXLEN bytes. If no '\0' terminator is found in that many bytes,
  344. return MAXLEN. */
  345. #if @GNULIB_STRNLEN@
  346. # if ! @HAVE_DECL_STRNLEN@
  347. _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
  348. __attribute__ ((__pure__))
  349. _GL_ARG_NONNULL ((1)));
  350. # endif
  351. _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
  352. _GL_CXXALIASWARN (strnlen);
  353. #elif defined GNULIB_POSIXCHECK
  354. # undef strnlen
  355. # if HAVE_RAW_DECL_STRNLEN
  356. _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
  357. "use gnulib module strnlen for portability");
  358. # endif
  359. #endif
  360. #if defined GNULIB_POSIXCHECK
  361. /* strcspn() assumes the second argument is a list of single-byte characters.
  362. Even in this simple case, it does not work with multibyte strings if the
  363. locale encoding is GB18030 and one of the characters to be searched is a
  364. digit. */
  365. # undef strcspn
  366. /* Assume strcspn is always declared. */
  367. _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
  368. "in multibyte locales - "
  369. "use mbscspn if you care about internationalization");
  370. #endif
  371. /* Find the first occurrence in S of any character in ACCEPT. */
  372. #if @GNULIB_STRPBRK@
  373. # if ! @HAVE_STRPBRK@
  374. _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
  375. __attribute__ ((__pure__))
  376. _GL_ARG_NONNULL ((1, 2)));
  377. # endif
  378. /* On some systems, this function is defined as an overloaded function:
  379. extern "C" { const char * strpbrk (const char *, const char *); }
  380. extern "C++" { char * strpbrk (char *, const char *); } */
  381. _GL_CXXALIAS_SYS_CAST2 (strpbrk,
  382. char *, (char const *__s, char const *__accept),
  383. const char *, (char const *__s, char const *__accept));
  384. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  385. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  386. _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
  387. _GL_CXXALIASWARN1 (strpbrk, char const *,
  388. (char const *__s, char const *__accept));
  389. # else
  390. _GL_CXXALIASWARN (strpbrk);
  391. # endif
  392. # if defined GNULIB_POSIXCHECK
  393. /* strpbrk() assumes the second argument is a list of single-byte characters.
  394. Even in this simple case, it does not work with multibyte strings if the
  395. locale encoding is GB18030 and one of the characters to be searched is a
  396. digit. */
  397. # undef strpbrk
  398. _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
  399. "in multibyte locales - "
  400. "use mbspbrk if you care about internationalization");
  401. # endif
  402. #elif defined GNULIB_POSIXCHECK
  403. # undef strpbrk
  404. # if HAVE_RAW_DECL_STRPBRK
  405. _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
  406. "use gnulib module strpbrk for portability");
  407. # endif
  408. #endif
  409. #if defined GNULIB_POSIXCHECK
  410. /* strspn() assumes the second argument is a list of single-byte characters.
  411. Even in this simple case, it cannot work with multibyte strings. */
  412. # undef strspn
  413. /* Assume strspn is always declared. */
  414. _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
  415. "in multibyte locales - "
  416. "use mbsspn if you care about internationalization");
  417. #endif
  418. #if defined GNULIB_POSIXCHECK
  419. /* strrchr() does not work with multibyte strings if the locale encoding is
  420. GB18030 and the character to be searched is a digit. */
  421. # undef strrchr
  422. /* Assume strrchr is always declared. */
  423. _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
  424. "in some multibyte locales - "
  425. "use mbsrchr if you care about internationalization");
  426. #endif
  427. /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
  428. If one is found, overwrite it with a NUL, and advance *STRINGP
  429. to point to the next char after it. Otherwise, set *STRINGP to NULL.
  430. If *STRINGP was already NULL, nothing happens.
  431. Return the old value of *STRINGP.
  432. This is a variant of strtok() that is multithread-safe and supports
  433. empty fields.
  434. Caveat: It modifies the original string.
  435. Caveat: These functions cannot be used on constant strings.
  436. Caveat: The identity of the delimiting character is lost.
  437. Caveat: It doesn't work with multibyte strings unless all of the delimiter
  438. characters are ASCII characters < 0x30.
  439. See also strtok_r(). */
  440. #if @GNULIB_STRSEP@
  441. # if ! @HAVE_STRSEP@
  442. _GL_FUNCDECL_SYS (strsep, char *,
  443. (char **restrict __stringp, char const *restrict __delim)
  444. _GL_ARG_NONNULL ((1, 2)));
  445. # endif
  446. _GL_CXXALIAS_SYS (strsep, char *,
  447. (char **restrict __stringp, char const *restrict __delim));
  448. _GL_CXXALIASWARN (strsep);
  449. # if defined GNULIB_POSIXCHECK
  450. # undef strsep
  451. _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
  452. "in multibyte locales - "
  453. "use mbssep if you care about internationalization");
  454. # endif
  455. #elif defined GNULIB_POSIXCHECK
  456. # undef strsep
  457. # if HAVE_RAW_DECL_STRSEP
  458. _GL_WARN_ON_USE (strsep, "strsep is unportable - "
  459. "use gnulib module strsep for portability");
  460. # endif
  461. #endif
  462. #if @GNULIB_STRSTR@
  463. # if @REPLACE_STRSTR@
  464. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  465. # define strstr rpl_strstr
  466. # endif
  467. _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
  468. __attribute__ ((__pure__))
  469. _GL_ARG_NONNULL ((1, 2)));
  470. _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
  471. # else
  472. /* On some systems, this function is defined as an overloaded function:
  473. extern "C++" { const char * strstr (const char *, const char *); }
  474. extern "C++" { char * strstr (char *, const char *); } */
  475. _GL_CXXALIAS_SYS_CAST2 (strstr,
  476. char *, (const char *haystack, const char *needle),
  477. const char *, (const char *haystack, const char *needle));
  478. # endif
  479. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  480. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  481. _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
  482. _GL_CXXALIASWARN1 (strstr, const char *,
  483. (const char *haystack, const char *needle));
  484. # else
  485. _GL_CXXALIASWARN (strstr);
  486. # endif
  487. #elif defined GNULIB_POSIXCHECK
  488. /* strstr() does not work with multibyte strings if the locale encoding is
  489. different from UTF-8:
  490. POSIX says that it operates on "strings", and "string" in POSIX is defined
  491. as a sequence of bytes, not of characters. */
  492. # undef strstr
  493. /* Assume strstr is always declared. */
  494. _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
  495. "work correctly on character strings in most "
  496. "multibyte locales - "
  497. "use mbsstr if you care about internationalization, "
  498. "or use strstr if you care about speed");
  499. #endif
  500. /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
  501. comparison. */
  502. #if @GNULIB_STRCASESTR@
  503. # if @REPLACE_STRCASESTR@
  504. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  505. # define strcasestr rpl_strcasestr
  506. # endif
  507. _GL_FUNCDECL_RPL (strcasestr, char *,
  508. (const char *haystack, const char *needle)
  509. __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
  510. _GL_CXXALIAS_RPL (strcasestr, char *,
  511. (const char *haystack, const char *needle));
  512. # else
  513. # if ! @HAVE_STRCASESTR@
  514. _GL_FUNCDECL_SYS (strcasestr, char *,
  515. (const char *haystack, const char *needle)
  516. __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
  517. # endif
  518. /* On some systems, this function is defined as an overloaded function:
  519. extern "C++" { const char * strcasestr (const char *, const char *); }
  520. extern "C++" { char * strcasestr (char *, const char *); } */
  521. _GL_CXXALIAS_SYS_CAST2 (strcasestr,
  522. char *, (const char *haystack, const char *needle),
  523. const char *, (const char *haystack, const char *needle));
  524. # endif
  525. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  526. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  527. _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
  528. _GL_CXXALIASWARN1 (strcasestr, const char *,
  529. (const char *haystack, const char *needle));
  530. # else
  531. _GL_CXXALIASWARN (strcasestr);
  532. # endif
  533. #elif defined GNULIB_POSIXCHECK
  534. /* strcasestr() does not work with multibyte strings:
  535. It is a glibc extension, and glibc implements it only for unibyte
  536. locales. */
  537. # undef strcasestr
  538. # if HAVE_RAW_DECL_STRCASESTR
  539. _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
  540. "strings in multibyte locales - "
  541. "use mbscasestr if you care about "
  542. "internationalization, or use c-strcasestr if you want "
  543. "a locale independent function");
  544. # endif
  545. #endif
  546. /* Parse S into tokens separated by characters in DELIM.
  547. If S is NULL, the saved pointer in SAVE_PTR is used as
  548. the next starting point. For example:
  549. char s[] = "-abc-=-def";
  550. char *sp;
  551. x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
  552. x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  553. x = strtok_r(NULL, "=", &sp); // x = NULL
  554. // s = "abc\0-def\0"
  555. This is a variant of strtok() that is multithread-safe.
  556. For the POSIX documentation for this function, see:
  557. http://www.opengroup.org/susv3xsh/strtok.html
  558. Caveat: It modifies the original string.
  559. Caveat: These functions cannot be used on constant strings.
  560. Caveat: The identity of the delimiting character is lost.
  561. Caveat: It doesn't work with multibyte strings unless all of the delimiter
  562. characters are ASCII characters < 0x30.
  563. See also strsep(). */
  564. #if @GNULIB_STRTOK_R@
  565. # if @REPLACE_STRTOK_R@
  566. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  567. # undef strtok_r
  568. # define strtok_r rpl_strtok_r
  569. # endif
  570. _GL_FUNCDECL_RPL (strtok_r, char *,
  571. (char *restrict s, char const *restrict delim,
  572. char **restrict save_ptr)
  573. _GL_ARG_NONNULL ((2, 3)));
  574. _GL_CXXALIAS_RPL (strtok_r, char *,
  575. (char *restrict s, char const *restrict delim,
  576. char **restrict save_ptr));
  577. # else
  578. # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
  579. # undef strtok_r
  580. # endif
  581. # if ! @HAVE_DECL_STRTOK_R@
  582. _GL_FUNCDECL_SYS (strtok_r, char *,
  583. (char *restrict s, char const *restrict delim,
  584. char **restrict save_ptr)
  585. _GL_ARG_NONNULL ((2, 3)));
  586. # endif
  587. _GL_CXXALIAS_SYS (strtok_r, char *,
  588. (char *restrict s, char const *restrict delim,
  589. char **restrict save_ptr));
  590. # endif
  591. _GL_CXXALIASWARN (strtok_r);
  592. # if defined GNULIB_POSIXCHECK
  593. _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
  594. "strings in multibyte locales - "
  595. "use mbstok_r if you care about internationalization");
  596. # endif
  597. #elif defined GNULIB_POSIXCHECK
  598. # undef strtok_r
  599. # if HAVE_RAW_DECL_STRTOK_R
  600. _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
  601. "use gnulib module strtok_r for portability");
  602. # endif
  603. #endif
  604. /* The following functions are not specified by POSIX. They are gnulib
  605. extensions. */
  606. #if @GNULIB_MBSLEN@
  607. /* Return the number of multibyte characters in the character string STRING.
  608. This considers multibyte characters, unlike strlen, which counts bytes. */
  609. # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
  610. # undef mbslen
  611. # endif
  612. # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
  613. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  614. # define mbslen rpl_mbslen
  615. # endif
  616. _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
  617. _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
  618. # else
  619. _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
  620. _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
  621. # endif
  622. _GL_CXXALIASWARN (mbslen);
  623. #endif
  624. #if @GNULIB_MBSNLEN@
  625. /* Return the number of multibyte characters in the character string starting
  626. at STRING and ending at STRING + LEN. */
  627. _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
  628. _GL_ARG_NONNULL ((1));
  629. #endif
  630. #if @GNULIB_MBSCHR@
  631. /* Locate the first single-byte character C in the character string STRING,
  632. and return a pointer to it. Return NULL if C is not found in STRING.
  633. Unlike strchr(), this function works correctly in multibyte locales with
  634. encodings such as GB18030. */
  635. # if defined __hpux
  636. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  637. # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
  638. # endif
  639. _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
  640. _GL_ARG_NONNULL ((1)));
  641. _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
  642. # else
  643. _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
  644. _GL_ARG_NONNULL ((1)));
  645. _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
  646. # endif
  647. _GL_CXXALIASWARN (mbschr);
  648. #endif
  649. #if @GNULIB_MBSRCHR@
  650. /* Locate the last single-byte character C in the character string STRING,
  651. and return a pointer to it. Return NULL if C is not found in STRING.
  652. Unlike strrchr(), this function works correctly in multibyte locales with
  653. encodings such as GB18030. */
  654. # if defined __hpux
  655. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  656. # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
  657. # endif
  658. _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
  659. _GL_ARG_NONNULL ((1)));
  660. _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
  661. # else
  662. _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
  663. _GL_ARG_NONNULL ((1)));
  664. _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
  665. # endif
  666. _GL_CXXALIASWARN (mbsrchr);
  667. #endif
  668. #if @GNULIB_MBSSTR@
  669. /* Find the first occurrence of the character string NEEDLE in the character
  670. string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
  671. Unlike strstr(), this function works correctly in multibyte locales with
  672. encodings different from UTF-8. */
  673. _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
  674. _GL_ARG_NONNULL ((1, 2));
  675. #endif
  676. #if @GNULIB_MBSCASECMP@
  677. /* Compare the character strings S1 and S2, ignoring case, returning less than,
  678. equal to or greater than zero if S1 is lexicographically less than, equal to
  679. or greater than S2.
  680. Note: This function may, in multibyte locales, return 0 for strings of
  681. different lengths!
  682. Unlike strcasecmp(), this function works correctly in multibyte locales. */
  683. _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
  684. _GL_ARG_NONNULL ((1, 2));
  685. #endif
  686. #if @GNULIB_MBSNCASECMP@
  687. /* Compare the initial segment of the character string S1 consisting of at most
  688. N characters with the initial segment of the character string S2 consisting
  689. of at most N characters, ignoring case, returning less than, equal to or
  690. greater than zero if the initial segment of S1 is lexicographically less
  691. than, equal to or greater than the initial segment of S2.
  692. Note: This function may, in multibyte locales, return 0 for initial segments
  693. of different lengths!
  694. Unlike strncasecmp(), this function works correctly in multibyte locales.
  695. But beware that N is not a byte count but a character count! */
  696. _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
  697. _GL_ARG_NONNULL ((1, 2));
  698. #endif
  699. #if @GNULIB_MBSPCASECMP@
  700. /* Compare the initial segment of the character string STRING consisting of
  701. at most mbslen (PREFIX) characters with the character string PREFIX,
  702. ignoring case, returning less than, equal to or greater than zero if this
  703. initial segment is lexicographically less than, equal to or greater than
  704. PREFIX.
  705. Note: This function may, in multibyte locales, return 0 if STRING is of
  706. smaller length than PREFIX!
  707. Unlike strncasecmp(), this function works correctly in multibyte
  708. locales. */
  709. _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
  710. _GL_ARG_NONNULL ((1, 2));
  711. #endif
  712. #if @GNULIB_MBSCASESTR@
  713. /* Find the first occurrence of the character string NEEDLE in the character
  714. string HAYSTACK, using case-insensitive comparison.
  715. Note: This function may, in multibyte locales, return success even if
  716. strlen (haystack) < strlen (needle) !
  717. Unlike strcasestr(), this function works correctly in multibyte locales. */
  718. _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
  719. _GL_ARG_NONNULL ((1, 2));
  720. #endif
  721. #if @GNULIB_MBSCSPN@
  722. /* Find the first occurrence in the character string STRING of any character
  723. in the character string ACCEPT. Return the number of bytes from the
  724. beginning of the string to this occurrence, or to the end of the string
  725. if none exists.
  726. Unlike strcspn(), this function works correctly in multibyte locales. */
  727. _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
  728. _GL_ARG_NONNULL ((1, 2));
  729. #endif
  730. #if @GNULIB_MBSPBRK@
  731. /* Find the first occurrence in the character string STRING of any character
  732. in the character string ACCEPT. Return the pointer to it, or NULL if none
  733. exists.
  734. Unlike strpbrk(), this function works correctly in multibyte locales. */
  735. # if defined __hpux
  736. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  737. # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
  738. # endif
  739. _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
  740. _GL_ARG_NONNULL ((1, 2)));
  741. _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
  742. # else
  743. _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
  744. _GL_ARG_NONNULL ((1, 2)));
  745. _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
  746. # endif
  747. _GL_CXXALIASWARN (mbspbrk);
  748. #endif
  749. #if @GNULIB_MBSSPN@
  750. /* Find the first occurrence in the character string STRING of any character
  751. not in the character string REJECT. Return the number of bytes from the
  752. beginning of the string to this occurrence, or to the end of the string
  753. if none exists.
  754. Unlike strspn(), this function works correctly in multibyte locales. */
  755. _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
  756. _GL_ARG_NONNULL ((1, 2));
  757. #endif
  758. #if @GNULIB_MBSSEP@
  759. /* Search the next delimiter (multibyte character listed in the character
  760. string DELIM) starting at the character string *STRINGP.
  761. If one is found, overwrite it with a NUL, and advance *STRINGP to point
  762. to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
  763. If *STRINGP was already NULL, nothing happens.
  764. Return the old value of *STRINGP.
  765. This is a variant of mbstok_r() that supports empty fields.
  766. Caveat: It modifies the original string.
  767. Caveat: These functions cannot be used on constant strings.
  768. Caveat: The identity of the delimiting character is lost.
  769. See also mbstok_r(). */
  770. _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
  771. _GL_ARG_NONNULL ((1, 2));
  772. #endif
  773. #if @GNULIB_MBSTOK_R@
  774. /* Parse the character string STRING into tokens separated by characters in
  775. the character string DELIM.
  776. If STRING is NULL, the saved pointer in SAVE_PTR is used as
  777. the next starting point. For example:
  778. char s[] = "-abc-=-def";
  779. char *sp;
  780. x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
  781. x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  782. x = mbstok_r(NULL, "=", &sp); // x = NULL
  783. // s = "abc\0-def\0"
  784. Caveat: It modifies the original string.
  785. Caveat: These functions cannot be used on constant strings.
  786. Caveat: The identity of the delimiting character is lost.
  787. See also mbssep(). */
  788. _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
  789. _GL_ARG_NONNULL ((2, 3));
  790. #endif
  791. /* Map any int, typically from errno, into an error message. */
  792. #if @GNULIB_STRERROR@
  793. # if @REPLACE_STRERROR@
  794. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  795. # undef strerror
  796. # define strerror rpl_strerror
  797. # endif
  798. _GL_FUNCDECL_RPL (strerror, char *, (int));
  799. _GL_CXXALIAS_RPL (strerror, char *, (int));
  800. # else
  801. _GL_CXXALIAS_SYS (strerror, char *, (int));
  802. # endif
  803. _GL_CXXALIASWARN (strerror);
  804. #elif defined GNULIB_POSIXCHECK
  805. # undef strerror
  806. /* Assume strerror is always declared. */
  807. _GL_WARN_ON_USE (strerror, "strerror is unportable - "
  808. "use gnulib module strerror to guarantee non-NULL result");
  809. #endif
  810. #if @GNULIB_STRSIGNAL@
  811. # if @REPLACE_STRSIGNAL@
  812. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  813. # define strsignal rpl_strsignal
  814. # endif
  815. _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
  816. _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
  817. # else
  818. # if ! @HAVE_DECL_STRSIGNAL@
  819. _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
  820. # endif
  821. /* Need to cast, because on Cygwin 1.5.x systems, the return type is
  822. 'const char *'. */
  823. _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
  824. # endif
  825. _GL_CXXALIASWARN (strsignal);
  826. #elif defined GNULIB_POSIXCHECK
  827. # undef strsignal
  828. # if HAVE_RAW_DECL_STRSIGNAL
  829. _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
  830. "use gnulib module strsignal for portability");
  831. # endif
  832. #endif
  833. #if @GNULIB_STRVERSCMP@
  834. # if !@HAVE_STRVERSCMP@
  835. _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
  836. _GL_ARG_NONNULL ((1, 2)));
  837. # endif
  838. _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
  839. _GL_CXXALIASWARN (strverscmp);
  840. #elif defined GNULIB_POSIXCHECK
  841. # undef strverscmp
  842. # if HAVE_RAW_DECL_STRVERSCMP
  843. _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
  844. "use gnulib module strverscmp for portability");
  845. # endif
  846. #endif
  847. #endif /* _GL_STRING_H */
  848. #endif /* _GL_STRING_H */