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 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@
29 /* This feature is available in gcc versions 2.5 and later. */
30 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
31 # define __attribute__(Spec) /* empty */
33 /* The attribute __pure__ was added in gcc 2.96. */
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
35 # define __pure__ /* empty */
40 /* The definition of GL_LINK_WARNING is copied here. */
48 /* Return the first occurrence of NEEDLE in HAYSTACK. */
51 # define memmem rpl_memmem
53 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
54 extern void *memmem (void const *__haystack, size_t __haystack_len,
55 void const *__needle, size_t __needle_len)
56 __attribute__ ((__pure__));
58 #elif defined GNULIB_POSIXCHECK
60 # define memmem(a,al,b,bl) \
61 (GL_LINK_WARNING ("memmem is unportable - " \
62 "use gnulib module memmem for portability"), \
63 memmem (a, al, b, bl))
66 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
70 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
73 #elif defined GNULIB_POSIXCHECK
75 # define mempcpy(a,b,n) \
76 (GL_LINK_WARNING ("mempcpy is unportable - " \
77 "use gnulib module mempcpy for portability"), \
81 /* Search backwards through a block for a byte (specified as an int). */
83 # if ! @HAVE_DECL_MEMRCHR@
84 extern void *memrchr (void const *, int, size_t)
85 __attribute__ ((__pure__));
87 #elif defined GNULIB_POSIXCHECK
89 # define memrchr(a,b,c) \
90 (GL_LINK_WARNING ("memrchr is unportable - " \
91 "use gnulib module memrchr for portability"), \
95 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
98 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
100 #elif defined GNULIB_POSIXCHECK
102 # define stpcpy(a,b) \
103 (GL_LINK_WARNING ("stpcpy is unportable - " \
104 "use gnulib module stpcpy for portability"), \
108 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
109 last non-NUL byte written into DST. */
111 # if ! @HAVE_STPNCPY@
112 # define stpncpy gnu_stpncpy
113 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
116 #elif defined GNULIB_POSIXCHECK
118 # define stpncpy(a,b,n) \
119 (GL_LINK_WARNING ("stpncpy is unportable - " \
120 "use gnulib module stpncpy for portability"), \
124 #if defined GNULIB_POSIXCHECK
125 /* strchr() does not work with multibyte strings if the locale encoding is
126 GB18030 and the character to be searched is a digit. */
128 # define strchr(s,c) \
129 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
130 "in some multibyte locales - " \
131 "use mbschr if you care about internationalization"), \
135 /* Find the first occurrence of C in S or the final NUL byte. */
136 #if @GNULIB_STRCHRNUL@
137 # if ! @HAVE_STRCHRNUL@
138 extern char *strchrnul (char const *__s, int __c_in)
139 __attribute__ ((__pure__));
141 #elif defined GNULIB_POSIXCHECK
143 # define strchrnul(a,b) \
144 (GL_LINK_WARNING ("strchrnul is unportable - " \
145 "use gnulib module strchrnul for portability"), \
149 /* Duplicate S, returning an identical malloc'd string. */
151 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
152 extern char *strdup (char const *__s);
154 #elif defined GNULIB_POSIXCHECK
157 (GL_LINK_WARNING ("strdup is unportable - " \
158 "use gnulib module strdup for portability"), \
162 /* Return a newly allocated copy of at most N bytes of STRING. */
164 # if ! @HAVE_STRNDUP@
166 # define strndup rpl_strndup
168 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
169 extern char *strndup (char const *__string, size_t __n);
171 #elif defined GNULIB_POSIXCHECK
173 # define strndup(a,n) \
174 (GL_LINK_WARNING ("strndup is unportable - " \
175 "use gnulib module strndup for portability"), \
179 /* Find the length (number of bytes) of STRING, but scan at most
180 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
183 # if ! @HAVE_DECL_STRNLEN@
184 extern size_t strnlen (char const *__string, size_t __maxlen)
185 __attribute__ ((__pure__));
187 #elif defined GNULIB_POSIXCHECK
189 # define strnlen(a,n) \
190 (GL_LINK_WARNING ("strnlen is unportable - " \
191 "use gnulib module strnlen for portability"), \
195 #if defined GNULIB_POSIXCHECK
196 /* strcspn() assumes the second argument is a list of single-byte characters.
197 Even in this simple case, it does not work with multibyte strings if the
198 locale encoding is GB18030 and one of the characters to be searched is a
201 # define strcspn(s,a) \
202 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
203 "in multibyte locales - " \
204 "use mbscspn if you care about internationalization"), \
208 /* Find the first occurrence in S of any character in ACCEPT. */
210 # if ! @HAVE_STRPBRK@
211 extern char *strpbrk (char const *__s, char const *__accept)
212 __attribute__ ((__pure__));
214 # if defined GNULIB_POSIXCHECK
215 /* strpbrk() assumes the second argument is a list of single-byte characters.
216 Even in this simple case, it does not work with multibyte strings if the
217 locale encoding is GB18030 and one of the characters to be searched is a
220 # define strpbrk(s,a) \
221 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
222 "in multibyte locales - " \
223 "use mbspbrk if you care about internationalization"), \
226 #elif defined GNULIB_POSIXCHECK
228 # define strpbrk(s,a) \
229 (GL_LINK_WARNING ("strpbrk is unportable - " \
230 "use gnulib module strpbrk for portability"), \
234 #if defined GNULIB_POSIXCHECK
235 /* strspn() assumes the second argument is a list of single-byte characters.
236 Even in this simple case, it cannot work with multibyte strings. */
238 # define strspn(s,a) \
239 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
240 "in multibyte locales - " \
241 "use mbsspn if you care about internationalization"), \
245 #if defined GNULIB_POSIXCHECK
246 /* strrchr() does not work with multibyte strings if the locale encoding is
247 GB18030 and the character to be searched is a digit. */
249 # define strrchr(s,c) \
250 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
251 "in some multibyte locales - " \
252 "use mbsrchr if you care about internationalization"), \
256 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
257 If one is found, overwrite it with a NUL, and advance *STRINGP
258 to point to the next char after it. Otherwise, set *STRINGP to NULL.
259 If *STRINGP was already NULL, nothing happens.
260 Return the old value of *STRINGP.
262 This is a variant of strtok() that is multithread-safe and supports
265 Caveat: It modifies the original string.
266 Caveat: These functions cannot be used on constant strings.
267 Caveat: The identity of the delimiting character is lost.
268 Caveat: It doesn't work with multibyte strings unless all of the delimiter
269 characters are ASCII characters < 0x30.
271 See also strtok_r(). */
274 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
276 # if defined GNULIB_POSIXCHECK
278 # define strsep(s,d) \
279 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
280 "in multibyte locales - " \
281 "use mbssep if you care about internationalization"), \
284 #elif defined GNULIB_POSIXCHECK
286 # define strsep(s,d) \
287 (GL_LINK_WARNING ("strsep is unportable - " \
288 "use gnulib module strsep for portability"), \
292 #if defined GNULIB_POSIXCHECK
293 /* strstr() does not work with multibyte strings if the locale encoding is
294 different from UTF-8:
295 POSIX says that it operates on "strings", and "string" in POSIX is defined
296 as a sequence of bytes, not of characters. */
298 # define strstr(a,b) \
299 (GL_LINK_WARNING ("strstr cannot work correctly on character strings " \
300 "in most multibyte locales - " \
301 "use mbsstr if you care about internationalization"), \
305 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
307 #if ! @HAVE_STRCASESTR@
308 extern char *strcasestr (const char *haystack, const char *needle)
309 __attribute__ ((__pure__));
311 #if defined GNULIB_POSIXCHECK
312 /* strcasestr() does not work with multibyte strings:
313 It is a glibc extension, and glibc implements it only for unibyte
316 # define strcasestr(a,b) \
317 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
318 "in multibyte locales - " \
319 "use mbscasestr if you care about " \
320 "internationalization, or use c-strcasestr if you want " \
321 "a locale independent function"), \
325 /* Parse S into tokens separated by characters in DELIM.
326 If S is NULL, the saved pointer in SAVE_PTR is used as
327 the next starting point. For example:
328 char s[] = "-abc-=-def";
330 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
331 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
332 x = strtok_r(NULL, "=", &sp); // x = NULL
335 This is a variant of strtok() that is multithread-safe.
337 For the POSIX documentation for this function, see:
338 http://www.opengroup.org/susv3xsh/strtok.html
340 Caveat: It modifies the original string.
341 Caveat: These functions cannot be used on constant strings.
342 Caveat: The identity of the delimiting character is lost.
343 Caveat: It doesn't work with multibyte strings unless all of the delimiter
344 characters are ASCII characters < 0x30.
346 See also strsep(). */
347 #if @GNULIB_STRTOK_R@
348 # if ! @HAVE_DECL_STRTOK_R@
349 extern char *strtok_r (char *restrict s, char const *restrict delim,
350 char **restrict save_ptr);
352 # if defined GNULIB_POSIXCHECK
354 # define strtok_r(s,d,p) \
355 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
356 "in multibyte locales - " \
357 "use mbstok_r if you care about internationalization"), \
360 #elif defined GNULIB_POSIXCHECK
362 # define strtok_r(s,d,p) \
363 (GL_LINK_WARNING ("strtok_r is unportable - " \
364 "use gnulib module strtok_r for portability"), \
369 /* The following functions are not specified by POSIX. They are gnulib
373 /* Return the number of multibyte characters in the character string STRING.
374 This considers multibyte characters, unlike strlen, which counts bytes. */
375 extern size_t mbslen (const char *string);
379 /* Return the number of multibyte characters in the character string starting
380 at STRING and ending at STRING + LEN. */
381 extern size_t mbsnlen (const char *string, size_t len);
385 /* Locate the first single-byte character C in the character string STRING,
386 and return a pointer to it. Return NULL if C is not found in STRING.
387 Unlike strchr(), this function works correctly in multibyte locales with
388 encodings such as GB18030. */
389 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
390 extern char * mbschr (const char *string, int c);
394 /* Locate the last single-byte character C in the character string STRING,
395 and return a pointer to it. Return NULL if C is not found in STRING.
396 Unlike strrchr(), this function works correctly in multibyte locales with
397 encodings such as GB18030. */
398 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
399 extern char * mbsrchr (const char *string, int c);
403 /* Find the first occurrence of the character string NEEDLE in the character
404 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
405 Unlike strstr(), this function works correctly in multibyte locales with
406 encodings different from UTF-8. */
407 extern char * mbsstr (const char *haystack, const char *needle);
410 #if @GNULIB_MBSCASECMP@
411 /* Compare the character strings S1 and S2, ignoring case, returning less than,
412 equal to or greater than zero if S1 is lexicographically less than, equal to
414 Note: This function may, in multibyte locales, return 0 for strings of
416 Unlike strcasecmp(), this function works correctly in multibyte locales. */
417 extern int mbscasecmp (const char *s1, const char *s2);
420 #if @GNULIB_MBSNCASECMP@
421 /* Compare the initial segment of the character string S1 consisting of at most
422 N characters with the initial segment of the character string S2 consisting
423 of at most N characters, ignoring case, returning less than, equal to or
424 greater than zero if the initial segment of S1 is lexicographically less
425 than, equal to or greater than the initial segment of S2.
426 Note: This function may, in multibyte locales, return 0 for initial segments
427 of different lengths!
428 Unlike strncasecmp(), this function works correctly in multibyte locales.
429 But beware that N is not a byte count but a character count! */
430 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
433 #if @GNULIB_MBSPCASECMP@
434 /* Compare the initial segment of the character string STRING consisting of
435 at most mbslen (PREFIX) characters with the character string PREFIX,
436 ignoring case, returning less than, equal to or greater than zero if this
437 initial segment is lexicographically less than, equal to or greater than
439 Note: This function may, in multibyte locales, return 0 if STRING is of
440 smaller length than PREFIX!
441 Unlike strncasecmp(), this function works correctly in multibyte
443 extern char * mbspcasecmp (const char *string, const char *prefix);
446 #if @GNULIB_MBSCASESTR@
447 /* Find the first occurrence of the character string NEEDLE in the character
448 string HAYSTACK, using case-insensitive comparison.
449 Note: This function may, in multibyte locales, return success even if
450 strlen (haystack) < strlen (needle) !
451 Unlike strcasestr(), this function works correctly in multibyte locales. */
452 extern char * mbscasestr (const char *haystack, const char *needle);
456 /* Find the first occurrence in the character string STRING of any character
457 in the character string ACCEPT. Return the number of bytes from the
458 beginning of the string to this occurrence, or to the end of the string
460 Unlike strcspn(), this function works correctly in multibyte locales. */
461 extern size_t mbscspn (const char *string, const char *accept);
465 /* Find the first occurrence in the character string STRING of any character
466 in the character string ACCEPT. Return the pointer to it, or NULL if none
468 Unlike strpbrk(), this function works correctly in multibyte locales. */
469 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
470 extern char * mbspbrk (const char *string, const char *accept);
474 /* Find the first occurrence in the character string STRING of any character
475 not in the character string REJECT. Return the number of bytes from the
476 beginning of the string to this occurrence, or to the end of the string
478 Unlike strspn(), this function works correctly in multibyte locales. */
479 extern size_t mbsspn (const char *string, const char *reject);
483 /* Search the next delimiter (multibyte character listed in the character
484 string DELIM) starting at the character string *STRINGP.
485 If one is found, overwrite it with a NUL, and advance *STRINGP to point
486 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
487 If *STRINGP was already NULL, nothing happens.
488 Return the old value of *STRINGP.
490 This is a variant of mbstok_r() that supports empty fields.
492 Caveat: It modifies the original string.
493 Caveat: These functions cannot be used on constant strings.
494 Caveat: The identity of the delimiting character is lost.
496 See also mbstok_r(). */
497 extern char * mbssep (char **stringp, const char *delim);
500 #if @GNULIB_MBSTOK_R@
501 /* Parse the character string STRING into tokens separated by characters in
502 the character string DELIM.
503 If STRING is NULL, the saved pointer in SAVE_PTR is used as
504 the next starting point. For example:
505 char s[] = "-abc-=-def";
507 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
508 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
509 x = mbstok_r(NULL, "=", &sp); // x = NULL
512 Caveat: It modifies the original string.
513 Caveat: These functions cannot be used on constant strings.
514 Caveat: The identity of the delimiting character is lost.
516 See also mbssep(). */
517 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
520 /* Map any int, typically from errno, into an error message. */
521 #if @GNULIB_STRERROR@
522 # if @REPLACE_STRERROR@
524 # define strerror rpl_strerror
525 extern char *strerror (int);
527 #elif defined GNULIB_POSIXCHECK
529 # define strerror(e) \
530 (GL_LINK_WARNING ("strerror is unportable - " \
531 "use gnulib module strerror to guarantee non-NULL result"), \
540 #endif /* _GL_STRING_H */
541 #endif /* _GL_STRING_H */