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 2, 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 */
52 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
53 /* But avoid namespace pollution on glibc systems. */
54 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) \
55 && ! defined __GLIBC__
59 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
61 /* The definition of _GL_ARG_NONNULL is copied here. */
63 /* The definition of _GL_WARN_ON_USE is copied here. */
66 /* Return the first instance of C within N bytes of S, or NULL. */
69 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
70 # define memchr rpl_memchr
72 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
74 _GL_ARG_NONNULL ((1)));
75 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
78 _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
80 _GL_ARG_NONNULL ((1)));
82 /* On some systems, this function is defined as an overloaded function:
83 extern "C" { const void * std::memchr (const void *, int, size_t); }
84 extern "C++" { void * std::memchr (void *, int, size_t); } */
85 _GL_CXXALIAS_SYS_CAST2 (memchr,
86 void *, (void const *__s, int __c, size_t __n),
87 void const *, (void const *__s, int __c, size_t __n));
89 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
90 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
91 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
92 _GL_CXXALIASWARN1 (memchr, void const *,
93 (void const *__s, int __c, size_t __n));
95 _GL_CXXALIASWARN (memchr);
97 #elif defined GNULIB_POSIXCHECK
99 /* Assume memchr is always declared. */
100 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
101 "use gnulib module memchr for portability" );
104 /* Return the first occurrence of NEEDLE in HAYSTACK. */
106 # if @REPLACE_MEMMEM@
107 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
108 # define memmem rpl_memmem
110 _GL_FUNCDECL_RPL (memmem, void *,
111 (void const *__haystack, size_t __haystack_len,
112 void const *__needle, size_t __needle_len)
114 _GL_ARG_NONNULL ((1, 3)));
115 _GL_CXXALIAS_RPL (memmem, void *,
116 (void const *__haystack, size_t __haystack_len,
117 void const *__needle, size_t __needle_len));
119 # if ! @HAVE_DECL_MEMMEM@
120 _GL_FUNCDECL_SYS (memmem, void *,
121 (void const *__haystack, size_t __haystack_len,
122 void const *__needle, size_t __needle_len)
124 _GL_ARG_NONNULL ((1, 3)));
126 _GL_CXXALIAS_SYS (memmem, void *,
127 (void const *__haystack, size_t __haystack_len,
128 void const *__needle, size_t __needle_len));
130 _GL_CXXALIASWARN (memmem);
131 #elif defined GNULIB_POSIXCHECK
133 # if HAVE_RAW_DECL_MEMMEM
134 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
135 "use gnulib module memmem-simple for portability, "
136 "and module memmem for speed" );
140 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
141 last written byte. */
143 # if ! @HAVE_MEMPCPY@
144 _GL_FUNCDECL_SYS (mempcpy, void *,
145 (void *restrict __dest, void const *restrict __src,
147 _GL_ARG_NONNULL ((1, 2)));
149 _GL_CXXALIAS_SYS (mempcpy, void *,
150 (void *restrict __dest, void const *restrict __src,
152 _GL_CXXALIASWARN (mempcpy);
153 #elif defined GNULIB_POSIXCHECK
155 # if HAVE_RAW_DECL_MEMPCPY
156 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
157 "use gnulib module mempcpy for portability");
161 /* Search backwards through a block for a byte (specified as an int). */
163 # if ! @HAVE_DECL_MEMRCHR@
164 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
166 _GL_ARG_NONNULL ((1)));
168 /* On some systems, this function is defined as an overloaded function:
169 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
170 extern "C++" { void * std::memrchr (void *, int, size_t); } */
171 _GL_CXXALIAS_SYS_CAST2 (memrchr,
172 void *, (void const *, int, size_t),
173 void const *, (void const *, int, size_t));
174 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
175 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
176 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
177 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
179 _GL_CXXALIASWARN (memrchr);
181 #elif defined GNULIB_POSIXCHECK
183 # if HAVE_RAW_DECL_MEMRCHR
184 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
185 "use gnulib module memrchr for portability");
189 /* Find the first occurrence of C in S. More efficient than
190 memchr(S,C,N), at the expense of undefined behavior if C does not
191 occur within N bytes. */
192 #if @GNULIB_RAWMEMCHR@
193 # if ! @HAVE_RAWMEMCHR@
194 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
196 _GL_ARG_NONNULL ((1)));
198 /* On some systems, this function is defined as an overloaded function:
199 extern "C++" { const void * std::rawmemchr (const void *, int); }
200 extern "C++" { void * std::rawmemchr (void *, int); } */
201 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
202 void *, (void const *__s, int __c_in),
203 void const *, (void const *__s, int __c_in));
204 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
205 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
206 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
207 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
209 _GL_CXXALIASWARN (rawmemchr);
211 #elif defined GNULIB_POSIXCHECK
213 # if HAVE_RAW_DECL_RAWMEMCHR
214 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
215 "use gnulib module rawmemchr for portability");
219 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
222 _GL_FUNCDECL_SYS (stpcpy, char *,
223 (char *restrict __dst, char const *restrict __src)
224 _GL_ARG_NONNULL ((1, 2)));
226 _GL_CXXALIAS_SYS (stpcpy, char *,
227 (char *restrict __dst, char const *restrict __src));
228 _GL_CXXALIASWARN (stpcpy);
229 #elif defined GNULIB_POSIXCHECK
231 # if HAVE_RAW_DECL_STPCPY
232 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
233 "use gnulib module stpcpy for portability");
237 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
238 last non-NUL byte written into DST. */
240 # if @REPLACE_STPNCPY@
241 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
243 # define stpncpy rpl_stpncpy
245 _GL_FUNCDECL_RPL (stpncpy, char *,
246 (char *restrict __dst, char const *restrict __src,
248 _GL_ARG_NONNULL ((1, 2)));
249 _GL_CXXALIAS_RPL (stpncpy, char *,
250 (char *restrict __dst, char const *restrict __src,
253 # if ! @HAVE_STPNCPY@
254 _GL_FUNCDECL_SYS (stpncpy, char *,
255 (char *restrict __dst, char const *restrict __src,
257 _GL_ARG_NONNULL ((1, 2)));
259 _GL_CXXALIAS_SYS (stpncpy, char *,
260 (char *restrict __dst, char const *restrict __src,
263 _GL_CXXALIASWARN (stpncpy);
264 #elif defined GNULIB_POSIXCHECK
266 # if HAVE_RAW_DECL_STPNCPY
267 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
268 "use gnulib module stpncpy for portability");
272 #if defined GNULIB_POSIXCHECK
273 /* strchr() does not work with multibyte strings if the locale encoding is
274 GB18030 and the character to be searched is a digit. */
276 /* Assume strchr is always declared. */
277 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
278 "in some multibyte locales - "
279 "use mbschr if you care about internationalization");
282 /* Find the first occurrence of C in S or the final NUL byte. */
283 #if @GNULIB_STRCHRNUL@
284 # if ! @HAVE_STRCHRNUL@
285 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
287 _GL_ARG_NONNULL ((1)));
289 /* On some systems, this function is defined as an overloaded function:
290 extern "C++" { const char * std::strchrnul (const char *, int); }
291 extern "C++" { char * std::strchrnul (char *, int); } */
292 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
293 char *, (char const *__s, int __c_in),
294 char const *, (char const *__s, int __c_in));
295 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
296 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
297 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
298 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
300 _GL_CXXALIASWARN (strchrnul);
302 #elif defined GNULIB_POSIXCHECK
304 # if HAVE_RAW_DECL_STRCHRNUL
305 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
306 "use gnulib module strchrnul for portability");
310 /* Duplicate S, returning an identical malloc'd string. */
312 # if @REPLACE_STRDUP@
313 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
315 # define strdup rpl_strdup
317 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
318 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
320 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
321 /* strdup exists as a function and as a macro. Get rid of the macro. */
324 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
325 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
327 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
329 _GL_CXXALIASWARN (strdup);
330 #elif defined GNULIB_POSIXCHECK
332 # if HAVE_RAW_DECL_STRDUP
333 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
334 "use gnulib module strdup for portability");
338 /* Append no more than N characters from SRC onto DEST. */
340 # if @REPLACE_STRNCAT@
341 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
343 # define strncat rpl_strncat
345 _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
346 _GL_ARG_NONNULL ((1, 2)));
347 _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
349 _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
351 _GL_CXXALIASWARN (strncat);
352 #elif defined GNULIB_POSIXCHECK
354 # if HAVE_RAW_DECL_STRNCAT
355 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
356 "use gnulib module strncat for portability");
360 /* Return a newly allocated copy of at most N bytes of STRING. */
362 # if @REPLACE_STRNDUP@
363 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
365 # define strndup rpl_strndup
367 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
368 _GL_ARG_NONNULL ((1)));
369 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
371 # if ! @HAVE_DECL_STRNDUP@
372 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
373 _GL_ARG_NONNULL ((1)));
375 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
377 _GL_CXXALIASWARN (strndup);
378 #elif defined GNULIB_POSIXCHECK
380 # if HAVE_RAW_DECL_STRNDUP
381 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
382 "use gnulib module strndup for portability");
386 /* Find the length (number of bytes) of STRING, but scan at most
387 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
390 # if @REPLACE_STRNLEN@
391 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
393 # define strnlen rpl_strnlen
395 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__string, size_t __maxlen)
397 _GL_ARG_NONNULL ((1)));
398 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__string, size_t __maxlen));
400 # if ! @HAVE_DECL_STRNLEN@
401 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
403 _GL_ARG_NONNULL ((1)));
405 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
407 _GL_CXXALIASWARN (strnlen);
408 #elif defined GNULIB_POSIXCHECK
410 # if HAVE_RAW_DECL_STRNLEN
411 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
412 "use gnulib module strnlen for portability");
416 #if defined GNULIB_POSIXCHECK
417 /* strcspn() assumes the second argument is a list of single-byte characters.
418 Even in this simple case, it does not work with multibyte strings if the
419 locale encoding is GB18030 and one of the characters to be searched is a
422 /* Assume strcspn is always declared. */
423 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
424 "in multibyte locales - "
425 "use mbscspn if you care about internationalization");
428 /* Find the first occurrence in S of any character in ACCEPT. */
430 # if ! @HAVE_STRPBRK@
431 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
433 _GL_ARG_NONNULL ((1, 2)));
435 /* On some systems, this function is defined as an overloaded function:
436 extern "C" { const char * strpbrk (const char *, const char *); }
437 extern "C++" { char * strpbrk (char *, const char *); } */
438 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
439 char *, (char const *__s, char const *__accept),
440 const char *, (char const *__s, char const *__accept));
441 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
442 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
443 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
444 _GL_CXXALIASWARN1 (strpbrk, char const *,
445 (char const *__s, char const *__accept));
447 _GL_CXXALIASWARN (strpbrk);
449 # if defined GNULIB_POSIXCHECK
450 /* strpbrk() assumes the second argument is a list of single-byte characters.
451 Even in this simple case, it does not work with multibyte strings if the
452 locale encoding is GB18030 and one of the characters to be searched is a
455 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
456 "in multibyte locales - "
457 "use mbspbrk if you care about internationalization");
459 #elif defined GNULIB_POSIXCHECK
461 # if HAVE_RAW_DECL_STRPBRK
462 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
463 "use gnulib module strpbrk for portability");
467 #if defined GNULIB_POSIXCHECK
468 /* strspn() assumes the second argument is a list of single-byte characters.
469 Even in this simple case, it cannot work with multibyte strings. */
471 /* Assume strspn is always declared. */
472 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
473 "in multibyte locales - "
474 "use mbsspn if you care about internationalization");
477 #if defined GNULIB_POSIXCHECK
478 /* strrchr() does not work with multibyte strings if the locale encoding is
479 GB18030 and the character to be searched is a digit. */
481 /* Assume strrchr is always declared. */
482 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
483 "in some multibyte locales - "
484 "use mbsrchr if you care about internationalization");
487 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
488 If one is found, overwrite it with a NUL, and advance *STRINGP
489 to point to the next char after it. Otherwise, set *STRINGP to NULL.
490 If *STRINGP was already NULL, nothing happens.
491 Return the old value of *STRINGP.
493 This is a variant of strtok() that is multithread-safe and supports
496 Caveat: It modifies the original string.
497 Caveat: These functions cannot be used on constant strings.
498 Caveat: The identity of the delimiting character is lost.
499 Caveat: It doesn't work with multibyte strings unless all of the delimiter
500 characters are ASCII characters < 0x30.
502 See also strtok_r(). */
505 _GL_FUNCDECL_SYS (strsep, char *,
506 (char **restrict __stringp, char const *restrict __delim)
507 _GL_ARG_NONNULL ((1, 2)));
509 _GL_CXXALIAS_SYS (strsep, char *,
510 (char **restrict __stringp, char const *restrict __delim));
511 _GL_CXXALIASWARN (strsep);
512 # if defined GNULIB_POSIXCHECK
514 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
515 "in multibyte locales - "
516 "use mbssep if you care about internationalization");
518 #elif defined GNULIB_POSIXCHECK
520 # if HAVE_RAW_DECL_STRSEP
521 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
522 "use gnulib module strsep for portability");
527 # if @REPLACE_STRSTR@
528 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
529 # define strstr rpl_strstr
531 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
533 _GL_ARG_NONNULL ((1, 2)));
534 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
536 /* On some systems, this function is defined as an overloaded function:
537 extern "C++" { const char * strstr (const char *, const char *); }
538 extern "C++" { char * strstr (char *, const char *); } */
539 _GL_CXXALIAS_SYS_CAST2 (strstr,
540 char *, (const char *haystack, const char *needle),
541 const char *, (const char *haystack, const char *needle));
543 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
544 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
545 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
546 _GL_CXXALIASWARN1 (strstr, const char *,
547 (const char *haystack, const char *needle));
549 _GL_CXXALIASWARN (strstr);
551 #elif defined GNULIB_POSIXCHECK
552 /* strstr() does not work with multibyte strings if the locale encoding is
553 different from UTF-8:
554 POSIX says that it operates on "strings", and "string" in POSIX is defined
555 as a sequence of bytes, not of characters. */
557 /* Assume strstr is always declared. */
558 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
559 "work correctly on character strings in most "
560 "multibyte locales - "
561 "use mbsstr if you care about internationalization, "
562 "or use strstr if you care about speed");
565 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
567 #if @GNULIB_STRCASESTR@
568 # if @REPLACE_STRCASESTR@
569 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
570 # define strcasestr rpl_strcasestr
572 _GL_FUNCDECL_RPL (strcasestr, char *,
573 (const char *haystack, const char *needle)
575 _GL_ARG_NONNULL ((1, 2)));
576 _GL_CXXALIAS_RPL (strcasestr, char *,
577 (const char *haystack, const char *needle));
579 # if ! @HAVE_STRCASESTR@
580 _GL_FUNCDECL_SYS (strcasestr, char *,
581 (const char *haystack, const char *needle)
583 _GL_ARG_NONNULL ((1, 2)));
585 /* On some systems, this function is defined as an overloaded function:
586 extern "C++" { const char * strcasestr (const char *, const char *); }
587 extern "C++" { char * strcasestr (char *, const char *); } */
588 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
589 char *, (const char *haystack, const char *needle),
590 const char *, (const char *haystack, const char *needle));
592 # if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 10 \
593 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
594 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
595 _GL_CXXALIASWARN1 (strcasestr, const char *,
596 (const char *haystack, const char *needle));
598 _GL_CXXALIASWARN (strcasestr);
600 #elif defined GNULIB_POSIXCHECK
601 /* strcasestr() does not work with multibyte strings:
602 It is a glibc extension, and glibc implements it only for unibyte
605 # if HAVE_RAW_DECL_STRCASESTR
606 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
607 "strings in multibyte locales - "
608 "use mbscasestr if you care about "
609 "internationalization, or use c-strcasestr if you want "
610 "a locale independent function");
614 /* Parse S into tokens separated by characters in DELIM.
615 If S is NULL, the saved pointer in SAVE_PTR is used as
616 the next starting point. For example:
617 char s[] = "-abc-=-def";
619 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
620 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
621 x = strtok_r(NULL, "=", &sp); // x = NULL
624 This is a variant of strtok() that is multithread-safe.
626 For the POSIX documentation for this function, see:
627 http://www.opengroup.org/susv3xsh/strtok.html
629 Caveat: It modifies the original string.
630 Caveat: These functions cannot be used on constant strings.
631 Caveat: The identity of the delimiting character is lost.
632 Caveat: It doesn't work with multibyte strings unless all of the delimiter
633 characters are ASCII characters < 0x30.
635 See also strsep(). */
636 #if @GNULIB_STRTOK_R@
637 # if @REPLACE_STRTOK_R@
638 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
640 # define strtok_r rpl_strtok_r
642 _GL_FUNCDECL_RPL (strtok_r, char *,
643 (char *restrict s, char const *restrict delim,
644 char **restrict save_ptr)
645 _GL_ARG_NONNULL ((2, 3)));
646 _GL_CXXALIAS_RPL (strtok_r, char *,
647 (char *restrict s, char const *restrict delim,
648 char **restrict save_ptr));
650 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
653 # if ! @HAVE_DECL_STRTOK_R@
654 _GL_FUNCDECL_SYS (strtok_r, char *,
655 (char *restrict s, char const *restrict delim,
656 char **restrict save_ptr)
657 _GL_ARG_NONNULL ((2, 3)));
659 _GL_CXXALIAS_SYS (strtok_r, char *,
660 (char *restrict s, char const *restrict delim,
661 char **restrict save_ptr));
663 _GL_CXXALIASWARN (strtok_r);
664 # if defined GNULIB_POSIXCHECK
665 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
666 "strings in multibyte locales - "
667 "use mbstok_r if you care about internationalization");
669 #elif defined GNULIB_POSIXCHECK
671 # if HAVE_RAW_DECL_STRTOK_R
672 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
673 "use gnulib module strtok_r for portability");
678 /* The following functions are not specified by POSIX. They are gnulib
682 /* Return the number of multibyte characters in the character string STRING.
683 This considers multibyte characters, unlike strlen, which counts bytes. */
684 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
687 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
688 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
689 # define mbslen rpl_mbslen
691 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
692 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
694 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
695 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
697 _GL_CXXALIASWARN (mbslen);
701 /* Return the number of multibyte characters in the character string starting
702 at STRING and ending at STRING + LEN. */
703 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
704 _GL_ARG_NONNULL ((1));
708 /* Locate the first single-byte character C in the character string STRING,
709 and return a pointer to it. Return NULL if C is not found in STRING.
710 Unlike strchr(), this function works correctly in multibyte locales with
711 encodings such as GB18030. */
713 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
714 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
716 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
717 _GL_ARG_NONNULL ((1)));
718 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
720 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
721 _GL_ARG_NONNULL ((1)));
722 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
724 _GL_CXXALIASWARN (mbschr);
728 /* Locate the last single-byte character C in the character string STRING,
729 and return a pointer to it. Return NULL if C is not found in STRING.
730 Unlike strrchr(), this function works correctly in multibyte locales with
731 encodings such as GB18030. */
733 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
734 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
736 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
737 _GL_ARG_NONNULL ((1)));
738 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
740 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
741 _GL_ARG_NONNULL ((1)));
742 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
744 _GL_CXXALIASWARN (mbsrchr);
748 /* Find the first occurrence of the character string NEEDLE in the character
749 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
750 Unlike strstr(), this function works correctly in multibyte locales with
751 encodings different from UTF-8. */
752 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
753 _GL_ARG_NONNULL ((1, 2));
756 #if @GNULIB_MBSCASECMP@
757 /* Compare the character strings S1 and S2, ignoring case, returning less than,
758 equal to or greater than zero if S1 is lexicographically less than, equal to
760 Note: This function may, in multibyte locales, return 0 for strings of
762 Unlike strcasecmp(), this function works correctly in multibyte locales. */
763 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
764 _GL_ARG_NONNULL ((1, 2));
767 #if @GNULIB_MBSNCASECMP@
768 /* Compare the initial segment of the character string S1 consisting of at most
769 N characters with the initial segment of the character string S2 consisting
770 of at most N characters, ignoring case, returning less than, equal to or
771 greater than zero if the initial segment of S1 is lexicographically less
772 than, equal to or greater than the initial segment of S2.
773 Note: This function may, in multibyte locales, return 0 for initial segments
774 of different lengths!
775 Unlike strncasecmp(), this function works correctly in multibyte locales.
776 But beware that N is not a byte count but a character count! */
777 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
778 _GL_ARG_NONNULL ((1, 2));
781 #if @GNULIB_MBSPCASECMP@
782 /* Compare the initial segment of the character string STRING consisting of
783 at most mbslen (PREFIX) characters with the character string PREFIX,
784 ignoring case. If the two match, return a pointer to the first byte
785 after this prefix in STRING. Otherwise, return NULL.
786 Note: This function may, in multibyte locales, return non-NULL if STRING
787 is of smaller length than PREFIX!
788 Unlike strncasecmp(), this function works correctly in multibyte
790 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
791 _GL_ARG_NONNULL ((1, 2));
794 #if @GNULIB_MBSCASESTR@
795 /* Find the first occurrence of the character string NEEDLE in the character
796 string HAYSTACK, using case-insensitive comparison.
797 Note: This function may, in multibyte locales, return success even if
798 strlen (haystack) < strlen (needle) !
799 Unlike strcasestr(), this function works correctly in multibyte locales. */
800 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
801 _GL_ARG_NONNULL ((1, 2));
805 /* Find the first occurrence in the character string STRING of any character
806 in the character string ACCEPT. Return the number of bytes from the
807 beginning of the string to this occurrence, or to the end of the string
809 Unlike strcspn(), this function works correctly in multibyte locales. */
810 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
811 _GL_ARG_NONNULL ((1, 2));
815 /* Find the first occurrence in the character string STRING of any character
816 in the character string ACCEPT. Return the pointer to it, or NULL if none
818 Unlike strpbrk(), this function works correctly in multibyte locales. */
820 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
821 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
823 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
824 _GL_ARG_NONNULL ((1, 2)));
825 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
827 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
828 _GL_ARG_NONNULL ((1, 2)));
829 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
831 _GL_CXXALIASWARN (mbspbrk);
835 /* Find the first occurrence in the character string STRING of any character
836 not in the character string REJECT. Return the number of bytes from the
837 beginning of the string to this occurrence, or to the end of the string
839 Unlike strspn(), this function works correctly in multibyte locales. */
840 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
841 _GL_ARG_NONNULL ((1, 2));
845 /* Search the next delimiter (multibyte character listed in the character
846 string DELIM) starting at the character string *STRINGP.
847 If one is found, overwrite it with a NUL, and advance *STRINGP to point
848 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
849 If *STRINGP was already NULL, nothing happens.
850 Return the old value of *STRINGP.
852 This is a variant of mbstok_r() that supports empty fields.
854 Caveat: It modifies the original string.
855 Caveat: These functions cannot be used on constant strings.
856 Caveat: The identity of the delimiting character is lost.
858 See also mbstok_r(). */
859 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
860 _GL_ARG_NONNULL ((1, 2));
863 #if @GNULIB_MBSTOK_R@
864 /* Parse the character string STRING into tokens separated by characters in
865 the character string DELIM.
866 If STRING is NULL, the saved pointer in SAVE_PTR is used as
867 the next starting point. For example:
868 char s[] = "-abc-=-def";
870 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
871 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
872 x = mbstok_r(NULL, "=", &sp); // x = NULL
875 Caveat: It modifies the original string.
876 Caveat: These functions cannot be used on constant strings.
877 Caveat: The identity of the delimiting character is lost.
879 See also mbssep(). */
880 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
881 _GL_ARG_NONNULL ((2, 3));
884 /* Map any int, typically from errno, into an error message. */
885 #if @GNULIB_STRERROR@
886 # if @REPLACE_STRERROR@
887 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
889 # define strerror rpl_strerror
891 _GL_FUNCDECL_RPL (strerror, char *, (int));
892 _GL_CXXALIAS_RPL (strerror, char *, (int));
894 _GL_CXXALIAS_SYS (strerror, char *, (int));
896 _GL_CXXALIASWARN (strerror);
897 #elif defined GNULIB_POSIXCHECK
899 /* Assume strerror is always declared. */
900 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
901 "use gnulib module strerror to guarantee non-NULL result");
904 #if @GNULIB_STRSIGNAL@
905 # if @REPLACE_STRSIGNAL@
906 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
907 # define strsignal rpl_strsignal
909 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
910 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
912 # if ! @HAVE_DECL_STRSIGNAL@
913 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
915 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
917 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
919 _GL_CXXALIASWARN (strsignal);
920 #elif defined GNULIB_POSIXCHECK
922 # if HAVE_RAW_DECL_STRSIGNAL
923 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
924 "use gnulib module strsignal for portability");
928 #if @GNULIB_STRVERSCMP@
929 # if !@HAVE_STRVERSCMP@
930 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
931 _GL_ARG_NONNULL ((1, 2)));
933 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
934 _GL_CXXALIASWARN (strverscmp);
935 #elif defined GNULIB_POSIXCHECK
937 # if HAVE_RAW_DECL_STRVERSCMP
938 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
939 "use gnulib module strverscmp for portability");
944 #endif /* _GL_STRING_H */
945 #endif /* _GL_STRING_H */