1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2010 Free Software Foundation, Inc.
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 3, or (at your option)
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.
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. */
22 @PRAGMA_SYSTEM_HEADER@
25 /* The include_next requires a split double-inclusion guard. */
26 #@INCLUDE_NEXT@ @NEXT_STRING_H@
31 /* NetBSD 5.0 mis-defines NULL. */
34 /* MirBSD defines mbslen as a macro. */
35 #if @GNULIB_MBSLEN@ && defined __MirBSD__
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 */
45 /* The attribute __pure__ was added in gcc 2.96. */
46 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
47 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
49 # define _GL_ATTRIBUTE_PURE /* empty */
53 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
55 /* The definition of _GL_ARG_NONNULL is copied here. */
57 /* The definition of _GL_WARN_ON_USE is copied here. */
60 /* Return the first instance of C within N bytes of S, or NULL. */
63 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
64 # define memchr rpl_memchr
66 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
68 _GL_ARG_NONNULL ((1)));
69 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
72 _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
74 _GL_ARG_NONNULL ((1)));
76 /* On some systems, this function is defined as an overloaded function:
77 extern "C" { const void * std::memchr (const void *, int, size_t); }
78 extern "C++" { void * std::memchr (void *, int, size_t); } */
79 _GL_CXXALIAS_SYS_CAST2 (memchr,
80 void *, (void const *__s, int __c, size_t __n),
81 void const *, (void const *__s, int __c, size_t __n));
83 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
84 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
85 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
86 _GL_CXXALIASWARN1 (memchr, void const *,
87 (void const *__s, int __c, size_t __n));
89 _GL_CXXALIASWARN (memchr);
91 #elif defined GNULIB_POSIXCHECK
93 /* Assume memchr is always declared. */
94 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
95 "use gnulib module memchr for portability" );
98 /* Return the first occurrence of NEEDLE in HAYSTACK. */
100 # if @REPLACE_MEMMEM@
101 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
102 # define memmem rpl_memmem
104 _GL_FUNCDECL_RPL (memmem, void *,
105 (void const *__haystack, size_t __haystack_len,
106 void const *__needle, size_t __needle_len)
108 _GL_ARG_NONNULL ((1, 3)));
109 _GL_CXXALIAS_RPL (memmem, void *,
110 (void const *__haystack, size_t __haystack_len,
111 void const *__needle, size_t __needle_len));
113 # if ! @HAVE_DECL_MEMMEM@
114 _GL_FUNCDECL_SYS (memmem, void *,
115 (void const *__haystack, size_t __haystack_len,
116 void const *__needle, size_t __needle_len)
118 _GL_ARG_NONNULL ((1, 3)));
120 _GL_CXXALIAS_SYS (memmem, void *,
121 (void const *__haystack, size_t __haystack_len,
122 void const *__needle, size_t __needle_len));
124 _GL_CXXALIASWARN (memmem);
125 #elif defined GNULIB_POSIXCHECK
127 # if HAVE_RAW_DECL_MEMMEM
128 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
129 "use gnulib module memmem-simple for portability, "
130 "and module memmem for speed" );
134 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
135 last written byte. */
137 # if ! @HAVE_MEMPCPY@
138 _GL_FUNCDECL_SYS (mempcpy, void *,
139 (void *restrict __dest, void const *restrict __src,
141 _GL_ARG_NONNULL ((1, 2)));
143 _GL_CXXALIAS_SYS (mempcpy, void *,
144 (void *restrict __dest, void const *restrict __src,
146 _GL_CXXALIASWARN (mempcpy);
147 #elif defined GNULIB_POSIXCHECK
149 # if HAVE_RAW_DECL_MEMPCPY
150 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
151 "use gnulib module mempcpy for portability");
155 /* Search backwards through a block for a byte (specified as an int). */
157 # if ! @HAVE_DECL_MEMRCHR@
158 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
160 _GL_ARG_NONNULL ((1)));
162 /* On some systems, this function is defined as an overloaded function:
163 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
164 extern "C++" { void * std::memrchr (void *, int, size_t); } */
165 _GL_CXXALIAS_SYS_CAST2 (memrchr,
166 void *, (void const *, int, size_t),
167 void const *, (void const *, int, size_t));
168 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
169 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
170 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
171 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
173 _GL_CXXALIASWARN (memrchr);
175 #elif defined GNULIB_POSIXCHECK
177 # if HAVE_RAW_DECL_MEMRCHR
178 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
179 "use gnulib module memrchr for portability");
183 /* Find the first occurrence of C in S. More efficient than
184 memchr(S,C,N), at the expense of undefined behavior if C does not
185 occur within N bytes. */
186 #if @GNULIB_RAWMEMCHR@
187 # if ! @HAVE_RAWMEMCHR@
188 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
190 _GL_ARG_NONNULL ((1)));
192 /* On some systems, this function is defined as an overloaded function:
193 extern "C++" { const void * std::rawmemchr (const void *, int); }
194 extern "C++" { void * std::rawmemchr (void *, int); } */
195 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
196 void *, (void const *__s, int __c_in),
197 void const *, (void const *__s, int __c_in));
198 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
199 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
200 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
201 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
203 _GL_CXXALIASWARN (rawmemchr);
205 #elif defined GNULIB_POSIXCHECK
207 # if HAVE_RAW_DECL_RAWMEMCHR
208 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
209 "use gnulib module rawmemchr for portability");
213 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
216 _GL_FUNCDECL_SYS (stpcpy, char *,
217 (char *restrict __dst, char const *restrict __src)
218 _GL_ARG_NONNULL ((1, 2)));
220 _GL_CXXALIAS_SYS (stpcpy, char *,
221 (char *restrict __dst, char const *restrict __src));
222 _GL_CXXALIASWARN (stpcpy);
223 #elif defined GNULIB_POSIXCHECK
225 # if HAVE_RAW_DECL_STPCPY
226 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
227 "use gnulib module stpcpy for portability");
231 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
232 last non-NUL byte written into DST. */
234 # if @REPLACE_STPNCPY@
235 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
237 # define stpncpy rpl_stpncpy
239 _GL_FUNCDECL_RPL (stpncpy, char *,
240 (char *restrict __dst, char const *restrict __src,
242 _GL_ARG_NONNULL ((1, 2)));
243 _GL_CXXALIAS_RPL (stpncpy, char *,
244 (char *restrict __dst, char const *restrict __src,
247 # if ! @HAVE_STPNCPY@
248 _GL_FUNCDECL_SYS (stpncpy, char *,
249 (char *restrict __dst, char const *restrict __src,
251 _GL_ARG_NONNULL ((1, 2)));
253 _GL_CXXALIAS_SYS (stpncpy, char *,
254 (char *restrict __dst, char const *restrict __src,
257 _GL_CXXALIASWARN (stpncpy);
258 #elif defined GNULIB_POSIXCHECK
260 # if HAVE_RAW_DECL_STPNCPY
261 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
262 "use gnulib module stpncpy for portability");
266 #if defined GNULIB_POSIXCHECK
267 /* strchr() does not work with multibyte strings if the locale encoding is
268 GB18030 and the character to be searched is a digit. */
270 /* Assume strchr is always declared. */
271 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
272 "in some multibyte locales - "
273 "use mbschr if you care about internationalization");
276 /* Find the first occurrence of C in S or the final NUL byte. */
277 #if @GNULIB_STRCHRNUL@
278 # if ! @HAVE_STRCHRNUL@
279 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
281 _GL_ARG_NONNULL ((1)));
283 /* On some systems, this function is defined as an overloaded function:
284 extern "C++" { const char * std::strchrnul (const char *, int); }
285 extern "C++" { char * std::strchrnul (char *, int); } */
286 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
287 char *, (char const *__s, int __c_in),
288 char const *, (char const *__s, int __c_in));
289 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
290 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
291 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
292 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
294 _GL_CXXALIASWARN (strchrnul);
296 #elif defined GNULIB_POSIXCHECK
298 # if HAVE_RAW_DECL_STRCHRNUL
299 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
300 "use gnulib module strchrnul for portability");
304 /* Duplicate S, returning an identical malloc'd string. */
306 # if @REPLACE_STRDUP@
307 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
309 # define strdup rpl_strdup
311 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
312 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
314 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
315 /* strdup exists as a function and as a macro. Get rid of the macro. */
318 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
319 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
321 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
323 _GL_CXXALIASWARN (strdup);
324 #elif defined GNULIB_POSIXCHECK
326 # if HAVE_RAW_DECL_STRDUP
327 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
328 "use gnulib module strdup for portability");
332 /* Append no more than N characters from SRC onto DEST. */
334 # if @REPLACE_STRNCAT@
335 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
337 # define strncat rpl_strncat
339 _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
340 _GL_ARG_NONNULL ((1, 2)));
341 _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
343 _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
345 _GL_CXXALIASWARN (strncat);
346 #elif defined GNULIB_POSIXCHECK
348 # if HAVE_RAW_DECL_STRNCAT
349 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
350 "use gnulib module strncat for portability");
354 /* Return a newly allocated copy of at most N bytes of STRING. */
356 # if @REPLACE_STRNDUP@
357 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
359 # define strndup rpl_strndup
361 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
362 _GL_ARG_NONNULL ((1)));
363 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
365 # if ! @HAVE_DECL_STRNDUP@
366 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
367 _GL_ARG_NONNULL ((1)));
369 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
371 _GL_CXXALIASWARN (strndup);
372 #elif defined GNULIB_POSIXCHECK
374 # if HAVE_RAW_DECL_STRNDUP
375 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
376 "use gnulib module strndup for portability");
380 /* Find the length (number of bytes) of STRING, but scan at most
381 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
384 # if @REPLACE_STRNLEN@
385 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
387 # define strnlen rpl_strnlen
389 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__string, size_t __maxlen)
391 _GL_ARG_NONNULL ((1)));
392 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__string, size_t __maxlen));
394 # if ! @HAVE_DECL_STRNLEN@
395 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
397 _GL_ARG_NONNULL ((1)));
399 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
401 _GL_CXXALIASWARN (strnlen);
402 #elif defined GNULIB_POSIXCHECK
404 # if HAVE_RAW_DECL_STRNLEN
405 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
406 "use gnulib module strnlen for portability");
410 #if defined GNULIB_POSIXCHECK
411 /* strcspn() assumes the second argument is a list of single-byte characters.
412 Even in this simple case, it does not work with multibyte strings if the
413 locale encoding is GB18030 and one of the characters to be searched is a
416 /* Assume strcspn is always declared. */
417 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
418 "in multibyte locales - "
419 "use mbscspn if you care about internationalization");
422 /* Find the first occurrence in S of any character in ACCEPT. */
424 # if ! @HAVE_STRPBRK@
425 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
427 _GL_ARG_NONNULL ((1, 2)));
429 /* On some systems, this function is defined as an overloaded function:
430 extern "C" { const char * strpbrk (const char *, const char *); }
431 extern "C++" { char * strpbrk (char *, const char *); } */
432 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
433 char *, (char const *__s, char const *__accept),
434 const char *, (char const *__s, char const *__accept));
435 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
436 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
437 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
438 _GL_CXXALIASWARN1 (strpbrk, char const *,
439 (char const *__s, char const *__accept));
441 _GL_CXXALIASWARN (strpbrk);
443 # if defined GNULIB_POSIXCHECK
444 /* strpbrk() assumes the second argument is a list of single-byte characters.
445 Even in this simple case, it does not work with multibyte strings if the
446 locale encoding is GB18030 and one of the characters to be searched is a
449 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
450 "in multibyte locales - "
451 "use mbspbrk if you care about internationalization");
453 #elif defined GNULIB_POSIXCHECK
455 # if HAVE_RAW_DECL_STRPBRK
456 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
457 "use gnulib module strpbrk for portability");
461 #if defined GNULIB_POSIXCHECK
462 /* strspn() assumes the second argument is a list of single-byte characters.
463 Even in this simple case, it cannot work with multibyte strings. */
465 /* Assume strspn is always declared. */
466 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
467 "in multibyte locales - "
468 "use mbsspn if you care about internationalization");
471 #if defined GNULIB_POSIXCHECK
472 /* strrchr() does not work with multibyte strings if the locale encoding is
473 GB18030 and the character to be searched is a digit. */
475 /* Assume strrchr is always declared. */
476 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
477 "in some multibyte locales - "
478 "use mbsrchr if you care about internationalization");
481 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
482 If one is found, overwrite it with a NUL, and advance *STRINGP
483 to point to the next char after it. Otherwise, set *STRINGP to NULL.
484 If *STRINGP was already NULL, nothing happens.
485 Return the old value of *STRINGP.
487 This is a variant of strtok() that is multithread-safe and supports
490 Caveat: It modifies the original string.
491 Caveat: These functions cannot be used on constant strings.
492 Caveat: The identity of the delimiting character is lost.
493 Caveat: It doesn't work with multibyte strings unless all of the delimiter
494 characters are ASCII characters < 0x30.
496 See also strtok_r(). */
499 _GL_FUNCDECL_SYS (strsep, char *,
500 (char **restrict __stringp, char const *restrict __delim)
501 _GL_ARG_NONNULL ((1, 2)));
503 _GL_CXXALIAS_SYS (strsep, char *,
504 (char **restrict __stringp, char const *restrict __delim));
505 _GL_CXXALIASWARN (strsep);
506 # if defined GNULIB_POSIXCHECK
508 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
509 "in multibyte locales - "
510 "use mbssep if you care about internationalization");
512 #elif defined GNULIB_POSIXCHECK
514 # if HAVE_RAW_DECL_STRSEP
515 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
516 "use gnulib module strsep for portability");
521 # if @REPLACE_STRSTR@
522 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
523 # define strstr rpl_strstr
525 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
527 _GL_ARG_NONNULL ((1, 2)));
528 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
530 /* On some systems, this function is defined as an overloaded function:
531 extern "C++" { const char * strstr (const char *, const char *); }
532 extern "C++" { char * strstr (char *, const char *); } */
533 _GL_CXXALIAS_SYS_CAST2 (strstr,
534 char *, (const char *haystack, const char *needle),
535 const char *, (const char *haystack, const char *needle));
537 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
538 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
539 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
540 _GL_CXXALIASWARN1 (strstr, const char *,
541 (const char *haystack, const char *needle));
543 _GL_CXXALIASWARN (strstr);
545 #elif defined GNULIB_POSIXCHECK
546 /* strstr() does not work with multibyte strings if the locale encoding is
547 different from UTF-8:
548 POSIX says that it operates on "strings", and "string" in POSIX is defined
549 as a sequence of bytes, not of characters. */
551 /* Assume strstr is always declared. */
552 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
553 "work correctly on character strings in most "
554 "multibyte locales - "
555 "use mbsstr if you care about internationalization, "
556 "or use strstr if you care about speed");
559 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
561 #if @GNULIB_STRCASESTR@
562 # if @REPLACE_STRCASESTR@
563 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
564 # define strcasestr rpl_strcasestr
566 _GL_FUNCDECL_RPL (strcasestr, char *,
567 (const char *haystack, const char *needle)
569 _GL_ARG_NONNULL ((1, 2)));
570 _GL_CXXALIAS_RPL (strcasestr, char *,
571 (const char *haystack, const char *needle));
573 # if ! @HAVE_STRCASESTR@
574 _GL_FUNCDECL_SYS (strcasestr, char *,
575 (const char *haystack, const char *needle)
577 _GL_ARG_NONNULL ((1, 2)));
579 /* On some systems, this function is defined as an overloaded function:
580 extern "C++" { const char * strcasestr (const char *, const char *); }
581 extern "C++" { char * strcasestr (char *, const char *); } */
582 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
583 char *, (const char *haystack, const char *needle),
584 const char *, (const char *haystack, const char *needle));
586 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
587 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
588 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
589 _GL_CXXALIASWARN1 (strcasestr, const char *,
590 (const char *haystack, const char *needle));
592 _GL_CXXALIASWARN (strcasestr);
594 #elif defined GNULIB_POSIXCHECK
595 /* strcasestr() does not work with multibyte strings:
596 It is a glibc extension, and glibc implements it only for unibyte
599 # if HAVE_RAW_DECL_STRCASESTR
600 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
601 "strings in multibyte locales - "
602 "use mbscasestr if you care about "
603 "internationalization, or use c-strcasestr if you want "
604 "a locale independent function");
608 /* Parse S into tokens separated by characters in DELIM.
609 If S is NULL, the saved pointer in SAVE_PTR is used as
610 the next starting point. For example:
611 char s[] = "-abc-=-def";
613 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
614 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
615 x = strtok_r(NULL, "=", &sp); // x = NULL
618 This is a variant of strtok() that is multithread-safe.
620 For the POSIX documentation for this function, see:
621 http://www.opengroup.org/susv3xsh/strtok.html
623 Caveat: It modifies the original string.
624 Caveat: These functions cannot be used on constant strings.
625 Caveat: The identity of the delimiting character is lost.
626 Caveat: It doesn't work with multibyte strings unless all of the delimiter
627 characters are ASCII characters < 0x30.
629 See also strsep(). */
630 #if @GNULIB_STRTOK_R@
631 # if @REPLACE_STRTOK_R@
632 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
634 # define strtok_r rpl_strtok_r
636 _GL_FUNCDECL_RPL (strtok_r, char *,
637 (char *restrict s, char const *restrict delim,
638 char **restrict save_ptr)
639 _GL_ARG_NONNULL ((2, 3)));
640 _GL_CXXALIAS_RPL (strtok_r, char *,
641 (char *restrict s, char const *restrict delim,
642 char **restrict save_ptr));
644 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
647 # if ! @HAVE_DECL_STRTOK_R@
648 _GL_FUNCDECL_SYS (strtok_r, char *,
649 (char *restrict s, char const *restrict delim,
650 char **restrict save_ptr)
651 _GL_ARG_NONNULL ((2, 3)));
653 _GL_CXXALIAS_SYS (strtok_r, char *,
654 (char *restrict s, char const *restrict delim,
655 char **restrict save_ptr));
657 _GL_CXXALIASWARN (strtok_r);
658 # if defined GNULIB_POSIXCHECK
659 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
660 "strings in multibyte locales - "
661 "use mbstok_r if you care about internationalization");
663 #elif defined GNULIB_POSIXCHECK
665 # if HAVE_RAW_DECL_STRTOK_R
666 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
667 "use gnulib module strtok_r for portability");
672 /* The following functions are not specified by POSIX. They are gnulib
676 /* Return the number of multibyte characters in the character string STRING.
677 This considers multibyte characters, unlike strlen, which counts bytes. */
678 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
681 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
682 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
683 # define mbslen rpl_mbslen
685 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
686 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
688 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
689 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
691 _GL_CXXALIASWARN (mbslen);
695 /* Return the number of multibyte characters in the character string starting
696 at STRING and ending at STRING + LEN. */
697 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
698 _GL_ARG_NONNULL ((1));
702 /* Locate the first single-byte character C in the character string STRING,
703 and return a pointer to it. Return NULL if C is not found in STRING.
704 Unlike strchr(), this function works correctly in multibyte locales with
705 encodings such as GB18030. */
707 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
708 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
710 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
711 _GL_ARG_NONNULL ((1)));
712 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
714 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
715 _GL_ARG_NONNULL ((1)));
716 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
718 _GL_CXXALIASWARN (mbschr);
722 /* Locate the last single-byte character C in the character string STRING,
723 and return a pointer to it. Return NULL if C is not found in STRING.
724 Unlike strrchr(), this function works correctly in multibyte locales with
725 encodings such as GB18030. */
727 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
728 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
730 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
731 _GL_ARG_NONNULL ((1)));
732 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
734 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
735 _GL_ARG_NONNULL ((1)));
736 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
738 _GL_CXXALIASWARN (mbsrchr);
742 /* Find the first occurrence of the character string NEEDLE in the character
743 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
744 Unlike strstr(), this function works correctly in multibyte locales with
745 encodings different from UTF-8. */
746 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
747 _GL_ARG_NONNULL ((1, 2));
750 #if @GNULIB_MBSCASECMP@
751 /* Compare the character strings S1 and S2, ignoring case, returning less than,
752 equal to or greater than zero if S1 is lexicographically less than, equal to
754 Note: This function may, in multibyte locales, return 0 for strings of
756 Unlike strcasecmp(), this function works correctly in multibyte locales. */
757 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
758 _GL_ARG_NONNULL ((1, 2));
761 #if @GNULIB_MBSNCASECMP@
762 /* Compare the initial segment of the character string S1 consisting of at most
763 N characters with the initial segment of the character string S2 consisting
764 of at most N characters, ignoring case, returning less than, equal to or
765 greater than zero if the initial segment of S1 is lexicographically less
766 than, equal to or greater than the initial segment of S2.
767 Note: This function may, in multibyte locales, return 0 for initial segments
768 of different lengths!
769 Unlike strncasecmp(), this function works correctly in multibyte locales.
770 But beware that N is not a byte count but a character count! */
771 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
772 _GL_ARG_NONNULL ((1, 2));
775 #if @GNULIB_MBSPCASECMP@
776 /* Compare the initial segment of the character string STRING consisting of
777 at most mbslen (PREFIX) characters with the character string PREFIX,
778 ignoring case. If the two match, return a pointer to the first byte
779 after this prefix in STRING. Otherwise, return NULL.
780 Note: This function may, in multibyte locales, return non-NULL if STRING
781 is of smaller length than PREFIX!
782 Unlike strncasecmp(), this function works correctly in multibyte
784 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
785 _GL_ARG_NONNULL ((1, 2));
788 #if @GNULIB_MBSCASESTR@
789 /* Find the first occurrence of the character string NEEDLE in the character
790 string HAYSTACK, using case-insensitive comparison.
791 Note: This function may, in multibyte locales, return success even if
792 strlen (haystack) < strlen (needle) !
793 Unlike strcasestr(), this function works correctly in multibyte locales. */
794 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
795 _GL_ARG_NONNULL ((1, 2));
799 /* Find the first occurrence in the character string STRING of any character
800 in the character string ACCEPT. Return the number of bytes from the
801 beginning of the string to this occurrence, or to the end of the string
803 Unlike strcspn(), this function works correctly in multibyte locales. */
804 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
805 _GL_ARG_NONNULL ((1, 2));
809 /* Find the first occurrence in the character string STRING of any character
810 in the character string ACCEPT. Return the pointer to it, or NULL if none
812 Unlike strpbrk(), this function works correctly in multibyte locales. */
814 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
815 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
817 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
818 _GL_ARG_NONNULL ((1, 2)));
819 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
821 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
822 _GL_ARG_NONNULL ((1, 2)));
823 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
825 _GL_CXXALIASWARN (mbspbrk);
829 /* Find the first occurrence in the character string STRING of any character
830 not in the character string REJECT. Return the number of bytes from the
831 beginning of the string to this occurrence, or to the end of the string
833 Unlike strspn(), this function works correctly in multibyte locales. */
834 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
835 _GL_ARG_NONNULL ((1, 2));
839 /* Search the next delimiter (multibyte character listed in the character
840 string DELIM) starting at the character string *STRINGP.
841 If one is found, overwrite it with a NUL, and advance *STRINGP to point
842 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
843 If *STRINGP was already NULL, nothing happens.
844 Return the old value of *STRINGP.
846 This is a variant of mbstok_r() that supports empty fields.
848 Caveat: It modifies the original string.
849 Caveat: These functions cannot be used on constant strings.
850 Caveat: The identity of the delimiting character is lost.
852 See also mbstok_r(). */
853 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
854 _GL_ARG_NONNULL ((1, 2));
857 #if @GNULIB_MBSTOK_R@
858 /* Parse the character string STRING into tokens separated by characters in
859 the character string DELIM.
860 If STRING is NULL, the saved pointer in SAVE_PTR is used as
861 the next starting point. For example:
862 char s[] = "-abc-=-def";
864 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
865 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
866 x = mbstok_r(NULL, "=", &sp); // x = NULL
869 Caveat: It modifies the original string.
870 Caveat: These functions cannot be used on constant strings.
871 Caveat: The identity of the delimiting character is lost.
873 See also mbssep(). */
874 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
875 _GL_ARG_NONNULL ((2, 3));
878 /* Map any int, typically from errno, into an error message. */
879 #if @GNULIB_STRERROR@
880 # if @REPLACE_STRERROR@
881 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
883 # define strerror rpl_strerror
885 _GL_FUNCDECL_RPL (strerror, char *, (int));
886 _GL_CXXALIAS_RPL (strerror, char *, (int));
888 _GL_CXXALIAS_SYS (strerror, char *, (int));
890 _GL_CXXALIASWARN (strerror);
891 #elif defined GNULIB_POSIXCHECK
893 /* Assume strerror is always declared. */
894 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
895 "use gnulib module strerror to guarantee non-NULL result");
898 #if @GNULIB_STRSIGNAL@
899 # if @REPLACE_STRSIGNAL@
900 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
901 # define strsignal rpl_strsignal
903 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
904 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
906 # if ! @HAVE_DECL_STRSIGNAL@
907 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
909 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
911 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
913 _GL_CXXALIASWARN (strsignal);
914 #elif defined GNULIB_POSIXCHECK
916 # if HAVE_RAW_DECL_STRSIGNAL
917 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
918 "use gnulib module strsignal for portability");
922 #if @GNULIB_STRVERSCMP@
923 # if !@HAVE_STRVERSCMP@
924 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
925 _GL_ARG_NONNULL ((1, 2)));
927 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
928 _GL_CXXALIASWARN (strverscmp);
929 #elif defined GNULIB_POSIXCHECK
931 # if HAVE_RAW_DECL_STRVERSCMP
932 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
933 "use gnulib module strverscmp for portability");
938 #endif /* _GL_STRING_H */
939 #endif /* _GL_STRING_H */