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