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. */
21 /* The include_next requires a split double-inclusion guard. */
22 #@INCLUDE_NEXT@ @NEXT_STRING_H@
28 /* The definition of GL_LINK_WARNING is copied here. */
31 /* GCC 2.95 and later have "__restrict"; C99 compilers have
32 "restrict", and "configure" may have defined "restrict".
33 Other compilers use __restrict, __restrict__, and _Restrict, and
34 'configure' might #define 'restrict' to those words. */
36 # if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
37 # if 199901L <= __STDC_VERSION__
38 # define __restrict restrict
49 /* Return the first occurrence of NEEDLE in HAYSTACK. */
51 # if ! @HAVE_DECL_MEMMEM@
52 extern void *memmem (void const *__haystack
, size_t __haystack_len
,
53 void const *__needle
, size_t __needle_len
);
55 #elif defined GNULIB_POSIXCHECK
57 # define memmem(a,al,b,bl) \
58 (GL_LINK_WARNING ("memmem is unportable - " \
59 "use gnulib module memmem for portability"), \
60 memmem (a, al, b, bl))
63 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
67 extern void *mempcpy (void *__restrict __dest
, void const *__restrict __src
,
70 #elif defined GNULIB_POSIXCHECK
72 # define mempcpy(a,b,n) \
73 (GL_LINK_WARNING ("mempcpy is unportable - " \
74 "use gnulib module mempcpy for portability"), \
78 /* Search backwards through a block for a byte (specified as an int). */
80 # if ! @HAVE_DECL_MEMRCHR@
81 extern void *memrchr (void const *, int, size_t);
83 #elif defined GNULIB_POSIXCHECK
85 # define memrchr(a,b,c) \
86 (GL_LINK_WARNING ("memrchr is unportable - " \
87 "use gnulib module memrchr for portability"), \
91 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
94 extern char *stpcpy (char *__restrict __dst
, char const *__restrict __src
);
96 #elif defined GNULIB_POSIXCHECK
98 # define stpcpy(a,b) \
99 (GL_LINK_WARNING ("stpcpy is unportable - " \
100 "use gnulib module stpcpy for portability"), \
104 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
105 last non-NUL byte written into DST. */
107 # if ! @HAVE_STPNCPY@
108 # define stpncpy gnu_stpncpy
109 extern char *stpncpy (char *__restrict __dst
, char const *__restrict __src
,
112 #elif defined GNULIB_POSIXCHECK
114 # define stpncpy(a,b,n) \
115 (GL_LINK_WARNING ("stpncpy is unportable - " \
116 "use gnulib module stpncpy for portability"), \
120 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
121 greater than zero if S1 is lexicographically less than, equal to or greater
123 Note: This function does not work in multibyte locales. */
124 #if ! @HAVE_STRCASECMP@
125 extern int strcasecmp (char const *s1
, char const *s2
);
127 #if defined GNULIB_POSIXCHECK
128 /* strcasecmp() does not work with multibyte strings:
129 POSIX says that it operates on "strings", and "string" in POSIX is defined
130 as a sequence of bytes, not of characters. */
132 # define strcasecmp(a,b) \
133 (GL_LINK_WARNING ("strcasecmp cannot work correctly on character strings " \
134 "in multibyte locales - " \
135 "use mbscasecmp if you care about " \
136 "internationalization, or use c_strcasecmp (from " \
137 "gnulib module c-strcase) if you want a locale " \
138 "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 " \
156 "strings in multibyte locales - " \
157 "use mbsncasecmp or mbspcasecmp if you care about " \
158 "internationalization, or use c_strncasecmp (from " \
159 "gnulib module c-strcase) if you want a locale " \
160 "independent function"), \
161 strncasecmp (a, b, n))
164 #if defined GNULIB_POSIXCHECK
165 /* strchr() does not work with multibyte strings if the locale encoding is
166 GB18030 and the character to be searched is a digit. */
168 # define strchr(s,c) \
169 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
170 "in some multibyte locales - " \
171 "use mbschr if you care about internationalization"), \
175 /* Find the first occurrence of C in S or the final NUL byte. */
176 #if @GNULIB_STRCHRNUL@
177 # if ! @HAVE_STRCHRNUL@
178 extern char *strchrnul (char const *__s
, int __c_in
);
180 #elif defined GNULIB_POSIXCHECK
182 # define strchrnul(a,b) \
183 (GL_LINK_WARNING ("strchrnul is unportable - " \
184 "use gnulib module strchrnul for portability"), \
188 /* Duplicate S, returning an identical malloc'd string. */
190 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
191 extern char *strdup (char const *__s
);
193 #elif defined GNULIB_POSIXCHECK
196 (GL_LINK_WARNING ("strdup is unportable - " \
197 "use gnulib module strdup for portability"), \
201 /* Return a newly allocated copy of at most N bytes of STRING. */
203 # if ! @HAVE_STRNDUP@
205 # define strndup rpl_strndup
207 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
208 extern char *strndup (char const *__string
, size_t __n
);
210 #elif defined GNULIB_POSIXCHECK
212 # define strndup(a,n) \
213 (GL_LINK_WARNING ("strndup is unportable - " \
214 "use gnulib module strndup for portability"), \
218 /* Find the length (number of bytes) of STRING, but scan at most
219 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
222 # if ! @HAVE_DECL_STRNLEN@
223 extern size_t strnlen (char const *__string
, size_t __maxlen
);
225 #elif defined GNULIB_POSIXCHECK
227 # define strnlen(a,n) \
228 (GL_LINK_WARNING ("strnlen is unportable - " \
229 "use gnulib module strnlen for portability"), \
233 #if defined GNULIB_POSIXCHECK
234 /* strcspn() assumes the second argument is a list of single-byte characters.
235 Even in this simple case, it does not work with multibyte strings if the
236 locale encoding is GB18030 and one of the characters to be searched is a
239 # define strcspn(s,a) \
240 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
241 "in multibyte locales - " \
242 "use mbscspn if you care about internationalization"), \
246 /* Find the first occurrence in S of any character in ACCEPT. */
248 # if ! @HAVE_STRPBRK@
249 extern char *strpbrk (char const *__s
, char const *__accept
);
251 # if defined GNULIB_POSIXCHECK
252 /* strpbrk() assumes the second argument is a list of single-byte characters.
253 Even in this simple case, it does not work with multibyte strings if the
254 locale encoding is GB18030 and one of the characters to be searched is a
257 # define strpbrk(s,a) \
258 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
259 "in multibyte locales - " \
260 "use mbspbrk if you care about internationalization"), \
263 #elif defined GNULIB_POSIXCHECK
265 # define strpbrk(s,a) \
266 (GL_LINK_WARNING ("strpbrk is unportable - " \
267 "use gnulib module strpbrk for portability"), \
271 #if defined GNULIB_POSIXCHECK
272 /* strspn() assumes the second argument is a list of single-byte characters.
273 Even in this simple case, it cannot work with multibyte strings. */
275 # define strspn(s,a) \
276 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
277 "in multibyte locales - " \
278 "use mbsspn if you care about internationalization"), \
282 #if defined GNULIB_POSIXCHECK
283 /* strrchr() does not work with multibyte strings if the locale encoding is
284 GB18030 and the character to be searched is a digit. */
286 # define strrchr(s,c) \
287 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
288 "in some multibyte locales - " \
289 "use mbsrchr if you care about internationalization"), \
293 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
294 If one is found, overwrite it with a NUL, and advance *STRINGP
295 to point to the next char after it. Otherwise, set *STRINGP to NULL.
296 If *STRINGP was already NULL, nothing happens.
297 Return the old value of *STRINGP.
299 This is a variant of strtok() that is multithread-safe and supports
302 Caveat: It modifies the original string.
303 Caveat: These functions cannot be used on constant strings.
304 Caveat: The identity of the delimiting character is lost.
305 Caveat: It doesn't work with multibyte strings unless all of the delimiter
306 characters are ASCII characters < 0x30.
308 See also strtok_r(). */
311 extern char *strsep (char **__restrict __stringp
, char const *__restrict __delim
);
313 # if defined GNULIB_POSIXCHECK
315 # define strsep(s,d) \
316 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
317 "in multibyte locales - " \
318 "use mbssep if you care about internationalization"), \
321 #elif defined GNULIB_POSIXCHECK
323 # define strsep(s,d) \
324 (GL_LINK_WARNING ("strsep is unportable - " \
325 "use gnulib module strsep for portability"), \
329 #if defined GNULIB_POSIXCHECK
330 /* strstr() does not work with multibyte strings if the locale encoding is
331 different from UTF-8:
332 POSIX says that it operates on "strings", and "string" in POSIX is defined
333 as a sequence of bytes, not of characters. */
335 # define strstr(a,b) \
336 (GL_LINK_WARNING ("strstr cannot work correctly on character strings " \
337 "in most multibyte locales - " \
338 "use mbsstr if you care about internationalization"), \
342 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
344 #if ! @HAVE_STRCASESTR@
345 extern char *strcasestr (const char *haystack
, const char *needle
);
347 #if defined GNULIB_POSIXCHECK
348 /* strcasestr() does not work with multibyte strings:
349 It is a glibc extension, and glibc implements it only for unibyte
352 # define strcasestr(a,b) \
353 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
354 "in multibyte locales - " \
355 "use mbscasestr if you care about " \
356 "internationalization, or use c-strcasestr if you want " \
357 "a locale independent function"), \
361 /* Parse S into tokens separated by characters in DELIM.
362 If S is NULL, the saved pointer in SAVE_PTR is used as
363 the next starting point. For example:
364 char s[] = "-abc-=-def";
366 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
367 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
368 x = strtok_r(NULL, "=", &sp); // x = NULL
371 This is a variant of strtok() that is multithread-safe.
373 For the POSIX documentation for this function, see:
374 http://www.opengroup.org/susv3xsh/strtok.html
376 Caveat: It modifies the original string.
377 Caveat: These functions cannot be used on constant strings.
378 Caveat: The identity of the delimiting character is lost.
379 Caveat: It doesn't work with multibyte strings unless all of the delimiter
380 characters are ASCII characters < 0x30.
382 See also strsep(). */
383 #if @GNULIB_STRTOK_R@
384 # if ! @HAVE_DECL_STRTOK_R@
385 extern char *strtok_r (char *__restrict s
, char const *__restrict delim
,
386 char **__restrict save_ptr
);
388 # if defined GNULIB_POSIXCHECK
390 # define strtok_r(s,d,p) \
391 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
392 "in multibyte locales - " \
393 "use mbstok_r if you care about internationalization"), \
396 #elif defined GNULIB_POSIXCHECK
398 # define strtok_r(s,d,p) \
399 (GL_LINK_WARNING ("strtok_r is unportable - " \
400 "use gnulib module strtok_r for portability"), \
405 /* The following functions are not specified by POSIX. They are gnulib
409 /* Return the number of multibyte characters in the character string STRING.
410 This considers multibyte characters, unlike strlen, which counts bytes. */
411 extern size_t mbslen (const char *string
);
415 /* Return the number of multibyte characters in the character string starting
416 at STRING and ending at STRING + LEN. */
417 extern size_t mbsnlen (const char *string
, size_t len
);
421 /* Locate the first single-byte character C in the character string STRING,
422 and return a pointer to it. Return NULL if C is not found in STRING.
423 Unlike strchr(), this function works correctly in multibyte locales with
424 encodings such as GB18030. */
425 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
426 extern char * mbschr (const char *string
, int c
);
430 /* Locate the last single-byte character C in the character string STRING,
431 and return a pointer to it. Return NULL if C is not found in STRING.
432 Unlike strrchr(), this function works correctly in multibyte locales with
433 encodings such as GB18030. */
434 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
435 extern char * mbsrchr (const char *string
, int c
);
439 /* Find the first occurrence of the character string NEEDLE in the character
440 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
441 Unlike strstr(), this function works correctly in multibyte locales with
442 encodings different from UTF-8. */
443 extern char * mbsstr (const char *haystack
, const char *needle
);
446 #if @GNULIB_MBSCASECMP@
447 /* Compare the character strings S1 and S2, ignoring case, returning less than,
448 equal to or greater than zero if S1 is lexicographically less than, equal to
450 Note: This function may, in multibyte locales, return 0 for strings of
452 Unlike strcasecmp(), this function works correctly in multibyte locales. */
453 extern int mbscasecmp (const char *s1
, const char *s2
);
456 #if @GNULIB_MBSNCASECMP@
457 /* Compare the initial segment of the character string S1 consisting of at most
458 N characters with the initial segment of the character string S2 consisting
459 of at most N characters, ignoring case, returning less than, equal to or
460 greater than zero if the initial segment of S1 is lexicographically less
461 than, equal to or greater than the initial segment of S2.
462 Note: This function may, in multibyte locales, return 0 for initial segments
463 of different lengths!
464 Unlike strncasecmp(), this function works correctly in multibyte locales.
465 But beware that N is not a byte count but a character count! */
466 extern int mbsncasecmp (const char *s1
, const char *s2
, size_t n
);
469 #if @GNULIB_MBSPCASECMP@
470 /* Compare the initial segment of the character string STRING consisting of
471 at most mbslen (PREFIX) characters with the character string PREFIX,
472 ignoring case, returning less than, equal to or greater than zero if this
473 initial segment is lexicographically less than, equal to or greater than
475 Note: This function may, in multibyte locales, return 0 if STRING is of
476 smaller length than PREFIX!
477 Unlike strncasecmp(), this function works correctly in multibyte
479 extern char * mbspcasecmp (const char *string
, const char *prefix
);
482 #if @GNULIB_MBSCASESTR@
483 /* Find the first occurrence of the character string NEEDLE in the character
484 string HAYSTACK, using case-insensitive comparison.
485 Note: This function may, in multibyte locales, return success even if
486 strlen (haystack) < strlen (needle) !
487 Unlike strcasestr(), this function works correctly in multibyte locales. */
488 extern char * mbscasestr (const char *haystack
, const char *needle
);
492 /* Find the first occurrence in the character string STRING of any character
493 in the character string ACCEPT. Return the number of bytes from the
494 beginning of the string to this occurrence, or to the end of the string
496 Unlike strcspn(), this function works correctly in multibyte locales. */
497 extern size_t mbscspn (const char *string
, const char *accept
);
501 /* Find the first occurrence in the character string STRING of any character
502 in the character string ACCEPT. Return the pointer to it, or NULL if none
504 Unlike strpbrk(), this function works correctly in multibyte locales. */
505 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
506 extern char * mbspbrk (const char *string
, const char *accept
);
510 /* Find the first occurrence in the character string STRING of any character
511 not in the character string REJECT. Return the number of bytes from the
512 beginning of the string to this occurrence, or to the end of the string
514 Unlike strspn(), this function works correctly in multibyte locales. */
515 extern size_t mbsspn (const char *string
, const char *reject
);
519 /* Search the next delimiter (multibyte character listed in the character
520 string DELIM) starting at the character string *STRINGP.
521 If one is found, overwrite it with a NUL, and advance *STRINGP to point
522 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
523 If *STRINGP was already NULL, nothing happens.
524 Return the old value of *STRINGP.
526 This is a variant of mbstok_r() that supports empty fields.
528 Caveat: It modifies the original string.
529 Caveat: These functions cannot be used on constant strings.
530 Caveat: The identity of the delimiting character is lost.
532 See also mbstok_r(). */
533 extern char * mbssep (char **stringp
, const char *delim
);
536 #if @GNULIB_MBSTOK_R@
537 /* Parse the character string STRING into tokens separated by characters in
538 the character string DELIM.
539 If STRING is NULL, the saved pointer in SAVE_PTR is used as
540 the next starting point. For example:
541 char s[] = "-abc-=-def";
543 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
544 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
545 x = mbstok_r(NULL, "=", &sp); // x = NULL
548 Caveat: It modifies the original string.
549 Caveat: These functions cannot be used on constant strings.
550 Caveat: The identity of the delimiting character is lost.
552 See also mbssep(). */
553 extern char * mbstok_r (char *string
, const char *delim
, char **save_ptr
);
556 /* Map any int, typically from errno, into an error message. */
557 #if @GNULIB_STRERROR@
558 # if @REPLACE_STRERROR@
560 # define strerror rpl_strerror
562 # if !@HAVE_DECL_STRERROR@ || @REPLACE_STRERROR@
563 extern char *strerror (int);
565 #elif defined GNULIB_POSIXCHECK
567 # define strerror(e) \
568 (GL_LINK_WARNING ("strerror is unportable - " \
569 "use gnulib module strerror to guarantee non-NULL result"), \
578 #endif /* _GL_STRING_H */
579 #endif /* _GL_STRING_H */