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