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 3, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 @PRAGMA_SYSTEM_HEADER@
25 /* The include_next requires a split double-inclusion guard. */
26 #@INCLUDE_NEXT@ @NEXT_STRING_H@
33 /* This feature is available in gcc versions 2.5 and later. */
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
35 # define __attribute__(Spec) /* empty */
37 /* The attribute __pure__ was added in gcc 2.96. */
38 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
39 # define __pure__ /* empty */
44 /* The definition of GL_LINK_WARNING is copied here. */
52 /* Return the first instance of C within N bytes of S, or NULL. */
55 # define memchr rpl_memchr
56 extern void *memchr (void const *__s, int __c, size_t __n)
57 __attribute__ ((__pure__));
59 #elif defined GNULIB_POSIXCHECK
61 # define memchr(s,c,n) \
62 (GL_LINK_WARNING ("memchr has platform-specific bugs - " \
63 "use gnulib module memchr for portability" ), \
67 /* Return the first occurrence of NEEDLE in HAYSTACK. */
70 # define memmem rpl_memmem
72 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
73 extern void *memmem (void const *__haystack, size_t __haystack_len,
74 void const *__needle, size_t __needle_len)
75 __attribute__ ((__pure__));
77 #elif defined GNULIB_POSIXCHECK
79 # define memmem(a,al,b,bl) \
80 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
81 "use gnulib module memmem-simple for portability, " \
82 "and module memmem for speed" ), \
83 memmem (a, al, b, bl))
86 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
90 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
93 #elif defined GNULIB_POSIXCHECK
95 # define mempcpy(a,b,n) \
96 (GL_LINK_WARNING ("mempcpy is unportable - " \
97 "use gnulib module mempcpy for portability"), \
101 /* Search backwards through a block for a byte (specified as an int). */
103 # if ! @HAVE_DECL_MEMRCHR@
104 extern void *memrchr (void const *, int, size_t)
105 __attribute__ ((__pure__));
107 #elif defined GNULIB_POSIXCHECK
109 # define memrchr(a,b,c) \
110 (GL_LINK_WARNING ("memrchr is unportable - " \
111 "use gnulib module memrchr for portability"), \
115 /* Find the first occurrence of C in S. More efficient than
116 memchr(S,C,N), at the expense of undefined behavior if C does not
117 occur within N bytes. */
118 #if @GNULIB_RAWMEMCHR@
119 # if ! @HAVE_RAWMEMCHR@
120 extern void *rawmemchr (void const *__s, int __c_in)
121 __attribute__ ((__pure__));
123 #elif defined GNULIB_POSIXCHECK
125 # define rawmemchr(a,b) \
126 (GL_LINK_WARNING ("rawmemchr is unportable - " \
127 "use gnulib module rawmemchr for portability"), \
131 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
134 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
136 #elif defined GNULIB_POSIXCHECK
138 # define stpcpy(a,b) \
139 (GL_LINK_WARNING ("stpcpy is unportable - " \
140 "use gnulib module stpcpy for portability"), \
144 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
145 last non-NUL byte written into DST. */
147 # if ! @HAVE_STPNCPY@
148 # define stpncpy gnu_stpncpy
149 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
152 #elif defined GNULIB_POSIXCHECK
154 # define stpncpy(a,b,n) \
155 (GL_LINK_WARNING ("stpncpy is unportable - " \
156 "use gnulib module stpncpy for portability"), \
160 #if defined GNULIB_POSIXCHECK
161 /* strchr() does not work with multibyte strings if the locale encoding is
162 GB18030 and the character to be searched is a digit. */
164 # define strchr(s,c) \
165 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
166 "in some multibyte locales - " \
167 "use mbschr if you care about internationalization"), \
171 /* Find the first occurrence of C in S or the final NUL byte. */
172 #if @GNULIB_STRCHRNUL@
173 # if ! @HAVE_STRCHRNUL@
174 extern char *strchrnul (char const *__s, int __c_in)
175 __attribute__ ((__pure__));
177 #elif defined GNULIB_POSIXCHECK
179 # define strchrnul(a,b) \
180 (GL_LINK_WARNING ("strchrnul is unportable - " \
181 "use gnulib module strchrnul for portability"), \
185 /* Duplicate S, returning an identical malloc'd string. */
187 # if @REPLACE_STRDUP@
189 # define strdup rpl_strdup
191 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
192 extern char *strdup (char const *__s);
194 #elif defined GNULIB_POSIXCHECK
197 (GL_LINK_WARNING ("strdup is unportable - " \
198 "use gnulib module strdup for portability"), \
202 /* Return a newly allocated copy of at most N bytes of STRING. */
204 # if ! @HAVE_STRNDUP@
206 # define strndup rpl_strndup
208 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
209 extern char *strndup (char const *__string, size_t __n);
211 #elif defined GNULIB_POSIXCHECK
213 # define strndup(a,n) \
214 (GL_LINK_WARNING ("strndup is unportable - " \
215 "use gnulib module strndup for portability"), \
219 /* Find the length (number of bytes) of STRING, but scan at most
220 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
223 # if ! @HAVE_DECL_STRNLEN@
224 extern size_t strnlen (char const *__string, size_t __maxlen)
225 __attribute__ ((__pure__));
227 #elif defined GNULIB_POSIXCHECK
229 # define strnlen(a,n) \
230 (GL_LINK_WARNING ("strnlen is unportable - " \
231 "use gnulib module strnlen for portability"), \
235 #if defined GNULIB_POSIXCHECK
236 /* strcspn() assumes the second argument is a list of single-byte characters.
237 Even in this simple case, it does not work with multibyte strings if the
238 locale encoding is GB18030 and one of the characters to be searched is a
241 # define strcspn(s,a) \
242 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
243 "in multibyte locales - " \
244 "use mbscspn if you care about internationalization"), \
248 /* Find the first occurrence in S of any character in ACCEPT. */
250 # if ! @HAVE_STRPBRK@
251 extern char *strpbrk (char const *__s, char const *__accept)
252 __attribute__ ((__pure__));
254 # if defined GNULIB_POSIXCHECK
255 /* strpbrk() assumes the second argument is a list of single-byte characters.
256 Even in this simple case, it does not work with multibyte strings if the
257 locale encoding is GB18030 and one of the characters to be searched is a
260 # define strpbrk(s,a) \
261 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
262 "in multibyte locales - " \
263 "use mbspbrk if you care about internationalization"), \
266 #elif defined GNULIB_POSIXCHECK
268 # define strpbrk(s,a) \
269 (GL_LINK_WARNING ("strpbrk is unportable - " \
270 "use gnulib module strpbrk for portability"), \
274 #if defined GNULIB_POSIXCHECK
275 /* strspn() assumes the second argument is a list of single-byte characters.
276 Even in this simple case, it cannot work with multibyte strings. */
278 # define strspn(s,a) \
279 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
280 "in multibyte locales - " \
281 "use mbsspn if you care about internationalization"), \
285 #if defined GNULIB_POSIXCHECK
286 /* strrchr() does not work with multibyte strings if the locale encoding is
287 GB18030 and the character to be searched is a digit. */
289 # define strrchr(s,c) \
290 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
291 "in some multibyte locales - " \
292 "use mbsrchr if you care about internationalization"), \
296 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
297 If one is found, overwrite it with a NUL, and advance *STRINGP
298 to point to the next char after it. Otherwise, set *STRINGP to NULL.
299 If *STRINGP was already NULL, nothing happens.
300 Return the old value of *STRINGP.
302 This is a variant of strtok() that is multithread-safe and supports
305 Caveat: It modifies the original string.
306 Caveat: These functions cannot be used on constant strings.
307 Caveat: The identity of the delimiting character is lost.
308 Caveat: It doesn't work with multibyte strings unless all of the delimiter
309 characters are ASCII characters < 0x30.
311 See also strtok_r(). */
314 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
316 # if defined GNULIB_POSIXCHECK
318 # define strsep(s,d) \
319 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
320 "in multibyte locales - " \
321 "use mbssep if you care about internationalization"), \
324 #elif defined GNULIB_POSIXCHECK
326 # define strsep(s,d) \
327 (GL_LINK_WARNING ("strsep is unportable - " \
328 "use gnulib module strsep for portability"), \
333 # if @REPLACE_STRSTR@
334 # define strstr rpl_strstr
335 char *strstr (const char *haystack, const char *needle)
336 __attribute__ ((__pure__));
338 #elif defined GNULIB_POSIXCHECK
339 /* strstr() does not work with multibyte strings if the locale encoding is
340 different from UTF-8:
341 POSIX says that it operates on "strings", and "string" in POSIX is defined
342 as a sequence of bytes, not of characters. */
344 # define strstr(a,b) \
345 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
346 "work correctly on character strings in most " \
347 "multibyte locales - " \
348 "use mbsstr if you care about internationalization, " \
349 "or use strstr if you care about speed"), \
353 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
355 #if @GNULIB_STRCASESTR@
356 # if @REPLACE_STRCASESTR@
357 # define strcasestr rpl_strcasestr
359 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
360 extern char *strcasestr (const char *haystack, const char *needle)
361 __attribute__ ((__pure__));
363 #elif defined GNULIB_POSIXCHECK
364 /* strcasestr() does not work with multibyte strings:
365 It is a glibc extension, and glibc implements it only for unibyte
368 # define strcasestr(a,b) \
369 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
370 "in multibyte locales - " \
371 "use mbscasestr if you care about " \
372 "internationalization, or use c-strcasestr if you want " \
373 "a locale independent function"), \
377 /* Parse S into tokens separated by characters in DELIM.
378 If S is NULL, the saved pointer in SAVE_PTR is used as
379 the next starting point. For example:
380 char s[] = "-abc-=-def";
382 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
383 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
384 x = strtok_r(NULL, "=", &sp); // x = NULL
387 This is a variant of strtok() that is multithread-safe.
389 For the POSIX documentation for this function, see:
390 http://www.opengroup.org/susv3xsh/strtok.html
392 Caveat: It modifies the original string.
393 Caveat: These functions cannot be used on constant strings.
394 Caveat: The identity of the delimiting character is lost.
395 Caveat: It doesn't work with multibyte strings unless all of the delimiter
396 characters are ASCII characters < 0x30.
398 See also strsep(). */
399 #if @GNULIB_STRTOK_R@
400 # if ! @HAVE_DECL_STRTOK_R@
401 extern char *strtok_r (char *restrict s, char const *restrict delim,
402 char **restrict save_ptr);
404 # if defined GNULIB_POSIXCHECK
406 # define strtok_r(s,d,p) \
407 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
408 "in multibyte locales - " \
409 "use mbstok_r if you care about internationalization"), \
412 #elif defined GNULIB_POSIXCHECK
414 # define strtok_r(s,d,p) \
415 (GL_LINK_WARNING ("strtok_r is unportable - " \
416 "use gnulib module strtok_r for portability"), \
421 /* The following functions are not specified by POSIX. They are gnulib
425 /* Return the number of multibyte characters in the character string STRING.
426 This considers multibyte characters, unlike strlen, which counts bytes. */
427 extern size_t mbslen (const char *string);
431 /* Return the number of multibyte characters in the character string starting
432 at STRING and ending at STRING + LEN. */
433 extern size_t mbsnlen (const char *string, size_t len);
437 /* Locate the first single-byte character C in the character string STRING,
438 and return a pointer to it. Return NULL if C is not found in STRING.
439 Unlike strchr(), this function works correctly in multibyte locales with
440 encodings such as GB18030. */
441 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
442 extern char * mbschr (const char *string, int c);
446 /* Locate the last single-byte character C in the character string STRING,
447 and return a pointer to it. Return NULL if C is not found in STRING.
448 Unlike strrchr(), this function works correctly in multibyte locales with
449 encodings such as GB18030. */
450 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
451 extern char * mbsrchr (const char *string, int c);
455 /* Find the first occurrence of the character string NEEDLE in the character
456 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
457 Unlike strstr(), this function works correctly in multibyte locales with
458 encodings different from UTF-8. */
459 extern char * mbsstr (const char *haystack, const char *needle);
462 #if @GNULIB_MBSCASECMP@
463 /* Compare the character strings S1 and S2, ignoring case, returning less than,
464 equal to or greater than zero if S1 is lexicographically less than, equal to
466 Note: This function may, in multibyte locales, return 0 for strings of
468 Unlike strcasecmp(), this function works correctly in multibyte locales. */
469 extern int mbscasecmp (const char *s1, const char *s2);
472 #if @GNULIB_MBSNCASECMP@
473 /* Compare the initial segment of the character string S1 consisting of at most
474 N characters with the initial segment of the character string S2 consisting
475 of at most N characters, ignoring case, returning less than, equal to or
476 greater than zero if the initial segment of S1 is lexicographically less
477 than, equal to or greater than the initial segment of S2.
478 Note: This function may, in multibyte locales, return 0 for initial segments
479 of different lengths!
480 Unlike strncasecmp(), this function works correctly in multibyte locales.
481 But beware that N is not a byte count but a character count! */
482 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
485 #if @GNULIB_MBSPCASECMP@
486 /* Compare the initial segment of the character string STRING consisting of
487 at most mbslen (PREFIX) characters with the character string PREFIX,
488 ignoring case, returning less than, equal to or greater than zero if this
489 initial segment is lexicographically less than, equal to or greater than
491 Note: This function may, in multibyte locales, return 0 if STRING is of
492 smaller length than PREFIX!
493 Unlike strncasecmp(), this function works correctly in multibyte
495 extern char * mbspcasecmp (const char *string, const char *prefix);
498 #if @GNULIB_MBSCASESTR@
499 /* Find the first occurrence of the character string NEEDLE in the character
500 string HAYSTACK, using case-insensitive comparison.
501 Note: This function may, in multibyte locales, return success even if
502 strlen (haystack) < strlen (needle) !
503 Unlike strcasestr(), this function works correctly in multibyte locales. */
504 extern char * mbscasestr (const char *haystack, const char *needle);
508 /* Find the first occurrence in the character string STRING of any character
509 in the character string ACCEPT. Return the number of bytes from the
510 beginning of the string to this occurrence, or to the end of the string
512 Unlike strcspn(), this function works correctly in multibyte locales. */
513 extern size_t mbscspn (const char *string, const char *accept);
517 /* Find the first occurrence in the character string STRING of any character
518 in the character string ACCEPT. Return the pointer to it, or NULL if none
520 Unlike strpbrk(), this function works correctly in multibyte locales. */
521 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
522 extern char * mbspbrk (const char *string, const char *accept);
526 /* Find the first occurrence in the character string STRING of any character
527 not in the character string REJECT. Return the number of bytes from the
528 beginning of the string to this occurrence, or to the end of the string
530 Unlike strspn(), this function works correctly in multibyte locales. */
531 extern size_t mbsspn (const char *string, const char *reject);
535 /* Search the next delimiter (multibyte character listed in the character
536 string DELIM) starting at the character string *STRINGP.
537 If one is found, overwrite it with a NUL, and advance *STRINGP to point
538 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
539 If *STRINGP was already NULL, nothing happens.
540 Return the old value of *STRINGP.
542 This is a variant of mbstok_r() that supports empty fields.
544 Caveat: It modifies the original string.
545 Caveat: These functions cannot be used on constant strings.
546 Caveat: The identity of the delimiting character is lost.
548 See also mbstok_r(). */
549 extern char * mbssep (char **stringp, const char *delim);
552 #if @GNULIB_MBSTOK_R@
553 /* Parse the character string STRING into tokens separated by characters in
554 the character string DELIM.
555 If STRING is NULL, the saved pointer in SAVE_PTR is used as
556 the next starting point. For example:
557 char s[] = "-abc-=-def";
559 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
560 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
561 x = mbstok_r(NULL, "=", &sp); // x = NULL
564 Caveat: It modifies the original string.
565 Caveat: These functions cannot be used on constant strings.
566 Caveat: The identity of the delimiting character is lost.
568 See also mbssep(). */
569 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
572 /* Map any int, typically from errno, into an error message. */
573 #if @GNULIB_STRERROR@
574 # if @REPLACE_STRERROR@
576 # define strerror rpl_strerror
577 extern char *strerror (int);
579 #elif defined GNULIB_POSIXCHECK
581 # define strerror(e) \
582 (GL_LINK_WARNING ("strerror is unportable - " \
583 "use gnulib module strerror to guarantee non-NULL result"), \
587 #if @GNULIB_STRSIGNAL@
588 # if @REPLACE_STRSIGNAL@
589 # define strsignal rpl_strsignal
591 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
592 extern char *strsignal (int __sig);
594 #elif defined GNULIB_POSIXCHECK
596 # define strsignal(a) \
597 (GL_LINK_WARNING ("strsignal is unportable - " \
598 "use gnulib module strsignal for portability"), \
602 #if @GNULIB_STRVERSCMP@
603 # if !@HAVE_STRVERSCMP@
604 extern int strverscmp (const char *, const char *);
606 #elif defined GNULIB_POSIXCHECK
608 # define strverscmp(a, b) \
609 (GL_LINK_WARNING ("strverscmp is unportable - " \
610 "use gnulib module strverscmp for portability"), \
619 #endif /* _GL_STRING_H */
620 #endif /* _GL_STRING_H */