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 - use gnulib module memmem for portability"), \
69 memmem (a, al, b, bl))
72 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
76 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
79 #elif defined GNULIB_POSIXCHECK
81 # define mempcpy(a,b,n) \
82 (GL_LINK_WARNING ("mempcpy is unportable - 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);
91 #elif defined GNULIB_POSIXCHECK
93 # define memrchr(a,b,c) \
94 (GL_LINK_WARNING ("memrchr is unportable - use gnulib module memrchr for portability"), \
98 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
101 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
103 #elif defined GNULIB_POSIXCHECK
105 # define stpcpy(a,b) \
106 (GL_LINK_WARNING ("stpcpy is unportable - use gnulib module stpcpy for portability"), \
110 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
111 last non-NUL byte written into DST. */
113 # if ! @HAVE_STPNCPY@
114 # define stpncpy gnu_stpncpy
115 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
118 #elif defined GNULIB_POSIXCHECK
120 # define stpncpy(a,b,n) \
121 (GL_LINK_WARNING ("stpncpy is unportable - use gnulib module stpncpy for portability"), \
125 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
126 greater than zero if S1 is lexicographically less than, equal to or greater
128 Note: This function does not work in multibyte locales. */
129 #if ! @HAVE_STRCASECMP@
130 extern int strcasecmp (char const *s1, char const *s2);
132 #if defined GNULIB_POSIXCHECK
133 /* strcasecmp() does not work with multibyte strings:
134 POSIX says that it operates on "strings", and "string" in POSIX is defined
135 as a sequence of bytes, not of characters. */
137 # define strcasecmp(a,b) \
138 (GL_LINK_WARNING ("strcasecmp cannot work correctly on character strings in multibyte locales - use mbscasecmp if you care about internationalization, or use c_strcasecmp (from gnulib module c-strcase) if you want a locale independent function"), \
142 /* Compare no more than N bytes of strings S1 and S2, ignoring case,
143 returning less than, equal to or greater than zero if S1 is
144 lexicographically less than, equal to or greater than S2.
145 Note: This function cannot work correctly in multibyte locales. */
146 #if ! @HAVE_DECL_STRNCASECMP@
147 extern int strncasecmp (char const *s1, char const *s2, size_t n);
149 #if defined GNULIB_POSIXCHECK
150 /* strncasecmp() does not work with multibyte strings:
151 POSIX says that it operates on "strings", and "string" in POSIX is defined
152 as a sequence of bytes, not of characters. */
154 # define strncasecmp(a,b,n) \
155 (GL_LINK_WARNING ("strncasecmp cannot work correctly on character strings in multibyte locales - use mbsncasecmp or mbspcasecmp if you care about internationalization, or use c_strncasecmp (from gnulib module c-strcase) if you want a locale independent function"), \
156 strncasecmp (a, b, n))
159 #if defined GNULIB_POSIXCHECK
160 /* strchr() does not work with multibyte strings if the locale encoding is
161 GB18030 and the character to be searched is a digit. */
163 # define strchr(s,c) \
164 (GL_LINK_WARNING ("strchr cannot work correctly on character strings in some multibyte locales - use mbschr if you care about internationalization"), \
168 /* Find the first occurrence of C in S or the final NUL byte. */
169 #if @GNULIB_STRCHRNUL@
170 # if ! @HAVE_STRCHRNUL@
171 extern char *strchrnul (char const *__s, int __c_in);
173 #elif defined GNULIB_POSIXCHECK
175 # define strchrnul(a,b) \
176 (GL_LINK_WARNING ("strchrnul is unportable - use gnulib module strchrnul for portability"), \
180 /* Duplicate S, returning an identical malloc'd string. */
182 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
183 extern char *strdup (char const *__s);
185 #elif defined GNULIB_POSIXCHECK
188 (GL_LINK_WARNING ("strdup is unportable - use gnulib module strdup for portability"), \
192 /* Return a newly allocated copy of at most N bytes of STRING. */
194 # if ! @HAVE_STRNDUP@
196 # define strndup rpl_strndup
197 # if ! @HAVE_DECL_STRNDUP@
198 extern char *strndup (char const *__string, size_t __n);
201 #elif defined GNULIB_POSIXCHECK
203 # define strndup(a,n) \
204 (GL_LINK_WARNING ("strndup is unportable - use gnulib module strndup for portability"), \
208 /* Find the length (number of bytes) of STRING, but scan at most
209 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
212 # if ! @HAVE_DECL_STRNLEN@
213 extern size_t strnlen (char const *__string, size_t __maxlen);
215 #elif defined GNULIB_POSIXCHECK
217 # define strnlen(a,n) \
218 (GL_LINK_WARNING ("strnlen is unportable - use gnulib module strnlen for portability"), \
222 #if defined GNULIB_POSIXCHECK
223 /* strcspn() assumes the second argument is a list of single-byte characters.
224 Even in this simple case, it does not work with multibyte strings if the
225 locale encoding is GB18030 and one of the characters to be searched is a
228 # define strcspn(s,a) \
229 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings in multibyte locales - 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);
238 # if defined GNULIB_POSIXCHECK
239 /* strpbrk() assumes the second argument is a list of single-byte characters.
240 Even in this simple case, it does not work with multibyte strings if the
241 locale encoding is GB18030 and one of the characters to be searched is a
244 # define strpbrk(s,a) \
245 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings in multibyte locales - use mbspbrk if you care about internationalization"), \
248 #elif defined GNULIB_POSIXCHECK
250 # define strpbrk(s,a) \
251 (GL_LINK_WARNING ("strpbrk is unportable - use gnulib module strpbrk for portability"), \
255 #if defined GNULIB_POSIXCHECK
256 /* strspn() assumes the second argument is a list of single-byte characters.
257 Even in this simple case, it cannot work with multibyte strings. */
259 # define strspn(s,a) \
260 (GL_LINK_WARNING ("strspn cannot work correctly on character strings in multibyte locales - 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 in some multibyte locales - 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 in multibyte locales - use mbssep if you care about internationalization"), \
299 #elif defined GNULIB_POSIXCHECK
301 # define strsep(s,d) \
302 (GL_LINK_WARNING ("strsep is unportable - use gnulib module strsep for portability"), \
306 #if defined GNULIB_POSIXCHECK
307 /* strstr() does not work with multibyte strings if the locale encoding is
308 different from UTF-8:
309 POSIX says that it operates on "strings", and "string" in POSIX is defined
310 as a sequence of bytes, not of characters. */
312 # define strstr(a,b) \
313 (GL_LINK_WARNING ("strstr cannot work correctly on character strings in most multibyte locales - use mbsstr if you care about internationalization"), \
317 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
319 #if ! @HAVE_STRCASESTR@
320 extern char *strcasestr (const char *haystack, const char *needle);
322 #if defined GNULIB_POSIXCHECK
323 /* strcasestr() does not work with multibyte strings:
324 It is a glibc extension, and glibc implements it only for unibyte
327 # define strcasestr(a,b) \
328 (GL_LINK_WARNING ("strcasestr does work correctly on character strings in multibyte locales - use mbscasestr if you care about internationalization, or use c-strcasestr if you want a locale independent function"), \
332 /* Parse S into tokens separated by characters in DELIM.
333 If S is NULL, the saved pointer in SAVE_PTR is used as
334 the next starting point. For example:
335 char s[] = "-abc-=-def";
337 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
338 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
339 x = strtok_r(NULL, "=", &sp); // x = NULL
342 This is a variant of strtok() that is multithread-safe.
344 For the POSIX documentation for this function, see:
345 http://www.opengroup.org/susv3xsh/strtok.html
347 Caveat: It modifies the original string.
348 Caveat: These functions cannot be used on constant strings.
349 Caveat: The identity of the delimiting character is lost.
350 Caveat: It doesn't work with multibyte strings unless all of the delimiter
351 characters are ASCII characters < 0x30.
353 See also strsep(). */
354 #if @GNULIB_STRTOK_R@
355 # if ! @HAVE_DECL_STRTOK_R@
356 extern char *strtok_r (char *restrict s, char const *restrict delim,
357 char **restrict save_ptr);
359 # if defined GNULIB_POSIXCHECK
361 # define strtok_r(s,d,p) \
362 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings in multibyte locales - use mbstok_r if you care about internationalization"), \
365 #elif defined GNULIB_POSIXCHECK
367 # define strtok_r(s,d,p) \
368 (GL_LINK_WARNING ("strtok_r is unportable - use gnulib module strtok_r for portability"), \
373 /* The following functions are not specified by POSIX. They are gnulib
377 /* Return the number of multibyte characters in the character string STRING.
378 This considers multibyte characters, unlike strlen, which counts bytes. */
379 extern size_t mbslen (const char *string);
383 /* Locate the first single-byte character C in the character string STRING,
384 and return a pointer to it. Return NULL if C is not found in STRING.
385 Unlike strchr(), this function works correctly in multibyte locales with
386 encodings such as GB18030. */
387 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
388 extern char * mbschr (const char *string, int c);
392 /* Locate the last single-byte character C in the character string STRING,
393 and return a pointer to it. Return NULL if C is not found in STRING.
394 Unlike strrchr(), this function works correctly in multibyte locales with
395 encodings such as GB18030. */
396 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
397 extern char * mbsrchr (const char *string, int c);
401 /* Find the first occurrence of the character string NEEDLE in the character
402 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
403 Unlike strstr(), this function works correctly in multibyte locales with
404 encodings different from UTF-8. */
405 extern char * mbsstr (const char *haystack, const char *needle);
408 #if @GNULIB_MBSCASECMP@
409 /* Compare the character strings S1 and S2, ignoring case, returning less than,
410 equal to or greater than zero if S1 is lexicographically less than, equal to
412 Note: This function may, in multibyte locales, return 0 for strings of
414 Unlike strcasecmp(), this function works correctly in multibyte locales. */
415 extern int mbscasecmp (const char *s1, const char *s2);
418 #if @GNULIB_MBSNCASECMP@
419 /* Compare the initial segment of the character string S1 consisting of at most
420 N characters with the initial segment of the character string S2 consisting
421 of at most N characters, ignoring case, returning less than, equal to or
422 greater than zero if the initial segment of S1 is lexicographically less
423 than, equal to or greater than the initial segment of S2.
424 Note: This function may, in multibyte locales, return 0 for initial segments
425 of different lengths!
426 Unlike strncasecmp(), this function works correctly in multibyte locales.
427 But beware that N is not a byte count but a character count! */
428 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
431 #if @GNULIB_MBSPCASECMP@
432 /* Compare the initial segment of the character string STRING consisting of
433 at most mbslen (PREFIX) characters with the character string PREFIX,
434 ignoring case, returning less than, equal to or greater than zero if this
435 initial segment is lexicographically less than, equal to or greater than
437 Note: This function may, in multibyte locales, return 0 if STRING is of
438 smaller length than PREFIX!
439 Unlike strncasecmp(), this function works correctly in multibyte
441 extern char * mbspcasecmp (const char *string, const char *prefix);
444 #if @GNULIB_MBSCASESTR@
445 /* Find the first occurrence of the character string NEEDLE in the character
446 string HAYSTACK, using case-insensitive comparison.
447 Note: This function may, in multibyte locales, return success even if
448 strlen (haystack) < strlen (needle) !
449 Unlike strcasestr(), this function works correctly in multibyte locales. */
450 extern char * mbscasestr (const char *haystack, const char *needle);
454 /* Find the first occurrence in the character string STRING of any character
455 in the character string ACCEPT. Return the number of bytes from the
456 beginning of the string to this occurrence, or to the end of the string
458 Unlike strcspn(), this function works correctly in multibyte locales. */
459 extern size_t mbscspn (const char *string, const char *accept);
463 /* Find the first occurrence in the character string STRING of any character
464 in the character string ACCEPT. Return the pointer to it, or NULL if none
466 Unlike strpbrk(), this function works correctly in multibyte locales. */
467 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
468 extern char * mbspbrk (const char *string, const char *accept);
472 /* Find the first occurrence in the character string STRING of any character
473 not in the character string REJECT. Return the number of bytes from the
474 beginning of the string to this occurrence, or to the end of the string
476 Unlike strspn(), this function works correctly in multibyte locales. */
477 extern size_t mbsspn (const char *string, const char *reject);
481 /* Search the next delimiter (multibyte character listed in the character
482 string DELIM) starting at the character string *STRINGP.
483 If one is found, overwrite it with a NUL, and advance *STRINGP to point
484 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
485 If *STRINGP was already NULL, nothing happens.
486 Return the old value of *STRINGP.
488 This is a variant of mbstok_r() that supports empty fields.
490 Caveat: It modifies the original string.
491 Caveat: These functions cannot be used on constant strings.
492 Caveat: The identity of the delimiting character is lost.
494 See also mbstok_r(). */
495 extern char * mbssep (char **stringp, const char *delim);
498 #if @GNULIB_MBSTOK_R@
499 /* Parse the character string STRING into tokens separated by characters in
500 the character string DELIM.
501 If STRING is NULL, the saved pointer in SAVE_PTR is used as
502 the next starting point. For example:
503 char s[] = "-abc-=-def";
505 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
506 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
507 x = mbstok_r(NULL, "=", &sp); // x = NULL
510 Caveat: It modifies the original string.
511 Caveat: These functions cannot be used on constant strings.
512 Caveat: The identity of the delimiting character is lost.
514 See also mbssep(). */
515 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);