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 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 occurrence of NEEDLE in HAYSTACK. */
55 # define memmem rpl_memmem
57 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
58 extern void *memmem (void const *__haystack
, size_t __haystack_len
,
59 void const *__needle
, size_t __needle_len
)
60 __attribute__ ((__pure__
));
62 #elif defined GNULIB_POSIXCHECK
64 # define memmem(a,al,b,bl) \
65 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
66 "use gnulib module memmem-simple for portability, " \
67 "and module memmem for speed" ), \
68 memmem (a, al, b, bl))
71 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
75 extern void *mempcpy (void *restrict __dest
, void const *restrict __src
,
78 #elif defined GNULIB_POSIXCHECK
80 # define mempcpy(a,b,n) \
81 (GL_LINK_WARNING ("mempcpy is unportable - " \
82 "use gnulib module mempcpy for portability"), \
86 /* Search backwards through a block for a byte (specified as an int). */
88 # if ! @HAVE_DECL_MEMRCHR@
89 extern void *memrchr (void const *, int, size_t)
90 __attribute__ ((__pure__
));
92 #elif defined GNULIB_POSIXCHECK
94 # define memrchr(a,b,c) \
95 (GL_LINK_WARNING ("memrchr is unportable - " \
96 "use gnulib module memrchr for portability"), \
100 /* Find the first occurrence of C in S. More efficient than
101 memchr(S,C,N), at the expense of undefined behavior if C does not
102 occur within N bytes. */
103 #if @GNULIB_RAWMEMCHR@
104 # if ! @HAVE_RAWMEMCHR@
105 extern void *rawmemchr (void const *__s
, int __c_in
)
106 __attribute__ ((__pure__
));
108 #elif defined GNULIB_POSIXCHECK
110 # define rawmemchr(a,b) \
111 (GL_LINK_WARNING ("rawmemchr is unportable - " \
112 "use gnulib module rawmemchr for portability"), \
116 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
119 extern char *stpcpy (char *restrict __dst
, char const *restrict __src
);
121 #elif defined GNULIB_POSIXCHECK
123 # define stpcpy(a,b) \
124 (GL_LINK_WARNING ("stpcpy is unportable - " \
125 "use gnulib module stpcpy for portability"), \
129 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
130 last non-NUL byte written into DST. */
132 # if ! @HAVE_STPNCPY@
133 # define stpncpy gnu_stpncpy
134 extern char *stpncpy (char *restrict __dst
, char const *restrict __src
,
137 #elif defined GNULIB_POSIXCHECK
139 # define stpncpy(a,b,n) \
140 (GL_LINK_WARNING ("stpncpy is unportable - " \
141 "use gnulib module stpncpy for portability"), \
145 #if defined GNULIB_POSIXCHECK
146 /* strchr() does not work with multibyte strings if the locale encoding is
147 GB18030 and the character to be searched is a digit. */
149 # define strchr(s,c) \
150 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
151 "in some multibyte locales - " \
152 "use mbschr if you care about internationalization"), \
156 /* Find the first occurrence of C in S or the final NUL byte. */
157 #if @GNULIB_STRCHRNUL@
158 # if ! @HAVE_STRCHRNUL@
159 extern char *strchrnul (char const *__s
, int __c_in
)
160 __attribute__ ((__pure__
));
162 #elif defined GNULIB_POSIXCHECK
164 # define strchrnul(a,b) \
165 (GL_LINK_WARNING ("strchrnul is unportable - " \
166 "use gnulib module strchrnul for portability"), \
170 /* Duplicate S, returning an identical malloc'd string. */
172 # if @REPLACE_STRDUP@
174 # define strdup rpl_strdup
176 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
177 extern char *strdup (char const *__s
);
179 #elif defined GNULIB_POSIXCHECK
182 (GL_LINK_WARNING ("strdup is unportable - " \
183 "use gnulib module strdup for portability"), \
187 /* Return a newly allocated copy of at most N bytes of STRING. */
189 # if ! @HAVE_STRNDUP@
191 # define strndup rpl_strndup
193 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
194 extern char *strndup (char const *__string
, size_t __n
);
196 #elif defined GNULIB_POSIXCHECK
198 # define strndup(a,n) \
199 (GL_LINK_WARNING ("strndup is unportable - " \
200 "use gnulib module strndup for portability"), \
204 /* Find the length (number of bytes) of STRING, but scan at most
205 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
208 # if ! @HAVE_DECL_STRNLEN@
209 extern size_t strnlen (char const *__string
, size_t __maxlen
)
210 __attribute__ ((__pure__
));
212 #elif defined GNULIB_POSIXCHECK
214 # define strnlen(a,n) \
215 (GL_LINK_WARNING ("strnlen is unportable - " \
216 "use gnulib module strnlen for portability"), \
220 #if defined GNULIB_POSIXCHECK
221 /* strcspn() assumes the second argument is a list of single-byte characters.
222 Even in this simple case, it does not work with multibyte strings if the
223 locale encoding is GB18030 and one of the characters to be searched is a
226 # define strcspn(s,a) \
227 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
228 "in multibyte locales - " \
229 "use mbscspn if you care about internationalization"), \
233 /* Find the first occurrence in S of any character in ACCEPT. */
235 # if ! @HAVE_STRPBRK@
236 extern char *strpbrk (char const *__s
, char const *__accept
)
237 __attribute__ ((__pure__
));
239 # if defined GNULIB_POSIXCHECK
240 /* strpbrk() assumes the second argument is a list of single-byte characters.
241 Even in this simple case, it does not work with multibyte strings if the
242 locale encoding is GB18030 and one of the characters to be searched is a
245 # define strpbrk(s,a) \
246 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
247 "in multibyte locales - " \
248 "use mbspbrk if you care about internationalization"), \
251 #elif defined GNULIB_POSIXCHECK
253 # define strpbrk(s,a) \
254 (GL_LINK_WARNING ("strpbrk is unportable - " \
255 "use gnulib module strpbrk for portability"), \
259 #if defined GNULIB_POSIXCHECK
260 /* strspn() assumes the second argument is a list of single-byte characters.
261 Even in this simple case, it cannot work with multibyte strings. */
263 # define strspn(s,a) \
264 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
265 "in multibyte locales - " \
266 "use mbsspn if you care about internationalization"), \
270 #if defined GNULIB_POSIXCHECK
271 /* strrchr() does not work with multibyte strings if the locale encoding is
272 GB18030 and the character to be searched is a digit. */
274 # define strrchr(s,c) \
275 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
276 "in some multibyte locales - " \
277 "use mbsrchr if you care about internationalization"), \
281 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
282 If one is found, overwrite it with a NUL, and advance *STRINGP
283 to point to the next char after it. Otherwise, set *STRINGP to NULL.
284 If *STRINGP was already NULL, nothing happens.
285 Return the old value of *STRINGP.
287 This is a variant of strtok() that is multithread-safe and supports
290 Caveat: It modifies the original string.
291 Caveat: These functions cannot be used on constant strings.
292 Caveat: The identity of the delimiting character is lost.
293 Caveat: It doesn't work with multibyte strings unless all of the delimiter
294 characters are ASCII characters < 0x30.
296 See also strtok_r(). */
299 extern char *strsep (char **restrict __stringp
, char const *restrict __delim
);
301 # if defined GNULIB_POSIXCHECK
303 # define strsep(s,d) \
304 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
305 "in multibyte locales - " \
306 "use mbssep if you care about internationalization"), \
309 #elif defined GNULIB_POSIXCHECK
311 # define strsep(s,d) \
312 (GL_LINK_WARNING ("strsep is unportable - " \
313 "use gnulib module strsep for portability"), \
318 # if @REPLACE_STRSTR@
319 # define strstr rpl_strstr
320 char *strstr (const char *haystack
, const char *needle
)
321 __attribute__ ((__pure__
));
323 #elif defined GNULIB_POSIXCHECK
324 /* strstr() does not work with multibyte strings if the locale encoding is
325 different from UTF-8:
326 POSIX says that it operates on "strings", and "string" in POSIX is defined
327 as a sequence of bytes, not of characters. */
329 # define strstr(a,b) \
330 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
331 "work correctly on character strings in most " \
332 "multibyte locales - " \
333 "use mbsstr if you care about internationalization, " \
334 "or use strstr if you care about speed"), \
338 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
340 #if @GNULIB_STRCASESTR@
341 # if @REPLACE_STRCASESTR@
342 # define strcasestr rpl_strcasestr
344 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
345 extern char *strcasestr (const char *haystack
, const char *needle
)
346 __attribute__ ((__pure__
));
348 #elif defined GNULIB_POSIXCHECK
349 /* strcasestr() does not work with multibyte strings:
350 It is a glibc extension, and glibc implements it only for unibyte
353 # define strcasestr(a,b) \
354 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
355 "in multibyte locales - " \
356 "use mbscasestr if you care about " \
357 "internationalization, or use c-strcasestr if you want " \
358 "a locale independent function"), \
362 /* Parse S into tokens separated by characters in DELIM.
363 If S is NULL, the saved pointer in SAVE_PTR is used as
364 the next starting point. For example:
365 char s[] = "-abc-=-def";
367 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
368 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
369 x = strtok_r(NULL, "=", &sp); // x = NULL
372 This is a variant of strtok() that is multithread-safe.
374 For the POSIX documentation for this function, see:
375 http://www.opengroup.org/susv3xsh/strtok.html
377 Caveat: It modifies the original string.
378 Caveat: These functions cannot be used on constant strings.
379 Caveat: The identity of the delimiting character is lost.
380 Caveat: It doesn't work with multibyte strings unless all of the delimiter
381 characters are ASCII characters < 0x30.
383 See also strsep(). */
384 #if @GNULIB_STRTOK_R@
385 # if ! @HAVE_DECL_STRTOK_R@
386 extern char *strtok_r (char *restrict s
, char const *restrict delim
,
387 char **restrict save_ptr
);
389 # if defined GNULIB_POSIXCHECK
391 # define strtok_r(s,d,p) \
392 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
393 "in multibyte locales - " \
394 "use mbstok_r if you care about internationalization"), \
397 #elif defined GNULIB_POSIXCHECK
399 # define strtok_r(s,d,p) \
400 (GL_LINK_WARNING ("strtok_r is unportable - " \
401 "use gnulib module strtok_r for portability"), \
406 /* The following functions are not specified by POSIX. They are gnulib
410 /* Return the number of multibyte characters in the character string STRING.
411 This considers multibyte characters, unlike strlen, which counts bytes. */
412 extern size_t mbslen (const char *string
);
416 /* Return the number of multibyte characters in the character string starting
417 at STRING and ending at STRING + LEN. */
418 extern size_t mbsnlen (const char *string
, size_t len
);
422 /* Locate the first single-byte character C in the character string STRING,
423 and return a pointer to it. Return NULL if C is not found in STRING.
424 Unlike strchr(), this function works correctly in multibyte locales with
425 encodings such as GB18030. */
426 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
427 extern char * mbschr (const char *string
, int c
);
431 /* Locate the last single-byte character C in the character string STRING,
432 and return a pointer to it. Return NULL if C is not found in STRING.
433 Unlike strrchr(), this function works correctly in multibyte locales with
434 encodings such as GB18030. */
435 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
436 extern char * mbsrchr (const char *string
, int c
);
440 /* Find the first occurrence of the character string NEEDLE in the character
441 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
442 Unlike strstr(), this function works correctly in multibyte locales with
443 encodings different from UTF-8. */
444 extern char * mbsstr (const char *haystack
, const char *needle
);
447 #if @GNULIB_MBSCASECMP@
448 /* Compare the character strings S1 and S2, ignoring case, returning less than,
449 equal to or greater than zero if S1 is lexicographically less than, equal to
451 Note: This function may, in multibyte locales, return 0 for strings of
453 Unlike strcasecmp(), this function works correctly in multibyte locales. */
454 extern int mbscasecmp (const char *s1
, const char *s2
);
457 #if @GNULIB_MBSNCASECMP@
458 /* Compare the initial segment of the character string S1 consisting of at most
459 N characters with the initial segment of the character string S2 consisting
460 of at most N characters, ignoring case, returning less than, equal to or
461 greater than zero if the initial segment of S1 is lexicographically less
462 than, equal to or greater than the initial segment of S2.
463 Note: This function may, in multibyte locales, return 0 for initial segments
464 of different lengths!
465 Unlike strncasecmp(), this function works correctly in multibyte locales.
466 But beware that N is not a byte count but a character count! */
467 extern int mbsncasecmp (const char *s1
, const char *s2
, size_t n
);
470 #if @GNULIB_MBSPCASECMP@
471 /* Compare the initial segment of the character string STRING consisting of
472 at most mbslen (PREFIX) characters with the character string PREFIX,
473 ignoring case, returning less than, equal to or greater than zero if this
474 initial segment is lexicographically less than, equal to or greater than
476 Note: This function may, in multibyte locales, return 0 if STRING is of
477 smaller length than PREFIX!
478 Unlike strncasecmp(), this function works correctly in multibyte
480 extern char * mbspcasecmp (const char *string
, const char *prefix
);
483 #if @GNULIB_MBSCASESTR@
484 /* Find the first occurrence of the character string NEEDLE in the character
485 string HAYSTACK, using case-insensitive comparison.
486 Note: This function may, in multibyte locales, return success even if
487 strlen (haystack) < strlen (needle) !
488 Unlike strcasestr(), this function works correctly in multibyte locales. */
489 extern char * mbscasestr (const char *haystack
, const char *needle
);
493 /* Find the first occurrence in the character string STRING of any character
494 in the character string ACCEPT. Return the number of bytes from the
495 beginning of the string to this occurrence, or to the end of the string
497 Unlike strcspn(), this function works correctly in multibyte locales. */
498 extern size_t mbscspn (const char *string
, const char *accept
);
502 /* Find the first occurrence in the character string STRING of any character
503 in the character string ACCEPT. Return the pointer to it, or NULL if none
505 Unlike strpbrk(), this function works correctly in multibyte locales. */
506 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
507 extern char * mbspbrk (const char *string
, const char *accept
);
511 /* Find the first occurrence in the character string STRING of any character
512 not in the character string REJECT. Return the number of bytes from the
513 beginning of the string to this occurrence, or to the end of the string
515 Unlike strspn(), this function works correctly in multibyte locales. */
516 extern size_t mbsspn (const char *string
, const char *reject
);
520 /* Search the next delimiter (multibyte character listed in the character
521 string DELIM) starting at the character string *STRINGP.
522 If one is found, overwrite it with a NUL, and advance *STRINGP to point
523 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
524 If *STRINGP was already NULL, nothing happens.
525 Return the old value of *STRINGP.
527 This is a variant of mbstok_r() that supports empty fields.
529 Caveat: It modifies the original string.
530 Caveat: These functions cannot be used on constant strings.
531 Caveat: The identity of the delimiting character is lost.
533 See also mbstok_r(). */
534 extern char * mbssep (char **stringp
, const char *delim
);
537 #if @GNULIB_MBSTOK_R@
538 /* Parse the character string STRING into tokens separated by characters in
539 the character string DELIM.
540 If STRING is NULL, the saved pointer in SAVE_PTR is used as
541 the next starting point. For example:
542 char s[] = "-abc-=-def";
544 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
545 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
546 x = mbstok_r(NULL, "=", &sp); // x = NULL
549 Caveat: It modifies the original string.
550 Caveat: These functions cannot be used on constant strings.
551 Caveat: The identity of the delimiting character is lost.
553 See also mbssep(). */
554 extern char * mbstok_r (char *string
, const char *delim
, char **save_ptr
);
557 /* Map any int, typically from errno, into an error message. */
558 #if @GNULIB_STRERROR@
559 # if @REPLACE_STRERROR@
561 # define strerror rpl_strerror
562 extern char *strerror (int);
564 #elif defined GNULIB_POSIXCHECK
566 # define strerror(e) \
567 (GL_LINK_WARNING ("strerror is unportable - " \
568 "use gnulib module strerror to guarantee non-NULL result"), \
572 #if @GNULIB_STRSIGNAL@
573 # if @REPLACE_STRSIGNAL@
574 # define strsignal rpl_strsignal
576 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
577 extern char *strsignal (int __sig
);
579 #elif defined GNULIB_POSIXCHECK
581 # define strsignal(a) \
582 (GL_LINK_WARNING ("strsignal is unportable - " \
583 "use gnulib module strsignal for portability"), \
587 #if @GNULIB_STRVERSCMP@
588 # if !@HAVE_STRVERSCMP@
589 extern int strverscmp (const char *, const char *);
591 #elif defined GNULIB_POSIXCHECK
593 # define strverscmp(a, b) \
594 (GL_LINK_WARNING ("strverscmp is unportable - " \
595 "use gnulib module strverscmp for portability"), \
604 #endif /* _GL_STRING_H */
605 #endif /* _GL_STRING_H */