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 */
44 /* The attribute __pure__ was added in gcc 2.96. */
45 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
46 # define __pure__ /* empty */
51 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
53 /* The definition of _GL_ARG_NONNULL is copied here. */
55 /* The definition of _GL_WARN_ON_USE is copied here. */
58 /* Return the first instance of C within N bytes of S, or NULL. */
61 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
62 # define memchr rpl_memchr
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));
69 /* On some systems, this function is defined as an overloaded function:
70 extern "C" { const void * std::memchr (const void *, int, size_t); }
71 extern "C++" { void * std::memchr (void *, int, size_t); } */
72 _GL_CXXALIAS_SYS_CAST2 (memchr,
73 void *, (void const *__s, int __c, size_t __n),
74 void const *, (void const *__s, int __c, size_t __n));
76 _GL_CXXALIASWARN (memchr);
77 #elif defined GNULIB_POSIXCHECK
79 /* Assume memchr is always declared. */
80 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
81 "use gnulib module memchr for portability" );
84 /* Return the first occurrence of NEEDLE in HAYSTACK. */
87 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
88 # define memmem rpl_memmem
90 _GL_FUNCDECL_RPL (memmem, void *,
91 (void const *__haystack, size_t __haystack_len,
92 void const *__needle, size_t __needle_len)
93 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3)));
94 _GL_CXXALIAS_RPL (memmem, void *,
95 (void const *__haystack, size_t __haystack_len,
96 void const *__needle, size_t __needle_len));
98 # if ! @HAVE_DECL_MEMMEM@
99 _GL_FUNCDECL_SYS (memmem, void *,
100 (void const *__haystack, size_t __haystack_len,
101 void const *__needle, size_t __needle_len)
102 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3)));
104 _GL_CXXALIAS_SYS (memmem, void *,
105 (void const *__haystack, size_t __haystack_len,
106 void const *__needle, size_t __needle_len));
108 _GL_CXXALIASWARN (memmem);
109 #elif defined GNULIB_POSIXCHECK
111 # if HAVE_RAW_DECL_MEMMEM
112 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
113 "use gnulib module memmem-simple for portability, "
114 "and module memmem for speed" );
118 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
119 last written byte. */
121 # if ! @HAVE_MEMPCPY@
122 _GL_FUNCDECL_SYS (mempcpy, void *,
123 (void *restrict __dest, void const *restrict __src,
125 _GL_ARG_NONNULL ((1, 2)));
127 _GL_CXXALIAS_SYS (mempcpy, void *,
128 (void *restrict __dest, void const *restrict __src,
130 _GL_CXXALIASWARN (mempcpy);
131 #elif defined GNULIB_POSIXCHECK
133 # if HAVE_RAW_DECL_MEMPCPY
134 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
135 "use gnulib module mempcpy for portability");
139 /* Search backwards through a block for a byte (specified as an int). */
141 # if ! @HAVE_DECL_MEMRCHR@
142 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
143 __attribute__ ((__pure__))
144 _GL_ARG_NONNULL ((1)));
146 /* On some systems, this function is defined as an overloaded function:
147 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
148 extern "C++" { void * std::memrchr (void *, int, size_t); } */
149 _GL_CXXALIAS_SYS_CAST2 (memrchr,
150 void *, (void const *, int, size_t),
151 void const *, (void const *, int, size_t));
152 _GL_CXXALIASWARN (memrchr);
153 #elif defined GNULIB_POSIXCHECK
155 # if HAVE_RAW_DECL_MEMRCHR
156 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
157 "use gnulib module memrchr for portability");
161 /* Find the first occurrence of C in S. More efficient than
162 memchr(S,C,N), at the expense of undefined behavior if C does not
163 occur within N bytes. */
164 #if @GNULIB_RAWMEMCHR@
165 # if ! @HAVE_RAWMEMCHR@
166 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
167 __attribute__ ((__pure__))
168 _GL_ARG_NONNULL ((1)));
170 /* On some systems, this function is defined as an overloaded function:
171 extern "C++" { const void * std::rawmemchr (const void *, int); }
172 extern "C++" { void * std::rawmemchr (void *, int); } */
173 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
174 void *, (void const *__s, int __c_in),
175 void const *, (void const *__s, int __c_in));
176 _GL_CXXALIASWARN (rawmemchr);
177 #elif defined GNULIB_POSIXCHECK
179 # if HAVE_RAW_DECL_RAWMEMCHR
180 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
181 "use gnulib module rawmemchr for portability");
185 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
188 _GL_FUNCDECL_SYS (stpcpy, char *,
189 (char *restrict __dst, char const *restrict __src)
190 _GL_ARG_NONNULL ((1, 2)));
192 _GL_CXXALIAS_SYS (stpcpy, char *,
193 (char *restrict __dst, char const *restrict __src));
194 _GL_CXXALIASWARN (stpcpy);
195 #elif defined GNULIB_POSIXCHECK
197 # if HAVE_RAW_DECL_STPCPY
198 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
199 "use gnulib module stpcpy for portability");
203 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
204 last non-NUL byte written into DST. */
206 # if ! @HAVE_STPNCPY@
207 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
208 # define stpncpy rpl_stpncpy
210 _GL_FUNCDECL_RPL (stpncpy, char *,
211 (char *restrict __dst, char const *restrict __src,
213 _GL_ARG_NONNULL ((1, 2)));
214 _GL_CXXALIAS_RPL (stpncpy, char *,
215 (char *restrict __dst, char const *restrict __src,
218 _GL_CXXALIAS_SYS (stpncpy, char *,
219 (char *restrict __dst, char const *restrict __src,
222 _GL_CXXALIASWARN (stpncpy);
223 #elif defined GNULIB_POSIXCHECK
225 # if HAVE_RAW_DECL_STPNCPY
226 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
227 "use gnulib module stpncpy for portability");
231 #if defined GNULIB_POSIXCHECK
232 /* strchr() does not work with multibyte strings if the locale encoding is
233 GB18030 and the character to be searched is a digit. */
235 /* Assume strchr is always declared. */
236 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
237 "in some multibyte locales - "
238 "use mbschr if you care about internationalization");
241 /* Find the first occurrence of C in S or the final NUL byte. */
242 #if @GNULIB_STRCHRNUL@
243 # if ! @HAVE_STRCHRNUL@
244 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
245 __attribute__ ((__pure__))
246 _GL_ARG_NONNULL ((1)));
248 /* On some systems, this function is defined as an overloaded function:
249 extern "C++" { const char * std::strchrnul (const char *, int); }
250 extern "C++" { char * std::strchrnul (char *, int); } */
251 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
252 char *, (char const *__s, int __c_in),
253 char const *, (char const *__s, int __c_in));
254 _GL_CXXALIASWARN (strchrnul);
255 #elif defined GNULIB_POSIXCHECK
257 # if HAVE_RAW_DECL_STRCHRNUL
258 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
259 "use gnulib module strchrnul for portability");
263 /* Duplicate S, returning an identical malloc'd string. */
265 # if @REPLACE_STRDUP@
266 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
268 # define strdup rpl_strdup
270 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
271 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
273 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
274 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
276 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
278 _GL_CXXALIASWARN (strdup);
279 #elif defined GNULIB_POSIXCHECK
281 # if HAVE_RAW_DECL_STRDUP
282 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
283 "use gnulib module strdup for portability");
287 /* Return a newly allocated copy of at most N bytes of STRING. */
289 # if @REPLACE_STRNDUP@
290 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
292 # define strndup rpl_strndup
294 _GL_FUNCDECL_RPL (strndup, char *, (char const *__string, size_t __n)
295 _GL_ARG_NONNULL ((1)));
296 _GL_CXXALIAS_RPL (strndup, char *, (char const *__string, size_t __n));
298 # if ! @HAVE_DECL_STRNDUP@
299 _GL_FUNCDECL_SYS (strndup, char *, (char const *__string, size_t __n)
300 _GL_ARG_NONNULL ((1)));
302 _GL_CXXALIAS_SYS (strndup, char *, (char const *__string, size_t __n));
304 _GL_CXXALIASWARN (strndup);
305 #elif defined GNULIB_POSIXCHECK
307 # if HAVE_RAW_DECL_STRNDUP
308 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
309 "use gnulib module strndup for portability");
313 /* Find the length (number of bytes) of STRING, but scan at most
314 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
317 # if ! @HAVE_DECL_STRNLEN@
318 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__string, size_t __maxlen)
319 __attribute__ ((__pure__))
320 _GL_ARG_NONNULL ((1)));
322 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__string, size_t __maxlen));
323 _GL_CXXALIASWARN (strnlen);
324 #elif defined GNULIB_POSIXCHECK
326 # if HAVE_RAW_DECL_STRNLEN
327 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
328 "use gnulib module strnlen for portability");
332 #if defined GNULIB_POSIXCHECK
333 /* strcspn() assumes the second argument is a list of single-byte characters.
334 Even in this simple case, it does not work with multibyte strings if the
335 locale encoding is GB18030 and one of the characters to be searched is a
338 /* Assume strcspn is always declared. */
339 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
340 "in multibyte locales - "
341 "use mbscspn if you care about internationalization");
344 /* Find the first occurrence in S of any character in ACCEPT. */
346 # if ! @HAVE_STRPBRK@
347 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
348 __attribute__ ((__pure__))
349 _GL_ARG_NONNULL ((1, 2)));
351 /* On some systems, this function is defined as an overloaded function:
352 extern "C" { const char * strpbrk (const char *, const char *); }
353 extern "C++" { char * strpbrk (char *, const char *); } */
354 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
355 char *, (char const *__s, char const *__accept),
356 const char *, (char const *__s, char const *__accept));
357 _GL_CXXALIASWARN (strpbrk);
358 # if defined GNULIB_POSIXCHECK
359 /* strpbrk() assumes the second argument is a list of single-byte characters.
360 Even in this simple case, it does not work with multibyte strings if the
361 locale encoding is GB18030 and one of the characters to be searched is a
364 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
365 "in multibyte locales - "
366 "use mbspbrk if you care about internationalization");
368 #elif defined GNULIB_POSIXCHECK
370 # if HAVE_RAW_DECL_STRPBRK
371 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
372 "use gnulib module strpbrk for portability");
376 #if defined GNULIB_POSIXCHECK
377 /* strspn() assumes the second argument is a list of single-byte characters.
378 Even in this simple case, it cannot work with multibyte strings. */
380 /* Assume strspn is always declared. */
381 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
382 "in multibyte locales - "
383 "use mbsspn if you care about internationalization");
386 #if defined GNULIB_POSIXCHECK
387 /* strrchr() does not work with multibyte strings if the locale encoding is
388 GB18030 and the character to be searched is a digit. */
390 /* Assume strrchr is always declared. */
391 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
392 "in some multibyte locales - "
393 "use mbsrchr if you care about internationalization");
396 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
397 If one is found, overwrite it with a NUL, and advance *STRINGP
398 to point to the next char after it. Otherwise, set *STRINGP to NULL.
399 If *STRINGP was already NULL, nothing happens.
400 Return the old value of *STRINGP.
402 This is a variant of strtok() that is multithread-safe and supports
405 Caveat: It modifies the original string.
406 Caveat: These functions cannot be used on constant strings.
407 Caveat: The identity of the delimiting character is lost.
408 Caveat: It doesn't work with multibyte strings unless all of the delimiter
409 characters are ASCII characters < 0x30.
411 See also strtok_r(). */
414 _GL_FUNCDECL_SYS (strsep, char *,
415 (char **restrict __stringp, char const *restrict __delim)
416 _GL_ARG_NONNULL ((1, 2)));
418 _GL_CXXALIAS_SYS (strsep, char *,
419 (char **restrict __stringp, char const *restrict __delim));
420 _GL_CXXALIASWARN (strsep);
421 # if defined GNULIB_POSIXCHECK
423 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
424 "in multibyte locales - "
425 "use mbssep if you care about internationalization");
427 #elif defined GNULIB_POSIXCHECK
429 # if HAVE_RAW_DECL_STRSEP
430 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
431 "use gnulib module strsep for portability");
436 # if @REPLACE_STRSTR@
437 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
438 # define strstr rpl_strstr
440 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
441 __attribute__ ((__pure__))
442 _GL_ARG_NONNULL ((1, 2)));
443 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
445 /* On some systems, this function is defined as an overloaded function:
446 extern "C++" { const char * strstr (const char *, const char *); }
447 extern "C++" { char * strstr (char *, const char *); } */
448 _GL_CXXALIAS_SYS_CAST2 (strstr,
449 char *, (const char *haystack, const char *needle),
450 const char *, (const char *haystack, const char *needle));
452 _GL_CXXALIASWARN (strstr);
453 #elif defined GNULIB_POSIXCHECK
454 /* strstr() does not work with multibyte strings if the locale encoding is
455 different from UTF-8:
456 POSIX says that it operates on "strings", and "string" in POSIX is defined
457 as a sequence of bytes, not of characters. */
459 /* Assume strstr is always declared. */
460 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
461 "work correctly on character strings in most "
462 "multibyte locales - "
463 "use mbsstr if you care about internationalization, "
464 "or use strstr if you care about speed");
467 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
469 #if @GNULIB_STRCASESTR@
470 # if @REPLACE_STRCASESTR@
471 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
472 # define strcasestr rpl_strcasestr
474 _GL_FUNCDECL_RPL (strcasestr, char *,
475 (const char *haystack, const char *needle)
476 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
477 _GL_CXXALIAS_RPL (strcasestr, char *,
478 (const char *haystack, const char *needle));
480 # if ! @HAVE_STRCASESTR@
481 _GL_FUNCDECL_SYS (strcasestr, char *,
482 (const char *haystack, const char *needle)
483 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2)));
485 /* On some systems, this function is defined as an overloaded function:
486 extern "C++" { const char * strcasestr (const char *, const char *); }
487 extern "C++" { char * strcasestr (char *, const char *); } */
488 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
489 char *, (const char *haystack, const char *needle),
490 const char *, (const char *haystack, const char *needle));
492 _GL_CXXALIASWARN (strcasestr);
493 #elif defined GNULIB_POSIXCHECK
494 /* strcasestr() does not work with multibyte strings:
495 It is a glibc extension, and glibc implements it only for unibyte
498 # if HAVE_RAW_DECL_STRCASESTR
499 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
500 "strings in multibyte locales - "
501 "use mbscasestr if you care about "
502 "internationalization, or use c-strcasestr if you want "
503 "a locale independent function");
507 /* Parse S into tokens separated by characters in DELIM.
508 If S is NULL, the saved pointer in SAVE_PTR is used as
509 the next starting point. For example:
510 char s[] = "-abc-=-def";
512 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
513 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
514 x = strtok_r(NULL, "=", &sp); // x = NULL
517 This is a variant of strtok() that is multithread-safe.
519 For the POSIX documentation for this function, see:
520 http://www.opengroup.org/susv3xsh/strtok.html
522 Caveat: It modifies the original string.
523 Caveat: These functions cannot be used on constant strings.
524 Caveat: The identity of the delimiting character is lost.
525 Caveat: It doesn't work with multibyte strings unless all of the delimiter
526 characters are ASCII characters < 0x30.
528 See also strsep(). */
529 #if @GNULIB_STRTOK_R@
530 # if @REPLACE_STRTOK_R@
531 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
533 # define strtok_r rpl_strtok_r
535 _GL_FUNCDECL_RPL (strtok_r, char *,
536 (char *restrict s, char const *restrict delim,
537 char **restrict save_ptr)
538 _GL_ARG_NONNULL ((2, 3)));
539 _GL_CXXALIAS_RPL (strtok_r, char *,
540 (char *restrict s, char const *restrict delim,
541 char **restrict save_ptr));
543 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
546 # if ! @HAVE_DECL_STRTOK_R@
547 _GL_FUNCDECL_SYS (strtok_r, char *,
548 (char *restrict s, char const *restrict delim,
549 char **restrict save_ptr)
550 _GL_ARG_NONNULL ((2, 3)));
552 _GL_CXXALIAS_SYS (strtok_r, char *,
553 (char *restrict s, char const *restrict delim,
554 char **restrict save_ptr));
556 _GL_CXXALIASWARN (strtok_r);
557 # if defined GNULIB_POSIXCHECK
558 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
559 "strings in multibyte locales - "
560 "use mbstok_r if you care about internationalization");
562 #elif defined GNULIB_POSIXCHECK
564 # if HAVE_RAW_DECL_STRTOK_R
565 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
566 "use gnulib module strtok_r for portability");
571 /* The following functions are not specified by POSIX. They are gnulib
575 /* Return the number of multibyte characters in the character string STRING.
576 This considers multibyte characters, unlike strlen, which counts bytes. */
577 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
580 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
581 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
582 # define mbslen rpl_mbslen
584 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
585 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
587 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string) _GL_ARG_NONNULL ((1)));
588 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
590 _GL_CXXALIASWARN (mbslen);
594 /* Return the number of multibyte characters in the character string starting
595 at STRING and ending at STRING + LEN. */
596 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
597 _GL_ARG_NONNULL ((1));
601 /* Locate the first single-byte character C in the character string STRING,
602 and return a pointer to it. Return NULL if C is not found in STRING.
603 Unlike strchr(), this function works correctly in multibyte locales with
604 encodings such as GB18030. */
606 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
607 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
609 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
610 _GL_ARG_NONNULL ((1)));
611 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
613 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
614 _GL_ARG_NONNULL ((1)));
615 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
617 _GL_CXXALIASWARN (mbschr);
621 /* Locate the last single-byte character C in the character string STRING,
622 and return a pointer to it. Return NULL if C is not found in STRING.
623 Unlike strrchr(), this function works correctly in multibyte locales with
624 encodings such as GB18030. */
626 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
627 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
629 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
630 _GL_ARG_NONNULL ((1)));
631 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
633 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
634 _GL_ARG_NONNULL ((1)));
635 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
637 _GL_CXXALIASWARN (mbsrchr);
641 /* Find the first occurrence of the character string NEEDLE in the character
642 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
643 Unlike strstr(), this function works correctly in multibyte locales with
644 encodings different from UTF-8. */
645 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
646 _GL_ARG_NONNULL ((1, 2));
649 #if @GNULIB_MBSCASECMP@
650 /* Compare the character strings S1 and S2, ignoring case, returning less than,
651 equal to or greater than zero if S1 is lexicographically less than, equal to
653 Note: This function may, in multibyte locales, return 0 for strings of
655 Unlike strcasecmp(), this function works correctly in multibyte locales. */
656 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
657 _GL_ARG_NONNULL ((1, 2));
660 #if @GNULIB_MBSNCASECMP@
661 /* Compare the initial segment of the character string S1 consisting of at most
662 N characters with the initial segment of the character string S2 consisting
663 of at most N characters, ignoring case, returning less than, equal to or
664 greater than zero if the initial segment of S1 is lexicographically less
665 than, equal to or greater than the initial segment of S2.
666 Note: This function may, in multibyte locales, return 0 for initial segments
667 of different lengths!
668 Unlike strncasecmp(), this function works correctly in multibyte locales.
669 But beware that N is not a byte count but a character count! */
670 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
671 _GL_ARG_NONNULL ((1, 2));
674 #if @GNULIB_MBSPCASECMP@
675 /* Compare the initial segment of the character string STRING consisting of
676 at most mbslen (PREFIX) characters with the character string PREFIX,
677 ignoring case, returning less than, equal to or greater than zero if this
678 initial segment is lexicographically less than, equal to or greater than
680 Note: This function may, in multibyte locales, return 0 if STRING is of
681 smaller length than PREFIX!
682 Unlike strncasecmp(), this function works correctly in multibyte
684 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
685 _GL_ARG_NONNULL ((1, 2));
688 #if @GNULIB_MBSCASESTR@
689 /* Find the first occurrence of the character string NEEDLE in the character
690 string HAYSTACK, using case-insensitive comparison.
691 Note: This function may, in multibyte locales, return success even if
692 strlen (haystack) < strlen (needle) !
693 Unlike strcasestr(), this function works correctly in multibyte locales. */
694 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
695 _GL_ARG_NONNULL ((1, 2));
699 /* Find the first occurrence in the character string STRING of any character
700 in the character string ACCEPT. Return the number of bytes from the
701 beginning of the string to this occurrence, or to the end of the string
703 Unlike strcspn(), this function works correctly in multibyte locales. */
704 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
705 _GL_ARG_NONNULL ((1, 2));
709 /* Find the first occurrence in the character string STRING of any character
710 in the character string ACCEPT. Return the pointer to it, or NULL if none
712 Unlike strpbrk(), this function works correctly in multibyte locales. */
714 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
715 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
717 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
718 _GL_ARG_NONNULL ((1, 2)));
719 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
721 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
722 _GL_ARG_NONNULL ((1, 2)));
723 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
725 _GL_CXXALIASWARN (mbspbrk);
729 /* Find the first occurrence in the character string STRING of any character
730 not in the character string REJECT. Return the number of bytes from the
731 beginning of the string to this occurrence, or to the end of the string
733 Unlike strspn(), this function works correctly in multibyte locales. */
734 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
735 _GL_ARG_NONNULL ((1, 2));
739 /* Search the next delimiter (multibyte character listed in the character
740 string DELIM) starting at the character string *STRINGP.
741 If one is found, overwrite it with a NUL, and advance *STRINGP to point
742 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
743 If *STRINGP was already NULL, nothing happens.
744 Return the old value of *STRINGP.
746 This is a variant of mbstok_r() that supports empty fields.
748 Caveat: It modifies the original string.
749 Caveat: These functions cannot be used on constant strings.
750 Caveat: The identity of the delimiting character is lost.
752 See also mbstok_r(). */
753 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
754 _GL_ARG_NONNULL ((1, 2));
757 #if @GNULIB_MBSTOK_R@
758 /* Parse the character string STRING into tokens separated by characters in
759 the character string DELIM.
760 If STRING is NULL, the saved pointer in SAVE_PTR is used as
761 the next starting point. For example:
762 char s[] = "-abc-=-def";
764 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
765 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
766 x = mbstok_r(NULL, "=", &sp); // x = NULL
769 Caveat: It modifies the original string.
770 Caveat: These functions cannot be used on constant strings.
771 Caveat: The identity of the delimiting character is lost.
773 See also mbssep(). */
774 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
775 _GL_ARG_NONNULL ((2, 3));
778 /* Map any int, typically from errno, into an error message. */
779 #if @GNULIB_STRERROR@
780 # if @REPLACE_STRERROR@
781 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
783 # define strerror rpl_strerror
785 _GL_FUNCDECL_RPL (strerror, char *, (int));
786 _GL_CXXALIAS_RPL (strerror, char *, (int));
788 _GL_CXXALIAS_SYS (strerror, char *, (int));
790 _GL_CXXALIASWARN (strerror);
791 #elif defined GNULIB_POSIXCHECK
793 /* Assume strerror is always declared. */
794 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
795 "use gnulib module strerror to guarantee non-NULL result");
798 #if @GNULIB_STRSIGNAL@
799 # if @REPLACE_STRSIGNAL@
800 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
801 # define strsignal rpl_strsignal
803 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
804 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
806 # if ! @HAVE_DECL_STRSIGNAL@
807 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
809 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
811 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
813 _GL_CXXALIASWARN (strsignal);
814 #elif defined GNULIB_POSIXCHECK
816 # if HAVE_RAW_DECL_STRSIGNAL
817 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
818 "use gnulib module strsignal for portability");
822 #if @GNULIB_STRVERSCMP@
823 # if !@HAVE_STRVERSCMP@
824 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
825 _GL_ARG_NONNULL ((1, 2)));
827 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
828 _GL_CXXALIASWARN (strverscmp);
829 #elif defined GNULIB_POSIXCHECK
831 # if HAVE_RAW_DECL_STRVERSCMP
832 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
833 "use gnulib module strverscmp for portability");
838 #endif /* _GL_STRING_H */
839 #endif /* _GL_STRING_H */