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 @REPLACE_STRDUP@
172 # define strdup rpl_strdup
174 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
175 extern char *strdup (char const *__s);
177 #elif defined GNULIB_POSIXCHECK
180 (GL_LINK_WARNING ("strdup is unportable - " \
181 "use gnulib module strdup for portability"), \
185 /* Return a newly allocated copy of at most N bytes of STRING. */
187 # if ! @HAVE_STRNDUP@
189 # define strndup rpl_strndup
191 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
192 extern char *strndup (char const *__string, size_t __n);
194 #elif defined GNULIB_POSIXCHECK
196 # define strndup(a,n) \
197 (GL_LINK_WARNING ("strndup is unportable - " \
198 "use gnulib module strndup for portability"), \
202 /* Find the length (number of bytes) of STRING, but scan at most
203 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
206 # if ! @HAVE_DECL_STRNLEN@
207 extern size_t strnlen (char const *__string, size_t __maxlen)
208 __attribute__ ((__pure__));
210 #elif defined GNULIB_POSIXCHECK
212 # define strnlen(a,n) \
213 (GL_LINK_WARNING ("strnlen is unportable - " \
214 "use gnulib module strnlen for portability"), \
218 #if defined GNULIB_POSIXCHECK
219 /* strcspn() assumes the second argument is a list of single-byte characters.
220 Even in this simple case, it does not work with multibyte strings if the
221 locale encoding is GB18030 and one of the characters to be searched is a
224 # define strcspn(s,a) \
225 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
226 "in multibyte locales - " \
227 "use mbscspn if you care about internationalization"), \
231 /* Find the first occurrence in S of any character in ACCEPT. */
233 # if ! @HAVE_STRPBRK@
234 extern char *strpbrk (char const *__s, char const *__accept)
235 __attribute__ ((__pure__));
237 # if defined GNULIB_POSIXCHECK
238 /* strpbrk() 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 strpbrk(s,a) \
244 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
245 "in multibyte locales - " \
246 "use mbspbrk if you care about internationalization"), \
249 #elif defined GNULIB_POSIXCHECK
251 # define strpbrk(s,a) \
252 (GL_LINK_WARNING ("strpbrk is unportable - " \
253 "use gnulib module strpbrk for portability"), \
257 #if defined GNULIB_POSIXCHECK
258 /* strspn() assumes the second argument is a list of single-byte characters.
259 Even in this simple case, it cannot work with multibyte strings. */
261 # define strspn(s,a) \
262 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
263 "in multibyte locales - " \
264 "use mbsspn if you care about internationalization"), \
268 #if defined GNULIB_POSIXCHECK
269 /* strrchr() does not work with multibyte strings if the locale encoding is
270 GB18030 and the character to be searched is a digit. */
272 # define strrchr(s,c) \
273 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
274 "in some multibyte locales - " \
275 "use mbsrchr if you care about internationalization"), \
279 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
280 If one is found, overwrite it with a NUL, and advance *STRINGP
281 to point to the next char after it. Otherwise, set *STRINGP to NULL.
282 If *STRINGP was already NULL, nothing happens.
283 Return the old value of *STRINGP.
285 This is a variant of strtok() that is multithread-safe and supports
288 Caveat: It modifies the original string.
289 Caveat: These functions cannot be used on constant strings.
290 Caveat: The identity of the delimiting character is lost.
291 Caveat: It doesn't work with multibyte strings unless all of the delimiter
292 characters are ASCII characters < 0x30.
294 See also strtok_r(). */
297 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
299 # if defined GNULIB_POSIXCHECK
301 # define strsep(s,d) \
302 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
303 "in multibyte locales - " \
304 "use mbssep if you care about internationalization"), \
307 #elif defined GNULIB_POSIXCHECK
309 # define strsep(s,d) \
310 (GL_LINK_WARNING ("strsep is unportable - " \
311 "use gnulib module strsep for portability"), \
316 # if @REPLACE_STRSTR@
317 # define strstr rpl_strstr
318 char *strstr (const char *haystack, const char *needle)
319 __attribute__ ((__pure__));
321 #elif defined GNULIB_POSIXCHECK
322 /* strstr() does not work with multibyte strings if the locale encoding is
323 different from UTF-8:
324 POSIX says that it operates on "strings", and "string" in POSIX is defined
325 as a sequence of bytes, not of characters. */
327 # define strstr(a,b) \
328 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
329 "work correctly on character strings in most " \
330 "multibyte locales - " \
331 "use mbsstr if you care about internationalization, " \
332 "or use strstr if you care about speed"), \
336 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
338 #if @GNULIB_STRCASESTR@
339 # if @REPLACE_STRCASESTR@
340 # define strcasestr rpl_strcasestr
342 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
343 extern char *strcasestr (const char *haystack, const char *needle)
344 __attribute__ ((__pure__));
346 #elif defined GNULIB_POSIXCHECK
347 /* strcasestr() does not work with multibyte strings:
348 It is a glibc extension, and glibc implements it only for unibyte
351 # define strcasestr(a,b) \
352 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
353 "in multibyte locales - " \
354 "use mbscasestr if you care about " \
355 "internationalization, or use c-strcasestr if you want " \
356 "a locale independent function"), \
360 /* Parse S into tokens separated by characters in DELIM.
361 If S is NULL, the saved pointer in SAVE_PTR is used as
362 the next starting point. For example:
363 char s[] = "-abc-=-def";
365 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
366 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
367 x = strtok_r(NULL, "=", &sp); // x = NULL
370 This is a variant of strtok() that is multithread-safe.
372 For the POSIX documentation for this function, see:
373 http://www.opengroup.org/susv3xsh/strtok.html
375 Caveat: It modifies the original string.
376 Caveat: These functions cannot be used on constant strings.
377 Caveat: The identity of the delimiting character is lost.
378 Caveat: It doesn't work with multibyte strings unless all of the delimiter
379 characters are ASCII characters < 0x30.
381 See also strsep(). */
382 #if @GNULIB_STRTOK_R@
383 # if ! @HAVE_DECL_STRTOK_R@
384 extern char *strtok_r (char *restrict s, char const *restrict delim,
385 char **restrict save_ptr);
387 # if defined GNULIB_POSIXCHECK
389 # define strtok_r(s,d,p) \
390 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
391 "in multibyte locales - " \
392 "use mbstok_r if you care about internationalization"), \
395 #elif defined GNULIB_POSIXCHECK
397 # define strtok_r(s,d,p) \
398 (GL_LINK_WARNING ("strtok_r is unportable - " \
399 "use gnulib module strtok_r for portability"), \
404 /* The following functions are not specified by POSIX. They are gnulib
408 /* Return the number of multibyte characters in the character string STRING.
409 This considers multibyte characters, unlike strlen, which counts bytes. */
410 extern size_t mbslen (const char *string);
414 /* Return the number of multibyte characters in the character string starting
415 at STRING and ending at STRING + LEN. */
416 extern size_t mbsnlen (const char *string, size_t len);
420 /* Locate the first single-byte character C in the character string STRING,
421 and return a pointer to it. Return NULL if C is not found in STRING.
422 Unlike strchr(), this function works correctly in multibyte locales with
423 encodings such as GB18030. */
424 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
425 extern char * mbschr (const char *string, int c);
429 /* Locate the last single-byte character C in the character string STRING,
430 and return a pointer to it. Return NULL if C is not found in STRING.
431 Unlike strrchr(), this function works correctly in multibyte locales with
432 encodings such as GB18030. */
433 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
434 extern char * mbsrchr (const char *string, int c);
438 /* Find the first occurrence of the character string NEEDLE in the character
439 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
440 Unlike strstr(), this function works correctly in multibyte locales with
441 encodings different from UTF-8. */
442 extern char * mbsstr (const char *haystack, const char *needle);
445 #if @GNULIB_MBSCASECMP@
446 /* Compare the character strings S1 and S2, ignoring case, returning less than,
447 equal to or greater than zero if S1 is lexicographically less than, equal to
449 Note: This function may, in multibyte locales, return 0 for strings of
451 Unlike strcasecmp(), this function works correctly in multibyte locales. */
452 extern int mbscasecmp (const char *s1, const char *s2);
455 #if @GNULIB_MBSNCASECMP@
456 /* Compare the initial segment of the character string S1 consisting of at most
457 N characters with the initial segment of the character string S2 consisting
458 of at most N characters, ignoring case, returning less than, equal to or
459 greater than zero if the initial segment of S1 is lexicographically less
460 than, equal to or greater than the initial segment of S2.
461 Note: This function may, in multibyte locales, return 0 for initial segments
462 of different lengths!
463 Unlike strncasecmp(), this function works correctly in multibyte locales.
464 But beware that N is not a byte count but a character count! */
465 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
468 #if @GNULIB_MBSPCASECMP@
469 /* Compare the initial segment of the character string STRING consisting of
470 at most mbslen (PREFIX) characters with the character string PREFIX,
471 ignoring case, returning less than, equal to or greater than zero if this
472 initial segment is lexicographically less than, equal to or greater than
474 Note: This function may, in multibyte locales, return 0 if STRING is of
475 smaller length than PREFIX!
476 Unlike strncasecmp(), this function works correctly in multibyte
478 extern char * mbspcasecmp (const char *string, const char *prefix);
481 #if @GNULIB_MBSCASESTR@
482 /* Find the first occurrence of the character string NEEDLE in the character
483 string HAYSTACK, using case-insensitive comparison.
484 Note: This function may, in multibyte locales, return success even if
485 strlen (haystack) < strlen (needle) !
486 Unlike strcasestr(), this function works correctly in multibyte locales. */
487 extern char * mbscasestr (const char *haystack, const char *needle);
491 /* Find the first occurrence in the character string STRING of any character
492 in the character string ACCEPT. Return the number of bytes from the
493 beginning of the string to this occurrence, or to the end of the string
495 Unlike strcspn(), this function works correctly in multibyte locales. */
496 extern size_t mbscspn (const char *string, const char *accept);
500 /* Find the first occurrence in the character string STRING of any character
501 in the character string ACCEPT. Return the pointer to it, or NULL if none
503 Unlike strpbrk(), this function works correctly in multibyte locales. */
504 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
505 extern char * mbspbrk (const char *string, const char *accept);
509 /* Find the first occurrence in the character string STRING of any character
510 not in the character string REJECT. Return the number of bytes from the
511 beginning of the string to this occurrence, or to the end of the string
513 Unlike strspn(), this function works correctly in multibyte locales. */
514 extern size_t mbsspn (const char *string, const char *reject);
518 /* Search the next delimiter (multibyte character listed in the character
519 string DELIM) starting at the character string *STRINGP.
520 If one is found, overwrite it with a NUL, and advance *STRINGP to point
521 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
522 If *STRINGP was already NULL, nothing happens.
523 Return the old value of *STRINGP.
525 This is a variant of mbstok_r() that supports empty fields.
527 Caveat: It modifies the original string.
528 Caveat: These functions cannot be used on constant strings.
529 Caveat: The identity of the delimiting character is lost.
531 See also mbstok_r(). */
532 extern char * mbssep (char **stringp, const char *delim);
535 #if @GNULIB_MBSTOK_R@
536 /* Parse the character string STRING into tokens separated by characters in
537 the character string DELIM.
538 If STRING is NULL, the saved pointer in SAVE_PTR is used as
539 the next starting point. For example:
540 char s[] = "-abc-=-def";
542 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
543 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
544 x = mbstok_r(NULL, "=", &sp); // x = NULL
547 Caveat: It modifies the original string.
548 Caveat: These functions cannot be used on constant strings.
549 Caveat: The identity of the delimiting character is lost.
551 See also mbssep(). */
552 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
555 /* Map any int, typically from errno, into an error message. */
556 #if @GNULIB_STRERROR@
557 # if @REPLACE_STRERROR@
559 # define strerror rpl_strerror
560 extern char *strerror (int);
562 #elif defined GNULIB_POSIXCHECK
564 # define strerror(e) \
565 (GL_LINK_WARNING ("strerror is unportable - " \
566 "use gnulib module strerror to guarantee non-NULL result"), \
570 #if @GNULIB_STRSIGNAL@
571 # if @REPLACE_STRSIGNAL@
572 # define strsignal rpl_strsignal
574 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
575 extern char *strsignal (int __sig);
577 #elif defined GNULIB_POSIXCHECK
579 # define strsignal(a) \
580 (GL_LINK_WARNING ("strsignal is unportable - " \
581 "use gnulib module strsignal for portability"), \
585 #if @GNULIB_STRVERSCMP@
586 # if !@HAVE_STRVERSCMP@
587 extern int strverscmp (const char *, const char *);
589 #elif defined GNULIB_POSIXCHECK
591 # define strverscmp(a, b) \
592 (GL_LINK_WARNING ("strverscmp is unportable - " \
593 "use gnulib module strverscmp for portability"), \
602 #endif /* _GL_STRING_H */
603 #endif /* _GL_STRING_H */