1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2008 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. */
21 @PRAGMA_SYSTEM_HEADER@
23 /* The include_next requires a split double-inclusion guard. */
24 #@INCLUDE_NEXT@ @NEXT_STRING_H@
31 /* This feature is available in gcc versions 2.5 and later. */
32 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
33 # define __attribute__(Spec) /* empty */
35 /* The attribute __pure__ was added in gcc 2.96. */
36 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
37 # define __pure__ /* empty */
42 /* The definition of GL_LINK_WARNING is copied here. */
50 /* Return the first occurrence of NEEDLE in HAYSTACK. */
53 # define memmem rpl_memmem
55 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
56 extern void *memmem (void const *__haystack, size_t __haystack_len,
57 void const *__needle, size_t __needle_len)
58 __attribute__ ((__pure__));
60 #elif defined GNULIB_POSIXCHECK
62 # define memmem(a,al,b,bl) \
63 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
64 "use gnulib module memmem-simple for portability, " \
65 "and module memmem for speed" ), \
66 memmem (a, al, b, bl))
69 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
73 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
76 #elif defined GNULIB_POSIXCHECK
78 # define mempcpy(a,b,n) \
79 (GL_LINK_WARNING ("mempcpy is unportable - " \
80 "use gnulib module mempcpy for portability"), \
84 /* Search backwards through a block for a byte (specified as an int). */
86 # if ! @HAVE_DECL_MEMRCHR@
87 extern void *memrchr (void const *, int, size_t)
88 __attribute__ ((__pure__));
90 #elif defined GNULIB_POSIXCHECK
92 # define memrchr(a,b,c) \
93 (GL_LINK_WARNING ("memrchr is unportable - " \
94 "use gnulib module memrchr for portability"), \
98 /* Find the first occurrence of C in S. More efficient than
99 memchr(S,C,N), at the expense of undefined behavior if C does not
100 occur within N bytes. */
101 #if @GNULIB_RAWMEMCHR@
102 # if ! @HAVE_RAWMEMCHR@
103 extern void *rawmemchr (void const *__s, int __c_in)
104 __attribute__ ((__pure__));
106 #elif defined GNULIB_POSIXCHECK
108 # define rawmemchr(a,b) \
109 (GL_LINK_WARNING ("rawmemchr is unportable - " \
110 "use gnulib module rawmemchr for portability"), \
114 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
117 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
119 #elif defined GNULIB_POSIXCHECK
121 # define stpcpy(a,b) \
122 (GL_LINK_WARNING ("stpcpy is unportable - " \
123 "use gnulib module stpcpy for portability"), \
127 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
128 last non-NUL byte written into DST. */
130 # if ! @HAVE_STPNCPY@
131 # define stpncpy gnu_stpncpy
132 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
135 #elif defined GNULIB_POSIXCHECK
137 # define stpncpy(a,b,n) \
138 (GL_LINK_WARNING ("stpncpy is unportable - " \
139 "use gnulib module stpncpy for portability"), \
143 #if defined GNULIB_POSIXCHECK
144 /* strchr() does not work with multibyte strings if the locale encoding is
145 GB18030 and the character to be searched is a digit. */
147 # define strchr(s,c) \
148 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
149 "in some multibyte locales - " \
150 "use mbschr if you care about internationalization"), \
154 /* Find the first occurrence of C in S or the final NUL byte. */
155 #if @GNULIB_STRCHRNUL@
156 # if ! @HAVE_STRCHRNUL@
157 extern char *strchrnul (char const *__s, int __c_in)
158 __attribute__ ((__pure__));
160 #elif defined GNULIB_POSIXCHECK
162 # define strchrnul(a,b) \
163 (GL_LINK_WARNING ("strchrnul is unportable - " \
164 "use gnulib module strchrnul for portability"), \
168 /* Duplicate S, returning an identical malloc'd string. */
170 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
171 extern char *strdup (char const *__s);
173 #elif defined GNULIB_POSIXCHECK
176 (GL_LINK_WARNING ("strdup is unportable - " \
177 "use gnulib module strdup for portability"), \
181 /* Return a newly allocated copy of at most N bytes of STRING. */
183 # if ! @HAVE_STRNDUP@
185 # define strndup rpl_strndup
187 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
188 extern char *strndup (char const *__string, size_t __n);
190 #elif defined GNULIB_POSIXCHECK
192 # define strndup(a,n) \
193 (GL_LINK_WARNING ("strndup is unportable - " \
194 "use gnulib module strndup for portability"), \
198 /* Find the length (number of bytes) of STRING, but scan at most
199 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
202 # if ! @HAVE_DECL_STRNLEN@
203 extern size_t strnlen (char const *__string, size_t __maxlen)
204 __attribute__ ((__pure__));
206 #elif defined GNULIB_POSIXCHECK
208 # define strnlen(a,n) \
209 (GL_LINK_WARNING ("strnlen is unportable - " \
210 "use gnulib module strnlen for portability"), \
214 #if defined GNULIB_POSIXCHECK
215 /* strcspn() assumes the second argument is a list of single-byte characters.
216 Even in this simple case, it does not work with multibyte strings if the
217 locale encoding is GB18030 and one of the characters to be searched is a
220 # define strcspn(s,a) \
221 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
222 "in multibyte locales - " \
223 "use mbscspn if you care about internationalization"), \
227 /* Find the first occurrence in S of any character in ACCEPT. */
229 # if ! @HAVE_STRPBRK@
230 extern char *strpbrk (char const *__s, char const *__accept)
231 __attribute__ ((__pure__));
233 # if defined GNULIB_POSIXCHECK
234 /* strpbrk() assumes the second argument is a list of single-byte characters.
235 Even in this simple case, it does not work with multibyte strings if the
236 locale encoding is GB18030 and one of the characters to be searched is a
239 # define strpbrk(s,a) \
240 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
241 "in multibyte locales - " \
242 "use mbspbrk if you care about internationalization"), \
245 #elif defined GNULIB_POSIXCHECK
247 # define strpbrk(s,a) \
248 (GL_LINK_WARNING ("strpbrk is unportable - " \
249 "use gnulib module strpbrk for portability"), \
253 #if defined GNULIB_POSIXCHECK
254 /* strspn() assumes the second argument is a list of single-byte characters.
255 Even in this simple case, it cannot work with multibyte strings. */
257 # define strspn(s,a) \
258 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
259 "in multibyte locales - " \
260 "use mbsspn if you care about internationalization"), \
264 #if defined GNULIB_POSIXCHECK
265 /* strrchr() does not work with multibyte strings if the locale encoding is
266 GB18030 and the character to be searched is a digit. */
268 # define strrchr(s,c) \
269 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
270 "in some multibyte locales - " \
271 "use mbsrchr if you care about internationalization"), \
275 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
276 If one is found, overwrite it with a NUL, and advance *STRINGP
277 to point to the next char after it. Otherwise, set *STRINGP to NULL.
278 If *STRINGP was already NULL, nothing happens.
279 Return the old value of *STRINGP.
281 This is a variant of strtok() that is multithread-safe and supports
284 Caveat: It modifies the original string.
285 Caveat: These functions cannot be used on constant strings.
286 Caveat: The identity of the delimiting character is lost.
287 Caveat: It doesn't work with multibyte strings unless all of the delimiter
288 characters are ASCII characters < 0x30.
290 See also strtok_r(). */
293 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
295 # if defined GNULIB_POSIXCHECK
297 # define strsep(s,d) \
298 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
299 "in multibyte locales - " \
300 "use mbssep if you care about internationalization"), \
303 #elif defined GNULIB_POSIXCHECK
305 # define strsep(s,d) \
306 (GL_LINK_WARNING ("strsep is unportable - " \
307 "use gnulib module strsep for portability"), \
312 # if @REPLACE_STRSTR@
313 # define strstr rpl_strstr
314 char *strstr (const char *haystack, const char *needle)
315 __attribute__ ((__pure__));
317 #elif defined GNULIB_POSIXCHECK
318 /* strstr() does not work with multibyte strings if the locale encoding is
319 different from UTF-8:
320 POSIX says that it operates on "strings", and "string" in POSIX is defined
321 as a sequence of bytes, not of characters. */
323 # define strstr(a,b) \
324 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
325 "work correctly on character strings in most " \
326 "multibyte locales - " \
327 "use mbsstr if you care about internationalization, " \
328 "or use strstr if you care about speed"), \
332 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
334 #if @GNULIB_STRCASESTR@
335 # if @REPLACE_STRCASESTR@
336 # define strcasestr rpl_strcasestr
338 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
339 extern char *strcasestr (const char *haystack, const char *needle)
340 __attribute__ ((__pure__));
342 #elif defined GNULIB_POSIXCHECK
343 /* strcasestr() does not work with multibyte strings:
344 It is a glibc extension, and glibc implements it only for unibyte
347 # define strcasestr(a,b) \
348 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
349 "in multibyte locales - " \
350 "use mbscasestr if you care about " \
351 "internationalization, or use c-strcasestr if you want " \
352 "a locale independent function"), \
356 /* Parse S into tokens separated by characters in DELIM.
357 If S is NULL, the saved pointer in SAVE_PTR is used as
358 the next starting point. For example:
359 char s[] = "-abc-=-def";
361 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
362 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
363 x = strtok_r(NULL, "=", &sp); // x = NULL
366 This is a variant of strtok() that is multithread-safe.
368 For the POSIX documentation for this function, see:
369 http://www.opengroup.org/susv3xsh/strtok.html
371 Caveat: It modifies the original string.
372 Caveat: These functions cannot be used on constant strings.
373 Caveat: The identity of the delimiting character is lost.
374 Caveat: It doesn't work with multibyte strings unless all of the delimiter
375 characters are ASCII characters < 0x30.
377 See also strsep(). */
378 #if @GNULIB_STRTOK_R@
379 # if ! @HAVE_DECL_STRTOK_R@
380 extern char *strtok_r (char *restrict s, char const *restrict delim,
381 char **restrict save_ptr);
383 # if defined GNULIB_POSIXCHECK
385 # define strtok_r(s,d,p) \
386 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
387 "in multibyte locales - " \
388 "use mbstok_r if you care about internationalization"), \
391 #elif defined GNULIB_POSIXCHECK
393 # define strtok_r(s,d,p) \
394 (GL_LINK_WARNING ("strtok_r is unportable - " \
395 "use gnulib module strtok_r for portability"), \
400 /* The following functions are not specified by POSIX. They are gnulib
404 /* Return the number of multibyte characters in the character string STRING.
405 This considers multibyte characters, unlike strlen, which counts bytes. */
406 extern size_t mbslen (const char *string);
410 /* Return the number of multibyte characters in the character string starting
411 at STRING and ending at STRING + LEN. */
412 extern size_t mbsnlen (const char *string, size_t len);
416 /* Locate the first single-byte character C in the character string STRING,
417 and return a pointer to it. Return NULL if C is not found in STRING.
418 Unlike strchr(), this function works correctly in multibyte locales with
419 encodings such as GB18030. */
420 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
421 extern char * mbschr (const char *string, int c);
425 /* Locate the last single-byte character C in the character string STRING,
426 and return a pointer to it. Return NULL if C is not found in STRING.
427 Unlike strrchr(), this function works correctly in multibyte locales with
428 encodings such as GB18030. */
429 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
430 extern char * mbsrchr (const char *string, int c);
434 /* Find the first occurrence of the character string NEEDLE in the character
435 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
436 Unlike strstr(), this function works correctly in multibyte locales with
437 encodings different from UTF-8. */
438 extern char * mbsstr (const char *haystack, const char *needle);
441 #if @GNULIB_MBSCASECMP@
442 /* Compare the character strings S1 and S2, ignoring case, returning less than,
443 equal to or greater than zero if S1 is lexicographically less than, equal to
445 Note: This function may, in multibyte locales, return 0 for strings of
447 Unlike strcasecmp(), this function works correctly in multibyte locales. */
448 extern int mbscasecmp (const char *s1, const char *s2);
451 #if @GNULIB_MBSNCASECMP@
452 /* Compare the initial segment of the character string S1 consisting of at most
453 N characters with the initial segment of the character string S2 consisting
454 of at most N characters, ignoring case, returning less than, equal to or
455 greater than zero if the initial segment of S1 is lexicographically less
456 than, equal to or greater than the initial segment of S2.
457 Note: This function may, in multibyte locales, return 0 for initial segments
458 of different lengths!
459 Unlike strncasecmp(), this function works correctly in multibyte locales.
460 But beware that N is not a byte count but a character count! */
461 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
464 #if @GNULIB_MBSPCASECMP@
465 /* Compare the initial segment of the character string STRING consisting of
466 at most mbslen (PREFIX) characters with the character string PREFIX,
467 ignoring case, returning less than, equal to or greater than zero if this
468 initial segment is lexicographically less than, equal to or greater than
470 Note: This function may, in multibyte locales, return 0 if STRING is of
471 smaller length than PREFIX!
472 Unlike strncasecmp(), this function works correctly in multibyte
474 extern char * mbspcasecmp (const char *string, const char *prefix);
477 #if @GNULIB_MBSCASESTR@
478 /* Find the first occurrence of the character string NEEDLE in the character
479 string HAYSTACK, using case-insensitive comparison.
480 Note: This function may, in multibyte locales, return success even if
481 strlen (haystack) < strlen (needle) !
482 Unlike strcasestr(), this function works correctly in multibyte locales. */
483 extern char * mbscasestr (const char *haystack, const char *needle);
487 /* Find the first occurrence in the character string STRING of any character
488 in the character string ACCEPT. Return the number of bytes from the
489 beginning of the string to this occurrence, or to the end of the string
491 Unlike strcspn(), this function works correctly in multibyte locales. */
492 extern size_t mbscspn (const char *string, const char *accept);
496 /* Find the first occurrence in the character string STRING of any character
497 in the character string ACCEPT. Return the pointer to it, or NULL if none
499 Unlike strpbrk(), this function works correctly in multibyte locales. */
500 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
501 extern char * mbspbrk (const char *string, const char *accept);
505 /* Find the first occurrence in the character string STRING of any character
506 not in the character string REJECT. Return the number of bytes from the
507 beginning of the string to this occurrence, or to the end of the string
509 Unlike strspn(), this function works correctly in multibyte locales. */
510 extern size_t mbsspn (const char *string, const char *reject);
514 /* Search the next delimiter (multibyte character listed in the character
515 string DELIM) starting at the character string *STRINGP.
516 If one is found, overwrite it with a NUL, and advance *STRINGP to point
517 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
518 If *STRINGP was already NULL, nothing happens.
519 Return the old value of *STRINGP.
521 This is a variant of mbstok_r() that supports empty fields.
523 Caveat: It modifies the original string.
524 Caveat: These functions cannot be used on constant strings.
525 Caveat: The identity of the delimiting character is lost.
527 See also mbstok_r(). */
528 extern char * mbssep (char **stringp, const char *delim);
531 #if @GNULIB_MBSTOK_R@
532 /* Parse the character string STRING into tokens separated by characters in
533 the character string DELIM.
534 If STRING is NULL, the saved pointer in SAVE_PTR is used as
535 the next starting point. For example:
536 char s[] = "-abc-=-def";
538 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
539 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
540 x = mbstok_r(NULL, "=", &sp); // x = NULL
543 Caveat: It modifies the original string.
544 Caveat: These functions cannot be used on constant strings.
545 Caveat: The identity of the delimiting character is lost.
547 See also mbssep(). */
548 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
551 /* Map any int, typically from errno, into an error message. */
552 #if @GNULIB_STRERROR@
553 # if @REPLACE_STRERROR@
555 # define strerror rpl_strerror
556 extern char *strerror (int);
558 #elif defined GNULIB_POSIXCHECK
560 # define strerror(e) \
561 (GL_LINK_WARNING ("strerror is unportable - " \
562 "use gnulib module strerror to guarantee non-NULL result"), \
566 #if @GNULIB_STRSIGNAL@
567 # if @REPLACE_STRSIGNAL@
568 # define strsignal rpl_strsignal
570 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
571 extern char *strsignal (int __sig);
573 #elif defined GNULIB_POSIXCHECK
575 # define strsignal(a) \
576 (GL_LINK_WARNING ("strsignal is unportable - " \
577 "use gnulib module strsignal for portability"), \
581 #if @GNULIB_STRVERSCMP@
582 # if !@HAVE_STRVERSCMP@
583 extern int strverscmp (const char *, const char *);
585 #elif defined GNULIB_POSIXCHECK
587 # define strverscmp(a, b) \
588 (GL_LINK_WARNING ("strverscmp is unportable - " \
589 "use gnulib module strverscmp for portability"), \
598 #endif /* _GL_STRING_H */
599 #endif /* _GL_STRING_H */