string.in.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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 @REPLACE_STRNLEN@
  347. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  348. # undef strnlen
  349. # define strnlen rpl_strnlen
  350. # endif
  351. _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__string, size_t __maxlen)
  352. __attribute__ ((__pure__))
  353. _GL_ARG_NONNULL ((1)));
  354. _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__string, size_t __maxlen));
  355. # else
  356. # if ! @HAVE_DECL_STRNLEN@
  357. _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
  358. __attribute__ ((__pure__))
  359. _GL_ARG_NONNULL ((1)));
  360. # endif
  361. _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
  362. # endif
  363. _GL_CXXALIASWARN (strnlen);
  364. #elif defined GNULIB_POSIXCHECK
  365. # undef strnlen
  366. # if HAVE_RAW_DECL_STRNLEN
  367. _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
  368. "use gnulib module strnlen for portability");
  369. # endif
  370. #endif
  371. #if defined GNULIB_POSIXCHECK
  372. /* strcspn() assumes the second argument is a list of single-byte characters.
  373. Even in this simple case, it does not work with multibyte strings if the
  374. locale encoding is GB18030 and one of the characters to be searched is a
  375. digit. */
  376. # undef strcspn
  377. /* Assume strcspn is always declared. */
  378. _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
  379. "in multibyte locales - "
  380. "use mbscspn if you care about internationalization");
  381. #endif
  382. /* Find the first occurrence in S of any character in ACCEPT. */
  383. #if @GNULIB_STRPBRK@
  384. # if ! @HAVE_STRPBRK@
  385. _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
  386. __attribute__ ((__pure__))
  387. _GL_ARG_NONNULL ((1, 2)));
  388. # endif
  389. /* On some systems, this function is defined as an overloaded function:
  390. extern "C" { const char * strpbrk (const char *, const char *); }
  391. extern "C++" { char * strpbrk (char *, const char *); } */
  392. _GL_CXXALIAS_SYS_CAST2 (strpbrk,
  393. char *, (char const *__s, char const *__accept),
  394. const char *, (char const *__s, char const *__accept));
  395. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  396. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  397. _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
  398. _GL_CXXALIASWARN1 (strpbrk, char const *,
  399. (char const *__s, char const *__accept));
  400. # else
  401. _GL_CXXALIASWARN (strpbrk);
  402. # endif
  403. # if defined GNULIB_POSIXCHECK
  404. /* strpbrk() assumes the second argument is a list of single-byte characters.
  405. Even in this simple case, it does not work with multibyte strings if the
  406. locale encoding is GB18030 and one of the characters to be searched is a
  407. digit. */
  408. # undef strpbrk
  409. _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
  410. "in multibyte locales - "
  411. "use mbspbrk if you care about internationalization");
  412. # endif
  413. #elif defined GNULIB_POSIXCHECK
  414. # undef strpbrk
  415. # if HAVE_RAW_DECL_STRPBRK
  416. _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
  417. "use gnulib module strpbrk for portability");
  418. # endif
  419. #endif
  420. #if defined GNULIB_POSIXCHECK
  421. /* strspn() assumes the second argument is a list of single-byte characters.
  422. Even in this simple case, it cannot work with multibyte strings. */
  423. # undef strspn
  424. /* Assume strspn is always declared. */
  425. _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
  426. "in multibyte locales - "
  427. "use mbsspn if you care about internationalization");
  428. #endif
  429. #if defined GNULIB_POSIXCHECK
  430. /* strrchr() does not work with multibyte strings if the locale encoding is
  431. GB18030 and the character to be searched is a digit. */
  432. # undef strrchr
  433. /* Assume strrchr is always declared. */
  434. _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
  435. "in some multibyte locales - "
  436. "use mbsrchr if you care about internationalization");
  437. #endif
  438. /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
  439. If one is found, overwrite it with a NUL, and advance *STRINGP
  440. to point to the next char after it. Otherwise, set *STRINGP to NULL.
  441. If *STRINGP was already NULL, nothing happens.
  442. Return the old value of *STRINGP.
  443. This is a variant of strtok() that is multithread-safe and supports
  444. empty fields.
  445. Caveat: It modifies the original string.
  446. Caveat: These functions cannot be used on constant strings.
  447. Caveat: The identity of the delimiting character is lost.
  448. Caveat: It doesn't work with multibyte strings unless all of the delimiter
  449. characters are ASCII characters < 0x30.
  450. See also strtok_r(). */
  451. #if @GNULIB_STRSEP@
  452. # if ! @HAVE_STRSEP@
  453. _GL_FUNCDECL_SYS (strsep, char *,
  454. (char **restrict __stringp, char const *restrict __delim)
  455. _GL_ARG_NONNULL ((1, 2)));
  456. # endif
  457. _GL_CXXALIAS_SYS (strsep, char *,
  458. (char **restrict __stringp, char const *restrict __delim));
  459. _GL_CXXALIASWARN (strsep);
  460. # if defined GNULIB_POSIXCHECK
  461. # undef strsep
  462. _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
  463. "in multibyte locales - "
  464. "use mbssep if you care about internationalization");
  465. # endif
  466. #elif defined GNULIB_POSIXCHECK
  467. # undef strsep
  468. # if HAVE_RAW_DECL_STRSEP
  469. _GL_WARN_ON_USE (strsep, "strsep is unportable - "
  470. "use gnulib module strsep for portability");
  471. # endif
  472. #endif
  473. #if @GNULIB_STRSTR@
  474. # if @REPLACE_STRSTR@
  475. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  476. # define strstr rpl_strstr
  477. # endif
  478. _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
  479. __attribute__ ((__pure__))
  480. _GL_ARG_NONNULL ((1, 2)));
  481. _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
  482. # else
  483. /* On some systems, this function is defined as an overloaded function:
  484. extern "C++" { const char * strstr (const char *, const char *); }
  485. extern "C++" { char * strstr (char *, const char *); } */
  486. _GL_CXXALIAS_SYS_CAST2 (strstr,
  487. char *, (const char *haystack, const char *needle),
  488. const char *, (const char *haystack, const char *needle));
  489. # endif
  490. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  491. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  492. _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
  493. _GL_CXXALIASWARN1 (strstr, const char *,
  494. (const char *haystack, const char *needle));
  495. # else
  496. _GL_CXXALIASWARN (strstr);
  497. # endif
  498. #elif defined GNULIB_POSIXCHECK
  499. /* strstr() does not work with multibyte strings if the locale encoding is
  500. different from UTF-8:
  501. POSIX says that it operates on "strings", and "string" in POSIX is defined
  502. as a sequence of bytes, not of characters. */
  503. # undef strstr
  504. /* Assume strstr is always declared. */
  505. _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
  506. "work correctly on character strings in most "
  507. "multibyte locales - "
  508. "use mbsstr if you care about internationalization, "
  509. "or use strstr if you care about speed");
  510. #endif
  511. /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
  512. comparison. */
  513. #if @GNULIB_STRCASESTR@
  514. # if @REPLACE_STRCASESTR@
  515. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  516. # define strcasestr rpl_strcasestr
  517. # endif
  518. _GL_FUNCDECL_RPL (strcasestr, char *,
  519. (const char *haystack, const char *needle)
  520. __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
  521. _GL_CXXALIAS_RPL (strcasestr, char *,
  522. (const char *haystack, const char *needle));
  523. # else
  524. # if ! @HAVE_STRCASESTR@
  525. _GL_FUNCDECL_SYS (strcasestr, char *,
  526. (const char *haystack, const char *needle)
  527. __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
  528. # endif
  529. /* On some systems, this function is defined as an overloaded function:
  530. extern "C++" { const char * strcasestr (const char *, const char *); }
  531. extern "C++" { char * strcasestr (char *, const char *); } */
  532. _GL_CXXALIAS_SYS_CAST2 (strcasestr,
  533. char *, (const char *haystack, const char *needle),
  534. const char *, (const char *haystack, const char *needle));
  535. # endif
  536. # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
  537. && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  538. _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
  539. _GL_CXXALIASWARN1 (strcasestr, const char *,
  540. (const char *haystack, const char *needle));
  541. # else
  542. _GL_CXXALIASWARN (strcasestr);
  543. # endif
  544. #elif defined GNULIB_POSIXCHECK
  545. /* strcasestr() does not work with multibyte strings:
  546. It is a glibc extension, and glibc implements it only for unibyte
  547. locales. */
  548. # undef strcasestr
  549. # if HAVE_RAW_DECL_STRCASESTR
  550. _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
  551. "strings in multibyte locales - "
  552. "use mbscasestr if you care about "
  553. "internationalization, or use c-strcasestr if you want "
  554. "a locale independent function");
  555. # endif
  556. #endif
  557. /* Parse S into tokens separated by characters in DELIM.
  558. If S is NULL, the saved pointer in SAVE_PTR is used as
  559. the next starting point. For example:
  560. char s[] = "-abc-=-def";
  561. char *sp;
  562. x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
  563. x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  564. x = strtok_r(NULL, "=", &sp); // x = NULL
  565. // s = "abc\0-def\0"
  566. This is a variant of strtok() that is multithread-safe.
  567. For the POSIX documentation for this function, see:
  568. http://www.opengroup.org/susv3xsh/strtok.html
  569. Caveat: It modifies the original string.
  570. Caveat: These functions cannot be used on constant strings.
  571. Caveat: The identity of the delimiting character is lost.
  572. Caveat: It doesn't work with multibyte strings unless all of the delimiter
  573. characters are ASCII characters < 0x30.
  574. See also strsep(). */
  575. #if @GNULIB_STRTOK_R@
  576. # if @REPLACE_STRTOK_R@
  577. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  578. # undef strtok_r
  579. # define strtok_r rpl_strtok_r
  580. # endif
  581. _GL_FUNCDECL_RPL (strtok_r, char *,
  582. (char *restrict s, char const *restrict delim,
  583. char **restrict save_ptr)
  584. _GL_ARG_NONNULL ((2, 3)));
  585. _GL_CXXALIAS_RPL (strtok_r, char *,
  586. (char *restrict s, char const *restrict delim,
  587. char **restrict save_ptr));
  588. # else
  589. # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
  590. # undef strtok_r
  591. # endif
  592. # if ! @HAVE_DECL_STRTOK_R@
  593. _GL_FUNCDECL_SYS (strtok_r, char *,
  594. (char *restrict s, char const *restrict delim,
  595. char **restrict save_ptr)
  596. _GL_ARG_NONNULL ((2, 3)));
  597. # endif
  598. _GL_CXXALIAS_SYS (strtok_r, char *,
  599. (char *restrict s, char const *restrict delim,
  600. char **restrict save_ptr));
  601. # endif
  602. _GL_CXXALIASWARN (strtok_r);
  603. # if defined GNULIB_POSIXCHECK
  604. _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
  605. "strings in multibyte locales - "
  606. "use mbstok_r if you care about internationalization");
  607. # endif
  608. #elif defined GNULIB_POSIXCHECK
  609. # undef strtok_r
  610. # if HAVE_RAW_DECL_STRTOK_R
  611. _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
  612. "use gnulib module strtok_r for portability");
  613. # endif
  614. #endif
  615. /* The following functions are not specified by POSIX. They are gnulib
  616. extensions. */
  617. #if @GNULIB_MBSLEN@
  618. /* Return the number of multibyte characters in the character string STRING.
  619. This considers multibyte characters, unlike strlen, which counts bytes. */
  620. # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
  621. # undef mbslen
  622. # endif
  623. # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
  624. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  625. # define mbslen rpl_mbslen
  626. # endif
  627. _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
  628. _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
  629. # else
  630. _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
  631. _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
  632. # endif
  633. _GL_CXXALIASWARN (mbslen);
  634. #endif
  635. #if @GNULIB_MBSNLEN@
  636. /* Return the number of multibyte characters in the character string starting
  637. at STRING and ending at STRING + LEN. */
  638. _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
  639. _GL_ARG_NONNULL ((1));
  640. #endif
  641. #if @GNULIB_MBSCHR@
  642. /* Locate the first single-byte character C in the character string STRING,
  643. and return a pointer to it. Return NULL if C is not found in STRING.
  644. Unlike strchr(), this function works correctly in multibyte locales with
  645. encodings such as GB18030. */
  646. # if defined __hpux
  647. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  648. # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
  649. # endif
  650. _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
  651. _GL_ARG_NONNULL ((1)));
  652. _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
  653. # else
  654. _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
  655. _GL_ARG_NONNULL ((1)));
  656. _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
  657. # endif
  658. _GL_CXXALIASWARN (mbschr);
  659. #endif
  660. #if @GNULIB_MBSRCHR@
  661. /* Locate the last single-byte character C in the character string STRING,
  662. and return a pointer to it. Return NULL if C is not found in STRING.
  663. Unlike strrchr(), this function works correctly in multibyte locales with
  664. encodings such as GB18030. */
  665. # if defined __hpux
  666. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  667. # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
  668. # endif
  669. _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
  670. _GL_ARG_NONNULL ((1)));
  671. _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
  672. # else
  673. _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
  674. _GL_ARG_NONNULL ((1)));
  675. _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
  676. # endif
  677. _GL_CXXALIASWARN (mbsrchr);
  678. #endif
  679. #if @GNULIB_MBSSTR@
  680. /* Find the first occurrence of the character string NEEDLE in the character
  681. string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
  682. Unlike strstr(), this function works correctly in multibyte locales with
  683. encodings different from UTF-8. */
  684. _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
  685. _GL_ARG_NONNULL ((1, 2));
  686. #endif
  687. #if @GNULIB_MBSCASECMP@
  688. /* Compare the character strings S1 and S2, ignoring case, returning less than,
  689. equal to or greater than zero if S1 is lexicographically less than, equal to
  690. or greater than S2.
  691. Note: This function may, in multibyte locales, return 0 for strings of
  692. different lengths!
  693. Unlike strcasecmp(), this function works correctly in multibyte locales. */
  694. _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
  695. _GL_ARG_NONNULL ((1, 2));
  696. #endif
  697. #if @GNULIB_MBSNCASECMP@
  698. /* Compare the initial segment of the character string S1 consisting of at most
  699. N characters with the initial segment of the character string S2 consisting
  700. of at most N characters, ignoring case, returning less than, equal to or
  701. greater than zero if the initial segment of S1 is lexicographically less
  702. than, equal to or greater than the initial segment of S2.
  703. Note: This function may, in multibyte locales, return 0 for initial segments
  704. of different lengths!
  705. Unlike strncasecmp(), this function works correctly in multibyte locales.
  706. But beware that N is not a byte count but a character count! */
  707. _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
  708. _GL_ARG_NONNULL ((1, 2));
  709. #endif
  710. #if @GNULIB_MBSPCASECMP@
  711. /* Compare the initial segment of the character string STRING consisting of
  712. at most mbslen (PREFIX) characters with the character string PREFIX,
  713. ignoring case, returning less than, equal to or greater than zero if this
  714. initial segment is lexicographically less than, equal to or greater than
  715. PREFIX.
  716. Note: This function may, in multibyte locales, return 0 if STRING is of
  717. smaller length than PREFIX!
  718. Unlike strncasecmp(), this function works correctly in multibyte
  719. locales. */
  720. _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
  721. _GL_ARG_NONNULL ((1, 2));
  722. #endif
  723. #if @GNULIB_MBSCASESTR@
  724. /* Find the first occurrence of the character string NEEDLE in the character
  725. string HAYSTACK, using case-insensitive comparison.
  726. Note: This function may, in multibyte locales, return success even if
  727. strlen (haystack) < strlen (needle) !
  728. Unlike strcasestr(), this function works correctly in multibyte locales. */
  729. _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
  730. _GL_ARG_NONNULL ((1, 2));
  731. #endif
  732. #if @GNULIB_MBSCSPN@
  733. /* Find the first occurrence in the character string STRING of any character
  734. in the character string ACCEPT. Return the number of bytes from the
  735. beginning of the string to this occurrence, or to the end of the string
  736. if none exists.
  737. Unlike strcspn(), this function works correctly in multibyte locales. */
  738. _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
  739. _GL_ARG_NONNULL ((1, 2));
  740. #endif
  741. #if @GNULIB_MBSPBRK@
  742. /* Find the first occurrence in the character string STRING of any character
  743. in the character string ACCEPT. Return the pointer to it, or NULL if none
  744. exists.
  745. Unlike strpbrk(), this function works correctly in multibyte locales. */
  746. # if defined __hpux
  747. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  748. # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
  749. # endif
  750. _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
  751. _GL_ARG_NONNULL ((1, 2)));
  752. _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
  753. # else
  754. _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
  755. _GL_ARG_NONNULL ((1, 2)));
  756. _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
  757. # endif
  758. _GL_CXXALIASWARN (mbspbrk);
  759. #endif
  760. #if @GNULIB_MBSSPN@
  761. /* Find the first occurrence in the character string STRING of any character
  762. not in the character string REJECT. Return the number of bytes from the
  763. beginning of the string to this occurrence, or to the end of the string
  764. if none exists.
  765. Unlike strspn(), this function works correctly in multibyte locales. */
  766. _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
  767. _GL_ARG_NONNULL ((1, 2));
  768. #endif
  769. #if @GNULIB_MBSSEP@
  770. /* Search the next delimiter (multibyte character listed in the character
  771. string DELIM) starting at the character string *STRINGP.
  772. If one is found, overwrite it with a NUL, and advance *STRINGP to point
  773. to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
  774. If *STRINGP was already NULL, nothing happens.
  775. Return the old value of *STRINGP.
  776. This is a variant of mbstok_r() that supports empty fields.
  777. Caveat: It modifies the original string.
  778. Caveat: These functions cannot be used on constant strings.
  779. Caveat: The identity of the delimiting character is lost.
  780. See also mbstok_r(). */
  781. _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
  782. _GL_ARG_NONNULL ((1, 2));
  783. #endif
  784. #if @GNULIB_MBSTOK_R@
  785. /* Parse the character string STRING into tokens separated by characters in
  786. the character string DELIM.
  787. If STRING is NULL, the saved pointer in SAVE_PTR is used as
  788. the next starting point. For example:
  789. char s[] = "-abc-=-def";
  790. char *sp;
  791. x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
  792. x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
  793. x = mbstok_r(NULL, "=", &sp); // x = NULL
  794. // s = "abc\0-def\0"
  795. Caveat: It modifies the original string.
  796. Caveat: These functions cannot be used on constant strings.
  797. Caveat: The identity of the delimiting character is lost.
  798. See also mbssep(). */
  799. _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
  800. _GL_ARG_NONNULL ((2, 3));
  801. #endif
  802. /* Map any int, typically from errno, into an error message. */
  803. #if @GNULIB_STRERROR@
  804. # if @REPLACE_STRERROR@
  805. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  806. # undef strerror
  807. # define strerror rpl_strerror
  808. # endif
  809. _GL_FUNCDECL_RPL (strerror, char *, (int));
  810. _GL_CXXALIAS_RPL (strerror, char *, (int));
  811. # else
  812. _GL_CXXALIAS_SYS (strerror, char *, (int));
  813. # endif
  814. _GL_CXXALIASWARN (strerror);
  815. #elif defined GNULIB_POSIXCHECK
  816. # undef strerror
  817. /* Assume strerror is always declared. */
  818. _GL_WARN_ON_USE (strerror, "strerror is unportable - "
  819. "use gnulib module strerror to guarantee non-NULL result");
  820. #endif
  821. #if @GNULIB_STRSIGNAL@
  822. # if @REPLACE_STRSIGNAL@
  823. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  824. # define strsignal rpl_strsignal
  825. # endif
  826. _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
  827. _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
  828. # else
  829. # if ! @HAVE_DECL_STRSIGNAL@
  830. _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
  831. # endif
  832. /* Need to cast, because on Cygwin 1.5.x systems, the return type is
  833. 'const char *'. */
  834. _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
  835. # endif
  836. _GL_CXXALIASWARN (strsignal);
  837. #elif defined GNULIB_POSIXCHECK
  838. # undef strsignal
  839. # if HAVE_RAW_DECL_STRSIGNAL
  840. _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
  841. "use gnulib module strsignal for portability");
  842. # endif
  843. #endif
  844. #if @GNULIB_STRVERSCMP@
  845. # if !@HAVE_STRVERSCMP@
  846. _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
  847. _GL_ARG_NONNULL ((1, 2)));
  848. # endif
  849. _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
  850. _GL_CXXALIASWARN (strverscmp);
  851. #elif defined GNULIB_POSIXCHECK
  852. # undef strverscmp
  853. # if HAVE_RAW_DECL_STRVERSCMP
  854. _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
  855. "use gnulib module strverscmp for portability");
  856. # endif
  857. #endif
  858. #endif /* _GL_STRING_H */
  859. #endif /* _GL_STRING_H */