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@
26 /* The include_next requires a split double-inclusion guard. */
27 #@INCLUDE_NEXT@ @NEXT_STRING_H@
32 /* NetBSD 5.0 mis-defines NULL. */
35 /* MirBSD defines mbslen as a macro. */
36 #if @GNULIB_MBSLEN@ && defined __MirBSD__
41 /* This feature is available in gcc versions 2.5 and later. */
42 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
43 # define __attribute__(Spec) /* empty */
46 /* The attribute __pure__ was added in gcc 2.96. */
47 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
48 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
50 # define _GL_ATTRIBUTE_PURE /* empty */
53 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
54 /* But in any case avoid namespace pollution on glibc systems. */
55 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
56 && ! defined __GLIBC__
60 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
62 /* The definition of _GL_ARG_NONNULL is copied here. */
64 /* The definition of _GL_WARN_ON_USE is copied here. */
67 /* Return the first instance of C within N bytes of S, or NULL. */
70 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
71 # define memchr rpl_memchr
73 _GL_FUNCDECL_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
75 _GL_ARG_NONNULL ((1)));
76 _GL_CXXALIAS_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
));
79 _GL_FUNCDECL_SYS (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
81 _GL_ARG_NONNULL ((1)));
83 /* On some systems, this function is defined as an overloaded function:
84 extern "C" { const void * std::memchr (const void *, int, size_t); }
85 extern "C++" { void * std::memchr (void *, int, size_t); } */
86 _GL_CXXALIAS_SYS_CAST2 (memchr
,
87 void *, (void const *__s
, int __c
, size_t __n
),
88 void const *, (void const *__s
, int __c
, size_t __n
));
90 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
91 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
92 _GL_CXXALIASWARN1 (memchr
, void *, (void *__s
, int __c
, size_t __n
));
93 _GL_CXXALIASWARN1 (memchr
, void const *,
94 (void const *__s
, int __c
, size_t __n
));
96 _GL_CXXALIASWARN (memchr
);
98 #elif defined GNULIB_POSIXCHECK
100 /* Assume memchr is always declared. */
101 _GL_WARN_ON_USE (memchr
, "memchr has platform-specific bugs - "
102 "use gnulib module memchr for portability" );
105 /* Return the first occurrence of NEEDLE in HAYSTACK. */
107 # if @REPLACE_MEMMEM@
108 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
109 # define memmem rpl_memmem
111 _GL_FUNCDECL_RPL (memmem
, void *,
112 (void const *__haystack
, size_t __haystack_len
,
113 void const *__needle
, size_t __needle_len
)
115 _GL_ARG_NONNULL ((1, 3)));
116 _GL_CXXALIAS_RPL (memmem
, void *,
117 (void const *__haystack
, size_t __haystack_len
,
118 void const *__needle
, size_t __needle_len
));
120 # if ! @HAVE_DECL_MEMMEM@
121 _GL_FUNCDECL_SYS (memmem
, void *,
122 (void const *__haystack
, size_t __haystack_len
,
123 void const *__needle
, size_t __needle_len
)
125 _GL_ARG_NONNULL ((1, 3)));
127 _GL_CXXALIAS_SYS (memmem
, void *,
128 (void const *__haystack
, size_t __haystack_len
,
129 void const *__needle
, size_t __needle_len
));
131 _GL_CXXALIASWARN (memmem
);
132 #elif defined GNULIB_POSIXCHECK
134 # if HAVE_RAW_DECL_MEMMEM
135 _GL_WARN_ON_USE (memmem
, "memmem is unportable and often quadratic - "
136 "use gnulib module memmem-simple for portability, "
137 "and module memmem for speed" );
141 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
142 last written byte. */
144 # if ! @HAVE_MEMPCPY@
145 _GL_FUNCDECL_SYS (mempcpy
, void *,
146 (void *restrict __dest
, void const *restrict __src
,
148 _GL_ARG_NONNULL ((1, 2)));
150 _GL_CXXALIAS_SYS (mempcpy
, void *,
151 (void *restrict __dest
, void const *restrict __src
,
153 _GL_CXXALIASWARN (mempcpy
);
154 #elif defined GNULIB_POSIXCHECK
156 # if HAVE_RAW_DECL_MEMPCPY
157 _GL_WARN_ON_USE (mempcpy
, "mempcpy is unportable - "
158 "use gnulib module mempcpy for portability");
162 /* Search backwards through a block for a byte (specified as an int). */
164 # if ! @HAVE_DECL_MEMRCHR@
165 _GL_FUNCDECL_SYS (memrchr
, void *, (void const *, int, size_t)
167 _GL_ARG_NONNULL ((1)));
169 /* On some systems, this function is defined as an overloaded function:
170 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
171 extern "C++" { void * std::memrchr (void *, int, size_t); } */
172 _GL_CXXALIAS_SYS_CAST2 (memrchr
,
173 void *, (void const *, int, size_t),
174 void const *, (void const *, int, size_t));
175 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
176 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
177 _GL_CXXALIASWARN1 (memrchr
, void *, (void *, int, size_t));
178 _GL_CXXALIASWARN1 (memrchr
, void const *, (void const *, int, size_t));
180 _GL_CXXALIASWARN (memrchr
);
182 #elif defined GNULIB_POSIXCHECK
184 # if HAVE_RAW_DECL_MEMRCHR
185 _GL_WARN_ON_USE (memrchr
, "memrchr is unportable - "
186 "use gnulib module memrchr for portability");
190 /* Find the first occurrence of C in S. More efficient than
191 memchr(S,C,N), at the expense of undefined behavior if C does not
192 occur within N bytes. */
193 #if @GNULIB_RAWMEMCHR@
194 # if ! @HAVE_RAWMEMCHR@
195 _GL_FUNCDECL_SYS (rawmemchr
, void *, (void const *__s
, int __c_in
)
197 _GL_ARG_NONNULL ((1)));
199 /* On some systems, this function is defined as an overloaded function:
200 extern "C++" { const void * std::rawmemchr (const void *, int); }
201 extern "C++" { void * std::rawmemchr (void *, int); } */
202 _GL_CXXALIAS_SYS_CAST2 (rawmemchr
,
203 void *, (void const *__s
, int __c_in
),
204 void const *, (void const *__s
, int __c_in
));
205 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
206 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
207 _GL_CXXALIASWARN1 (rawmemchr
, void *, (void *__s
, int __c_in
));
208 _GL_CXXALIASWARN1 (rawmemchr
, void const *, (void const *__s
, int __c_in
));
210 _GL_CXXALIASWARN (rawmemchr
);
212 #elif defined GNULIB_POSIXCHECK
214 # if HAVE_RAW_DECL_RAWMEMCHR
215 _GL_WARN_ON_USE (rawmemchr
, "rawmemchr is unportable - "
216 "use gnulib module rawmemchr for portability");
220 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
223 _GL_FUNCDECL_SYS (stpcpy
, char *,
224 (char *restrict __dst
, char const *restrict __src
)
225 _GL_ARG_NONNULL ((1, 2)));
227 _GL_CXXALIAS_SYS (stpcpy
, char *,
228 (char *restrict __dst
, char const *restrict __src
));
229 _GL_CXXALIASWARN (stpcpy
);
230 #elif defined GNULIB_POSIXCHECK
232 # if HAVE_RAW_DECL_STPCPY
233 _GL_WARN_ON_USE (stpcpy
, "stpcpy is unportable - "
234 "use gnulib module stpcpy for portability");
238 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
239 last non-NUL byte written into DST. */
241 # if @REPLACE_STPNCPY@
242 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
244 # define stpncpy rpl_stpncpy
246 _GL_FUNCDECL_RPL (stpncpy
, char *,
247 (char *restrict __dst
, char const *restrict __src
,
249 _GL_ARG_NONNULL ((1, 2)));
250 _GL_CXXALIAS_RPL (stpncpy
, char *,
251 (char *restrict __dst
, char const *restrict __src
,
254 # if ! @HAVE_STPNCPY@
255 _GL_FUNCDECL_SYS (stpncpy
, char *,
256 (char *restrict __dst
, char const *restrict __src
,
258 _GL_ARG_NONNULL ((1, 2)));
260 _GL_CXXALIAS_SYS (stpncpy
, char *,
261 (char *restrict __dst
, char const *restrict __src
,
264 _GL_CXXALIASWARN (stpncpy
);
265 #elif defined GNULIB_POSIXCHECK
267 # if HAVE_RAW_DECL_STPNCPY
268 _GL_WARN_ON_USE (stpncpy
, "stpncpy is unportable - "
269 "use gnulib module stpncpy for portability");
273 #if defined GNULIB_POSIXCHECK
274 /* strchr() does not work with multibyte strings if the locale encoding is
275 GB18030 and the character to be searched is a digit. */
277 /* Assume strchr is always declared. */
278 _GL_WARN_ON_USE (strchr
, "strchr cannot work correctly on character strings "
279 "in some multibyte locales - "
280 "use mbschr if you care about internationalization");
283 /* Find the first occurrence of C in S or the final NUL byte. */
284 #if @GNULIB_STRCHRNUL@
285 # if ! @HAVE_STRCHRNUL@
286 _GL_FUNCDECL_SYS (strchrnul
, char *, (char const *__s
, int __c_in
)
288 _GL_ARG_NONNULL ((1)));
290 /* On some systems, this function is defined as an overloaded function:
291 extern "C++" { const char * std::strchrnul (const char *, int); }
292 extern "C++" { char * std::strchrnul (char *, int); } */
293 _GL_CXXALIAS_SYS_CAST2 (strchrnul
,
294 char *, (char const *__s
, int __c_in
),
295 char const *, (char const *__s
, int __c_in
));
296 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
297 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
298 _GL_CXXALIASWARN1 (strchrnul
, char *, (char *__s
, int __c_in
));
299 _GL_CXXALIASWARN1 (strchrnul
, char const *, (char const *__s
, int __c_in
));
301 _GL_CXXALIASWARN (strchrnul
);
303 #elif defined GNULIB_POSIXCHECK
305 # if HAVE_RAW_DECL_STRCHRNUL
306 _GL_WARN_ON_USE (strchrnul
, "strchrnul is unportable - "
307 "use gnulib module strchrnul for portability");
311 /* Duplicate S, returning an identical malloc'd string. */
313 # if @REPLACE_STRDUP@
314 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
316 # define strdup rpl_strdup
318 _GL_FUNCDECL_RPL (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
319 _GL_CXXALIAS_RPL (strdup
, char *, (char const *__s
));
321 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
322 /* strdup exists as a function and as a macro. Get rid of the macro. */
325 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
326 _GL_FUNCDECL_SYS (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
328 _GL_CXXALIAS_SYS (strdup
, char *, (char const *__s
));
330 _GL_CXXALIASWARN (strdup
);
331 #elif defined GNULIB_POSIXCHECK
333 # if HAVE_RAW_DECL_STRDUP
334 _GL_WARN_ON_USE (strdup
, "strdup is unportable - "
335 "use gnulib module strdup for portability");
339 /* Append no more than N characters from SRC onto DEST. */
341 # if @REPLACE_STRNCAT@
342 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
344 # define strncat rpl_strncat
346 _GL_FUNCDECL_RPL (strncat
, char *, (char *dest
, const char *src
, size_t n
)
347 _GL_ARG_NONNULL ((1, 2)));
348 _GL_CXXALIAS_RPL (strncat
, char *, (char *dest
, const char *src
, size_t n
));
350 _GL_CXXALIAS_SYS (strncat
, char *, (char *dest
, const char *src
, size_t n
));
352 _GL_CXXALIASWARN (strncat
);
353 #elif defined GNULIB_POSIXCHECK
355 # if HAVE_RAW_DECL_STRNCAT
356 _GL_WARN_ON_USE (strncat
, "strncat is unportable - "
357 "use gnulib module strncat for portability");
361 /* Return a newly allocated copy of at most N bytes of STRING. */
363 # if @REPLACE_STRNDUP@
364 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
366 # define strndup rpl_strndup
368 _GL_FUNCDECL_RPL (strndup
, char *, (char const *__string
, size_t __n
)
369 _GL_ARG_NONNULL ((1)));
370 _GL_CXXALIAS_RPL (strndup
, char *, (char const *__string
, size_t __n
));
372 # if ! @HAVE_DECL_STRNDUP@
373 _GL_FUNCDECL_SYS (strndup
, char *, (char const *__string
, size_t __n
)
374 _GL_ARG_NONNULL ((1)));
376 _GL_CXXALIAS_SYS (strndup
, char *, (char const *__string
, size_t __n
));
378 _GL_CXXALIASWARN (strndup
);
379 #elif defined GNULIB_POSIXCHECK
381 # if HAVE_RAW_DECL_STRNDUP
382 _GL_WARN_ON_USE (strndup
, "strndup is unportable - "
383 "use gnulib module strndup for portability");
387 /* Find the length (number of bytes) of STRING, but scan at most
388 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
391 # if @REPLACE_STRNLEN@
392 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
394 # define strnlen rpl_strnlen
396 _GL_FUNCDECL_RPL (strnlen
, size_t, (char const *__string
, size_t __maxlen
)
398 _GL_ARG_NONNULL ((1)));
399 _GL_CXXALIAS_RPL (strnlen
, size_t, (char const *__string
, size_t __maxlen
));
401 # if ! @HAVE_DECL_STRNLEN@
402 _GL_FUNCDECL_SYS (strnlen
, size_t, (char const *__string
, size_t __maxlen
)
404 _GL_ARG_NONNULL ((1)));
406 _GL_CXXALIAS_SYS (strnlen
, size_t, (char const *__string
, size_t __maxlen
));
408 _GL_CXXALIASWARN (strnlen
);
409 #elif defined GNULIB_POSIXCHECK
411 # if HAVE_RAW_DECL_STRNLEN
412 _GL_WARN_ON_USE (strnlen
, "strnlen is unportable - "
413 "use gnulib module strnlen for portability");
417 #if defined GNULIB_POSIXCHECK
418 /* strcspn() assumes the second argument is a list of single-byte characters.
419 Even in this simple case, it does not work with multibyte strings if the
420 locale encoding is GB18030 and one of the characters to be searched is a
423 /* Assume strcspn is always declared. */
424 _GL_WARN_ON_USE (strcspn
, "strcspn cannot work correctly on character strings "
425 "in multibyte locales - "
426 "use mbscspn if you care about internationalization");
429 /* Find the first occurrence in S of any character in ACCEPT. */
431 # if ! @HAVE_STRPBRK@
432 _GL_FUNCDECL_SYS (strpbrk
, char *, (char const *__s
, char const *__accept
)
434 _GL_ARG_NONNULL ((1, 2)));
436 /* On some systems, this function is defined as an overloaded function:
437 extern "C" { const char * strpbrk (const char *, const char *); }
438 extern "C++" { char * strpbrk (char *, const char *); } */
439 _GL_CXXALIAS_SYS_CAST2 (strpbrk
,
440 char *, (char const *__s
, char const *__accept
),
441 const char *, (char const *__s
, char const *__accept
));
442 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
443 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
444 _GL_CXXALIASWARN1 (strpbrk
, char *, (char *__s
, char const *__accept
));
445 _GL_CXXALIASWARN1 (strpbrk
, char const *,
446 (char const *__s
, char const *__accept
));
448 _GL_CXXALIASWARN (strpbrk
);
450 # if defined GNULIB_POSIXCHECK
451 /* strpbrk() assumes the second argument is a list of single-byte characters.
452 Even in this simple case, it does not work with multibyte strings if the
453 locale encoding is GB18030 and one of the characters to be searched is a
456 _GL_WARN_ON_USE (strpbrk
, "strpbrk cannot work correctly on character strings "
457 "in multibyte locales - "
458 "use mbspbrk if you care about internationalization");
460 #elif defined GNULIB_POSIXCHECK
462 # if HAVE_RAW_DECL_STRPBRK
463 _GL_WARN_ON_USE (strpbrk
, "strpbrk is unportable - "
464 "use gnulib module strpbrk for portability");
468 #if defined GNULIB_POSIXCHECK
469 /* strspn() assumes the second argument is a list of single-byte characters.
470 Even in this simple case, it cannot work with multibyte strings. */
472 /* Assume strspn is always declared. */
473 _GL_WARN_ON_USE (strspn
, "strspn cannot work correctly on character strings "
474 "in multibyte locales - "
475 "use mbsspn if you care about internationalization");
478 #if defined GNULIB_POSIXCHECK
479 /* strrchr() does not work with multibyte strings if the locale encoding is
480 GB18030 and the character to be searched is a digit. */
482 /* Assume strrchr is always declared. */
483 _GL_WARN_ON_USE (strrchr
, "strrchr cannot work correctly on character strings "
484 "in some multibyte locales - "
485 "use mbsrchr if you care about internationalization");
488 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
489 If one is found, overwrite it with a NUL, and advance *STRINGP
490 to point to the next char after it. Otherwise, set *STRINGP to NULL.
491 If *STRINGP was already NULL, nothing happens.
492 Return the old value of *STRINGP.
494 This is a variant of strtok() that is multithread-safe and supports
497 Caveat: It modifies the original string.
498 Caveat: These functions cannot be used on constant strings.
499 Caveat: The identity of the delimiting character is lost.
500 Caveat: It doesn't work with multibyte strings unless all of the delimiter
501 characters are ASCII characters < 0x30.
503 See also strtok_r(). */
506 _GL_FUNCDECL_SYS (strsep
, char *,
507 (char **restrict __stringp
, char const *restrict __delim
)
508 _GL_ARG_NONNULL ((1, 2)));
510 _GL_CXXALIAS_SYS (strsep
, char *,
511 (char **restrict __stringp
, char const *restrict __delim
));
512 _GL_CXXALIASWARN (strsep
);
513 # if defined GNULIB_POSIXCHECK
515 _GL_WARN_ON_USE (strsep
, "strsep cannot work correctly on character strings "
516 "in multibyte locales - "
517 "use mbssep if you care about internationalization");
519 #elif defined GNULIB_POSIXCHECK
521 # if HAVE_RAW_DECL_STRSEP
522 _GL_WARN_ON_USE (strsep
, "strsep is unportable - "
523 "use gnulib module strsep for portability");
528 # if @REPLACE_STRSTR@
529 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
530 # define strstr rpl_strstr
532 _GL_FUNCDECL_RPL (strstr
, char *, (const char *haystack
, const char *needle
)
534 _GL_ARG_NONNULL ((1, 2)));
535 _GL_CXXALIAS_RPL (strstr
, char *, (const char *haystack
, const char *needle
));
537 /* On some systems, this function is defined as an overloaded function:
538 extern "C++" { const char * strstr (const char *, const char *); }
539 extern "C++" { char * strstr (char *, const char *); } */
540 _GL_CXXALIAS_SYS_CAST2 (strstr
,
541 char *, (const char *haystack
, const char *needle
),
542 const char *, (const char *haystack
, const char *needle
));
544 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
545 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
546 _GL_CXXALIASWARN1 (strstr
, char *, (char *haystack
, const char *needle
));
547 _GL_CXXALIASWARN1 (strstr
, const char *,
548 (const char *haystack
, const char *needle
));
550 _GL_CXXALIASWARN (strstr
);
552 #elif defined GNULIB_POSIXCHECK
553 /* strstr() does not work with multibyte strings if the locale encoding is
554 different from UTF-8:
555 POSIX says that it operates on "strings", and "string" in POSIX is defined
556 as a sequence of bytes, not of characters. */
558 /* Assume strstr is always declared. */
559 _GL_WARN_ON_USE (strstr
, "strstr is quadratic on many systems, and cannot "
560 "work correctly on character strings in most "
561 "multibyte locales - "
562 "use mbsstr if you care about internationalization, "
563 "or use strstr if you care about speed");
566 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
568 #if @GNULIB_STRCASESTR@
569 # if @REPLACE_STRCASESTR@
570 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
571 # define strcasestr rpl_strcasestr
573 _GL_FUNCDECL_RPL (strcasestr
, char *,
574 (const char *haystack
, const char *needle
)
576 _GL_ARG_NONNULL ((1, 2)));
577 _GL_CXXALIAS_RPL (strcasestr
, char *,
578 (const char *haystack
, const char *needle
));
580 # if ! @HAVE_STRCASESTR@
581 _GL_FUNCDECL_SYS (strcasestr
, char *,
582 (const char *haystack
, const char *needle
)
584 _GL_ARG_NONNULL ((1, 2)));
586 /* On some systems, this function is defined as an overloaded function:
587 extern "C++" { const char * strcasestr (const char *, const char *); }
588 extern "C++" { char * strcasestr (char *, const char *); } */
589 _GL_CXXALIAS_SYS_CAST2 (strcasestr
,
590 char *, (const char *haystack
, const char *needle
),
591 const char *, (const char *haystack
, const char *needle
));
593 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
594 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
595 _GL_CXXALIASWARN1 (strcasestr
, char *, (char *haystack
, const char *needle
));
596 _GL_CXXALIASWARN1 (strcasestr
, const char *,
597 (const char *haystack
, const char *needle
));
599 _GL_CXXALIASWARN (strcasestr
);
601 #elif defined GNULIB_POSIXCHECK
602 /* strcasestr() does not work with multibyte strings:
603 It is a glibc extension, and glibc implements it only for unibyte
606 # if HAVE_RAW_DECL_STRCASESTR
607 _GL_WARN_ON_USE (strcasestr
, "strcasestr does work correctly on character "
608 "strings in multibyte locales - "
609 "use mbscasestr if you care about "
610 "internationalization, or use c-strcasestr if you want "
611 "a locale independent function");
615 /* Parse S into tokens separated by characters in DELIM.
616 If S is NULL, the saved pointer in SAVE_PTR is used as
617 the next starting point. For example:
618 char s[] = "-abc-=-def";
620 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
621 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
622 x = strtok_r(NULL, "=", &sp); // x = NULL
625 This is a variant of strtok() that is multithread-safe.
627 For the POSIX documentation for this function, see:
628 http://www.opengroup.org/susv3xsh/strtok.html
630 Caveat: It modifies the original string.
631 Caveat: These functions cannot be used on constant strings.
632 Caveat: The identity of the delimiting character is lost.
633 Caveat: It doesn't work with multibyte strings unless all of the delimiter
634 characters are ASCII characters < 0x30.
636 See also strsep(). */
637 #if @GNULIB_STRTOK_R@
638 # if @REPLACE_STRTOK_R@
639 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
641 # define strtok_r rpl_strtok_r
643 _GL_FUNCDECL_RPL (strtok_r
, char *,
644 (char *restrict s
, char const *restrict delim
,
645 char **restrict save_ptr
)
646 _GL_ARG_NONNULL ((2, 3)));
647 _GL_CXXALIAS_RPL (strtok_r
, char *,
648 (char *restrict s
, char const *restrict delim
,
649 char **restrict save_ptr
));
651 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
654 # if ! @HAVE_DECL_STRTOK_R@
655 _GL_FUNCDECL_SYS (strtok_r
, char *,
656 (char *restrict s
, char const *restrict delim
,
657 char **restrict save_ptr
)
658 _GL_ARG_NONNULL ((2, 3)));
660 _GL_CXXALIAS_SYS (strtok_r
, char *,
661 (char *restrict s
, char const *restrict delim
,
662 char **restrict save_ptr
));
664 _GL_CXXALIASWARN (strtok_r
);
665 # if defined GNULIB_POSIXCHECK
666 _GL_WARN_ON_USE (strtok_r
, "strtok_r cannot work correctly on character "
667 "strings in multibyte locales - "
668 "use mbstok_r if you care about internationalization");
670 #elif defined GNULIB_POSIXCHECK
672 # if HAVE_RAW_DECL_STRTOK_R
673 _GL_WARN_ON_USE (strtok_r
, "strtok_r is unportable - "
674 "use gnulib module strtok_r for portability");
679 /* The following functions are not specified by POSIX. They are gnulib
683 /* Return the number of multibyte characters in the character string STRING.
684 This considers multibyte characters, unlike strlen, which counts bytes. */
685 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
688 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
689 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
690 # define mbslen rpl_mbslen
692 _GL_FUNCDECL_RPL (mbslen
, size_t, (const char *string
) _GL_ARG_NONNULL ((1)));
693 _GL_CXXALIAS_RPL (mbslen
, size_t, (const char *string
));
695 _GL_FUNCDECL_SYS (mbslen
, size_t, (const char *string
) _GL_ARG_NONNULL ((1)));
696 _GL_CXXALIAS_SYS (mbslen
, size_t, (const char *string
));
698 _GL_CXXALIASWARN (mbslen
);
702 /* Return the number of multibyte characters in the character string starting
703 at STRING and ending at STRING + LEN. */
704 _GL_EXTERN_C
size_t mbsnlen (const char *string
, size_t len
)
705 _GL_ARG_NONNULL ((1));
709 /* Locate the first single-byte character C in the character string STRING,
710 and return a pointer to it. Return NULL if C is not found in STRING.
711 Unlike strchr(), this function works correctly in multibyte locales with
712 encodings such as GB18030. */
714 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
715 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
717 _GL_FUNCDECL_RPL (mbschr
, char *, (const char *string
, int c
)
718 _GL_ARG_NONNULL ((1)));
719 _GL_CXXALIAS_RPL (mbschr
, char *, (const char *string
, int c
));
721 _GL_FUNCDECL_SYS (mbschr
, char *, (const char *string
, int c
)
722 _GL_ARG_NONNULL ((1)));
723 _GL_CXXALIAS_SYS (mbschr
, char *, (const char *string
, int c
));
725 _GL_CXXALIASWARN (mbschr
);
729 /* Locate the last single-byte character C in the character string STRING,
730 and return a pointer to it. Return NULL if C is not found in STRING.
731 Unlike strrchr(), this function works correctly in multibyte locales with
732 encodings such as GB18030. */
734 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
735 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
737 _GL_FUNCDECL_RPL (mbsrchr
, char *, (const char *string
, int c
)
738 _GL_ARG_NONNULL ((1)));
739 _GL_CXXALIAS_RPL (mbsrchr
, char *, (const char *string
, int c
));
741 _GL_FUNCDECL_SYS (mbsrchr
, char *, (const char *string
, int c
)
742 _GL_ARG_NONNULL ((1)));
743 _GL_CXXALIAS_SYS (mbsrchr
, char *, (const char *string
, int c
));
745 _GL_CXXALIASWARN (mbsrchr
);
749 /* Find the first occurrence of the character string NEEDLE in the character
750 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
751 Unlike strstr(), this function works correctly in multibyte locales with
752 encodings different from UTF-8. */
753 _GL_EXTERN_C
char * mbsstr (const char *haystack
, const char *needle
)
754 _GL_ARG_NONNULL ((1, 2));
757 #if @GNULIB_MBSCASECMP@
758 /* Compare the character strings S1 and S2, ignoring case, returning less than,
759 equal to or greater than zero if S1 is lexicographically less than, equal to
761 Note: This function may, in multibyte locales, return 0 for strings of
763 Unlike strcasecmp(), this function works correctly in multibyte locales. */
764 _GL_EXTERN_C
int mbscasecmp (const char *s1
, const char *s2
)
765 _GL_ARG_NONNULL ((1, 2));
768 #if @GNULIB_MBSNCASECMP@
769 /* Compare the initial segment of the character string S1 consisting of at most
770 N characters with the initial segment of the character string S2 consisting
771 of at most N characters, ignoring case, returning less than, equal to or
772 greater than zero if the initial segment of S1 is lexicographically less
773 than, equal to or greater than the initial segment of S2.
774 Note: This function may, in multibyte locales, return 0 for initial segments
775 of different lengths!
776 Unlike strncasecmp(), this function works correctly in multibyte locales.
777 But beware that N is not a byte count but a character count! */
778 _GL_EXTERN_C
int mbsncasecmp (const char *s1
, const char *s2
, size_t n
)
779 _GL_ARG_NONNULL ((1, 2));
782 #if @GNULIB_MBSPCASECMP@
783 /* Compare the initial segment of the character string STRING consisting of
784 at most mbslen (PREFIX) characters with the character string PREFIX,
785 ignoring case. If the two match, return a pointer to the first byte
786 after this prefix in STRING. Otherwise, return NULL.
787 Note: This function may, in multibyte locales, return non-NULL if STRING
788 is of smaller length than PREFIX!
789 Unlike strncasecmp(), this function works correctly in multibyte
791 _GL_EXTERN_C
char * mbspcasecmp (const char *string
, const char *prefix
)
792 _GL_ARG_NONNULL ((1, 2));
795 #if @GNULIB_MBSCASESTR@
796 /* Find the first occurrence of the character string NEEDLE in the character
797 string HAYSTACK, using case-insensitive comparison.
798 Note: This function may, in multibyte locales, return success even if
799 strlen (haystack) < strlen (needle) !
800 Unlike strcasestr(), this function works correctly in multibyte locales. */
801 _GL_EXTERN_C
char * mbscasestr (const char *haystack
, const char *needle
)
802 _GL_ARG_NONNULL ((1, 2));
806 /* Find the first occurrence in the character string STRING of any character
807 in the character string ACCEPT. Return the number of bytes from the
808 beginning of the string to this occurrence, or to the end of the string
810 Unlike strcspn(), this function works correctly in multibyte locales. */
811 _GL_EXTERN_C
size_t mbscspn (const char *string
, const char *accept
)
812 _GL_ARG_NONNULL ((1, 2));
816 /* Find the first occurrence in the character string STRING of any character
817 in the character string ACCEPT. Return the pointer to it, or NULL if none
819 Unlike strpbrk(), this function works correctly in multibyte locales. */
821 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
822 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
824 _GL_FUNCDECL_RPL (mbspbrk
, char *, (const char *string
, const char *accept
)
825 _GL_ARG_NONNULL ((1, 2)));
826 _GL_CXXALIAS_RPL (mbspbrk
, char *, (const char *string
, const char *accept
));
828 _GL_FUNCDECL_SYS (mbspbrk
, char *, (const char *string
, const char *accept
)
829 _GL_ARG_NONNULL ((1, 2)));
830 _GL_CXXALIAS_SYS (mbspbrk
, char *, (const char *string
, const char *accept
));
832 _GL_CXXALIASWARN (mbspbrk
);
836 /* Find the first occurrence in the character string STRING of any character
837 not in the character string REJECT. Return the number of bytes from the
838 beginning of the string to this occurrence, or to the end of the string
840 Unlike strspn(), this function works correctly in multibyte locales. */
841 _GL_EXTERN_C
size_t mbsspn (const char *string
, const char *reject
)
842 _GL_ARG_NONNULL ((1, 2));
846 /* Search the next delimiter (multibyte character listed in the character
847 string DELIM) starting at the character string *STRINGP.
848 If one is found, overwrite it with a NUL, and advance *STRINGP to point
849 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
850 If *STRINGP was already NULL, nothing happens.
851 Return the old value of *STRINGP.
853 This is a variant of mbstok_r() that supports empty fields.
855 Caveat: It modifies the original string.
856 Caveat: These functions cannot be used on constant strings.
857 Caveat: The identity of the delimiting character is lost.
859 See also mbstok_r(). */
860 _GL_EXTERN_C
char * mbssep (char **stringp
, const char *delim
)
861 _GL_ARG_NONNULL ((1, 2));
864 #if @GNULIB_MBSTOK_R@
865 /* Parse the character string STRING into tokens separated by characters in
866 the character string DELIM.
867 If STRING is NULL, the saved pointer in SAVE_PTR is used as
868 the next starting point. For example:
869 char s[] = "-abc-=-def";
871 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
872 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
873 x = mbstok_r(NULL, "=", &sp); // x = NULL
876 Caveat: It modifies the original string.
877 Caveat: These functions cannot be used on constant strings.
878 Caveat: The identity of the delimiting character is lost.
880 See also mbssep(). */
881 _GL_EXTERN_C
char * mbstok_r (char *string
, const char *delim
, char **save_ptr
)
882 _GL_ARG_NONNULL ((2, 3));
885 /* Map any int, typically from errno, into an error message. */
886 #if @GNULIB_STRERROR@
887 # if @REPLACE_STRERROR@
888 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
890 # define strerror rpl_strerror
892 _GL_FUNCDECL_RPL (strerror
, char *, (int));
893 _GL_CXXALIAS_RPL (strerror
, char *, (int));
895 _GL_CXXALIAS_SYS (strerror
, char *, (int));
897 _GL_CXXALIASWARN (strerror
);
898 #elif defined GNULIB_POSIXCHECK
900 /* Assume strerror is always declared. */
901 _GL_WARN_ON_USE (strerror
, "strerror is unportable - "
902 "use gnulib module strerror to guarantee non-NULL result");
905 /* Map any int, typically from errno, into an error message. Multithread-safe.
906 Uses the POSIX declaration, not the glibc declaration. */
907 #if @GNULIB_STRERROR_R@
908 # if @REPLACE_STRERROR_R@
909 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
911 # define strerror_r rpl_strerror_r
913 _GL_FUNCDECL_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
914 _GL_ARG_NONNULL ((2)));
915 _GL_CXXALIAS_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
917 # if !@HAVE_DECL_STRERROR_R@
918 _GL_FUNCDECL_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
919 _GL_ARG_NONNULL ((2)));
921 _GL_CXXALIAS_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
923 # if @HAVE_DECL_STRERROR_R@
924 _GL_CXXALIASWARN (strerror_r
);
926 #elif defined GNULIB_POSIXCHECK
928 # if HAVE_RAW_DECL_STRERROR_R
929 _GL_WARN_ON_USE (strerror_r
, "strerror_r is unportable - "
930 "use gnulib module strerror_r-posix for portability");
934 #if @GNULIB_STRSIGNAL@
935 # if @REPLACE_STRSIGNAL@
936 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
937 # define strsignal rpl_strsignal
939 _GL_FUNCDECL_RPL (strsignal
, char *, (int __sig
));
940 _GL_CXXALIAS_RPL (strsignal
, char *, (int __sig
));
942 # if ! @HAVE_DECL_STRSIGNAL@
943 _GL_FUNCDECL_SYS (strsignal
, char *, (int __sig
));
945 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
947 _GL_CXXALIAS_SYS_CAST (strsignal
, char *, (int __sig
));
949 _GL_CXXALIASWARN (strsignal
);
950 #elif defined GNULIB_POSIXCHECK
952 # if HAVE_RAW_DECL_STRSIGNAL
953 _GL_WARN_ON_USE (strsignal
, "strsignal is unportable - "
954 "use gnulib module strsignal for portability");
958 #if @GNULIB_STRVERSCMP@
959 # if !@HAVE_STRVERSCMP@
960 _GL_FUNCDECL_SYS (strverscmp
, int, (const char *, const char *)
961 _GL_ARG_NONNULL ((1, 2)));
963 _GL_CXXALIAS_SYS (strverscmp
, int, (const char *, const char *));
964 _GL_CXXALIASWARN (strverscmp
);
965 #elif defined GNULIB_POSIXCHECK
967 # if HAVE_RAW_DECL_STRVERSCMP
968 _GL_WARN_ON_USE (strverscmp
, "strverscmp is unportable - "
969 "use gnulib module strverscmp for portability");
974 #endif /* _GL_STRING_H */
975 #endif /* _GL_STRING_H */