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 /* The include_next requires a split double-inclusion guard. */
22 #@INCLUDE_NEXT@ @NEXT_STRING_H@
29 /* This feature is available in gcc versions 2.5 and later. */
30 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
31 # define __attribute__(Spec) /* empty */
33 /* The attribute __pure__ was added in gcc 2.96. */
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
35 # define __pure__ /* empty */
40 /* The definition of GL_LINK_WARNING is copied here. */
48 /* Return the first occurrence of NEEDLE in HAYSTACK. */
51 # define memmem rpl_memmem
53 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
54 extern void *memmem (void const *__haystack, size_t __haystack_len,
55 void const *__needle, size_t __needle_len)
56 __attribute__ ((__pure__));
58 #elif defined GNULIB_POSIXCHECK
60 # define memmem(a,al,b,bl) \
61 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
62 "use gnulib module memmem-simple for portability, " \
63 "and module memmem for speed" ), \
64 memmem (a, al, b, bl))
67 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
71 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
74 #elif defined GNULIB_POSIXCHECK
76 # define mempcpy(a,b,n) \
77 (GL_LINK_WARNING ("mempcpy is unportable - " \
78 "use gnulib module mempcpy for portability"), \
82 /* Search backwards through a block for a byte (specified as an int). */
84 # if ! @HAVE_DECL_MEMRCHR@
85 extern void *memrchr (void const *, int, size_t)
86 __attribute__ ((__pure__));
88 #elif defined GNULIB_POSIXCHECK
90 # define memrchr(a,b,c) \
91 (GL_LINK_WARNING ("memrchr is unportable - " \
92 "use gnulib module memrchr for portability"), \
96 /* Find the first occurrence of C in S. More efficient than
97 memchr(S,C,N), at the expense of undefined behavior if C does not
98 occur within N bytes. */
99 #if @GNULIB_RAWMEMCHR@
100 # if ! @HAVE_RAWMEMCHR@
101 extern void *rawmemchr (void const *__s, int __c_in)
102 __attribute__ ((__pure__));
104 #elif defined GNULIB_POSIXCHECK
106 # define rawmemchr(a,b) \
107 (GL_LINK_WARNING ("rawmemchr is unportable - " \
108 "use gnulib module rawmemchr for portability"), \
112 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
115 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
117 #elif defined GNULIB_POSIXCHECK
119 # define stpcpy(a,b) \
120 (GL_LINK_WARNING ("stpcpy is unportable - " \
121 "use gnulib module stpcpy for portability"), \
125 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
126 last non-NUL byte written into DST. */
128 # if ! @HAVE_STPNCPY@
129 # define stpncpy gnu_stpncpy
130 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
133 #elif defined GNULIB_POSIXCHECK
135 # define stpncpy(a,b,n) \
136 (GL_LINK_WARNING ("stpncpy is unportable - " \
137 "use gnulib module stpncpy for portability"), \
141 #if defined GNULIB_POSIXCHECK
142 /* strchr() does not work with multibyte strings if the locale encoding is
143 GB18030 and the character to be searched is a digit. */
145 # define strchr(s,c) \
146 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
147 "in some multibyte locales - " \
148 "use mbschr if you care about internationalization"), \
152 /* Find the first occurrence of C in S or the final NUL byte. */
153 #if @GNULIB_STRCHRNUL@
154 # if ! @HAVE_STRCHRNUL@
155 extern char *strchrnul (char const *__s, int __c_in)
156 __attribute__ ((__pure__));
158 #elif defined GNULIB_POSIXCHECK
160 # define strchrnul(a,b) \
161 (GL_LINK_WARNING ("strchrnul is unportable - " \
162 "use gnulib module strchrnul for portability"), \
166 /* Duplicate S, returning an identical malloc'd string. */
168 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
169 extern char *strdup (char const *__s);
171 #elif defined GNULIB_POSIXCHECK
174 (GL_LINK_WARNING ("strdup is unportable - " \
175 "use gnulib module strdup for portability"), \
179 /* Return a newly allocated copy of at most N bytes of STRING. */
181 # if ! @HAVE_STRNDUP@
183 # define strndup rpl_strndup
185 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
186 extern char *strndup (char const *__string, size_t __n);
188 #elif defined GNULIB_POSIXCHECK
190 # define strndup(a,n) \
191 (GL_LINK_WARNING ("strndup is unportable - " \
192 "use gnulib module strndup for portability"), \
196 /* Find the length (number of bytes) of STRING, but scan at most
197 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
200 # if ! @HAVE_DECL_STRNLEN@
201 extern size_t strnlen (char const *__string, size_t __maxlen)
202 __attribute__ ((__pure__));
204 #elif defined GNULIB_POSIXCHECK
206 # define strnlen(a,n) \
207 (GL_LINK_WARNING ("strnlen is unportable - " \
208 "use gnulib module strnlen for portability"), \
212 #if defined GNULIB_POSIXCHECK
213 /* strcspn() assumes the second argument is a list of single-byte characters.
214 Even in this simple case, it does not work with multibyte strings if the
215 locale encoding is GB18030 and one of the characters to be searched is a
218 # define strcspn(s,a) \
219 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
220 "in multibyte locales - " \
221 "use mbscspn if you care about internationalization"), \
225 /* Find the first occurrence in S of any character in ACCEPT. */
227 # if ! @HAVE_STRPBRK@
228 extern char *strpbrk (char const *__s, char const *__accept)
229 __attribute__ ((__pure__));
231 # if defined GNULIB_POSIXCHECK
232 /* strpbrk() assumes the second argument is a list of single-byte characters.
233 Even in this simple case, it does not work with multibyte strings if the
234 locale encoding is GB18030 and one of the characters to be searched is a
237 # define strpbrk(s,a) \
238 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
239 "in multibyte locales - " \
240 "use mbspbrk if you care about internationalization"), \
243 #elif defined GNULIB_POSIXCHECK
245 # define strpbrk(s,a) \
246 (GL_LINK_WARNING ("strpbrk is unportable - " \
247 "use gnulib module strpbrk for portability"), \
251 #if defined GNULIB_POSIXCHECK
252 /* strspn() assumes the second argument is a list of single-byte characters.
253 Even in this simple case, it cannot work with multibyte strings. */
255 # define strspn(s,a) \
256 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
257 "in multibyte locales - " \
258 "use mbsspn if you care about internationalization"), \
262 #if defined GNULIB_POSIXCHECK
263 /* strrchr() does not work with multibyte strings if the locale encoding is
264 GB18030 and the character to be searched is a digit. */
266 # define strrchr(s,c) \
267 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
268 "in some multibyte locales - " \
269 "use mbsrchr if you care about internationalization"), \
273 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
274 If one is found, overwrite it with a NUL, and advance *STRINGP
275 to point to the next char after it. Otherwise, set *STRINGP to NULL.
276 If *STRINGP was already NULL, nothing happens.
277 Return the old value of *STRINGP.
279 This is a variant of strtok() that is multithread-safe and supports
282 Caveat: It modifies the original string.
283 Caveat: These functions cannot be used on constant strings.
284 Caveat: The identity of the delimiting character is lost.
285 Caveat: It doesn't work with multibyte strings unless all of the delimiter
286 characters are ASCII characters < 0x30.
288 See also strtok_r(). */
291 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
293 # if defined GNULIB_POSIXCHECK
295 # define strsep(s,d) \
296 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
297 "in multibyte locales - " \
298 "use mbssep if you care about internationalization"), \
301 #elif defined GNULIB_POSIXCHECK
303 # define strsep(s,d) \
304 (GL_LINK_WARNING ("strsep is unportable - " \
305 "use gnulib module strsep for portability"), \
310 # if @REPLACE_STRSTR@
311 # define strstr rpl_strstr
312 char *strstr (const char *haystack, const char *needle)
313 __attribute__ ((__pure__));
315 #elif defined GNULIB_POSIXCHECK
316 /* strstr() does not work with multibyte strings if the locale encoding is
317 different from UTF-8:
318 POSIX says that it operates on "strings", and "string" in POSIX is defined
319 as a sequence of bytes, not of characters. */
321 # define strstr(a,b) \
322 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
323 "work correctly on character strings in most " \
324 "multibyte locales - " \
325 "use mbsstr if you care about internationalization, " \
326 "or use strstr if you care about speed"), \
330 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
332 #if @GNULIB_STRCASESTR@
333 # if @REPLACE_STRCASESTR@
334 # define strcasestr rpl_strcasestr
336 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
337 extern char *strcasestr (const char *haystack, const char *needle)
338 __attribute__ ((__pure__));
340 #elif defined GNULIB_POSIXCHECK
341 /* strcasestr() does not work with multibyte strings:
342 It is a glibc extension, and glibc implements it only for unibyte
345 # define strcasestr(a,b) \
346 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
347 "in multibyte locales - " \
348 "use mbscasestr if you care about " \
349 "internationalization, or use c-strcasestr if you want " \
350 "a locale independent function"), \
354 /* Parse S into tokens separated by characters in DELIM.
355 If S is NULL, the saved pointer in SAVE_PTR is used as
356 the next starting point. For example:
357 char s[] = "-abc-=-def";
359 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
360 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
361 x = strtok_r(NULL, "=", &sp); // x = NULL
364 This is a variant of strtok() that is multithread-safe.
366 For the POSIX documentation for this function, see:
367 http://www.opengroup.org/susv3xsh/strtok.html
369 Caveat: It modifies the original string.
370 Caveat: These functions cannot be used on constant strings.
371 Caveat: The identity of the delimiting character is lost.
372 Caveat: It doesn't work with multibyte strings unless all of the delimiter
373 characters are ASCII characters < 0x30.
375 See also strsep(). */
376 #if @GNULIB_STRTOK_R@
377 # if ! @HAVE_DECL_STRTOK_R@
378 extern char *strtok_r (char *restrict s, char const *restrict delim,
379 char **restrict save_ptr);
381 # if defined GNULIB_POSIXCHECK
383 # define strtok_r(s,d,p) \
384 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
385 "in multibyte locales - " \
386 "use mbstok_r if you care about internationalization"), \
389 #elif defined GNULIB_POSIXCHECK
391 # define strtok_r(s,d,p) \
392 (GL_LINK_WARNING ("strtok_r is unportable - " \
393 "use gnulib module strtok_r for portability"), \
398 /* The following functions are not specified by POSIX. They are gnulib
402 /* Return the number of multibyte characters in the character string STRING.
403 This considers multibyte characters, unlike strlen, which counts bytes. */
404 extern size_t mbslen (const char *string);
408 /* Return the number of multibyte characters in the character string starting
409 at STRING and ending at STRING + LEN. */
410 extern size_t mbsnlen (const char *string, size_t len);
414 /* Locate the first single-byte character C in the character string STRING,
415 and return a pointer to it. Return NULL if C is not found in STRING.
416 Unlike strchr(), this function works correctly in multibyte locales with
417 encodings such as GB18030. */
418 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
419 extern char * mbschr (const char *string, int c);
423 /* Locate the last single-byte character C in the character string STRING,
424 and return a pointer to it. Return NULL if C is not found in STRING.
425 Unlike strrchr(), this function works correctly in multibyte locales with
426 encodings such as GB18030. */
427 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
428 extern char * mbsrchr (const char *string, int c);
432 /* Find the first occurrence of the character string NEEDLE in the character
433 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
434 Unlike strstr(), this function works correctly in multibyte locales with
435 encodings different from UTF-8. */
436 extern char * mbsstr (const char *haystack, const char *needle);
439 #if @GNULIB_MBSCASECMP@
440 /* Compare the character strings S1 and S2, ignoring case, returning less than,
441 equal to or greater than zero if S1 is lexicographically less than, equal to
443 Note: This function may, in multibyte locales, return 0 for strings of
445 Unlike strcasecmp(), this function works correctly in multibyte locales. */
446 extern int mbscasecmp (const char *s1, const char *s2);
449 #if @GNULIB_MBSNCASECMP@
450 /* Compare the initial segment of the character string S1 consisting of at most
451 N characters with the initial segment of the character string S2 consisting
452 of at most N characters, ignoring case, returning less than, equal to or
453 greater than zero if the initial segment of S1 is lexicographically less
454 than, equal to or greater than the initial segment of S2.
455 Note: This function may, in multibyte locales, return 0 for initial segments
456 of different lengths!
457 Unlike strncasecmp(), this function works correctly in multibyte locales.
458 But beware that N is not a byte count but a character count! */
459 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
462 #if @GNULIB_MBSPCASECMP@
463 /* Compare the initial segment of the character string STRING consisting of
464 at most mbslen (PREFIX) characters with the character string PREFIX,
465 ignoring case, returning less than, equal to or greater than zero if this
466 initial segment is lexicographically less than, equal to or greater than
468 Note: This function may, in multibyte locales, return 0 if STRING is of
469 smaller length than PREFIX!
470 Unlike strncasecmp(), this function works correctly in multibyte
472 extern char * mbspcasecmp (const char *string, const char *prefix);
475 #if @GNULIB_MBSCASESTR@
476 /* Find the first occurrence of the character string NEEDLE in the character
477 string HAYSTACK, using case-insensitive comparison.
478 Note: This function may, in multibyte locales, return success even if
479 strlen (haystack) < strlen (needle) !
480 Unlike strcasestr(), this function works correctly in multibyte locales. */
481 extern char * mbscasestr (const char *haystack, const char *needle);
485 /* Find the first occurrence in the character string STRING of any character
486 in the character string ACCEPT. Return the number of bytes from the
487 beginning of the string to this occurrence, or to the end of the string
489 Unlike strcspn(), this function works correctly in multibyte locales. */
490 extern size_t mbscspn (const char *string, const char *accept);
494 /* Find the first occurrence in the character string STRING of any character
495 in the character string ACCEPT. Return the pointer to it, or NULL if none
497 Unlike strpbrk(), this function works correctly in multibyte locales. */
498 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
499 extern char * mbspbrk (const char *string, const char *accept);
503 /* Find the first occurrence in the character string STRING of any character
504 not in the character string REJECT. Return the number of bytes from the
505 beginning of the string to this occurrence, or to the end of the string
507 Unlike strspn(), this function works correctly in multibyte locales. */
508 extern size_t mbsspn (const char *string, const char *reject);
512 /* Search the next delimiter (multibyte character listed in the character
513 string DELIM) starting at the character string *STRINGP.
514 If one is found, overwrite it with a NUL, and advance *STRINGP to point
515 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
516 If *STRINGP was already NULL, nothing happens.
517 Return the old value of *STRINGP.
519 This is a variant of mbstok_r() that supports empty fields.
521 Caveat: It modifies the original string.
522 Caveat: These functions cannot be used on constant strings.
523 Caveat: The identity of the delimiting character is lost.
525 See also mbstok_r(). */
526 extern char * mbssep (char **stringp, const char *delim);
529 #if @GNULIB_MBSTOK_R@
530 /* Parse the character string STRING into tokens separated by characters in
531 the character string DELIM.
532 If STRING is NULL, the saved pointer in SAVE_PTR is used as
533 the next starting point. For example:
534 char s[] = "-abc-=-def";
536 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
537 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
538 x = mbstok_r(NULL, "=", &sp); // x = NULL
541 Caveat: It modifies the original string.
542 Caveat: These functions cannot be used on constant strings.
543 Caveat: The identity of the delimiting character is lost.
545 See also mbssep(). */
546 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
549 /* Map any int, typically from errno, into an error message. */
550 #if @GNULIB_STRERROR@
551 # if @REPLACE_STRERROR@
553 # define strerror rpl_strerror
554 extern char *strerror (int);
556 #elif defined GNULIB_POSIXCHECK
558 # define strerror(e) \
559 (GL_LINK_WARNING ("strerror is unportable - " \
560 "use gnulib module strerror to guarantee non-NULL result"), \
564 #if @GNULIB_STRSIGNAL@
565 # if @REPLACE_STRSIGNAL@
566 # define strsignal rpl_strsignal
568 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
569 extern char *strsignal (int __sig);
571 #elif defined GNULIB_POSIXCHECK
573 # define strsignal(a) \
574 (GL_LINK_WARNING ("strsignal is unportable - " \
575 "use gnulib module strsignal for portability"), \
584 #endif /* _GL_STRING_H */
585 #endif /* _GL_STRING_H */