1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 @PRAGMA_SYSTEM_HEADER@
25 /* The include_next requires a split double-inclusion guard. */
26 #@INCLUDE_NEXT@ @NEXT_STRING_H@
31 /* NetBSD 5.0 mis-defines NULL. */
34 /* MirBSD defines mbslen as a macro. */
35 #if @GNULIB_MBSLEN@ && defined __MirBSD__
40 /* This feature is available in gcc versions 2.5 and later. */
41 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
42 # define __attribute__(Spec) /* empty */
44 /* The attribute __pure__ was added in gcc 2.96. */
45 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
46 # define __pure__ /* empty */
51 /* The definition of GL_LINK_WARNING is copied here. */
53 /* The definition of _GL_ARG_NONNULL is copied here. */
61 /* Return the first instance of C within N bytes of S, or NULL. */
64 # define memchr rpl_memchr
65 extern void *memchr (void const *__s, int __c, size_t __n)
66 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
68 #elif defined GNULIB_POSIXCHECK
70 # define memchr(s,c,n) \
71 (GL_LINK_WARNING ("memchr has platform-specific bugs - " \
72 "use gnulib module memchr for portability" ), \
76 /* Return the first occurrence of NEEDLE in HAYSTACK. */
79 # define memmem rpl_memmem
81 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
82 extern void *memmem (void const *__haystack, size_t __haystack_len,
83 void const *__needle, size_t __needle_len)
84 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3));
86 #elif defined GNULIB_POSIXCHECK
88 # define memmem(a,al,b,bl) \
89 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
90 "use gnulib module memmem-simple for portability, " \
91 "and module memmem for speed" ), \
92 memmem (a, al, b, bl))
95 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
99 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
101 _GL_ARG_NONNULL ((1, 2));
103 #elif defined GNULIB_POSIXCHECK
105 # define mempcpy(a,b,n) \
106 (GL_LINK_WARNING ("mempcpy is unportable - " \
107 "use gnulib module mempcpy for portability"), \
111 /* Search backwards through a block for a byte (specified as an int). */
113 # if ! @HAVE_DECL_MEMRCHR@
114 extern void *memrchr (void const *, int, size_t)
115 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
117 #elif defined GNULIB_POSIXCHECK
119 # define memrchr(a,b,c) \
120 (GL_LINK_WARNING ("memrchr is unportable - " \
121 "use gnulib module memrchr for portability"), \
125 /* Find the first occurrence of C in S. More efficient than
126 memchr(S,C,N), at the expense of undefined behavior if C does not
127 occur within N bytes. */
128 #if @GNULIB_RAWMEMCHR@
129 # if ! @HAVE_RAWMEMCHR@
130 extern void *rawmemchr (void const *__s, int __c_in)
131 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
133 #elif defined GNULIB_POSIXCHECK
135 # define rawmemchr(a,b) \
136 (GL_LINK_WARNING ("rawmemchr is unportable - " \
137 "use gnulib module rawmemchr for portability"), \
141 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
144 extern char *stpcpy (char *restrict __dst, char const *restrict __src)
145 _GL_ARG_NONNULL ((1, 2));
147 #elif defined GNULIB_POSIXCHECK
149 # define stpcpy(a,b) \
150 (GL_LINK_WARNING ("stpcpy is unportable - " \
151 "use gnulib module stpcpy for portability"), \
155 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
156 last non-NUL byte written into DST. */
158 # if ! @HAVE_STPNCPY@
159 # define stpncpy gnu_stpncpy
160 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
162 _GL_ARG_NONNULL ((1, 2));
164 #elif defined GNULIB_POSIXCHECK
166 # define stpncpy(a,b,n) \
167 (GL_LINK_WARNING ("stpncpy is unportable - " \
168 "use gnulib module stpncpy for portability"), \
172 #if defined GNULIB_POSIXCHECK
173 /* strchr() does not work with multibyte strings if the locale encoding is
174 GB18030 and the character to be searched is a digit. */
176 # define strchr(s,c) \
177 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
178 "in some multibyte locales - " \
179 "use mbschr if you care about internationalization"), \
183 /* Find the first occurrence of C in S or the final NUL byte. */
184 #if @GNULIB_STRCHRNUL@
185 # if ! @HAVE_STRCHRNUL@
186 extern char *strchrnul (char const *__s, int __c_in)
187 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
189 #elif defined GNULIB_POSIXCHECK
191 # define strchrnul(a,b) \
192 (GL_LINK_WARNING ("strchrnul is unportable - " \
193 "use gnulib module strchrnul for portability"), \
197 /* Duplicate S, returning an identical malloc'd string. */
199 # if @REPLACE_STRDUP@
201 # define strdup rpl_strdup
203 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
204 extern char *strdup (char const *__s) _GL_ARG_NONNULL ((1));
206 #elif defined GNULIB_POSIXCHECK
209 (GL_LINK_WARNING ("strdup is unportable - " \
210 "use gnulib module strdup for portability"), \
214 /* Return a newly allocated copy of at most N bytes of STRING. */
216 # if @REPLACE_STRNDUP@
218 # define strndup rpl_strndup
220 # if @REPLACE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
221 extern char *strndup (char const *__string, size_t __n) _GL_ARG_NONNULL ((1));
223 #elif defined GNULIB_POSIXCHECK
225 # define strndup(a,n) \
226 (GL_LINK_WARNING ("strndup is unportable - " \
227 "use gnulib module strndup for portability"), \
231 /* Find the length (number of bytes) of STRING, but scan at most
232 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
235 # if ! @HAVE_DECL_STRNLEN@
236 extern size_t strnlen (char const *__string, size_t __maxlen)
237 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
239 #elif defined GNULIB_POSIXCHECK
241 # define strnlen(a,n) \
242 (GL_LINK_WARNING ("strnlen is unportable - " \
243 "use gnulib module strnlen for portability"), \
247 #if defined GNULIB_POSIXCHECK
248 /* strcspn() assumes the second argument is a list of single-byte characters.
249 Even in this simple case, it does not work with multibyte strings if the
250 locale encoding is GB18030 and one of the characters to be searched is a
253 # define strcspn(s,a) \
254 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
255 "in multibyte locales - " \
256 "use mbscspn if you care about internationalization"), \
260 /* Find the first occurrence in S of any character in ACCEPT. */
262 # if ! @HAVE_STRPBRK@
263 extern char *strpbrk (char const *__s, char const *__accept)
264 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2));
266 # if defined GNULIB_POSIXCHECK
267 /* strpbrk() assumes the second argument is a list of single-byte characters.
268 Even in this simple case, it does not work with multibyte strings if the
269 locale encoding is GB18030 and one of the characters to be searched is a
272 # define strpbrk(s,a) \
273 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
274 "in multibyte locales - " \
275 "use mbspbrk if you care about internationalization"), \
278 #elif defined GNULIB_POSIXCHECK
280 # define strpbrk(s,a) \
281 (GL_LINK_WARNING ("strpbrk is unportable - " \
282 "use gnulib module strpbrk for portability"), \
286 #if defined GNULIB_POSIXCHECK
287 /* strspn() assumes the second argument is a list of single-byte characters.
288 Even in this simple case, it cannot work with multibyte strings. */
290 # define strspn(s,a) \
291 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
292 "in multibyte locales - " \
293 "use mbsspn if you care about internationalization"), \
297 #if defined GNULIB_POSIXCHECK
298 /* strrchr() does not work with multibyte strings if the locale encoding is
299 GB18030 and the character to be searched is a digit. */
301 # define strrchr(s,c) \
302 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
303 "in some multibyte locales - " \
304 "use mbsrchr if you care about internationalization"), \
308 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
309 If one is found, overwrite it with a NUL, and advance *STRINGP
310 to point to the next char after it. Otherwise, set *STRINGP to NULL.
311 If *STRINGP was already NULL, nothing happens.
312 Return the old value of *STRINGP.
314 This is a variant of strtok() that is multithread-safe and supports
317 Caveat: It modifies the original string.
318 Caveat: These functions cannot be used on constant strings.
319 Caveat: The identity of the delimiting character is lost.
320 Caveat: It doesn't work with multibyte strings unless all of the delimiter
321 characters are ASCII characters < 0x30.
323 See also strtok_r(). */
326 extern char *strsep (char **restrict __stringp, char const *restrict __delim)
327 _GL_ARG_NONNULL ((1, 2));
329 # if defined GNULIB_POSIXCHECK
331 # define strsep(s,d) \
332 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
333 "in multibyte locales - " \
334 "use mbssep if you care about internationalization"), \
337 #elif defined GNULIB_POSIXCHECK
339 # define strsep(s,d) \
340 (GL_LINK_WARNING ("strsep is unportable - " \
341 "use gnulib module strsep for portability"), \
346 # if @REPLACE_STRSTR@
347 # define strstr rpl_strstr
348 extern char *strstr (const char *haystack, const char *needle)
349 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2));
351 #elif defined GNULIB_POSIXCHECK
352 /* strstr() does not work with multibyte strings if the locale encoding is
353 different from UTF-8:
354 POSIX says that it operates on "strings", and "string" in POSIX is defined
355 as a sequence of bytes, not of characters. */
357 # define strstr(a,b) \
358 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
359 "work correctly on character strings in most " \
360 "multibyte locales - " \
361 "use mbsstr if you care about internationalization, " \
362 "or use strstr if you care about speed"), \
366 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
368 #if @GNULIB_STRCASESTR@
369 # if @REPLACE_STRCASESTR@
370 # define strcasestr rpl_strcasestr
372 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
373 extern char *strcasestr (const char *haystack, const char *needle)
374 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2));
376 #elif defined GNULIB_POSIXCHECK
377 /* strcasestr() does not work with multibyte strings:
378 It is a glibc extension, and glibc implements it only for unibyte
381 # define strcasestr(a,b) \
382 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
383 "in multibyte locales - " \
384 "use mbscasestr if you care about " \
385 "internationalization, or use c-strcasestr if you want " \
386 "a locale independent function"), \
390 /* Parse S into tokens separated by characters in DELIM.
391 If S is NULL, the saved pointer in SAVE_PTR is used as
392 the next starting point. For example:
393 char s[] = "-abc-=-def";
395 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
396 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
397 x = strtok_r(NULL, "=", &sp); // x = NULL
400 This is a variant of strtok() that is multithread-safe.
402 For the POSIX documentation for this function, see:
403 http://www.opengroup.org/susv3xsh/strtok.html
405 Caveat: It modifies the original string.
406 Caveat: These functions cannot be used on constant strings.
407 Caveat: The identity of the delimiting character is lost.
408 Caveat: It doesn't work with multibyte strings unless all of the delimiter
409 characters are ASCII characters < 0x30.
411 See also strsep(). */
412 #if @GNULIB_STRTOK_R@
413 # if @REPLACE_STRTOK_R@
415 # define strtok_r rpl_strtok_r
416 # elif @UNDEFINE_STRTOK_R@
419 # if ! @HAVE_DECL_STRTOK_R@ || @REPLACE_STRTOK_R@
420 extern char *strtok_r (char *restrict s, char const *restrict delim,
421 char **restrict save_ptr)
422 _GL_ARG_NONNULL ((2, 3));
424 # if defined GNULIB_POSIXCHECK
426 # define strtok_r(s,d,p) \
427 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
428 "in multibyte locales - " \
429 "use mbstok_r if you care about internationalization"), \
432 #elif defined GNULIB_POSIXCHECK
434 # define strtok_r(s,d,p) \
435 (GL_LINK_WARNING ("strtok_r is unportable - " \
436 "use gnulib module strtok_r for portability"), \
441 /* The following functions are not specified by POSIX. They are gnulib
445 /* Return the number of multibyte characters in the character string STRING.
446 This considers multibyte characters, unlike strlen, which counts bytes. */
447 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
450 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
451 # define mbslen rpl_mbslen
453 extern size_t mbslen (const char *string) _GL_ARG_NONNULL ((1));
457 /* Return the number of multibyte characters in the character string starting
458 at STRING and ending at STRING + LEN. */
459 extern size_t mbsnlen (const char *string, size_t len) _GL_ARG_NONNULL ((1));
463 /* Locate the first single-byte character C in the character string STRING,
464 and return a pointer to it. Return NULL if C is not found in STRING.
465 Unlike strchr(), this function works correctly in multibyte locales with
466 encodings such as GB18030. */
467 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
468 extern char * mbschr (const char *string, int c) _GL_ARG_NONNULL ((1));
472 /* Locate the last single-byte character C in the character string STRING,
473 and return a pointer to it. Return NULL if C is not found in STRING.
474 Unlike strrchr(), this function works correctly in multibyte locales with
475 encodings such as GB18030. */
476 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
477 extern char * mbsrchr (const char *string, int c) _GL_ARG_NONNULL ((1));
481 /* Find the first occurrence of the character string NEEDLE in the character
482 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
483 Unlike strstr(), this function works correctly in multibyte locales with
484 encodings different from UTF-8. */
485 extern char * mbsstr (const char *haystack, const char *needle)
486 _GL_ARG_NONNULL ((1, 2));
489 #if @GNULIB_MBSCASECMP@
490 /* Compare the character strings S1 and S2, ignoring case, returning less than,
491 equal to or greater than zero if S1 is lexicographically less than, equal to
493 Note: This function may, in multibyte locales, return 0 for strings of
495 Unlike strcasecmp(), this function works correctly in multibyte locales. */
496 extern int mbscasecmp (const char *s1, const char *s2)
497 _GL_ARG_NONNULL ((1, 2));
500 #if @GNULIB_MBSNCASECMP@
501 /* Compare the initial segment of the character string S1 consisting of at most
502 N characters with the initial segment of the character string S2 consisting
503 of at most N characters, ignoring case, returning less than, equal to or
504 greater than zero if the initial segment of S1 is lexicographically less
505 than, equal to or greater than the initial segment of S2.
506 Note: This function may, in multibyte locales, return 0 for initial segments
507 of different lengths!
508 Unlike strncasecmp(), this function works correctly in multibyte locales.
509 But beware that N is not a byte count but a character count! */
510 extern int mbsncasecmp (const char *s1, const char *s2, size_t n)
511 _GL_ARG_NONNULL ((1, 2));
514 #if @GNULIB_MBSPCASECMP@
515 /* Compare the initial segment of the character string STRING consisting of
516 at most mbslen (PREFIX) characters with the character string PREFIX,
517 ignoring case, returning less than, equal to or greater than zero if this
518 initial segment is lexicographically less than, equal to or greater than
520 Note: This function may, in multibyte locales, return 0 if STRING is of
521 smaller length than PREFIX!
522 Unlike strncasecmp(), this function works correctly in multibyte
524 extern char * mbspcasecmp (const char *string, const char *prefix)
525 _GL_ARG_NONNULL ((1, 2));
528 #if @GNULIB_MBSCASESTR@
529 /* Find the first occurrence of the character string NEEDLE in the character
530 string HAYSTACK, using case-insensitive comparison.
531 Note: This function may, in multibyte locales, return success even if
532 strlen (haystack) < strlen (needle) !
533 Unlike strcasestr(), this function works correctly in multibyte locales. */
534 extern char * mbscasestr (const char *haystack, const char *needle)
535 _GL_ARG_NONNULL ((1, 2));
539 /* Find the first occurrence in the character string STRING of any character
540 in the character string ACCEPT. Return the number of bytes from the
541 beginning of the string to this occurrence, or to the end of the string
543 Unlike strcspn(), this function works correctly in multibyte locales. */
544 extern size_t mbscspn (const char *string, const char *accept)
545 _GL_ARG_NONNULL ((1, 2));
549 /* Find the first occurrence in the character string STRING of any character
550 in the character string ACCEPT. Return the pointer to it, or NULL if none
552 Unlike strpbrk(), this function works correctly in multibyte locales. */
553 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
554 extern char * mbspbrk (const char *string, const char *accept)
555 _GL_ARG_NONNULL ((1, 2));
559 /* Find the first occurrence in the character string STRING of any character
560 not in the character string REJECT. Return the number of bytes from the
561 beginning of the string to this occurrence, or to the end of the string
563 Unlike strspn(), this function works correctly in multibyte locales. */
564 extern size_t mbsspn (const char *string, const char *reject)
565 _GL_ARG_NONNULL ((1, 2));
569 /* Search the next delimiter (multibyte character listed in the character
570 string DELIM) starting at the character string *STRINGP.
571 If one is found, overwrite it with a NUL, and advance *STRINGP to point
572 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
573 If *STRINGP was already NULL, nothing happens.
574 Return the old value of *STRINGP.
576 This is a variant of mbstok_r() that supports empty fields.
578 Caveat: It modifies the original string.
579 Caveat: These functions cannot be used on constant strings.
580 Caveat: The identity of the delimiting character is lost.
582 See also mbstok_r(). */
583 extern char * mbssep (char **stringp, const char *delim)
584 _GL_ARG_NONNULL ((1, 2));
587 #if @GNULIB_MBSTOK_R@
588 /* Parse the character string STRING into tokens separated by characters in
589 the character string DELIM.
590 If STRING is NULL, the saved pointer in SAVE_PTR is used as
591 the next starting point. For example:
592 char s[] = "-abc-=-def";
594 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
595 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
596 x = mbstok_r(NULL, "=", &sp); // x = NULL
599 Caveat: It modifies the original string.
600 Caveat: These functions cannot be used on constant strings.
601 Caveat: The identity of the delimiting character is lost.
603 See also mbssep(). */
604 extern char * mbstok_r (char *string, const char *delim, char **save_ptr)
605 _GL_ARG_NONNULL ((2, 3));
608 /* Map any int, typically from errno, into an error message. */
609 #if @GNULIB_STRERROR@
610 # if @REPLACE_STRERROR@
612 # define strerror rpl_strerror
613 extern char *strerror (int);
615 #elif defined GNULIB_POSIXCHECK
617 # define strerror(e) \
618 (GL_LINK_WARNING ("strerror is unportable - " \
619 "use gnulib module strerror to guarantee non-NULL result"), \
623 #if @GNULIB_STRSIGNAL@
624 # if @REPLACE_STRSIGNAL@
625 # define strsignal rpl_strsignal
627 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
628 extern char *strsignal (int __sig);
630 #elif defined GNULIB_POSIXCHECK
632 # define strsignal(a) \
633 (GL_LINK_WARNING ("strsignal is unportable - " \
634 "use gnulib module strsignal for portability"), \
638 #if @GNULIB_STRVERSCMP@
639 # if !@HAVE_STRVERSCMP@
640 extern int strverscmp (const char *, const char *) _GL_ARG_NONNULL ((1, 2));
642 #elif defined GNULIB_POSIXCHECK
644 # define strverscmp(a, b) \
645 (GL_LINK_WARNING ("strverscmp is unportable - " \
646 "use gnulib module strverscmp for portability"), \
655 #endif /* _GL_STRING_H */
656 #endif /* _GL_STRING_H */