1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2021 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, see <https://www.gnu.org/licenses/>. */
19 @PRAGMA_SYSTEM_HEADER@
23 #if defined _GL_ALREADY_INCLUDING_STRING_H
24 /* Special invocation convention:
25 - On OS X/NetBSD we have a sequence of nested includes
26 <string.h> -> <strings.h> -> "string.h"
27 In this situation system _chk variants due to -D_FORTIFY_SOURCE
28 might be used after any replacements defined here. */
30 #@INCLUDE_NEXT@ @NEXT_STRING_H@
33 /* Normal invocation convention. */
35 #ifndef _@GUARD_PREFIX@_STRING_H
37 #define _GL_ALREADY_INCLUDING_STRING_H
39 /* The include_next requires a split double-inclusion guard. */
40 #@INCLUDE_NEXT@ @NEXT_STRING_H@
42 #undef _GL_ALREADY_INCLUDING_STRING_H
44 #ifndef _@GUARD_PREFIX@_STRING_H
45 #define _@GUARD_PREFIX@_STRING_H
47 /* NetBSD 5.0 mis-defines NULL. */
50 /* MirBSD defines mbslen as a macro. */
51 #if @GNULIB_MBSLEN@ && defined __MirBSD__
55 /* The __attribute__ feature is available in gcc versions 2.5 and later.
56 The attribute __pure__ was added in gcc 2.96. */
57 #ifndef _GL_ATTRIBUTE_PURE
58 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) || defined __clang__
59 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
61 # define _GL_ATTRIBUTE_PURE /* empty */
65 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
66 /* But in any case avoid namespace pollution on glibc systems. */
67 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
68 && ! defined __GLIBC__
72 /* AIX 7.2 declares ffsl and ffsll in <strings.h>, not in <string.h>. */
73 /* But in any case avoid namespace pollution on glibc systems. */
74 #if ((@GNULIB_FFSL@ || @GNULIB_FFSLL@ || defined GNULIB_POSIXCHECK) \
76 && ! defined __GLIBC__
80 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
82 /* The definition of _GL_ARG_NONNULL is copied here. */
84 /* The definition of _GL_WARN_ON_USE is copied here. */
87 /* Clear a block of memory. The compiler will not delete a call to
88 this function, even if the block is dead after the call. */
89 #if @GNULIB_EXPLICIT_BZERO@
90 # if ! @HAVE_EXPLICIT_BZERO@
91 _GL_FUNCDECL_SYS (explicit_bzero, void,
92 (void *__dest, size_t __n) _GL_ARG_NONNULL ((1)));
94 _GL_CXXALIAS_SYS (explicit_bzero, void, (void *__dest, size_t __n));
95 _GL_CXXALIASWARN (explicit_bzero);
96 #elif defined GNULIB_POSIXCHECK
97 # undef explicit_bzero
98 # if HAVE_RAW_DECL_EXPLICIT_BZERO
99 _GL_WARN_ON_USE (explicit_bzero, "explicit_bzero is unportable - "
100 "use gnulib module explicit_bzero for portability");
104 /* Find the index of the least-significant set bit. */
107 _GL_FUNCDECL_SYS (ffsl, int, (long int i));
109 _GL_CXXALIAS_SYS (ffsl, int, (long int i));
110 _GL_CXXALIASWARN (ffsl);
111 #elif defined GNULIB_POSIXCHECK
113 # if HAVE_RAW_DECL_FFSL
114 _GL_WARN_ON_USE (ffsl, "ffsl is not portable - use the ffsl module");
119 /* Find the index of the least-significant set bit. */
122 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
123 # define ffsll rpl_ffsll
125 _GL_FUNCDECL_RPL (ffsll, int, (long long int i));
126 _GL_CXXALIAS_RPL (ffsll, int, (long long int i));
129 _GL_FUNCDECL_SYS (ffsll, int, (long long int i));
131 _GL_CXXALIAS_SYS (ffsll, int, (long long int i));
133 _GL_CXXALIASWARN (ffsll);
134 #elif defined GNULIB_POSIXCHECK
136 # if HAVE_RAW_DECL_FFSLL
137 _GL_WARN_ON_USE (ffsll, "ffsll is not portable - use the ffsll module");
142 #if @GNULIB_MDA_MEMCCPY@
143 /* On native Windows, map 'memccpy' to '_memccpy', so that -loldnames is not
144 required. In C++ with GNULIB_NAMESPACE, avoid differences between
145 platforms by defining GNULIB_NAMESPACE::memccpy always. */
146 # if defined _WIN32 && !defined __CYGWIN__
147 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
149 # define memccpy _memccpy
151 _GL_CXXALIAS_MDA (memccpy, void *,
152 (void *dest, const void *src, int c, size_t n));
154 _GL_CXXALIAS_SYS (memccpy, void *,
155 (void *dest, const void *src, int c, size_t n));
157 _GL_CXXALIASWARN (memccpy);
161 /* Return the first instance of C within N bytes of S, or NULL. */
163 # if @REPLACE_MEMCHR@
164 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
166 # define memchr rpl_memchr
168 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
170 _GL_ARG_NONNULL ((1)));
171 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
173 /* On some systems, this function is defined as an overloaded function:
174 extern "C" { const void * std::memchr (const void *, int, size_t); }
175 extern "C++" { void * std::memchr (void *, int, size_t); } */
176 _GL_CXXALIAS_SYS_CAST2 (memchr,
177 void *, (void const *__s, int __c, size_t __n),
178 void const *, (void const *__s, int __c, size_t __n));
180 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
181 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
182 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
183 _GL_CXXALIASWARN1 (memchr, void const *,
184 (void const *__s, int __c, size_t __n));
185 # elif __GLIBC__ >= 2
186 _GL_CXXALIASWARN (memchr);
188 #elif defined GNULIB_POSIXCHECK
190 /* Assume memchr is always declared. */
191 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
192 "use gnulib module memchr for portability" );
195 /* Return the first occurrence of NEEDLE in HAYSTACK. */
197 # if @REPLACE_MEMMEM@
198 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
199 # define memmem rpl_memmem
201 _GL_FUNCDECL_RPL (memmem, void *,
202 (void const *__haystack, size_t __haystack_len,
203 void const *__needle, size_t __needle_len)
205 _GL_ARG_NONNULL ((1, 3)));
206 _GL_CXXALIAS_RPL (memmem, void *,
207 (void const *__haystack, size_t __haystack_len,
208 void const *__needle, size_t __needle_len));
210 # if ! @HAVE_DECL_MEMMEM@
211 _GL_FUNCDECL_SYS (memmem, void *,
212 (void const *__haystack, size_t __haystack_len,
213 void const *__needle, size_t __needle_len)
215 _GL_ARG_NONNULL ((1, 3)));
217 _GL_CXXALIAS_SYS (memmem, void *,
218 (void const *__haystack, size_t __haystack_len,
219 void const *__needle, size_t __needle_len));
221 _GL_CXXALIASWARN (memmem);
222 #elif defined GNULIB_POSIXCHECK
224 # if HAVE_RAW_DECL_MEMMEM
225 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
226 "use gnulib module memmem-simple for portability, "
227 "and module memmem for speed" );
231 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
232 last written byte. */
234 # if ! @HAVE_MEMPCPY@
235 _GL_FUNCDECL_SYS (mempcpy, void *,
236 (void *restrict __dest, void const *restrict __src,
238 _GL_ARG_NONNULL ((1, 2)));
240 _GL_CXXALIAS_SYS (mempcpy, void *,
241 (void *restrict __dest, void const *restrict __src,
243 _GL_CXXALIASWARN (mempcpy);
244 #elif defined GNULIB_POSIXCHECK
246 # if HAVE_RAW_DECL_MEMPCPY
247 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
248 "use gnulib module mempcpy for portability");
252 /* Search backwards through a block for a byte (specified as an int). */
254 # if ! @HAVE_DECL_MEMRCHR@
255 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
257 _GL_ARG_NONNULL ((1)));
259 /* On some systems, this function is defined as an overloaded function:
260 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
261 extern "C++" { void * std::memrchr (void *, int, size_t); } */
262 _GL_CXXALIAS_SYS_CAST2 (memrchr,
263 void *, (void const *, int, size_t),
264 void const *, (void const *, int, size_t));
265 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
266 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
267 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
268 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
270 _GL_CXXALIASWARN (memrchr);
272 #elif defined GNULIB_POSIXCHECK
274 # if HAVE_RAW_DECL_MEMRCHR
275 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
276 "use gnulib module memrchr for portability");
280 /* Find the first occurrence of C in S. More efficient than
281 memchr(S,C,N), at the expense of undefined behavior if C does not
282 occur within N bytes. */
283 #if @GNULIB_RAWMEMCHR@
284 # if ! @HAVE_RAWMEMCHR@
285 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
287 _GL_ARG_NONNULL ((1)));
289 /* On some systems, this function is defined as an overloaded function:
290 extern "C++" { const void * std::rawmemchr (const void *, int); }
291 extern "C++" { void * std::rawmemchr (void *, int); } */
292 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
293 void *, (void const *__s, int __c_in),
294 void const *, (void const *__s, int __c_in));
295 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
296 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
297 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
298 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
300 _GL_CXXALIASWARN (rawmemchr);
302 #elif defined GNULIB_POSIXCHECK
304 # if HAVE_RAW_DECL_RAWMEMCHR
305 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
306 "use gnulib module rawmemchr for portability");
310 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
313 _GL_FUNCDECL_SYS (stpcpy, char *,
314 (char *restrict __dst, char const *restrict __src)
315 _GL_ARG_NONNULL ((1, 2)));
317 _GL_CXXALIAS_SYS (stpcpy, char *,
318 (char *restrict __dst, char const *restrict __src));
319 _GL_CXXALIASWARN (stpcpy);
320 #elif defined GNULIB_POSIXCHECK
322 # if HAVE_RAW_DECL_STPCPY
323 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
324 "use gnulib module stpcpy for portability");
328 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
329 last non-NUL byte written into DST. */
331 # if @REPLACE_STPNCPY@
332 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
334 # define stpncpy rpl_stpncpy
336 _GL_FUNCDECL_RPL (stpncpy, char *,
337 (char *restrict __dst, char const *restrict __src,
339 _GL_ARG_NONNULL ((1, 2)));
340 _GL_CXXALIAS_RPL (stpncpy, char *,
341 (char *restrict __dst, char const *restrict __src,
344 # if ! @HAVE_STPNCPY@
345 _GL_FUNCDECL_SYS (stpncpy, char *,
346 (char *restrict __dst, char const *restrict __src,
348 _GL_ARG_NONNULL ((1, 2)));
350 _GL_CXXALIAS_SYS (stpncpy, char *,
351 (char *restrict __dst, char const *restrict __src,
354 _GL_CXXALIASWARN (stpncpy);
355 #elif defined GNULIB_POSIXCHECK
357 # if HAVE_RAW_DECL_STPNCPY
358 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
359 "use gnulib module stpncpy for portability");
363 #if defined GNULIB_POSIXCHECK
364 /* strchr() does not work with multibyte strings if the locale encoding is
365 GB18030 and the character to be searched is a digit. */
367 /* Assume strchr is always declared. */
368 _GL_WARN_ON_USE_CXX (strchr,
369 const char *, char *, (const char *, int),
370 "strchr cannot work correctly on character strings "
371 "in some multibyte locales - "
372 "use mbschr if you care about internationalization");
375 /* Find the first occurrence of C in S or the final NUL byte. */
376 #if @GNULIB_STRCHRNUL@
377 # if @REPLACE_STRCHRNUL@
378 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
379 # define strchrnul rpl_strchrnul
381 _GL_FUNCDECL_RPL (strchrnul, char *, (const char *__s, int __c_in)
383 _GL_ARG_NONNULL ((1)));
384 _GL_CXXALIAS_RPL (strchrnul, char *,
385 (const char *str, int ch));
387 # if ! @HAVE_STRCHRNUL@
388 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
390 _GL_ARG_NONNULL ((1)));
392 /* On some systems, this function is defined as an overloaded function:
393 extern "C++" { const char * std::strchrnul (const char *, int); }
394 extern "C++" { char * std::strchrnul (char *, int); } */
395 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
396 char *, (char const *__s, int __c_in),
397 char const *, (char const *__s, int __c_in));
399 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
400 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
401 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
402 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
404 _GL_CXXALIASWARN (strchrnul);
406 #elif defined GNULIB_POSIXCHECK
408 # if HAVE_RAW_DECL_STRCHRNUL
409 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
410 "use gnulib module strchrnul for portability");
414 /* Duplicate S, returning an identical malloc'd string. */
416 # if @REPLACE_STRDUP@
417 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
419 # define strdup rpl_strdup
421 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
422 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
423 # elif defined _WIN32 && !defined __CYGWIN__
424 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
426 # define strdup _strdup
428 _GL_CXXALIAS_MDA (strdup, char *, (char const *__s));
430 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
431 /* strdup exists as a function and as a macro. Get rid of the macro. */
434 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
435 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
437 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
439 _GL_CXXALIASWARN (strdup);
440 #elif defined GNULIB_POSIXCHECK
442 # if HAVE_RAW_DECL_STRDUP
443 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
444 "use gnulib module strdup for portability");
446 #elif @GNULIB_MDA_STRDUP@
447 /* On native Windows, map 'creat' to '_creat', so that -loldnames is not
448 required. In C++ with GNULIB_NAMESPACE, avoid differences between
449 platforms by defining GNULIB_NAMESPACE::creat always. */
450 # if defined _WIN32 && !defined __CYGWIN__
451 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
453 # define strdup _strdup
455 _GL_CXXALIAS_MDA (strdup, char *, (char const *__s));
457 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
460 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
462 _GL_CXXALIASWARN (strdup);
465 /* Append no more than N characters from SRC onto DEST. */
467 # if @REPLACE_STRNCAT@
468 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
470 # define strncat rpl_strncat
472 _GL_FUNCDECL_RPL (strncat, char *,
473 (char *restrict dest, const char *restrict src, size_t n)
474 _GL_ARG_NONNULL ((1, 2)));
475 _GL_CXXALIAS_RPL (strncat, char *,
476 (char *restrict dest, const char *restrict src, size_t n));
478 _GL_CXXALIAS_SYS (strncat, char *,
479 (char *restrict dest, const char *restrict src, size_t n));
482 _GL_CXXALIASWARN (strncat);
484 #elif defined GNULIB_POSIXCHECK
486 # if HAVE_RAW_DECL_STRNCAT
487 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
488 "use gnulib module strncat for portability");
492 /* Return a newly allocated copy of at most N bytes of STRING. */
494 # if @REPLACE_STRNDUP@
495 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
497 # define strndup rpl_strndup
499 _GL_FUNCDECL_RPL (strndup, char *, (char const *__s, size_t __n)
500 _GL_ARG_NONNULL ((1)));
501 _GL_CXXALIAS_RPL (strndup, char *, (char const *__s, size_t __n));
503 # if ! @HAVE_DECL_STRNDUP@
504 _GL_FUNCDECL_SYS (strndup, char *, (char const *__s, size_t __n)
505 _GL_ARG_NONNULL ((1)));
507 _GL_CXXALIAS_SYS (strndup, char *, (char const *__s, size_t __n));
509 _GL_CXXALIASWARN (strndup);
510 #elif defined GNULIB_POSIXCHECK
512 # if HAVE_RAW_DECL_STRNDUP
513 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
514 "use gnulib module strndup for portability");
518 /* Find the length (number of bytes) of STRING, but scan at most
519 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
522 # if @REPLACE_STRNLEN@
523 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
525 # define strnlen rpl_strnlen
527 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__s, size_t __maxlen)
529 _GL_ARG_NONNULL ((1)));
530 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__s, size_t __maxlen));
532 # if ! @HAVE_DECL_STRNLEN@
533 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__s, size_t __maxlen)
535 _GL_ARG_NONNULL ((1)));
537 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__s, size_t __maxlen));
539 _GL_CXXALIASWARN (strnlen);
540 #elif defined GNULIB_POSIXCHECK
542 # if HAVE_RAW_DECL_STRNLEN
543 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
544 "use gnulib module strnlen for portability");
548 #if defined GNULIB_POSIXCHECK
549 /* strcspn() assumes the second argument is a list of single-byte characters.
550 Even in this simple case, it does not work with multibyte strings if the
551 locale encoding is GB18030 and one of the characters to be searched is a
554 /* Assume strcspn is always declared. */
555 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
556 "in multibyte locales - "
557 "use mbscspn if you care about internationalization");
560 /* Find the first occurrence in S of any character in ACCEPT. */
562 # if ! @HAVE_STRPBRK@
563 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
565 _GL_ARG_NONNULL ((1, 2)));
567 /* On some systems, this function is defined as an overloaded function:
568 extern "C" { const char * strpbrk (const char *, const char *); }
569 extern "C++" { char * strpbrk (char *, const char *); } */
570 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
571 char *, (char const *__s, char const *__accept),
572 const char *, (char const *__s, char const *__accept));
573 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
574 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
575 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
576 _GL_CXXALIASWARN1 (strpbrk, char const *,
577 (char const *__s, char const *__accept));
578 # elif __GLIBC__ >= 2
579 _GL_CXXALIASWARN (strpbrk);
581 # if defined GNULIB_POSIXCHECK
582 /* strpbrk() assumes the second argument is a list of single-byte characters.
583 Even in this simple case, it does not work with multibyte strings if the
584 locale encoding is GB18030 and one of the characters to be searched is a
587 _GL_WARN_ON_USE_CXX (strpbrk,
588 const char *, char *, (const char *, const char *),
589 "strpbrk cannot work correctly on character strings "
590 "in multibyte locales - "
591 "use mbspbrk if you care about internationalization");
593 #elif defined GNULIB_POSIXCHECK
595 # if HAVE_RAW_DECL_STRPBRK
596 _GL_WARN_ON_USE_CXX (strpbrk,
597 const char *, char *, (const char *, const char *),
598 "strpbrk is unportable - "
599 "use gnulib module strpbrk for portability");
603 #if defined GNULIB_POSIXCHECK
604 /* strspn() assumes the second argument is a list of single-byte characters.
605 Even in this simple case, it cannot work with multibyte strings. */
607 /* Assume strspn is always declared. */
608 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
609 "in multibyte locales - "
610 "use mbsspn if you care about internationalization");
613 #if defined GNULIB_POSIXCHECK
614 /* strrchr() does not work with multibyte strings if the locale encoding is
615 GB18030 and the character to be searched is a digit. */
617 /* Assume strrchr is always declared. */
618 _GL_WARN_ON_USE_CXX (strrchr,
619 const char *, char *, (const char *, int),
620 "strrchr cannot work correctly on character strings "
621 "in some multibyte locales - "
622 "use mbsrchr if you care about internationalization");
625 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
626 If one is found, overwrite it with a NUL, and advance *STRINGP
627 to point to the next char after it. Otherwise, set *STRINGP to NULL.
628 If *STRINGP was already NULL, nothing happens.
629 Return the old value of *STRINGP.
631 This is a variant of strtok() that is multithread-safe and supports
634 Caveat: It modifies the original string.
635 Caveat: These functions cannot be used on constant strings.
636 Caveat: The identity of the delimiting character is lost.
637 Caveat: It doesn't work with multibyte strings unless all of the delimiter
638 characters are ASCII characters < 0x30.
640 See also strtok_r(). */
643 _GL_FUNCDECL_SYS (strsep, char *,
644 (char **restrict __stringp, char const *restrict __delim)
645 _GL_ARG_NONNULL ((1, 2)));
647 _GL_CXXALIAS_SYS (strsep, char *,
648 (char **restrict __stringp, char const *restrict __delim));
649 _GL_CXXALIASWARN (strsep);
650 # if defined GNULIB_POSIXCHECK
652 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
653 "in multibyte locales - "
654 "use mbssep if you care about internationalization");
656 #elif defined GNULIB_POSIXCHECK
658 # if HAVE_RAW_DECL_STRSEP
659 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
660 "use gnulib module strsep for portability");
665 # if @REPLACE_STRSTR@
666 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
667 # define strstr rpl_strstr
669 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
671 _GL_ARG_NONNULL ((1, 2)));
672 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
674 /* On some systems, this function is defined as an overloaded function:
675 extern "C++" { const char * strstr (const char *, const char *); }
676 extern "C++" { char * strstr (char *, const char *); } */
677 _GL_CXXALIAS_SYS_CAST2 (strstr,
678 char *, (const char *haystack, const char *needle),
679 const char *, (const char *haystack, const char *needle));
681 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
682 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
683 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
684 _GL_CXXALIASWARN1 (strstr, const char *,
685 (const char *haystack, const char *needle));
686 # elif __GLIBC__ >= 2
687 _GL_CXXALIASWARN (strstr);
689 #elif defined GNULIB_POSIXCHECK
690 /* strstr() does not work with multibyte strings if the locale encoding is
691 different from UTF-8:
692 POSIX says that it operates on "strings", and "string" in POSIX is defined
693 as a sequence of bytes, not of characters. */
695 /* Assume strstr is always declared. */
696 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
697 "work correctly on character strings in most "
698 "multibyte locales - "
699 "use mbsstr if you care about internationalization, "
700 "or use strstr if you care about speed");
703 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
705 #if @GNULIB_STRCASESTR@
706 # if @REPLACE_STRCASESTR@
707 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
708 # define strcasestr rpl_strcasestr
710 _GL_FUNCDECL_RPL (strcasestr, char *,
711 (const char *haystack, const char *needle)
713 _GL_ARG_NONNULL ((1, 2)));
714 _GL_CXXALIAS_RPL (strcasestr, char *,
715 (const char *haystack, const char *needle));
717 # if ! @HAVE_STRCASESTR@
718 _GL_FUNCDECL_SYS (strcasestr, char *,
719 (const char *haystack, const char *needle)
721 _GL_ARG_NONNULL ((1, 2)));
723 /* On some systems, this function is defined as an overloaded function:
724 extern "C++" { const char * strcasestr (const char *, const char *); }
725 extern "C++" { char * strcasestr (char *, const char *); } */
726 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
727 char *, (const char *haystack, const char *needle),
728 const char *, (const char *haystack, const char *needle));
730 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
731 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
732 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
733 _GL_CXXALIASWARN1 (strcasestr, const char *,
734 (const char *haystack, const char *needle));
736 _GL_CXXALIASWARN (strcasestr);
738 #elif defined GNULIB_POSIXCHECK
739 /* strcasestr() does not work with multibyte strings:
740 It is a glibc extension, and glibc implements it only for unibyte
743 # if HAVE_RAW_DECL_STRCASESTR
744 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
745 "strings in multibyte locales - "
746 "use mbscasestr if you care about "
747 "internationalization, or use c-strcasestr if you want "
748 "a locale independent function");
752 /* Parse S into tokens separated by characters in DELIM.
753 If S is NULL, the saved pointer in SAVE_PTR is used as
754 the next starting point. For example:
755 char s[] = "-abc-=-def";
757 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
758 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
759 x = strtok_r(NULL, "=", &sp); // x = NULL
762 This is a variant of strtok() that is multithread-safe.
764 For the POSIX documentation for this function, see:
765 https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtok.html
767 Caveat: It modifies the original string.
768 Caveat: These functions cannot be used on constant strings.
769 Caveat: The identity of the delimiting character is lost.
770 Caveat: It doesn't work with multibyte strings unless all of the delimiter
771 characters are ASCII characters < 0x30.
773 See also strsep(). */
774 #if @GNULIB_STRTOK_R@
775 # if @REPLACE_STRTOK_R@
776 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
778 # define strtok_r rpl_strtok_r
780 _GL_FUNCDECL_RPL (strtok_r, char *,
781 (char *restrict s, char const *restrict delim,
782 char **restrict save_ptr)
783 _GL_ARG_NONNULL ((2, 3)));
784 _GL_CXXALIAS_RPL (strtok_r, char *,
785 (char *restrict s, char const *restrict delim,
786 char **restrict save_ptr));
788 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
791 # if ! @HAVE_DECL_STRTOK_R@
792 _GL_FUNCDECL_SYS (strtok_r, char *,
793 (char *restrict s, char const *restrict delim,
794 char **restrict save_ptr)
795 _GL_ARG_NONNULL ((2, 3)));
797 _GL_CXXALIAS_SYS (strtok_r, char *,
798 (char *restrict s, char const *restrict delim,
799 char **restrict save_ptr));
801 _GL_CXXALIASWARN (strtok_r);
802 # if defined GNULIB_POSIXCHECK
803 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
804 "strings in multibyte locales - "
805 "use mbstok_r if you care about internationalization");
807 #elif defined GNULIB_POSIXCHECK
809 # if HAVE_RAW_DECL_STRTOK_R
810 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
811 "use gnulib module strtok_r for portability");
816 /* The following functions are not specified by POSIX. They are gnulib
820 /* Return the number of multibyte characters in the character string STRING.
821 This considers multibyte characters, unlike strlen, which counts bytes. */
822 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
825 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
826 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
827 # define mbslen rpl_mbslen
829 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string)
831 _GL_ARG_NONNULL ((1)));
832 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
834 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string)
836 _GL_ARG_NONNULL ((1)));
837 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
839 _GL_CXXALIASWARN (mbslen);
843 /* Return the number of multibyte characters in the character string starting
844 at STRING and ending at STRING + LEN. */
845 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
847 _GL_ARG_NONNULL ((1));
851 /* Locate the first single-byte character C in the character string STRING,
852 and return a pointer to it. Return NULL if C is not found in STRING.
853 Unlike strchr(), this function works correctly in multibyte locales with
854 encodings such as GB18030. */
856 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
857 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
859 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
861 _GL_ARG_NONNULL ((1)));
862 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
864 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
866 _GL_ARG_NONNULL ((1)));
867 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
869 _GL_CXXALIASWARN (mbschr);
873 /* Locate the last single-byte character C in the character string STRING,
874 and return a pointer to it. Return NULL if C is not found in STRING.
875 Unlike strrchr(), this function works correctly in multibyte locales with
876 encodings such as GB18030. */
877 # if defined __hpux || defined __INTERIX
878 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
879 # define mbsrchr rpl_mbsrchr /* avoid collision with system function */
881 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
883 _GL_ARG_NONNULL ((1)));
884 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
886 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
888 _GL_ARG_NONNULL ((1)));
889 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
891 _GL_CXXALIASWARN (mbsrchr);
895 /* Find the first occurrence of the character string NEEDLE in the character
896 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
897 Unlike strstr(), this function works correctly in multibyte locales with
898 encodings different from UTF-8. */
899 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
901 _GL_ARG_NONNULL ((1, 2));
904 #if @GNULIB_MBSCASECMP@
905 /* Compare the character strings S1 and S2, ignoring case, returning less than,
906 equal to or greater than zero if S1 is lexicographically less than, equal to
908 Note: This function may, in multibyte locales, return 0 for strings of
910 Unlike strcasecmp(), this function works correctly in multibyte locales. */
911 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
913 _GL_ARG_NONNULL ((1, 2));
916 #if @GNULIB_MBSNCASECMP@
917 /* Compare the initial segment of the character string S1 consisting of at most
918 N characters with the initial segment of the character string S2 consisting
919 of at most N characters, ignoring case, returning less than, equal to or
920 greater than zero if the initial segment of S1 is lexicographically less
921 than, equal to or greater than the initial segment of S2.
922 Note: This function may, in multibyte locales, return 0 for initial segments
923 of different lengths!
924 Unlike strncasecmp(), this function works correctly in multibyte locales.
925 But beware that N is not a byte count but a character count! */
926 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
928 _GL_ARG_NONNULL ((1, 2));
931 #if @GNULIB_MBSPCASECMP@
932 /* Compare the initial segment of the character string STRING consisting of
933 at most mbslen (PREFIX) characters with the character string PREFIX,
934 ignoring case. If the two match, return a pointer to the first byte
935 after this prefix in STRING. Otherwise, return NULL.
936 Note: This function may, in multibyte locales, return non-NULL if STRING
937 is of smaller length than PREFIX!
938 Unlike strncasecmp(), this function works correctly in multibyte
940 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
942 _GL_ARG_NONNULL ((1, 2));
945 #if @GNULIB_MBSCASESTR@
946 /* Find the first occurrence of the character string NEEDLE in the character
947 string HAYSTACK, using case-insensitive comparison.
948 Note: This function may, in multibyte locales, return success even if
949 strlen (haystack) < strlen (needle) !
950 Unlike strcasestr(), this function works correctly in multibyte locales. */
951 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
953 _GL_ARG_NONNULL ((1, 2));
957 /* Find the first occurrence in the character string STRING of any character
958 in the character string ACCEPT. Return the number of bytes from the
959 beginning of the string to this occurrence, or to the end of the string
961 Unlike strcspn(), this function works correctly in multibyte locales. */
962 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
964 _GL_ARG_NONNULL ((1, 2));
968 /* Find the first occurrence in the character string STRING of any character
969 in the character string ACCEPT. Return the pointer to it, or NULL if none
971 Unlike strpbrk(), this function works correctly in multibyte locales. */
973 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
974 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
976 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
978 _GL_ARG_NONNULL ((1, 2)));
979 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
981 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
983 _GL_ARG_NONNULL ((1, 2)));
984 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
986 _GL_CXXALIASWARN (mbspbrk);
990 /* Find the first occurrence in the character string STRING of any character
991 not in the character string REJECT. Return the number of bytes from the
992 beginning of the string to this occurrence, or to the end of the string
994 Unlike strspn(), this function works correctly in multibyte locales. */
995 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
997 _GL_ARG_NONNULL ((1, 2));
1001 /* Search the next delimiter (multibyte character listed in the character
1002 string DELIM) starting at the character string *STRINGP.
1003 If one is found, overwrite it with a NUL, and advance *STRINGP to point
1004 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
1005 If *STRINGP was already NULL, nothing happens.
1006 Return the old value of *STRINGP.
1008 This is a variant of mbstok_r() that supports empty fields.
1010 Caveat: It modifies the original string.
1011 Caveat: These functions cannot be used on constant strings.
1012 Caveat: The identity of the delimiting character is lost.
1014 See also mbstok_r(). */
1015 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
1016 _GL_ARG_NONNULL ((1, 2));
1019 #if @GNULIB_MBSTOK_R@
1020 /* Parse the character string STRING into tokens separated by characters in
1021 the character string DELIM.
1022 If STRING is NULL, the saved pointer in SAVE_PTR is used as
1023 the next starting point. For example:
1024 char s[] = "-abc-=-def";
1026 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
1027 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
1028 x = mbstok_r(NULL, "=", &sp); // x = NULL
1029 // s = "abc\0-def\0"
1031 Caveat: It modifies the original string.
1032 Caveat: These functions cannot be used on constant strings.
1033 Caveat: The identity of the delimiting character is lost.
1035 See also mbssep(). */
1036 _GL_EXTERN_C char * mbstok_r (char *restrict string, const char *delim,
1038 _GL_ARG_NONNULL ((2, 3));
1041 /* Map any int, typically from errno, into an error message. */
1042 #if @GNULIB_STRERROR@
1043 # if @REPLACE_STRERROR@
1044 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1046 # define strerror rpl_strerror
1048 _GL_FUNCDECL_RPL (strerror, char *, (int));
1049 _GL_CXXALIAS_RPL (strerror, char *, (int));
1051 _GL_CXXALIAS_SYS (strerror, char *, (int));
1054 _GL_CXXALIASWARN (strerror);
1056 #elif defined GNULIB_POSIXCHECK
1058 /* Assume strerror is always declared. */
1059 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
1060 "use gnulib module strerror to guarantee non-NULL result");
1063 /* Map any int, typically from errno, into an error message. Multithread-safe.
1064 Uses the POSIX declaration, not the glibc declaration. */
1065 #if @GNULIB_STRERROR_R@
1066 # if @REPLACE_STRERROR_R@
1067 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1069 # define strerror_r rpl_strerror_r
1071 _GL_FUNCDECL_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen)
1072 _GL_ARG_NONNULL ((2)));
1073 _GL_CXXALIAS_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen));
1075 # if !@HAVE_DECL_STRERROR_R@
1076 _GL_FUNCDECL_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen)
1077 _GL_ARG_NONNULL ((2)));
1079 _GL_CXXALIAS_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen));
1081 # if @HAVE_DECL_STRERROR_R@
1082 _GL_CXXALIASWARN (strerror_r);
1084 #elif defined GNULIB_POSIXCHECK
1086 # if HAVE_RAW_DECL_STRERROR_R
1087 _GL_WARN_ON_USE (strerror_r, "strerror_r is unportable - "
1088 "use gnulib module strerror_r-posix for portability");
1092 /* Return the name of the system error code ERRNUM. */
1093 #if @GNULIB_STRERRORNAME_NP@
1094 # if @REPLACE_STRERRORNAME_NP@
1095 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1096 # undef strerrorname_np
1097 # define strerrorname_np rpl_strerrorname_np
1099 _GL_FUNCDECL_RPL (strerrorname_np, const char *, (int errnum));
1100 _GL_CXXALIAS_RPL (strerrorname_np, const char *, (int errnum));
1102 # if !@HAVE_STRERRORNAME_NP@
1103 _GL_FUNCDECL_SYS (strerrorname_np, const char *, (int errnum));
1105 _GL_CXXALIAS_SYS (strerrorname_np, const char *, (int errnum));
1107 _GL_CXXALIASWARN (strerrorname_np);
1108 #elif defined GNULIB_POSIXCHECK
1109 # undef strerrorname_np
1110 # if HAVE_RAW_DECL_STRERRORNAME_NP
1111 _GL_WARN_ON_USE (strerrorname_np, "strerrorname_np is unportable - "
1112 "use gnulib module strerrorname_np for portability");
1116 /* Return an abbreviation string for the signal number SIG. */
1117 #if @GNULIB_SIGABBREV_NP@
1118 # if ! @HAVE_SIGABBREV_NP@
1119 _GL_FUNCDECL_SYS (sigabbrev_np, const char *, (int sig));
1121 _GL_CXXALIAS_SYS (sigabbrev_np, const char *, (int sig));
1122 _GL_CXXALIASWARN (sigabbrev_np);
1123 #elif defined GNULIB_POSIXCHECK
1124 # undef sigabbrev_np
1125 # if HAVE_RAW_DECL_SIGABBREV_NP
1126 _GL_WARN_ON_USE (sigabbrev_np, "sigabbrev_np is unportable - "
1127 "use gnulib module sigabbrev_np for portability");
1131 /* Return an English description string for the signal number SIG. */
1132 #if @GNULIB_SIGDESCR_NP@
1133 # if ! @HAVE_SIGDESCR_NP@
1134 _GL_FUNCDECL_SYS (sigdescr_np, const char *, (int sig));
1136 _GL_CXXALIAS_SYS (sigdescr_np, const char *, (int sig));
1137 _GL_CXXALIASWARN (sigdescr_np);
1138 #elif defined GNULIB_POSIXCHECK
1140 # if HAVE_RAW_DECL_SIGDESCR_NP
1141 _GL_WARN_ON_USE (sigdescr_np, "sigdescr_np is unportable - "
1142 "use gnulib module sigdescr_np for portability");
1146 #if @GNULIB_STRSIGNAL@
1147 # if @REPLACE_STRSIGNAL@
1148 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1149 # define strsignal rpl_strsignal
1151 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
1152 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
1154 # if ! @HAVE_DECL_STRSIGNAL@
1155 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
1157 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
1159 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
1161 _GL_CXXALIASWARN (strsignal);
1162 #elif defined GNULIB_POSIXCHECK
1164 # if HAVE_RAW_DECL_STRSIGNAL
1165 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
1166 "use gnulib module strsignal for portability");
1170 #if @GNULIB_STRVERSCMP@
1171 # if !@HAVE_STRVERSCMP@
1172 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
1174 _GL_ARG_NONNULL ((1, 2)));
1176 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
1177 _GL_CXXALIASWARN (strverscmp);
1178 #elif defined GNULIB_POSIXCHECK
1180 # if HAVE_RAW_DECL_STRVERSCMP
1181 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
1182 "use gnulib module strverscmp for portability");
1187 #endif /* _@GUARD_PREFIX@_STRING_H */
1188 #endif /* _@GUARD_PREFIX@_STRING_H */