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. */
36 /* Return the first occurrence of NEEDLE in HAYSTACK. */
38 # if ! @HAVE_DECL_MEMMEM@
39 extern void *memmem (void const *__haystack, size_t __haystack_len,
40 void const *__needle, size_t __needle_len);
42 #elif defined GNULIB_POSIXCHECK
44 # define memmem(a,al,b,bl) \
45 (GL_LINK_WARNING ("memmem is unportable - " \
46 "use gnulib module memmem for portability"), \
47 memmem (a, al, b, bl))
50 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
54 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
57 #elif defined GNULIB_POSIXCHECK
59 # define mempcpy(a,b,n) \
60 (GL_LINK_WARNING ("mempcpy is unportable - " \
61 "use gnulib module mempcpy for portability"), \
65 /* Search backwards through a block for a byte (specified as an int). */
67 # if ! @HAVE_DECL_MEMRCHR@
68 extern void *memrchr (void const *, int, size_t);
70 #elif defined GNULIB_POSIXCHECK
72 # define memrchr(a,b,c) \
73 (GL_LINK_WARNING ("memrchr is unportable - " \
74 "use gnulib module memrchr for portability"), \
78 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
81 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
83 #elif defined GNULIB_POSIXCHECK
85 # define stpcpy(a,b) \
86 (GL_LINK_WARNING ("stpcpy is unportable - " \
87 "use gnulib module stpcpy for portability"), \
91 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
92 last non-NUL byte written into DST. */
95 # define stpncpy gnu_stpncpy
96 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
99 #elif defined GNULIB_POSIXCHECK
101 # define stpncpy(a,b,n) \
102 (GL_LINK_WARNING ("stpncpy is unportable - " \
103 "use gnulib module stpncpy for portability"), \
107 #if defined GNULIB_POSIXCHECK
108 /* strchr() does not work with multibyte strings if the locale encoding is
109 GB18030 and the character to be searched is a digit. */
111 # define strchr(s,c) \
112 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
113 "in some multibyte locales - " \
114 "use mbschr if you care about internationalization"), \
118 /* Find the first occurrence of C in S or the final NUL byte. */
119 #if @GNULIB_STRCHRNUL@
120 # if ! @HAVE_STRCHRNUL@
121 extern char *strchrnul (char const *__s, int __c_in);
123 #elif defined GNULIB_POSIXCHECK
125 # define strchrnul(a,b) \
126 (GL_LINK_WARNING ("strchrnul is unportable - " \
127 "use gnulib module strchrnul for portability"), \
131 /* Duplicate S, returning an identical malloc'd string. */
133 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
134 extern char *strdup (char const *__s);
136 #elif defined GNULIB_POSIXCHECK
139 (GL_LINK_WARNING ("strdup is unportable - " \
140 "use gnulib module strdup for portability"), \
144 /* Return a newly allocated copy of at most N bytes of STRING. */
146 # if ! @HAVE_STRNDUP@
148 # define strndup rpl_strndup
150 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
151 extern char *strndup (char const *__string, size_t __n);
153 #elif defined GNULIB_POSIXCHECK
155 # define strndup(a,n) \
156 (GL_LINK_WARNING ("strndup is unportable - " \
157 "use gnulib module strndup for portability"), \
161 /* Find the length (number of bytes) of STRING, but scan at most
162 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
165 # if ! @HAVE_DECL_STRNLEN@
166 extern size_t strnlen (char const *__string, size_t __maxlen);
168 #elif defined GNULIB_POSIXCHECK
170 # define strnlen(a,n) \
171 (GL_LINK_WARNING ("strnlen is unportable - " \
172 "use gnulib module strnlen for portability"), \
176 #if defined GNULIB_POSIXCHECK
177 /* strcspn() assumes the second argument is a list of single-byte characters.
178 Even in this simple case, it does not work with multibyte strings if the
179 locale encoding is GB18030 and one of the characters to be searched is a
182 # define strcspn(s,a) \
183 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
184 "in multibyte locales - " \
185 "use mbscspn if you care about internationalization"), \
189 /* Find the first occurrence in S of any character in ACCEPT. */
191 # if ! @HAVE_STRPBRK@
192 extern char *strpbrk (char const *__s, char const *__accept);
194 # if defined GNULIB_POSIXCHECK
195 /* strpbrk() assumes the second argument is a list of single-byte characters.
196 Even in this simple case, it does not work with multibyte strings if the
197 locale encoding is GB18030 and one of the characters to be searched is a
200 # define strpbrk(s,a) \
201 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
202 "in multibyte locales - " \
203 "use mbspbrk if you care about internationalization"), \
206 #elif defined GNULIB_POSIXCHECK
208 # define strpbrk(s,a) \
209 (GL_LINK_WARNING ("strpbrk is unportable - " \
210 "use gnulib module strpbrk for portability"), \
214 #if defined GNULIB_POSIXCHECK
215 /* strspn() assumes the second argument is a list of single-byte characters.
216 Even in this simple case, it cannot work with multibyte strings. */
218 # define strspn(s,a) \
219 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
220 "in multibyte locales - " \
221 "use mbsspn if you care about internationalization"), \
225 #if defined GNULIB_POSIXCHECK
226 /* strrchr() does not work with multibyte strings if the locale encoding is
227 GB18030 and the character to be searched is a digit. */
229 # define strrchr(s,c) \
230 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
231 "in some multibyte locales - " \
232 "use mbsrchr if you care about internationalization"), \
236 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
237 If one is found, overwrite it with a NUL, and advance *STRINGP
238 to point to the next char after it. Otherwise, set *STRINGP to NULL.
239 If *STRINGP was already NULL, nothing happens.
240 Return the old value of *STRINGP.
242 This is a variant of strtok() that is multithread-safe and supports
245 Caveat: It modifies the original string.
246 Caveat: These functions cannot be used on constant strings.
247 Caveat: The identity of the delimiting character is lost.
248 Caveat: It doesn't work with multibyte strings unless all of the delimiter
249 characters are ASCII characters < 0x30.
251 See also strtok_r(). */
254 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
256 # if defined GNULIB_POSIXCHECK
258 # define strsep(s,d) \
259 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
260 "in multibyte locales - " \
261 "use mbssep if you care about internationalization"), \
264 #elif defined GNULIB_POSIXCHECK
266 # define strsep(s,d) \
267 (GL_LINK_WARNING ("strsep is unportable - " \
268 "use gnulib module strsep for portability"), \
272 #if defined GNULIB_POSIXCHECK
273 /* strstr() does not work with multibyte strings if the locale encoding is
274 different from UTF-8:
275 POSIX says that it operates on "strings", and "string" in POSIX is defined
276 as a sequence of bytes, not of characters. */
278 # define strstr(a,b) \
279 (GL_LINK_WARNING ("strstr cannot work correctly on character strings " \
280 "in most multibyte locales - " \
281 "use mbsstr if you care about internationalization"), \
285 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
287 #if ! @HAVE_STRCASESTR@
288 extern char *strcasestr (const char *haystack, const char *needle);
290 #if defined GNULIB_POSIXCHECK
291 /* strcasestr() does not work with multibyte strings:
292 It is a glibc extension, and glibc implements it only for unibyte
295 # define strcasestr(a,b) \
296 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
297 "in multibyte locales - " \
298 "use mbscasestr if you care about " \
299 "internationalization, or use c-strcasestr if you want " \
300 "a locale independent function"), \
304 /* Parse S into tokens separated by characters in DELIM.
305 If S is NULL, the saved pointer in SAVE_PTR is used as
306 the next starting point. For example:
307 char s[] = "-abc-=-def";
309 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
310 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
311 x = strtok_r(NULL, "=", &sp); // x = NULL
314 This is a variant of strtok() that is multithread-safe.
316 For the POSIX documentation for this function, see:
317 http://www.opengroup.org/susv3xsh/strtok.html
319 Caveat: It modifies the original string.
320 Caveat: These functions cannot be used on constant strings.
321 Caveat: The identity of the delimiting character is lost.
322 Caveat: It doesn't work with multibyte strings unless all of the delimiter
323 characters are ASCII characters < 0x30.
325 See also strsep(). */
326 #if @GNULIB_STRTOK_R@
327 # if ! @HAVE_DECL_STRTOK_R@
328 extern char *strtok_r (char *restrict s, char const *restrict delim,
329 char **restrict save_ptr);
331 # if defined GNULIB_POSIXCHECK
333 # define strtok_r(s,d,p) \
334 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
335 "in multibyte locales - " \
336 "use mbstok_r if you care about internationalization"), \
339 #elif defined GNULIB_POSIXCHECK
341 # define strtok_r(s,d,p) \
342 (GL_LINK_WARNING ("strtok_r is unportable - " \
343 "use gnulib module strtok_r for portability"), \
348 /* The following functions are not specified by POSIX. They are gnulib
352 /* Return the number of multibyte characters in the character string STRING.
353 This considers multibyte characters, unlike strlen, which counts bytes. */
354 extern size_t mbslen (const char *string);
358 /* Return the number of multibyte characters in the character string starting
359 at STRING and ending at STRING + LEN. */
360 extern size_t mbsnlen (const char *string, size_t len);
364 /* Locate the first single-byte character C in the character string STRING,
365 and return a pointer to it. Return NULL if C is not found in STRING.
366 Unlike strchr(), this function works correctly in multibyte locales with
367 encodings such as GB18030. */
368 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
369 extern char * mbschr (const char *string, int c);
373 /* Locate the last single-byte character C in the character string STRING,
374 and return a pointer to it. Return NULL if C is not found in STRING.
375 Unlike strrchr(), this function works correctly in multibyte locales with
376 encodings such as GB18030. */
377 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
378 extern char * mbsrchr (const char *string, int c);
382 /* Find the first occurrence of the character string NEEDLE in the character
383 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
384 Unlike strstr(), this function works correctly in multibyte locales with
385 encodings different from UTF-8. */
386 extern char * mbsstr (const char *haystack, const char *needle);
389 #if @GNULIB_MBSCASECMP@
390 /* Compare the character strings S1 and S2, ignoring case, returning less than,
391 equal to or greater than zero if S1 is lexicographically less than, equal to
393 Note: This function may, in multibyte locales, return 0 for strings of
395 Unlike strcasecmp(), this function works correctly in multibyte locales. */
396 extern int mbscasecmp (const char *s1, const char *s2);
399 #if @GNULIB_MBSNCASECMP@
400 /* Compare the initial segment of the character string S1 consisting of at most
401 N characters with the initial segment of the character string S2 consisting
402 of at most N characters, ignoring case, returning less than, equal to or
403 greater than zero if the initial segment of S1 is lexicographically less
404 than, equal to or greater than the initial segment of S2.
405 Note: This function may, in multibyte locales, return 0 for initial segments
406 of different lengths!
407 Unlike strncasecmp(), this function works correctly in multibyte locales.
408 But beware that N is not a byte count but a character count! */
409 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
412 #if @GNULIB_MBSPCASECMP@
413 /* Compare the initial segment of the character string STRING consisting of
414 at most mbslen (PREFIX) characters with the character string PREFIX,
415 ignoring case, returning less than, equal to or greater than zero if this
416 initial segment is lexicographically less than, equal to or greater than
418 Note: This function may, in multibyte locales, return 0 if STRING is of
419 smaller length than PREFIX!
420 Unlike strncasecmp(), this function works correctly in multibyte
422 extern char * mbspcasecmp (const char *string, const char *prefix);
425 #if @GNULIB_MBSCASESTR@
426 /* Find the first occurrence of the character string NEEDLE in the character
427 string HAYSTACK, using case-insensitive comparison.
428 Note: This function may, in multibyte locales, return success even if
429 strlen (haystack) < strlen (needle) !
430 Unlike strcasestr(), this function works correctly in multibyte locales. */
431 extern char * mbscasestr (const char *haystack, const char *needle);
435 /* Find the first occurrence in the character string STRING of any character
436 in the character string ACCEPT. Return the number of bytes from the
437 beginning of the string to this occurrence, or to the end of the string
439 Unlike strcspn(), this function works correctly in multibyte locales. */
440 extern size_t mbscspn (const char *string, const char *accept);
444 /* Find the first occurrence in the character string STRING of any character
445 in the character string ACCEPT. Return the pointer to it, or NULL if none
447 Unlike strpbrk(), this function works correctly in multibyte locales. */
448 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
449 extern char * mbspbrk (const char *string, const char *accept);
453 /* Find the first occurrence in the character string STRING of any character
454 not in the character string REJECT. Return the number of bytes from the
455 beginning of the string to this occurrence, or to the end of the string
457 Unlike strspn(), this function works correctly in multibyte locales. */
458 extern size_t mbsspn (const char *string, const char *reject);
462 /* Search the next delimiter (multibyte character listed in the character
463 string DELIM) starting at the character string *STRINGP.
464 If one is found, overwrite it with a NUL, and advance *STRINGP to point
465 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
466 If *STRINGP was already NULL, nothing happens.
467 Return the old value of *STRINGP.
469 This is a variant of mbstok_r() that supports empty fields.
471 Caveat: It modifies the original string.
472 Caveat: These functions cannot be used on constant strings.
473 Caveat: The identity of the delimiting character is lost.
475 See also mbstok_r(). */
476 extern char * mbssep (char **stringp, const char *delim);
479 #if @GNULIB_MBSTOK_R@
480 /* Parse the character string STRING into tokens separated by characters in
481 the character string DELIM.
482 If STRING is NULL, the saved pointer in SAVE_PTR is used as
483 the next starting point. For example:
484 char s[] = "-abc-=-def";
486 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
487 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
488 x = mbstok_r(NULL, "=", &sp); // x = NULL
491 Caveat: It modifies the original string.
492 Caveat: These functions cannot be used on constant strings.
493 Caveat: The identity of the delimiting character is lost.
495 See also mbssep(). */
496 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
499 /* Map any int, typically from errno, into an error message. */
500 #if @GNULIB_STRERROR@
501 # if @REPLACE_STRERROR@
503 # define strerror rpl_strerror
504 extern char *strerror (int);
506 #elif defined GNULIB_POSIXCHECK
508 # define strerror(e) \
509 (GL_LINK_WARNING ("strerror is unportable - " \
510 "use gnulib module strerror to guarantee non-NULL result"), \
519 #endif /* _GL_STRING_H */
520 #endif /* _GL_STRING_H */