1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2009 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. */
35 /* This feature is available in gcc versions 2.5 and later. */
36 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
37 # define __attribute__(Spec) /* empty */
39 /* The attribute __pure__ was added in gcc 2.96. */
40 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
41 # define __pure__ /* empty */
46 /* The definition of GL_LINK_WARNING is copied here. */
48 /* The definition of _GL_ARG_NONNULL is copied here. */
56 /* Return the first instance of C within N bytes of S, or NULL. */
59 # define memchr rpl_memchr
60 extern void *memchr (void const *__s, int __c, size_t __n)
61 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
63 #elif defined GNULIB_POSIXCHECK
65 # define memchr(s,c,n) \
66 (GL_LINK_WARNING ("memchr has platform-specific bugs - " \
67 "use gnulib module memchr for portability" ), \
71 /* Return the first occurrence of NEEDLE in HAYSTACK. */
74 # define memmem rpl_memmem
76 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
77 extern void *memmem (void const *__haystack, size_t __haystack_len,
78 void const *__needle, size_t __needle_len)
79 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 3));
81 #elif defined GNULIB_POSIXCHECK
83 # define memmem(a,al,b,bl) \
84 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
85 "use gnulib module memmem-simple for portability, " \
86 "and module memmem for speed" ), \
87 memmem (a, al, b, bl))
90 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
94 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
96 _GL_ARG_NONNULL ((1, 2));
98 #elif defined GNULIB_POSIXCHECK
100 # define mempcpy(a,b,n) \
101 (GL_LINK_WARNING ("mempcpy is unportable - " \
102 "use gnulib module mempcpy for portability"), \
106 /* Search backwards through a block for a byte (specified as an int). */
108 # if ! @HAVE_DECL_MEMRCHR@
109 extern void *memrchr (void const *, int, size_t)
110 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
112 #elif defined GNULIB_POSIXCHECK
114 # define memrchr(a,b,c) \
115 (GL_LINK_WARNING ("memrchr is unportable - " \
116 "use gnulib module memrchr for portability"), \
120 /* Find the first occurrence of C in S. More efficient than
121 memchr(S,C,N), at the expense of undefined behavior if C does not
122 occur within N bytes. */
123 #if @GNULIB_RAWMEMCHR@
124 # if ! @HAVE_RAWMEMCHR@
125 extern void *rawmemchr (void const *__s, int __c_in)
126 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
128 #elif defined GNULIB_POSIXCHECK
130 # define rawmemchr(a,b) \
131 (GL_LINK_WARNING ("rawmemchr is unportable - " \
132 "use gnulib module rawmemchr for portability"), \
136 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
139 extern char *stpcpy (char *restrict __dst, char const *restrict __src)
140 _GL_ARG_NONNULL ((1, 2));
142 #elif defined GNULIB_POSIXCHECK
144 # define stpcpy(a,b) \
145 (GL_LINK_WARNING ("stpcpy is unportable - " \
146 "use gnulib module stpcpy for portability"), \
150 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
151 last non-NUL byte written into DST. */
153 # if ! @HAVE_STPNCPY@
154 # define stpncpy gnu_stpncpy
155 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
157 _GL_ARG_NONNULL ((1, 2));
159 #elif defined GNULIB_POSIXCHECK
161 # define stpncpy(a,b,n) \
162 (GL_LINK_WARNING ("stpncpy is unportable - " \
163 "use gnulib module stpncpy for portability"), \
167 #if defined GNULIB_POSIXCHECK
168 /* strchr() does not work with multibyte strings if the locale encoding is
169 GB18030 and the character to be searched is a digit. */
171 # define strchr(s,c) \
172 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
173 "in some multibyte locales - " \
174 "use mbschr if you care about internationalization"), \
178 /* Find the first occurrence of C in S or the final NUL byte. */
179 #if @GNULIB_STRCHRNUL@
180 # if ! @HAVE_STRCHRNUL@
181 extern char *strchrnul (char const *__s, int __c_in)
182 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
184 #elif defined GNULIB_POSIXCHECK
186 # define strchrnul(a,b) \
187 (GL_LINK_WARNING ("strchrnul is unportable - " \
188 "use gnulib module strchrnul for portability"), \
192 /* Duplicate S, returning an identical malloc'd string. */
194 # if @REPLACE_STRDUP@
196 # define strdup rpl_strdup
198 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
199 extern char *strdup (char const *__s) _GL_ARG_NONNULL ((1));
201 #elif defined GNULIB_POSIXCHECK
204 (GL_LINK_WARNING ("strdup is unportable - " \
205 "use gnulib module strdup for portability"), \
209 /* Return a newly allocated copy of at most N bytes of STRING. */
211 # if @REPLACE_STRNDUP@
213 # define strndup rpl_strndup
215 # if @REPLACE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
216 extern char *strndup (char const *__string, size_t __n) _GL_ARG_NONNULL ((1));
218 #elif defined GNULIB_POSIXCHECK
220 # define strndup(a,n) \
221 (GL_LINK_WARNING ("strndup is unportable - " \
222 "use gnulib module strndup for portability"), \
226 /* Find the length (number of bytes) of STRING, but scan at most
227 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
230 # if ! @HAVE_DECL_STRNLEN@
231 extern size_t strnlen (char const *__string, size_t __maxlen)
232 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1));
234 #elif defined GNULIB_POSIXCHECK
236 # define strnlen(a,n) \
237 (GL_LINK_WARNING ("strnlen is unportable - " \
238 "use gnulib module strnlen for portability"), \
242 #if defined GNULIB_POSIXCHECK
243 /* strcspn() assumes the second argument is a list of single-byte characters.
244 Even in this simple case, it does not work with multibyte strings if the
245 locale encoding is GB18030 and one of the characters to be searched is a
248 # define strcspn(s,a) \
249 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
250 "in multibyte locales - " \
251 "use mbscspn if you care about internationalization"), \
255 /* Find the first occurrence in S of any character in ACCEPT. */
257 # if ! @HAVE_STRPBRK@
258 extern char *strpbrk (char const *__s, char const *__accept)
259 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2));
261 # if defined GNULIB_POSIXCHECK
262 /* strpbrk() assumes the second argument is a list of single-byte characters.
263 Even in this simple case, it does not work with multibyte strings if the
264 locale encoding is GB18030 and one of the characters to be searched is a
267 # define strpbrk(s,a) \
268 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
269 "in multibyte locales - " \
270 "use mbspbrk if you care about internationalization"), \
273 #elif defined GNULIB_POSIXCHECK
275 # define strpbrk(s,a) \
276 (GL_LINK_WARNING ("strpbrk is unportable - " \
277 "use gnulib module strpbrk for portability"), \
281 #if defined GNULIB_POSIXCHECK
282 /* strspn() assumes the second argument is a list of single-byte characters.
283 Even in this simple case, it cannot work with multibyte strings. */
285 # define strspn(s,a) \
286 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
287 "in multibyte locales - " \
288 "use mbsspn if you care about internationalization"), \
292 #if defined GNULIB_POSIXCHECK
293 /* strrchr() does not work with multibyte strings if the locale encoding is
294 GB18030 and the character to be searched is a digit. */
296 # define strrchr(s,c) \
297 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
298 "in some multibyte locales - " \
299 "use mbsrchr if you care about internationalization"), \
303 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
304 If one is found, overwrite it with a NUL, and advance *STRINGP
305 to point to the next char after it. Otherwise, set *STRINGP to NULL.
306 If *STRINGP was already NULL, nothing happens.
307 Return the old value of *STRINGP.
309 This is a variant of strtok() that is multithread-safe and supports
312 Caveat: It modifies the original string.
313 Caveat: These functions cannot be used on constant strings.
314 Caveat: The identity of the delimiting character is lost.
315 Caveat: It doesn't work with multibyte strings unless all of the delimiter
316 characters are ASCII characters < 0x30.
318 See also strtok_r(). */
321 extern char *strsep (char **restrict __stringp, char const *restrict __delim)
322 _GL_ARG_NONNULL ((1, 2));
324 # if defined GNULIB_POSIXCHECK
326 # define strsep(s,d) \
327 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
328 "in multibyte locales - " \
329 "use mbssep if you care about internationalization"), \
332 #elif defined GNULIB_POSIXCHECK
334 # define strsep(s,d) \
335 (GL_LINK_WARNING ("strsep is unportable - " \
336 "use gnulib module strsep for portability"), \
341 # if @REPLACE_STRSTR@
342 # define strstr rpl_strstr
343 extern char *strstr (const char *haystack, const char *needle)
344 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2));
346 #elif defined GNULIB_POSIXCHECK
347 /* strstr() does not work with multibyte strings if the locale encoding is
348 different from UTF-8:
349 POSIX says that it operates on "strings", and "string" in POSIX is defined
350 as a sequence of bytes, not of characters. */
352 # define strstr(a,b) \
353 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
354 "work correctly on character strings in most " \
355 "multibyte locales - " \
356 "use mbsstr if you care about internationalization, " \
357 "or use strstr if you care about speed"), \
361 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
363 #if @GNULIB_STRCASESTR@
364 # if @REPLACE_STRCASESTR@
365 # define strcasestr rpl_strcasestr
367 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
368 extern char *strcasestr (const char *haystack, const char *needle)
369 __attribute__ ((__pure__)) _GL_ARG_NONNULL ((1, 2));
371 #elif defined GNULIB_POSIXCHECK
372 /* strcasestr() does not work with multibyte strings:
373 It is a glibc extension, and glibc implements it only for unibyte
376 # define strcasestr(a,b) \
377 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
378 "in multibyte locales - " \
379 "use mbscasestr if you care about " \
380 "internationalization, or use c-strcasestr if you want " \
381 "a locale independent function"), \
385 /* Parse S into tokens separated by characters in DELIM.
386 If S is NULL, the saved pointer in SAVE_PTR is used as
387 the next starting point. For example:
388 char s[] = "-abc-=-def";
390 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
391 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
392 x = strtok_r(NULL, "=", &sp); // x = NULL
395 This is a variant of strtok() that is multithread-safe.
397 For the POSIX documentation for this function, see:
398 http://www.opengroup.org/susv3xsh/strtok.html
400 Caveat: It modifies the original string.
401 Caveat: These functions cannot be used on constant strings.
402 Caveat: The identity of the delimiting character is lost.
403 Caveat: It doesn't work with multibyte strings unless all of the delimiter
404 characters are ASCII characters < 0x30.
406 See also strsep(). */
407 #if @GNULIB_STRTOK_R@
408 # if @REPLACE_STRTOK_R@
410 # define strtok_r rpl_strtok_r
411 # elif @UNDEFINE_STRTOK_R@
414 # if ! @HAVE_DECL_STRTOK_R@ || @REPLACE_STRTOK_R@
415 extern char *strtok_r (char *restrict s, char const *restrict delim,
416 char **restrict save_ptr)
417 _GL_ARG_NONNULL ((2, 3));
419 # if defined GNULIB_POSIXCHECK
421 # define strtok_r(s,d,p) \
422 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
423 "in multibyte locales - " \
424 "use mbstok_r if you care about internationalization"), \
427 #elif defined GNULIB_POSIXCHECK
429 # define strtok_r(s,d,p) \
430 (GL_LINK_WARNING ("strtok_r is unportable - " \
431 "use gnulib module strtok_r for portability"), \
436 /* The following functions are not specified by POSIX. They are gnulib
440 /* Return the number of multibyte characters in the character string STRING.
441 This considers multibyte characters, unlike strlen, which counts bytes. */
442 extern size_t mbslen (const char *string) _GL_ARG_NONNULL ((1));
446 /* Return the number of multibyte characters in the character string starting
447 at STRING and ending at STRING + LEN. */
448 extern size_t mbsnlen (const char *string, size_t len) _GL_ARG_NONNULL ((1));
452 /* Locate the first single-byte character C in the character string STRING,
453 and return a pointer to it. Return NULL if C is not found in STRING.
454 Unlike strchr(), this function works correctly in multibyte locales with
455 encodings such as GB18030. */
456 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
457 extern char * mbschr (const char *string, int c) _GL_ARG_NONNULL ((1));
461 /* Locate the last single-byte character C in the character string STRING,
462 and return a pointer to it. Return NULL if C is not found in STRING.
463 Unlike strrchr(), this function works correctly in multibyte locales with
464 encodings such as GB18030. */
465 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
466 extern char * mbsrchr (const char *string, int c) _GL_ARG_NONNULL ((1));
470 /* Find the first occurrence of the character string NEEDLE in the character
471 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
472 Unlike strstr(), this function works correctly in multibyte locales with
473 encodings different from UTF-8. */
474 extern char * mbsstr (const char *haystack, const char *needle)
475 _GL_ARG_NONNULL ((1, 2));
478 #if @GNULIB_MBSCASECMP@
479 /* Compare the character strings S1 and S2, ignoring case, returning less than,
480 equal to or greater than zero if S1 is lexicographically less than, equal to
482 Note: This function may, in multibyte locales, return 0 for strings of
484 Unlike strcasecmp(), this function works correctly in multibyte locales. */
485 extern int mbscasecmp (const char *s1, const char *s2)
486 _GL_ARG_NONNULL ((1, 2));
489 #if @GNULIB_MBSNCASECMP@
490 /* Compare the initial segment of the character string S1 consisting of at most
491 N characters with the initial segment of the character string S2 consisting
492 of at most N characters, ignoring case, returning less than, equal to or
493 greater than zero if the initial segment of S1 is lexicographically less
494 than, equal to or greater than the initial segment of S2.
495 Note: This function may, in multibyte locales, return 0 for initial segments
496 of different lengths!
497 Unlike strncasecmp(), this function works correctly in multibyte locales.
498 But beware that N is not a byte count but a character count! */
499 extern int mbsncasecmp (const char *s1, const char *s2, size_t n)
500 _GL_ARG_NONNULL ((1, 2));
503 #if @GNULIB_MBSPCASECMP@
504 /* Compare the initial segment of the character string STRING consisting of
505 at most mbslen (PREFIX) characters with the character string PREFIX,
506 ignoring case, returning less than, equal to or greater than zero if this
507 initial segment is lexicographically less than, equal to or greater than
509 Note: This function may, in multibyte locales, return 0 if STRING is of
510 smaller length than PREFIX!
511 Unlike strncasecmp(), this function works correctly in multibyte
513 extern char * mbspcasecmp (const char *string, const char *prefix)
514 _GL_ARG_NONNULL ((1, 2));
517 #if @GNULIB_MBSCASESTR@
518 /* Find the first occurrence of the character string NEEDLE in the character
519 string HAYSTACK, using case-insensitive comparison.
520 Note: This function may, in multibyte locales, return success even if
521 strlen (haystack) < strlen (needle) !
522 Unlike strcasestr(), this function works correctly in multibyte locales. */
523 extern char * mbscasestr (const char *haystack, const char *needle)
524 _GL_ARG_NONNULL ((1, 2));
528 /* Find the first occurrence in the character string STRING of any character
529 in the character string ACCEPT. Return the number of bytes from the
530 beginning of the string to this occurrence, or to the end of the string
532 Unlike strcspn(), this function works correctly in multibyte locales. */
533 extern size_t mbscspn (const char *string, const char *accept)
534 _GL_ARG_NONNULL ((1, 2));
538 /* Find the first occurrence in the character string STRING of any character
539 in the character string ACCEPT. Return the pointer to it, or NULL if none
541 Unlike strpbrk(), this function works correctly in multibyte locales. */
542 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
543 extern char * mbspbrk (const char *string, const char *accept)
544 _GL_ARG_NONNULL ((1, 2));
548 /* Find the first occurrence in the character string STRING of any character
549 not in the character string REJECT. Return the number of bytes from the
550 beginning of the string to this occurrence, or to the end of the string
552 Unlike strspn(), this function works correctly in multibyte locales. */
553 extern size_t mbsspn (const char *string, const char *reject)
554 _GL_ARG_NONNULL ((1, 2));
558 /* Search the next delimiter (multibyte character listed in the character
559 string DELIM) starting at the character string *STRINGP.
560 If one is found, overwrite it with a NUL, and advance *STRINGP to point
561 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
562 If *STRINGP was already NULL, nothing happens.
563 Return the old value of *STRINGP.
565 This is a variant of mbstok_r() that supports empty fields.
567 Caveat: It modifies the original string.
568 Caveat: These functions cannot be used on constant strings.
569 Caveat: The identity of the delimiting character is lost.
571 See also mbstok_r(). */
572 extern char * mbssep (char **stringp, const char *delim)
573 _GL_ARG_NONNULL ((1, 2));
576 #if @GNULIB_MBSTOK_R@
577 /* Parse the character string STRING into tokens separated by characters in
578 the character string DELIM.
579 If STRING is NULL, the saved pointer in SAVE_PTR is used as
580 the next starting point. For example:
581 char s[] = "-abc-=-def";
583 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
584 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
585 x = mbstok_r(NULL, "=", &sp); // x = NULL
588 Caveat: It modifies the original string.
589 Caveat: These functions cannot be used on constant strings.
590 Caveat: The identity of the delimiting character is lost.
592 See also mbssep(). */
593 extern char * mbstok_r (char *string, const char *delim, char **save_ptr)
594 _GL_ARG_NONNULL ((2, 3));
597 /* Map any int, typically from errno, into an error message. */
598 #if @GNULIB_STRERROR@
599 # if @REPLACE_STRERROR@
601 # define strerror rpl_strerror
602 extern char *strerror (int);
604 #elif defined GNULIB_POSIXCHECK
606 # define strerror(e) \
607 (GL_LINK_WARNING ("strerror is unportable - " \
608 "use gnulib module strerror to guarantee non-NULL result"), \
612 #if @GNULIB_STRSIGNAL@
613 # if @REPLACE_STRSIGNAL@
614 # define strsignal rpl_strsignal
616 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
617 extern char *strsignal (int __sig);
619 #elif defined GNULIB_POSIXCHECK
621 # define strsignal(a) \
622 (GL_LINK_WARNING ("strsignal is unportable - " \
623 "use gnulib module strsignal for portability"), \
627 #if @GNULIB_STRVERSCMP@
628 # if !@HAVE_STRVERSCMP@
629 extern int strverscmp (const char *, const char *) _GL_ARG_NONNULL ((1, 2));
631 #elif defined GNULIB_POSIXCHECK
633 # define strverscmp(a, b) \
634 (GL_LINK_WARNING ("strverscmp is unportable - " \
635 "use gnulib module strverscmp for portability"), \
644 #endif /* _GL_STRING_H */
645 #endif /* _GL_STRING_H */