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@
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. */
54 /* Return the first instance of C within N bytes of S, or NULL. */
57 # define memchr rpl_memchr
58 extern void *memchr (void const *__s
, int __c
, size_t __n
)
59 __attribute__ ((__pure__
));
61 #elif defined GNULIB_POSIXCHECK
63 # define memchr(s,c,n) \
64 (GL_LINK_WARNING ("memchr has platform-specific bugs - " \
65 "use gnulib module memchr for portability" ), \
69 /* Return the first occurrence of NEEDLE in HAYSTACK. */
72 # define memmem rpl_memmem
74 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
75 extern void *memmem (void const *__haystack
, size_t __haystack_len
,
76 void const *__needle
, size_t __needle_len
)
77 __attribute__ ((__pure__
));
79 #elif defined GNULIB_POSIXCHECK
81 # define memmem(a,al,b,bl) \
82 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
83 "use gnulib module memmem-simple for portability, " \
84 "and module memmem for speed" ), \
85 memmem (a, al, b, bl))
88 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
92 extern void *mempcpy (void *restrict __dest
, void const *restrict __src
,
95 #elif defined GNULIB_POSIXCHECK
97 # define mempcpy(a,b,n) \
98 (GL_LINK_WARNING ("mempcpy is unportable - " \
99 "use gnulib module mempcpy for portability"), \
103 /* Search backwards through a block for a byte (specified as an int). */
105 # if ! @HAVE_DECL_MEMRCHR@
106 extern void *memrchr (void const *, int, size_t)
107 __attribute__ ((__pure__
));
109 #elif defined GNULIB_POSIXCHECK
111 # define memrchr(a,b,c) \
112 (GL_LINK_WARNING ("memrchr is unportable - " \
113 "use gnulib module memrchr for portability"), \
117 /* Find the first occurrence of C in S. More efficient than
118 memchr(S,C,N), at the expense of undefined behavior if C does not
119 occur within N bytes. */
120 #if @GNULIB_RAWMEMCHR@
121 # if ! @HAVE_RAWMEMCHR@
122 extern void *rawmemchr (void const *__s
, int __c_in
)
123 __attribute__ ((__pure__
));
125 #elif defined GNULIB_POSIXCHECK
127 # define rawmemchr(a,b) \
128 (GL_LINK_WARNING ("rawmemchr is unportable - " \
129 "use gnulib module rawmemchr for portability"), \
133 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
136 extern char *stpcpy (char *restrict __dst
, char const *restrict __src
);
138 #elif defined GNULIB_POSIXCHECK
140 # define stpcpy(a,b) \
141 (GL_LINK_WARNING ("stpcpy is unportable - " \
142 "use gnulib module stpcpy for portability"), \
146 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
147 last non-NUL byte written into DST. */
149 # if ! @HAVE_STPNCPY@
150 # define stpncpy gnu_stpncpy
151 extern char *stpncpy (char *restrict __dst
, char const *restrict __src
,
154 #elif defined GNULIB_POSIXCHECK
156 # define stpncpy(a,b,n) \
157 (GL_LINK_WARNING ("stpncpy is unportable - " \
158 "use gnulib module stpncpy for portability"), \
162 #if defined GNULIB_POSIXCHECK
163 /* strchr() does not work with multibyte strings if the locale encoding is
164 GB18030 and the character to be searched is a digit. */
166 # define strchr(s,c) \
167 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
168 "in some multibyte locales - " \
169 "use mbschr if you care about internationalization"), \
173 /* Find the first occurrence of C in S or the final NUL byte. */
174 #if @GNULIB_STRCHRNUL@
175 # if ! @HAVE_STRCHRNUL@
176 extern char *strchrnul (char const *__s
, int __c_in
)
177 __attribute__ ((__pure__
));
179 #elif defined GNULIB_POSIXCHECK
181 # define strchrnul(a,b) \
182 (GL_LINK_WARNING ("strchrnul is unportable - " \
183 "use gnulib module strchrnul for portability"), \
187 /* Duplicate S, returning an identical malloc'd string. */
189 # if @REPLACE_STRDUP@
191 # define strdup rpl_strdup
193 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
194 extern char *strdup (char const *__s
);
196 #elif defined GNULIB_POSIXCHECK
199 (GL_LINK_WARNING ("strdup is unportable - " \
200 "use gnulib module strdup for portability"), \
204 /* Return a newly allocated copy of at most N bytes of STRING. */
206 # if @REPLACE_STRNDUP@
208 # define strndup rpl_strndup
210 # if @REPLACE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
211 extern char *strndup (char const *__string
, size_t __n
);
213 #elif defined GNULIB_POSIXCHECK
215 # define strndup(a,n) \
216 (GL_LINK_WARNING ("strndup is unportable - " \
217 "use gnulib module strndup for portability"), \
221 /* Find the length (number of bytes) of STRING, but scan at most
222 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
225 # if ! @HAVE_DECL_STRNLEN@
226 extern size_t strnlen (char const *__string
, size_t __maxlen
)
227 __attribute__ ((__pure__
));
229 #elif defined GNULIB_POSIXCHECK
231 # define strnlen(a,n) \
232 (GL_LINK_WARNING ("strnlen is unportable - " \
233 "use gnulib module strnlen for portability"), \
237 #if defined GNULIB_POSIXCHECK
238 /* strcspn() assumes the second argument is a list of single-byte characters.
239 Even in this simple case, it does not work with multibyte strings if the
240 locale encoding is GB18030 and one of the characters to be searched is a
243 # define strcspn(s,a) \
244 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
245 "in multibyte locales - " \
246 "use mbscspn if you care about internationalization"), \
250 /* Find the first occurrence in S of any character in ACCEPT. */
252 # if ! @HAVE_STRPBRK@
253 extern char *strpbrk (char const *__s
, char const *__accept
)
254 __attribute__ ((__pure__
));
256 # if defined GNULIB_POSIXCHECK
257 /* strpbrk() assumes the second argument is a list of single-byte characters.
258 Even in this simple case, it does not work with multibyte strings if the
259 locale encoding is GB18030 and one of the characters to be searched is a
262 # define strpbrk(s,a) \
263 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
264 "in multibyte locales - " \
265 "use mbspbrk if you care about internationalization"), \
268 #elif defined GNULIB_POSIXCHECK
270 # define strpbrk(s,a) \
271 (GL_LINK_WARNING ("strpbrk is unportable - " \
272 "use gnulib module strpbrk for portability"), \
276 #if defined GNULIB_POSIXCHECK
277 /* strspn() assumes the second argument is a list of single-byte characters.
278 Even in this simple case, it cannot work with multibyte strings. */
280 # define strspn(s,a) \
281 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
282 "in multibyte locales - " \
283 "use mbsspn if you care about internationalization"), \
287 #if defined GNULIB_POSIXCHECK
288 /* strrchr() does not work with multibyte strings if the locale encoding is
289 GB18030 and the character to be searched is a digit. */
291 # define strrchr(s,c) \
292 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
293 "in some multibyte locales - " \
294 "use mbsrchr if you care about internationalization"), \
298 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
299 If one is found, overwrite it with a NUL, and advance *STRINGP
300 to point to the next char after it. Otherwise, set *STRINGP to NULL.
301 If *STRINGP was already NULL, nothing happens.
302 Return the old value of *STRINGP.
304 This is a variant of strtok() that is multithread-safe and supports
307 Caveat: It modifies the original string.
308 Caveat: These functions cannot be used on constant strings.
309 Caveat: The identity of the delimiting character is lost.
310 Caveat: It doesn't work with multibyte strings unless all of the delimiter
311 characters are ASCII characters < 0x30.
313 See also strtok_r(). */
316 extern char *strsep (char **restrict __stringp
, char const *restrict __delim
);
318 # if defined GNULIB_POSIXCHECK
320 # define strsep(s,d) \
321 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
322 "in multibyte locales - " \
323 "use mbssep if you care about internationalization"), \
326 #elif defined GNULIB_POSIXCHECK
328 # define strsep(s,d) \
329 (GL_LINK_WARNING ("strsep is unportable - " \
330 "use gnulib module strsep for portability"), \
335 # if @REPLACE_STRSTR@
336 # define strstr rpl_strstr
337 char *strstr (const char *haystack
, const char *needle
)
338 __attribute__ ((__pure__
));
340 #elif defined GNULIB_POSIXCHECK
341 /* strstr() does not work with multibyte strings if the locale encoding is
342 different from UTF-8:
343 POSIX says that it operates on "strings", and "string" in POSIX is defined
344 as a sequence of bytes, not of characters. */
346 # define strstr(a,b) \
347 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
348 "work correctly on character strings in most " \
349 "multibyte locales - " \
350 "use mbsstr if you care about internationalization, " \
351 "or use strstr if you care about speed"), \
355 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
357 #if @GNULIB_STRCASESTR@
358 # if @REPLACE_STRCASESTR@
359 # define strcasestr rpl_strcasestr
361 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
362 extern char *strcasestr (const char *haystack
, const char *needle
)
363 __attribute__ ((__pure__
));
365 #elif defined GNULIB_POSIXCHECK
366 /* strcasestr() does not work with multibyte strings:
367 It is a glibc extension, and glibc implements it only for unibyte
370 # define strcasestr(a,b) \
371 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
372 "in multibyte locales - " \
373 "use mbscasestr if you care about " \
374 "internationalization, or use c-strcasestr if you want " \
375 "a locale independent function"), \
379 /* Parse S into tokens separated by characters in DELIM.
380 If S is NULL, the saved pointer in SAVE_PTR is used as
381 the next starting point. For example:
382 char s[] = "-abc-=-def";
384 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
385 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
386 x = strtok_r(NULL, "=", &sp); // x = NULL
389 This is a variant of strtok() that is multithread-safe.
391 For the POSIX documentation for this function, see:
392 http://www.opengroup.org/susv3xsh/strtok.html
394 Caveat: It modifies the original string.
395 Caveat: These functions cannot be used on constant strings.
396 Caveat: The identity of the delimiting character is lost.
397 Caveat: It doesn't work with multibyte strings unless all of the delimiter
398 characters are ASCII characters < 0x30.
400 See also strsep(). */
401 #if @GNULIB_STRTOK_R@
402 # if @REPLACE_STRTOK_R@
404 # define strtok_r rpl_strtok_r
405 # elif @UNDEFINE_STRTOK_R@
408 # if ! @HAVE_DECL_STRTOK_R@ || @REPLACE_STRTOK_R@
409 extern char *strtok_r (char *restrict s
, char const *restrict delim
,
410 char **restrict save_ptr
);
412 # if defined GNULIB_POSIXCHECK
414 # define strtok_r(s,d,p) \
415 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
416 "in multibyte locales - " \
417 "use mbstok_r if you care about internationalization"), \
420 #elif defined GNULIB_POSIXCHECK
422 # define strtok_r(s,d,p) \
423 (GL_LINK_WARNING ("strtok_r is unportable - " \
424 "use gnulib module strtok_r for portability"), \
429 /* The following functions are not specified by POSIX. They are gnulib
433 /* Return the number of multibyte characters in the character string STRING.
434 This considers multibyte characters, unlike strlen, which counts bytes. */
435 extern size_t mbslen (const char *string
);
439 /* Return the number of multibyte characters in the character string starting
440 at STRING and ending at STRING + LEN. */
441 extern size_t mbsnlen (const char *string
, size_t len
);
445 /* Locate the first single-byte character C in the character string STRING,
446 and return a pointer to it. Return NULL if C is not found in STRING.
447 Unlike strchr(), this function works correctly in multibyte locales with
448 encodings such as GB18030. */
449 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
450 extern char * mbschr (const char *string
, int c
);
454 /* Locate the last single-byte character C in the character string STRING,
455 and return a pointer to it. Return NULL if C is not found in STRING.
456 Unlike strrchr(), this function works correctly in multibyte locales with
457 encodings such as GB18030. */
458 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
459 extern char * mbsrchr (const char *string
, int c
);
463 /* Find the first occurrence of the character string NEEDLE in the character
464 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
465 Unlike strstr(), this function works correctly in multibyte locales with
466 encodings different from UTF-8. */
467 extern char * mbsstr (const char *haystack
, const char *needle
);
470 #if @GNULIB_MBSCASECMP@
471 /* Compare the character strings S1 and S2, ignoring case, returning less than,
472 equal to or greater than zero if S1 is lexicographically less than, equal to
474 Note: This function may, in multibyte locales, return 0 for strings of
476 Unlike strcasecmp(), this function works correctly in multibyte locales. */
477 extern int mbscasecmp (const char *s1
, const char *s2
);
480 #if @GNULIB_MBSNCASECMP@
481 /* Compare the initial segment of the character string S1 consisting of at most
482 N characters with the initial segment of the character string S2 consisting
483 of at most N characters, ignoring case, returning less than, equal to or
484 greater than zero if the initial segment of S1 is lexicographically less
485 than, equal to or greater than the initial segment of S2.
486 Note: This function may, in multibyte locales, return 0 for initial segments
487 of different lengths!
488 Unlike strncasecmp(), this function works correctly in multibyte locales.
489 But beware that N is not a byte count but a character count! */
490 extern int mbsncasecmp (const char *s1
, const char *s2
, size_t n
);
493 #if @GNULIB_MBSPCASECMP@
494 /* Compare the initial segment of the character string STRING consisting of
495 at most mbslen (PREFIX) characters with the character string PREFIX,
496 ignoring case, returning less than, equal to or greater than zero if this
497 initial segment is lexicographically less than, equal to or greater than
499 Note: This function may, in multibyte locales, return 0 if STRING is of
500 smaller length than PREFIX!
501 Unlike strncasecmp(), this function works correctly in multibyte
503 extern char * mbspcasecmp (const char *string
, const char *prefix
);
506 #if @GNULIB_MBSCASESTR@
507 /* Find the first occurrence of the character string NEEDLE in the character
508 string HAYSTACK, using case-insensitive comparison.
509 Note: This function may, in multibyte locales, return success even if
510 strlen (haystack) < strlen (needle) !
511 Unlike strcasestr(), this function works correctly in multibyte locales. */
512 extern char * mbscasestr (const char *haystack
, const char *needle
);
516 /* Find the first occurrence in the character string STRING of any character
517 in the character string ACCEPT. Return the number of bytes from the
518 beginning of the string to this occurrence, or to the end of the string
520 Unlike strcspn(), this function works correctly in multibyte locales. */
521 extern size_t mbscspn (const char *string
, const char *accept
);
525 /* Find the first occurrence in the character string STRING of any character
526 in the character string ACCEPT. Return the pointer to it, or NULL if none
528 Unlike strpbrk(), this function works correctly in multibyte locales. */
529 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
530 extern char * mbspbrk (const char *string
, const char *accept
);
534 /* Find the first occurrence in the character string STRING of any character
535 not in the character string REJECT. Return the number of bytes from the
536 beginning of the string to this occurrence, or to the end of the string
538 Unlike strspn(), this function works correctly in multibyte locales. */
539 extern size_t mbsspn (const char *string
, const char *reject
);
543 /* Search the next delimiter (multibyte character listed in the character
544 string DELIM) starting at the character string *STRINGP.
545 If one is found, overwrite it with a NUL, and advance *STRINGP to point
546 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
547 If *STRINGP was already NULL, nothing happens.
548 Return the old value of *STRINGP.
550 This is a variant of mbstok_r() that supports empty fields.
552 Caveat: It modifies the original string.
553 Caveat: These functions cannot be used on constant strings.
554 Caveat: The identity of the delimiting character is lost.
556 See also mbstok_r(). */
557 extern char * mbssep (char **stringp
, const char *delim
);
560 #if @GNULIB_MBSTOK_R@
561 /* Parse the character string STRING into tokens separated by characters in
562 the character string DELIM.
563 If STRING is NULL, the saved pointer in SAVE_PTR is used as
564 the next starting point. For example:
565 char s[] = "-abc-=-def";
567 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
568 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
569 x = mbstok_r(NULL, "=", &sp); // x = NULL
572 Caveat: It modifies the original string.
573 Caveat: These functions cannot be used on constant strings.
574 Caveat: The identity of the delimiting character is lost.
576 See also mbssep(). */
577 extern char * mbstok_r (char *string
, const char *delim
, char **save_ptr
);
580 /* Map any int, typically from errno, into an error message. */
581 #if @GNULIB_STRERROR@
582 # if @REPLACE_STRERROR@
584 # define strerror rpl_strerror
585 extern char *strerror (int);
587 #elif defined GNULIB_POSIXCHECK
589 # define strerror(e) \
590 (GL_LINK_WARNING ("strerror is unportable - " \
591 "use gnulib module strerror to guarantee non-NULL result"), \
595 #if @GNULIB_STRSIGNAL@
596 # if @REPLACE_STRSIGNAL@
597 # define strsignal rpl_strsignal
599 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
600 extern char *strsignal (int __sig
);
602 #elif defined GNULIB_POSIXCHECK
604 # define strsignal(a) \
605 (GL_LINK_WARNING ("strsignal is unportable - " \
606 "use gnulib module strsignal for portability"), \
610 #if @GNULIB_STRVERSCMP@
611 # if !@HAVE_STRVERSCMP@
612 extern int strverscmp (const char *, const char *);
614 #elif defined GNULIB_POSIXCHECK
616 # define strverscmp(a, b) \
617 (GL_LINK_WARNING ("strverscmp is unportable - " \
618 "use gnulib module strverscmp for portability"), \
627 #endif /* _GL_STRING_H */
628 #endif /* _GL_STRING_H */