1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2007 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. */
22 #include @ABSOLUTE_STRING_H@
25 /* GL_LINK_WARNING("literal string") arranges to emit the literal string as
26 a linker warning on most glibc systems.
27 We use a linker warning rather than a preprocessor warning, because
28 #warning cannot be used inside macros. */
29 #ifndef GL_LINK_WARNING
30 /* This works on platforms with GNU ld and ELF object format.
31 Testing __GLIBC__ is sufficient for asserting that GNU ld is in use.
32 Testing __ELF__ guarantees the ELF object format.
33 Testing __GNUC__ is necessary for the compound expression syntax. */
34 # if defined __GLIBC__ && defined __ELF__ && defined __GNUC__
35 # define GL_LINK_WARNING(message) \
36 GL_LINK_WARNING1 (__FILE__, __LINE__, message)
37 # define GL_LINK_WARNING1(file, line, message) \
38 GL_LINK_WARNING2 (file, line, message) /* macroexpand file and line */
39 # define GL_LINK_WARNING2(file, line, message) \
40 GL_LINK_WARNING3 (file ":" #line ": warning: " message)
41 # define GL_LINK_WARNING3(message) \
42 ({ static const char warning[sizeof (message)] \
43 __attribute__ ((__unused__, \
44 __section__ (".gnu.warning"), \
50 # define GL_LINK_WARNING(message) ((void) 0)
59 /* Return the first occurrence of NEEDLE in HAYSTACK. */
61 # if ! @HAVE_DECL_MEMMEM@
62 extern void *memmem (void const *__haystack, size_t __haystack_len,
63 void const *__needle, size_t __needle_len);
65 #elif defined GNULIB_POSIXCHECK
67 # define memmem(a,al,b,bl) \
68 (GL_LINK_WARNING ("memmem is unportable - "\
69 "use gnulib module memmem for portability"), \
70 memmem (a, al, b, bl))
73 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
77 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
80 #elif defined GNULIB_POSIXCHECK
82 # define mempcpy(a,b,n) \
83 (GL_LINK_WARNING ("mempcpy is unportable - "\
84 "use gnulib module mempcpy for portability"), \
88 /* Search backwards through a block for a byte (specified as an int). */
90 # if ! @HAVE_DECL_MEMRCHR@
91 extern void *memrchr (void const *, int, size_t);
93 #elif defined GNULIB_POSIXCHECK
95 # define memrchr(a,b,c) \
96 (GL_LINK_WARNING ("memrchr is unportable - "\
97 "use gnulib module memrchr for portability"), \
101 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
104 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
106 #elif defined GNULIB_POSIXCHECK
108 # define stpcpy(a,b) \
109 (GL_LINK_WARNING ("stpcpy is unportable - "\
110 "use gnulib module stpcpy for portability"), \
114 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
115 last non-NUL byte written into DST. */
117 # if ! @HAVE_STPNCPY@
118 # define stpncpy gnu_stpncpy
119 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
122 #elif defined GNULIB_POSIXCHECK
124 # define stpncpy(a,b,n) \
125 (GL_LINK_WARNING ("stpncpy is unportable - "\
126 "use gnulib module stpncpy for portability"), \
130 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
131 greater than zero if S1 is lexicographically less than, equal to or greater
133 Note: This function does not work in multibyte locales. */
134 #if ! @HAVE_STRCASECMP@
135 extern int strcasecmp (char const *s1, char const *s2);
137 #if defined GNULIB_POSIXCHECK
138 /* strcasecmp() does not work with multibyte strings:
139 POSIX says that it operates on "strings", and "string" in POSIX is defined
140 as a sequence of bytes, not of characters. */
142 # define strcasecmp(a,b) \
143 (GL_LINK_WARNING ("strcasecmp cannot work correctly on character strings " \
144 "in multibyte locales - "\
145 "use mbscasecmp if you care about " \
146 "internationalization, or use c_strcasecmp (from " \
147 "gnulib module c-strcase) if you want a locale " \
148 "independent function"), \
152 /* Compare no more than N bytes of strings S1 and S2, ignoring case,
153 returning less than, equal to or greater than zero if S1 is
154 lexicographically less than, equal to or greater than S2.
155 Note: This function cannot work correctly in multibyte locales. */
156 #if ! @HAVE_DECL_STRNCASECMP@
157 extern int strncasecmp (char const *s1, char const *s2, size_t n);
159 #if defined GNULIB_POSIXCHECK
160 /* strncasecmp() does not work with multibyte strings:
161 POSIX says that it operates on "strings", and "string" in POSIX is defined
162 as a sequence of bytes, not of characters. */
164 # define strncasecmp(a,b,n) \
165 (GL_LINK_WARNING ("strncasecmp cannot work correctly on character " \
166 "strings in multibyte locales - "\
167 "use mbsncasecmp or mbspcasecmp if you care about " \
168 "internationalization, or use c_strncasecmp (from " \
169 "gnulib module c-strcase) if you want a locale " \
170 "independent function"), \
171 strncasecmp (a, b, n))
174 #if defined GNULIB_POSIXCHECK
175 /* strchr() does not work with multibyte strings if the locale encoding is
176 GB18030 and the character to be searched is a digit. */
178 # define strchr(s,c) \
179 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
180 "in some multibyte locales - "\
181 "use mbschr if you care about internationalization"), \
185 /* Find the first occurrence of C in S or the final NUL byte. */
186 #if @GNULIB_STRCHRNUL@
187 # if ! @HAVE_STRCHRNUL@
188 extern char *strchrnul (char const *__s, int __c_in);
190 #elif defined GNULIB_POSIXCHECK
192 # define strchrnul(a,b) \
193 (GL_LINK_WARNING ("strchrnul is unportable - "\
194 "use gnulib module strchrnul for portability"), \
198 /* Duplicate S, returning an identical malloc'd string. */
200 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
201 extern char *strdup (char const *__s);
203 #elif defined GNULIB_POSIXCHECK
206 (GL_LINK_WARNING ("strdup is unportable - "\
207 "use gnulib module strdup for portability"), \
211 /* Return a newly allocated copy of at most N bytes of STRING. */
213 # if ! @HAVE_STRNDUP@
215 # define strndup rpl_strndup
216 # if ! @HAVE_DECL_STRNDUP@
217 extern char *strndup (char const *__string, size_t __n);
220 #elif defined GNULIB_POSIXCHECK
222 # define strndup(a,n) \
223 (GL_LINK_WARNING ("strndup is unportable - "\
224 "use gnulib module strndup for portability"), \
228 /* Find the length (number of bytes) of STRING, but scan at most
229 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
232 # if ! @HAVE_DECL_STRNLEN@
233 extern size_t strnlen (char const *__string, size_t __maxlen);
235 #elif defined GNULIB_POSIXCHECK
237 # define strnlen(a,n) \
238 (GL_LINK_WARNING ("strnlen is unportable - "\
239 "use gnulib module strnlen for portability"), \
243 #if defined GNULIB_POSIXCHECK
244 /* strcspn() assumes the second argument is a list of single-byte characters.
245 Even in this simple case, it does not work with multibyte strings if the
246 locale encoding is GB18030 and one of the characters to be searched is a
249 # define strcspn(s,a) \
250 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
251 "in multibyte locales - "\
252 "use mbscspn if you care about internationalization"), \
256 /* Find the first occurrence in S of any character in ACCEPT. */
258 # if ! @HAVE_STRPBRK@
259 extern char *strpbrk (char const *__s, char const *__accept);
261 # if defined GNULIB_POSIXCHECK
262 /* strpbrk() assumes the second argument is a list of single-byte characters.
263 Even in this simple case, it does not work with multibyte strings if the
264 locale encoding is GB18030 and one of the characters to be searched is a
267 # define strpbrk(s,a) \
268 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
269 "in multibyte locales - "\
270 "use mbspbrk if you care about internationalization"), \
273 #elif defined GNULIB_POSIXCHECK
275 # define strpbrk(s,a) \
276 (GL_LINK_WARNING ("strpbrk is unportable - "\
277 "use gnulib module strpbrk for portability"), \
281 #if defined GNULIB_POSIXCHECK
282 /* strspn() assumes the second argument is a list of single-byte characters.
283 Even in this simple case, it cannot work with multibyte strings. */
285 # define strspn(s,a) \
286 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
287 "in multibyte locales - "\
288 "use mbsspn if you care about internationalization"), \
292 #if defined GNULIB_POSIXCHECK
293 /* strrchr() does not work with multibyte strings if the locale encoding is
294 GB18030 and the character to be searched is a digit. */
296 # define strrchr(s,c) \
297 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
298 "in some multibyte locales - "\
299 "use mbsrchr if you care about internationalization"), \
303 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
304 If one is found, overwrite it with a NUL, and advance *STRINGP
305 to point to the next char after it. Otherwise, set *STRINGP to NULL.
306 If *STRINGP was already NULL, nothing happens.
307 Return the old value of *STRINGP.
309 This is a variant of strtok() that is multithread-safe and supports
312 Caveat: It modifies the original string.
313 Caveat: These functions cannot be used on constant strings.
314 Caveat: The identity of the delimiting character is lost.
315 Caveat: It doesn't work with multibyte strings unless all of the delimiter
316 characters are ASCII characters < 0x30.
318 See also strtok_r(). */
321 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
323 # if defined GNULIB_POSIXCHECK
325 # define strsep(s,d) \
326 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
327 "in multibyte locales - "\
328 "use mbssep if you care about internationalization"), \
331 #elif defined GNULIB_POSIXCHECK
333 # define strsep(s,d) \
334 (GL_LINK_WARNING ("strsep is unportable - "\
335 "use gnulib module strsep for portability"), \
339 #if defined GNULIB_POSIXCHECK
340 /* strstr() does not work with multibyte strings if the locale encoding is
341 different from UTF-8:
342 POSIX says that it operates on "strings", and "string" in POSIX is defined
343 as a sequence of bytes, not of characters. */
345 # define strstr(a,b) \
346 (GL_LINK_WARNING ("strstr cannot work correctly on character strings " \
347 "in most multibyte locales - "\
348 "use mbsstr if you care about internationalization"), \
352 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
354 #if ! @HAVE_STRCASESTR@
355 extern char *strcasestr (const char *haystack, const char *needle);
357 #if defined GNULIB_POSIXCHECK
358 /* strcasestr() does not work with multibyte strings:
359 It is a glibc extension, and glibc implements it only for unibyte
362 # define strcasestr(a,b) \
363 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
364 "in multibyte locales - "\
365 "use mbscasestr if you care about " \
366 "internationalization, or use c-strcasestr if you want " \
367 "a locale independent function"), \
371 /* Parse S into tokens separated by characters in DELIM.
372 If S is NULL, the saved pointer in SAVE_PTR is used as
373 the next starting point. For example:
374 char s[] = "-abc-=-def";
376 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
377 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
378 x = strtok_r(NULL, "=", &sp); // x = NULL
381 This is a variant of strtok() that is multithread-safe.
383 For the POSIX documentation for this function, see:
384 http://www.opengroup.org/susv3xsh/strtok.html
386 Caveat: It modifies the original string.
387 Caveat: These functions cannot be used on constant strings.
388 Caveat: The identity of the delimiting character is lost.
389 Caveat: It doesn't work with multibyte strings unless all of the delimiter
390 characters are ASCII characters < 0x30.
392 See also strsep(). */
393 #if @GNULIB_STRTOK_R@
394 # if ! @HAVE_DECL_STRTOK_R@
395 extern char *strtok_r (char *restrict s, char const *restrict delim,
396 char **restrict save_ptr);
398 # if defined GNULIB_POSIXCHECK
400 # define strtok_r(s,d,p) \
401 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
402 "in multibyte locales - "\
403 "use mbstok_r if you care about internationalization"), \
406 #elif defined GNULIB_POSIXCHECK
408 # define strtok_r(s,d,p) \
409 (GL_LINK_WARNING ("strtok_r is unportable - "\
410 "use gnulib module strtok_r for portability"), \
415 /* The following functions are not specified by POSIX. They are gnulib
419 /* Return the number of multibyte characters in the character string STRING.
420 This considers multibyte characters, unlike strlen, which counts bytes. */
421 extern size_t mbslen (const char *string);
425 /* Locate the first 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 strchr(), this function works correctly in multibyte locales with
428 encodings such as GB18030. */
429 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
430 extern char * mbschr (const char *string, int c);
434 /* Locate the last single-byte character C in the character string STRING,
435 and return a pointer to it. Return NULL if C is not found in STRING.
436 Unlike strrchr(), this function works correctly in multibyte locales with
437 encodings such as GB18030. */
438 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
439 extern char * mbsrchr (const char *string, int c);
443 /* Find the first occurrence of the character string NEEDLE in the character
444 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
445 Unlike strstr(), this function works correctly in multibyte locales with
446 encodings different from UTF-8. */
447 extern char * mbsstr (const char *haystack, const char *needle);
450 #if @GNULIB_MBSCASECMP@
451 /* Compare the character strings S1 and S2, ignoring case, returning less than,
452 equal to or greater than zero if S1 is lexicographically less than, equal to
454 Note: This function may, in multibyte locales, return 0 for strings of
456 Unlike strcasecmp(), this function works correctly in multibyte locales. */
457 extern int mbscasecmp (const char *s1, const char *s2);
460 #if @GNULIB_MBSNCASECMP@
461 /* Compare the initial segment of the character string S1 consisting of at most
462 N characters with the initial segment of the character string S2 consisting
463 of at most N characters, ignoring case, returning less than, equal to or
464 greater than zero if the initial segment of S1 is lexicographically less
465 than, equal to or greater than the initial segment of S2.
466 Note: This function may, in multibyte locales, return 0 for initial segments
467 of different lengths!
468 Unlike strncasecmp(), this function works correctly in multibyte locales.
469 But beware that N is not a byte count but a character count! */
470 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
473 #if @GNULIB_MBSPCASECMP@
474 /* Compare the initial segment of the character string STRING consisting of
475 at most mbslen (PREFIX) characters with the character string PREFIX,
476 ignoring case, returning less than, equal to or greater than zero if this
477 initial segment is lexicographically less than, equal to or greater than
479 Note: This function may, in multibyte locales, return 0 if STRING is of
480 smaller length than PREFIX!
481 Unlike strncasecmp(), this function works correctly in multibyte
483 extern char * mbspcasecmp (const char *string, const char *prefix);
486 #if @GNULIB_MBSCASESTR@
487 /* Find the first occurrence of the character string NEEDLE in the character
488 string HAYSTACK, using case-insensitive comparison.
489 Note: This function may, in multibyte locales, return success even if
490 strlen (haystack) < strlen (needle) !
491 Unlike strcasestr(), this function works correctly in multibyte locales. */
492 extern char * mbscasestr (const char *haystack, const char *needle);
496 /* Find the first occurrence in the character string STRING of any character
497 in the character string ACCEPT. Return the number of bytes from the
498 beginning of the string to this occurrence, or to the end of the string
500 Unlike strcspn(), this function works correctly in multibyte locales. */
501 extern size_t mbscspn (const char *string, const char *accept);
505 /* Find the first occurrence in the character string STRING of any character
506 in the character string ACCEPT. Return the pointer to it, or NULL if none
508 Unlike strpbrk(), this function works correctly in multibyte locales. */
509 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
510 extern char * mbspbrk (const char *string, const char *accept);
514 /* Find the first occurrence in the character string STRING of any character
515 not in the character string REJECT. Return the number of bytes from the
516 beginning of the string to this occurrence, or to the end of the string
518 Unlike strspn(), this function works correctly in multibyte locales. */
519 extern size_t mbsspn (const char *string, const char *reject);
523 /* Search the next delimiter (multibyte character listed in the character
524 string DELIM) starting at the character string *STRINGP.
525 If one is found, overwrite it with a NUL, and advance *STRINGP to point
526 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
527 If *STRINGP was already NULL, nothing happens.
528 Return the old value of *STRINGP.
530 This is a variant of mbstok_r() that supports empty fields.
532 Caveat: It modifies the original string.
533 Caveat: These functions cannot be used on constant strings.
534 Caveat: The identity of the delimiting character is lost.
536 See also mbstok_r(). */
537 extern char * mbssep (char **stringp, const char *delim);
540 #if @GNULIB_MBSTOK_R@
541 /* Parse the character string STRING into tokens separated by characters in
542 the character string DELIM.
543 If STRING is NULL, the saved pointer in SAVE_PTR is used as
544 the next starting point. For example:
545 char s[] = "-abc-=-def";
547 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
548 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
549 x = mbstok_r(NULL, "=", &sp); // x = NULL
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 mbssep(). */
557 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);