1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2011 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. */
19 #ifndef _@GUARD_PREFIX@_STRING_H
22 @PRAGMA_SYSTEM_HEADER@
26 /* The include_next requires a split double-inclusion guard. */
27 #@INCLUDE_NEXT@ @NEXT_STRING_H@
29 #ifndef _@GUARD_PREFIX@_STRING_H
30 #define _@GUARD_PREFIX@_STRING_H
32 /* NetBSD 5.0 mis-defines NULL. */
35 /* MirBSD defines mbslen as a macro. */
36 #if @GNULIB_MBSLEN@ && defined __MirBSD__
40 /* The __attribute__ feature is available in gcc versions 2.5 and later.
41 The attribute __pure__ was added in gcc 2.96. */
42 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
43 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
45 # define _GL_ATTRIBUTE_PURE /* empty */
48 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
49 /* But in any case avoid namespace pollution on glibc systems. */
50 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
51 && ! defined __GLIBC__
55 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
57 /* The definition of _GL_ARG_NONNULL is copied here. */
59 /* The definition of _GL_WARN_ON_USE is copied here. */
62 /* Find the index of the least-significant set bit. */
65 _GL_FUNCDECL_SYS (ffsl
, int, (long int i
));
67 _GL_CXXALIAS_SYS (ffsl
, int, (long int i
));
68 _GL_CXXALIASWARN (ffsl
);
69 #elif defined GNULIB_POSIXCHECK
71 # if HAVE_RAW_DECL_FFSL
72 _GL_WARN_ON_USE (ffsl
, "ffsl is not portable - use the ffsl module");
77 /* Find the index of the least-significant set bit. */
80 _GL_FUNCDECL_SYS (ffsll
, int, (long long int i
));
82 _GL_CXXALIAS_SYS (ffsll
, int, (long long int i
));
83 _GL_CXXALIASWARN (ffsll
);
84 #elif defined GNULIB_POSIXCHECK
86 # if HAVE_RAW_DECL_FFSLL
87 _GL_WARN_ON_USE (ffsll
, "ffsll is not portable - use the ffsll module");
92 /* Return the first instance of C within N bytes of S, or NULL. */
95 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
96 # define memchr rpl_memchr
98 _GL_FUNCDECL_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
100 _GL_ARG_NONNULL ((1)));
101 _GL_CXXALIAS_RPL (memchr
, void *, (void const *__s
, int __c
, size_t __n
));
104 _GL_FUNCDECL_SYS (memchr
, void *, (void const *__s
, int __c
, size_t __n
)
106 _GL_ARG_NONNULL ((1)));
108 /* On some systems, this function is defined as an overloaded function:
109 extern "C" { const void * std::memchr (const void *, int, size_t); }
110 extern "C++" { void * std::memchr (void *, int, size_t); } */
111 _GL_CXXALIAS_SYS_CAST2 (memchr
,
112 void *, (void const *__s
, int __c
, size_t __n
),
113 void const *, (void const *__s
, int __c
, size_t __n
));
115 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
116 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
117 _GL_CXXALIASWARN1 (memchr
, void *, (void *__s
, int __c
, size_t __n
));
118 _GL_CXXALIASWARN1 (memchr
, void const *,
119 (void const *__s
, int __c
, size_t __n
));
121 _GL_CXXALIASWARN (memchr
);
123 #elif defined GNULIB_POSIXCHECK
125 /* Assume memchr is always declared. */
126 _GL_WARN_ON_USE (memchr
, "memchr has platform-specific bugs - "
127 "use gnulib module memchr for portability" );
130 /* Return the first occurrence of NEEDLE in HAYSTACK. */
132 # if @REPLACE_MEMMEM@
133 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
134 # define memmem rpl_memmem
136 _GL_FUNCDECL_RPL (memmem
, void *,
137 (void const *__haystack
, size_t __haystack_len
,
138 void const *__needle
, size_t __needle_len
)
140 _GL_ARG_NONNULL ((1, 3)));
141 _GL_CXXALIAS_RPL (memmem
, void *,
142 (void const *__haystack
, size_t __haystack_len
,
143 void const *__needle
, size_t __needle_len
));
145 # if ! @HAVE_DECL_MEMMEM@
146 _GL_FUNCDECL_SYS (memmem
, void *,
147 (void const *__haystack
, size_t __haystack_len
,
148 void const *__needle
, size_t __needle_len
)
150 _GL_ARG_NONNULL ((1, 3)));
152 _GL_CXXALIAS_SYS (memmem
, void *,
153 (void const *__haystack
, size_t __haystack_len
,
154 void const *__needle
, size_t __needle_len
));
156 _GL_CXXALIASWARN (memmem
);
157 #elif defined GNULIB_POSIXCHECK
159 # if HAVE_RAW_DECL_MEMMEM
160 _GL_WARN_ON_USE (memmem
, "memmem is unportable and often quadratic - "
161 "use gnulib module memmem-simple for portability, "
162 "and module memmem for speed" );
166 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
167 last written byte. */
169 # if ! @HAVE_MEMPCPY@
170 _GL_FUNCDECL_SYS (mempcpy
, void *,
171 (void *restrict __dest
, void const *restrict __src
,
173 _GL_ARG_NONNULL ((1, 2)));
175 _GL_CXXALIAS_SYS (mempcpy
, void *,
176 (void *restrict __dest
, void const *restrict __src
,
178 _GL_CXXALIASWARN (mempcpy
);
179 #elif defined GNULIB_POSIXCHECK
181 # if HAVE_RAW_DECL_MEMPCPY
182 _GL_WARN_ON_USE (mempcpy
, "mempcpy is unportable - "
183 "use gnulib module mempcpy for portability");
187 /* Search backwards through a block for a byte (specified as an int). */
189 # if ! @HAVE_DECL_MEMRCHR@
190 _GL_FUNCDECL_SYS (memrchr
, void *, (void const *, int, size_t)
192 _GL_ARG_NONNULL ((1)));
194 /* On some systems, this function is defined as an overloaded function:
195 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
196 extern "C++" { void * std::memrchr (void *, int, size_t); } */
197 _GL_CXXALIAS_SYS_CAST2 (memrchr
,
198 void *, (void const *, int, size_t),
199 void const *, (void const *, int, size_t));
200 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
201 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
202 _GL_CXXALIASWARN1 (memrchr
, void *, (void *, int, size_t));
203 _GL_CXXALIASWARN1 (memrchr
, void const *, (void const *, int, size_t));
205 _GL_CXXALIASWARN (memrchr
);
207 #elif defined GNULIB_POSIXCHECK
209 # if HAVE_RAW_DECL_MEMRCHR
210 _GL_WARN_ON_USE (memrchr
, "memrchr is unportable - "
211 "use gnulib module memrchr for portability");
215 /* Find the first occurrence of C in S. More efficient than
216 memchr(S,C,N), at the expense of undefined behavior if C does not
217 occur within N bytes. */
218 #if @GNULIB_RAWMEMCHR@
219 # if ! @HAVE_RAWMEMCHR@
220 _GL_FUNCDECL_SYS (rawmemchr
, void *, (void const *__s
, int __c_in
)
222 _GL_ARG_NONNULL ((1)));
224 /* On some systems, this function is defined as an overloaded function:
225 extern "C++" { const void * std::rawmemchr (const void *, int); }
226 extern "C++" { void * std::rawmemchr (void *, int); } */
227 _GL_CXXALIAS_SYS_CAST2 (rawmemchr
,
228 void *, (void const *__s
, int __c_in
),
229 void const *, (void const *__s
, int __c_in
));
230 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
231 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
232 _GL_CXXALIASWARN1 (rawmemchr
, void *, (void *__s
, int __c_in
));
233 _GL_CXXALIASWARN1 (rawmemchr
, void const *, (void const *__s
, int __c_in
));
235 _GL_CXXALIASWARN (rawmemchr
);
237 #elif defined GNULIB_POSIXCHECK
239 # if HAVE_RAW_DECL_RAWMEMCHR
240 _GL_WARN_ON_USE (rawmemchr
, "rawmemchr is unportable - "
241 "use gnulib module rawmemchr for portability");
245 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
248 _GL_FUNCDECL_SYS (stpcpy
, char *,
249 (char *restrict __dst
, char const *restrict __src
)
250 _GL_ARG_NONNULL ((1, 2)));
252 _GL_CXXALIAS_SYS (stpcpy
, char *,
253 (char *restrict __dst
, char const *restrict __src
));
254 _GL_CXXALIASWARN (stpcpy
);
255 #elif defined GNULIB_POSIXCHECK
257 # if HAVE_RAW_DECL_STPCPY
258 _GL_WARN_ON_USE (stpcpy
, "stpcpy is unportable - "
259 "use gnulib module stpcpy for portability");
263 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
264 last non-NUL byte written into DST. */
266 # if @REPLACE_STPNCPY@
267 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
269 # define stpncpy rpl_stpncpy
271 _GL_FUNCDECL_RPL (stpncpy
, char *,
272 (char *restrict __dst
, char const *restrict __src
,
274 _GL_ARG_NONNULL ((1, 2)));
275 _GL_CXXALIAS_RPL (stpncpy
, char *,
276 (char *restrict __dst
, char const *restrict __src
,
279 # if ! @HAVE_STPNCPY@
280 _GL_FUNCDECL_SYS (stpncpy
, char *,
281 (char *restrict __dst
, char const *restrict __src
,
283 _GL_ARG_NONNULL ((1, 2)));
285 _GL_CXXALIAS_SYS (stpncpy
, char *,
286 (char *restrict __dst
, char const *restrict __src
,
289 _GL_CXXALIASWARN (stpncpy
);
290 #elif defined GNULIB_POSIXCHECK
292 # if HAVE_RAW_DECL_STPNCPY
293 _GL_WARN_ON_USE (stpncpy
, "stpncpy is unportable - "
294 "use gnulib module stpncpy for portability");
298 #if defined GNULIB_POSIXCHECK
299 /* strchr() does not work with multibyte strings if the locale encoding is
300 GB18030 and the character to be searched is a digit. */
302 /* Assume strchr is always declared. */
303 _GL_WARN_ON_USE (strchr
, "strchr cannot work correctly on character strings "
304 "in some multibyte locales - "
305 "use mbschr if you care about internationalization");
308 /* Find the first occurrence of C in S or the final NUL byte. */
309 #if @GNULIB_STRCHRNUL@
310 # if @REPLACE_STRCHRNUL@
311 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
312 # define strchrnul rpl_strchrnul
314 _GL_FUNCDECL_RPL (strchrnul
, char *, (const char *__s
, int __c_in
)
316 _GL_ARG_NONNULL ((1)));
317 _GL_CXXALIAS_RPL (strchrnul
, char *,
318 (const char *str
, int ch
));
320 # if ! @HAVE_STRCHRNUL@
321 _GL_FUNCDECL_SYS (strchrnul
, char *, (char const *__s
, int __c_in
)
323 _GL_ARG_NONNULL ((1)));
325 /* On some systems, this function is defined as an overloaded function:
326 extern "C++" { const char * std::strchrnul (const char *, int); }
327 extern "C++" { char * std::strchrnul (char *, int); } */
328 _GL_CXXALIAS_SYS_CAST2 (strchrnul
,
329 char *, (char const *__s
, int __c_in
),
330 char const *, (char const *__s
, int __c_in
));
332 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
333 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
334 _GL_CXXALIASWARN1 (strchrnul
, char *, (char *__s
, int __c_in
));
335 _GL_CXXALIASWARN1 (strchrnul
, char const *, (char const *__s
, int __c_in
));
337 _GL_CXXALIASWARN (strchrnul
);
339 #elif defined GNULIB_POSIXCHECK
341 # if HAVE_RAW_DECL_STRCHRNUL
342 _GL_WARN_ON_USE (strchrnul
, "strchrnul is unportable - "
343 "use gnulib module strchrnul for portability");
347 /* Duplicate S, returning an identical malloc'd string. */
349 # if @REPLACE_STRDUP@
350 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
352 # define strdup rpl_strdup
354 _GL_FUNCDECL_RPL (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
355 _GL_CXXALIAS_RPL (strdup
, char *, (char const *__s
));
357 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
358 /* strdup exists as a function and as a macro. Get rid of the macro. */
361 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
362 _GL_FUNCDECL_SYS (strdup
, char *, (char const *__s
) _GL_ARG_NONNULL ((1)));
364 _GL_CXXALIAS_SYS (strdup
, char *, (char const *__s
));
366 _GL_CXXALIASWARN (strdup
);
367 #elif defined GNULIB_POSIXCHECK
369 # if HAVE_RAW_DECL_STRDUP
370 _GL_WARN_ON_USE (strdup
, "strdup is unportable - "
371 "use gnulib module strdup for portability");
375 /* Append no more than N characters from SRC onto DEST. */
377 # if @REPLACE_STRNCAT@
378 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
380 # define strncat rpl_strncat
382 _GL_FUNCDECL_RPL (strncat
, char *, (char *dest
, const char *src
, size_t n
)
383 _GL_ARG_NONNULL ((1, 2)));
384 _GL_CXXALIAS_RPL (strncat
, char *, (char *dest
, const char *src
, size_t n
));
386 _GL_CXXALIAS_SYS (strncat
, char *, (char *dest
, const char *src
, size_t n
));
388 _GL_CXXALIASWARN (strncat
);
389 #elif defined GNULIB_POSIXCHECK
391 # if HAVE_RAW_DECL_STRNCAT
392 _GL_WARN_ON_USE (strncat
, "strncat is unportable - "
393 "use gnulib module strncat for portability");
397 /* Return a newly allocated copy of at most N bytes of STRING. */
399 # if @REPLACE_STRNDUP@
400 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
402 # define strndup rpl_strndup
404 _GL_FUNCDECL_RPL (strndup
, char *, (char const *__string
, size_t __n
)
405 _GL_ARG_NONNULL ((1)));
406 _GL_CXXALIAS_RPL (strndup
, char *, (char const *__string
, size_t __n
));
408 # if ! @HAVE_DECL_STRNDUP@
409 _GL_FUNCDECL_SYS (strndup
, char *, (char const *__string
, size_t __n
)
410 _GL_ARG_NONNULL ((1)));
412 _GL_CXXALIAS_SYS (strndup
, char *, (char const *__string
, size_t __n
));
414 _GL_CXXALIASWARN (strndup
);
415 #elif defined GNULIB_POSIXCHECK
417 # if HAVE_RAW_DECL_STRNDUP
418 _GL_WARN_ON_USE (strndup
, "strndup is unportable - "
419 "use gnulib module strndup for portability");
423 /* Find the length (number of bytes) of STRING, but scan at most
424 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
427 # if @REPLACE_STRNLEN@
428 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
430 # define strnlen rpl_strnlen
432 _GL_FUNCDECL_RPL (strnlen
, size_t, (char const *__string
, size_t __maxlen
)
434 _GL_ARG_NONNULL ((1)));
435 _GL_CXXALIAS_RPL (strnlen
, size_t, (char const *__string
, size_t __maxlen
));
437 # if ! @HAVE_DECL_STRNLEN@
438 _GL_FUNCDECL_SYS (strnlen
, size_t, (char const *__string
, size_t __maxlen
)
440 _GL_ARG_NONNULL ((1)));
442 _GL_CXXALIAS_SYS (strnlen
, size_t, (char const *__string
, size_t __maxlen
));
444 _GL_CXXALIASWARN (strnlen
);
445 #elif defined GNULIB_POSIXCHECK
447 # if HAVE_RAW_DECL_STRNLEN
448 _GL_WARN_ON_USE (strnlen
, "strnlen is unportable - "
449 "use gnulib module strnlen for portability");
453 #if defined GNULIB_POSIXCHECK
454 /* strcspn() assumes the second argument is a list of single-byte characters.
455 Even in this simple case, it does not work with multibyte strings if the
456 locale encoding is GB18030 and one of the characters to be searched is a
459 /* Assume strcspn is always declared. */
460 _GL_WARN_ON_USE (strcspn
, "strcspn cannot work correctly on character strings "
461 "in multibyte locales - "
462 "use mbscspn if you care about internationalization");
465 /* Find the first occurrence in S of any character in ACCEPT. */
467 # if ! @HAVE_STRPBRK@
468 _GL_FUNCDECL_SYS (strpbrk
, char *, (char const *__s
, char const *__accept
)
470 _GL_ARG_NONNULL ((1, 2)));
472 /* On some systems, this function is defined as an overloaded function:
473 extern "C" { const char * strpbrk (const char *, const char *); }
474 extern "C++" { char * strpbrk (char *, const char *); } */
475 _GL_CXXALIAS_SYS_CAST2 (strpbrk
,
476 char *, (char const *__s
, char const *__accept
),
477 const char *, (char const *__s
, char const *__accept
));
478 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
479 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
480 _GL_CXXALIASWARN1 (strpbrk
, char *, (char *__s
, char const *__accept
));
481 _GL_CXXALIASWARN1 (strpbrk
, char const *,
482 (char const *__s
, char const *__accept
));
484 _GL_CXXALIASWARN (strpbrk
);
486 # if defined GNULIB_POSIXCHECK
487 /* strpbrk() assumes the second argument is a list of single-byte characters.
488 Even in this simple case, it does not work with multibyte strings if the
489 locale encoding is GB18030 and one of the characters to be searched is a
492 _GL_WARN_ON_USE (strpbrk
, "strpbrk cannot work correctly on character strings "
493 "in multibyte locales - "
494 "use mbspbrk if you care about internationalization");
496 #elif defined GNULIB_POSIXCHECK
498 # if HAVE_RAW_DECL_STRPBRK
499 _GL_WARN_ON_USE (strpbrk
, "strpbrk is unportable - "
500 "use gnulib module strpbrk for portability");
504 #if defined GNULIB_POSIXCHECK
505 /* strspn() assumes the second argument is a list of single-byte characters.
506 Even in this simple case, it cannot work with multibyte strings. */
508 /* Assume strspn is always declared. */
509 _GL_WARN_ON_USE (strspn
, "strspn cannot work correctly on character strings "
510 "in multibyte locales - "
511 "use mbsspn if you care about internationalization");
514 #if defined GNULIB_POSIXCHECK
515 /* strrchr() does not work with multibyte strings if the locale encoding is
516 GB18030 and the character to be searched is a digit. */
518 /* Assume strrchr is always declared. */
519 _GL_WARN_ON_USE (strrchr
, "strrchr cannot work correctly on character strings "
520 "in some multibyte locales - "
521 "use mbsrchr if you care about internationalization");
524 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
525 If one is found, overwrite it with a NUL, and advance *STRINGP
526 to point to the next char after it. Otherwise, set *STRINGP to NULL.
527 If *STRINGP was already NULL, nothing happens.
528 Return the old value of *STRINGP.
530 This is a variant of strtok() that is multithread-safe and supports
533 Caveat: It modifies the original string.
534 Caveat: These functions cannot be used on constant strings.
535 Caveat: The identity of the delimiting character is lost.
536 Caveat: It doesn't work with multibyte strings unless all of the delimiter
537 characters are ASCII characters < 0x30.
539 See also strtok_r(). */
542 _GL_FUNCDECL_SYS (strsep
, char *,
543 (char **restrict __stringp
, char const *restrict __delim
)
544 _GL_ARG_NONNULL ((1, 2)));
546 _GL_CXXALIAS_SYS (strsep
, char *,
547 (char **restrict __stringp
, char const *restrict __delim
));
548 _GL_CXXALIASWARN (strsep
);
549 # if defined GNULIB_POSIXCHECK
551 _GL_WARN_ON_USE (strsep
, "strsep cannot work correctly on character strings "
552 "in multibyte locales - "
553 "use mbssep if you care about internationalization");
555 #elif defined GNULIB_POSIXCHECK
557 # if HAVE_RAW_DECL_STRSEP
558 _GL_WARN_ON_USE (strsep
, "strsep is unportable - "
559 "use gnulib module strsep for portability");
564 # if @REPLACE_STRSTR@
565 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
566 # define strstr rpl_strstr
568 _GL_FUNCDECL_RPL (strstr
, char *, (const char *haystack
, const char *needle
)
570 _GL_ARG_NONNULL ((1, 2)));
571 _GL_CXXALIAS_RPL (strstr
, char *, (const char *haystack
, const char *needle
));
573 /* On some systems, this function is defined as an overloaded function:
574 extern "C++" { const char * strstr (const char *, const char *); }
575 extern "C++" { char * strstr (char *, const char *); } */
576 _GL_CXXALIAS_SYS_CAST2 (strstr
,
577 char *, (const char *haystack
, const char *needle
),
578 const char *, (const char *haystack
, const char *needle
));
580 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
581 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
582 _GL_CXXALIASWARN1 (strstr
, char *, (char *haystack
, const char *needle
));
583 _GL_CXXALIASWARN1 (strstr
, const char *,
584 (const char *haystack
, const char *needle
));
586 _GL_CXXALIASWARN (strstr
);
588 #elif defined GNULIB_POSIXCHECK
589 /* strstr() does not work with multibyte strings if the locale encoding is
590 different from UTF-8:
591 POSIX says that it operates on "strings", and "string" in POSIX is defined
592 as a sequence of bytes, not of characters. */
594 /* Assume strstr is always declared. */
595 _GL_WARN_ON_USE (strstr
, "strstr is quadratic on many systems, and cannot "
596 "work correctly on character strings in most "
597 "multibyte locales - "
598 "use mbsstr if you care about internationalization, "
599 "or use strstr if you care about speed");
602 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
604 #if @GNULIB_STRCASESTR@
605 # if @REPLACE_STRCASESTR@
606 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
607 # define strcasestr rpl_strcasestr
609 _GL_FUNCDECL_RPL (strcasestr
, char *,
610 (const char *haystack
, const char *needle
)
612 _GL_ARG_NONNULL ((1, 2)));
613 _GL_CXXALIAS_RPL (strcasestr
, char *,
614 (const char *haystack
, const char *needle
));
616 # if ! @HAVE_STRCASESTR@
617 _GL_FUNCDECL_SYS (strcasestr
, char *,
618 (const char *haystack
, const char *needle
)
620 _GL_ARG_NONNULL ((1, 2)));
622 /* On some systems, this function is defined as an overloaded function:
623 extern "C++" { const char * strcasestr (const char *, const char *); }
624 extern "C++" { char * strcasestr (char *, const char *); } */
625 _GL_CXXALIAS_SYS_CAST2 (strcasestr
,
626 char *, (const char *haystack
, const char *needle
),
627 const char *, (const char *haystack
, const char *needle
));
629 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
630 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
631 _GL_CXXALIASWARN1 (strcasestr
, char *, (char *haystack
, const char *needle
));
632 _GL_CXXALIASWARN1 (strcasestr
, const char *,
633 (const char *haystack
, const char *needle
));
635 _GL_CXXALIASWARN (strcasestr
);
637 #elif defined GNULIB_POSIXCHECK
638 /* strcasestr() does not work with multibyte strings:
639 It is a glibc extension, and glibc implements it only for unibyte
642 # if HAVE_RAW_DECL_STRCASESTR
643 _GL_WARN_ON_USE (strcasestr
, "strcasestr does work correctly on character "
644 "strings in multibyte locales - "
645 "use mbscasestr if you care about "
646 "internationalization, or use c-strcasestr if you want "
647 "a locale independent function");
651 /* Parse S into tokens separated by characters in DELIM.
652 If S is NULL, the saved pointer in SAVE_PTR is used as
653 the next starting point. For example:
654 char s[] = "-abc-=-def";
656 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
657 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
658 x = strtok_r(NULL, "=", &sp); // x = NULL
661 This is a variant of strtok() that is multithread-safe.
663 For the POSIX documentation for this function, see:
664 http://www.opengroup.org/susv3xsh/strtok.html
666 Caveat: It modifies the original string.
667 Caveat: These functions cannot be used on constant strings.
668 Caveat: The identity of the delimiting character is lost.
669 Caveat: It doesn't work with multibyte strings unless all of the delimiter
670 characters are ASCII characters < 0x30.
672 See also strsep(). */
673 #if @GNULIB_STRTOK_R@
674 # if @REPLACE_STRTOK_R@
675 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
677 # define strtok_r rpl_strtok_r
679 _GL_FUNCDECL_RPL (strtok_r
, char *,
680 (char *restrict s
, char const *restrict delim
,
681 char **restrict save_ptr
)
682 _GL_ARG_NONNULL ((2, 3)));
683 _GL_CXXALIAS_RPL (strtok_r
, char *,
684 (char *restrict s
, char const *restrict delim
,
685 char **restrict save_ptr
));
687 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
690 # if ! @HAVE_DECL_STRTOK_R@
691 _GL_FUNCDECL_SYS (strtok_r
, char *,
692 (char *restrict s
, char const *restrict delim
,
693 char **restrict save_ptr
)
694 _GL_ARG_NONNULL ((2, 3)));
696 _GL_CXXALIAS_SYS (strtok_r
, char *,
697 (char *restrict s
, char const *restrict delim
,
698 char **restrict save_ptr
));
700 _GL_CXXALIASWARN (strtok_r
);
701 # if defined GNULIB_POSIXCHECK
702 _GL_WARN_ON_USE (strtok_r
, "strtok_r cannot work correctly on character "
703 "strings in multibyte locales - "
704 "use mbstok_r if you care about internationalization");
706 #elif defined GNULIB_POSIXCHECK
708 # if HAVE_RAW_DECL_STRTOK_R
709 _GL_WARN_ON_USE (strtok_r
, "strtok_r is unportable - "
710 "use gnulib module strtok_r for portability");
715 /* The following functions are not specified by POSIX. They are gnulib
719 /* Return the number of multibyte characters in the character string STRING.
720 This considers multibyte characters, unlike strlen, which counts bytes. */
721 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
724 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
725 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
726 # define mbslen rpl_mbslen
728 _GL_FUNCDECL_RPL (mbslen
, size_t, (const char *string
) _GL_ARG_NONNULL ((1)));
729 _GL_CXXALIAS_RPL (mbslen
, size_t, (const char *string
));
731 _GL_FUNCDECL_SYS (mbslen
, size_t, (const char *string
) _GL_ARG_NONNULL ((1)));
732 _GL_CXXALIAS_SYS (mbslen
, size_t, (const char *string
));
734 _GL_CXXALIASWARN (mbslen
);
738 /* Return the number of multibyte characters in the character string starting
739 at STRING and ending at STRING + LEN. */
740 _GL_EXTERN_C
size_t mbsnlen (const char *string
, size_t len
)
741 _GL_ARG_NONNULL ((1));
745 /* Locate the first single-byte character C in the character string STRING,
746 and return a pointer to it. Return NULL if C is not found in STRING.
747 Unlike strchr(), this function works correctly in multibyte locales with
748 encodings such as GB18030. */
750 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
751 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
753 _GL_FUNCDECL_RPL (mbschr
, char *, (const char *string
, int c
)
754 _GL_ARG_NONNULL ((1)));
755 _GL_CXXALIAS_RPL (mbschr
, char *, (const char *string
, int c
));
757 _GL_FUNCDECL_SYS (mbschr
, char *, (const char *string
, int c
)
758 _GL_ARG_NONNULL ((1)));
759 _GL_CXXALIAS_SYS (mbschr
, char *, (const char *string
, int c
));
761 _GL_CXXALIASWARN (mbschr
);
765 /* Locate the last single-byte character C in the character string STRING,
766 and return a pointer to it. Return NULL if C is not found in STRING.
767 Unlike strrchr(), this function works correctly in multibyte locales with
768 encodings such as GB18030. */
769 # if defined __hpux || defined __INTERIX
770 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
771 # define mbsrchr rpl_mbsrchr /* avoid collision with system function */
773 _GL_FUNCDECL_RPL (mbsrchr
, char *, (const char *string
, int c
)
774 _GL_ARG_NONNULL ((1)));
775 _GL_CXXALIAS_RPL (mbsrchr
, char *, (const char *string
, int c
));
777 _GL_FUNCDECL_SYS (mbsrchr
, char *, (const char *string
, int c
)
778 _GL_ARG_NONNULL ((1)));
779 _GL_CXXALIAS_SYS (mbsrchr
, char *, (const char *string
, int c
));
781 _GL_CXXALIASWARN (mbsrchr
);
785 /* Find the first occurrence of the character string NEEDLE in the character
786 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
787 Unlike strstr(), this function works correctly in multibyte locales with
788 encodings different from UTF-8. */
789 _GL_EXTERN_C
char * mbsstr (const char *haystack
, const char *needle
)
790 _GL_ARG_NONNULL ((1, 2));
793 #if @GNULIB_MBSCASECMP@
794 /* Compare the character strings S1 and S2, ignoring case, returning less than,
795 equal to or greater than zero if S1 is lexicographically less than, equal to
797 Note: This function may, in multibyte locales, return 0 for strings of
799 Unlike strcasecmp(), this function works correctly in multibyte locales. */
800 _GL_EXTERN_C
int mbscasecmp (const char *s1
, const char *s2
)
801 _GL_ARG_NONNULL ((1, 2));
804 #if @GNULIB_MBSNCASECMP@
805 /* Compare the initial segment of the character string S1 consisting of at most
806 N characters with the initial segment of the character string S2 consisting
807 of at most N characters, ignoring case, returning less than, equal to or
808 greater than zero if the initial segment of S1 is lexicographically less
809 than, equal to or greater than the initial segment of S2.
810 Note: This function may, in multibyte locales, return 0 for initial segments
811 of different lengths!
812 Unlike strncasecmp(), this function works correctly in multibyte locales.
813 But beware that N is not a byte count but a character count! */
814 _GL_EXTERN_C
int mbsncasecmp (const char *s1
, const char *s2
, size_t n
)
815 _GL_ARG_NONNULL ((1, 2));
818 #if @GNULIB_MBSPCASECMP@
819 /* Compare the initial segment of the character string STRING consisting of
820 at most mbslen (PREFIX) characters with the character string PREFIX,
821 ignoring case. If the two match, return a pointer to the first byte
822 after this prefix in STRING. Otherwise, return NULL.
823 Note: This function may, in multibyte locales, return non-NULL if STRING
824 is of smaller length than PREFIX!
825 Unlike strncasecmp(), this function works correctly in multibyte
827 _GL_EXTERN_C
char * mbspcasecmp (const char *string
, const char *prefix
)
828 _GL_ARG_NONNULL ((1, 2));
831 #if @GNULIB_MBSCASESTR@
832 /* Find the first occurrence of the character string NEEDLE in the character
833 string HAYSTACK, using case-insensitive comparison.
834 Note: This function may, in multibyte locales, return success even if
835 strlen (haystack) < strlen (needle) !
836 Unlike strcasestr(), this function works correctly in multibyte locales. */
837 _GL_EXTERN_C
char * mbscasestr (const char *haystack
, const char *needle
)
838 _GL_ARG_NONNULL ((1, 2));
842 /* Find the first occurrence in the character string STRING of any character
843 in the character string ACCEPT. Return the number of bytes from the
844 beginning of the string to this occurrence, or to the end of the string
846 Unlike strcspn(), this function works correctly in multibyte locales. */
847 _GL_EXTERN_C
size_t mbscspn (const char *string
, const char *accept
)
848 _GL_ARG_NONNULL ((1, 2));
852 /* Find the first occurrence in the character string STRING of any character
853 in the character string ACCEPT. Return the pointer to it, or NULL if none
855 Unlike strpbrk(), this function works correctly in multibyte locales. */
857 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
858 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
860 _GL_FUNCDECL_RPL (mbspbrk
, char *, (const char *string
, const char *accept
)
861 _GL_ARG_NONNULL ((1, 2)));
862 _GL_CXXALIAS_RPL (mbspbrk
, char *, (const char *string
, const char *accept
));
864 _GL_FUNCDECL_SYS (mbspbrk
, char *, (const char *string
, const char *accept
)
865 _GL_ARG_NONNULL ((1, 2)));
866 _GL_CXXALIAS_SYS (mbspbrk
, char *, (const char *string
, const char *accept
));
868 _GL_CXXALIASWARN (mbspbrk
);
872 /* Find the first occurrence in the character string STRING of any character
873 not in the character string REJECT. Return the number of bytes from the
874 beginning of the string to this occurrence, or to the end of the string
876 Unlike strspn(), this function works correctly in multibyte locales. */
877 _GL_EXTERN_C
size_t mbsspn (const char *string
, const char *reject
)
878 _GL_ARG_NONNULL ((1, 2));
882 /* Search the next delimiter (multibyte character listed in the character
883 string DELIM) starting at the character string *STRINGP.
884 If one is found, overwrite it with a NUL, and advance *STRINGP to point
885 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
886 If *STRINGP was already NULL, nothing happens.
887 Return the old value of *STRINGP.
889 This is a variant of mbstok_r() that supports empty fields.
891 Caveat: It modifies the original string.
892 Caveat: These functions cannot be used on constant strings.
893 Caveat: The identity of the delimiting character is lost.
895 See also mbstok_r(). */
896 _GL_EXTERN_C
char * mbssep (char **stringp
, const char *delim
)
897 _GL_ARG_NONNULL ((1, 2));
900 #if @GNULIB_MBSTOK_R@
901 /* Parse the character string STRING into tokens separated by characters in
902 the character string DELIM.
903 If STRING is NULL, the saved pointer in SAVE_PTR is used as
904 the next starting point. For example:
905 char s[] = "-abc-=-def";
907 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
908 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
909 x = mbstok_r(NULL, "=", &sp); // x = NULL
912 Caveat: It modifies the original string.
913 Caveat: These functions cannot be used on constant strings.
914 Caveat: The identity of the delimiting character is lost.
916 See also mbssep(). */
917 _GL_EXTERN_C
char * mbstok_r (char *string
, const char *delim
, char **save_ptr
)
918 _GL_ARG_NONNULL ((2, 3));
921 /* Map any int, typically from errno, into an error message. */
922 #if @GNULIB_STRERROR@
923 # if @REPLACE_STRERROR@
924 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
926 # define strerror rpl_strerror
928 _GL_FUNCDECL_RPL (strerror
, char *, (int));
929 _GL_CXXALIAS_RPL (strerror
, char *, (int));
931 _GL_CXXALIAS_SYS (strerror
, char *, (int));
933 _GL_CXXALIASWARN (strerror
);
934 #elif defined GNULIB_POSIXCHECK
936 /* Assume strerror is always declared. */
937 _GL_WARN_ON_USE (strerror
, "strerror is unportable - "
938 "use gnulib module strerror to guarantee non-NULL result");
941 /* Map any int, typically from errno, into an error message. Multithread-safe.
942 Uses the POSIX declaration, not the glibc declaration. */
943 #if @GNULIB_STRERROR_R@
944 # if @REPLACE_STRERROR_R@
945 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
947 # define strerror_r rpl_strerror_r
949 _GL_FUNCDECL_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
950 _GL_ARG_NONNULL ((2)));
951 _GL_CXXALIAS_RPL (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
953 # if !@HAVE_DECL_STRERROR_R@
954 _GL_FUNCDECL_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
)
955 _GL_ARG_NONNULL ((2)));
957 _GL_CXXALIAS_SYS (strerror_r
, int, (int errnum
, char *buf
, size_t buflen
));
959 # if @HAVE_DECL_STRERROR_R@
960 _GL_CXXALIASWARN (strerror_r
);
962 #elif defined GNULIB_POSIXCHECK
964 # if HAVE_RAW_DECL_STRERROR_R
965 _GL_WARN_ON_USE (strerror_r
, "strerror_r is unportable - "
966 "use gnulib module strerror_r-posix for portability");
970 #if @GNULIB_STRSIGNAL@
971 # if @REPLACE_STRSIGNAL@
972 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
973 # define strsignal rpl_strsignal
975 _GL_FUNCDECL_RPL (strsignal
, char *, (int __sig
));
976 _GL_CXXALIAS_RPL (strsignal
, char *, (int __sig
));
978 # if ! @HAVE_DECL_STRSIGNAL@
979 _GL_FUNCDECL_SYS (strsignal
, char *, (int __sig
));
981 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
983 _GL_CXXALIAS_SYS_CAST (strsignal
, char *, (int __sig
));
985 _GL_CXXALIASWARN (strsignal
);
986 #elif defined GNULIB_POSIXCHECK
988 # if HAVE_RAW_DECL_STRSIGNAL
989 _GL_WARN_ON_USE (strsignal
, "strsignal is unportable - "
990 "use gnulib module strsignal for portability");
994 #if @GNULIB_STRVERSCMP@
995 # if !@HAVE_STRVERSCMP@
996 _GL_FUNCDECL_SYS (strverscmp
, int, (const char *, const char *)
997 _GL_ARG_NONNULL ((1, 2)));
999 _GL_CXXALIAS_SYS (strverscmp
, int, (const char *, const char *));
1000 _GL_CXXALIASWARN (strverscmp
);
1001 #elif defined GNULIB_POSIXCHECK
1003 # if HAVE_RAW_DECL_STRVERSCMP
1004 _GL_WARN_ON_USE (strverscmp
, "strverscmp is unportable - "
1005 "use gnulib module strverscmp for portability");
1010 #endif /* _@GUARD_PREFIX@_STRING_H */
1011 #endif /* _@GUARD_PREFIX@_STRING_H */