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 and often quadratic - " \
62 "use gnulib module memmem-simple for portability, " \
63 "and module memmem for speed" ), \
64 memmem (a, al, b, bl))
67 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
71 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
74 #elif defined GNULIB_POSIXCHECK
76 # define mempcpy(a,b,n) \
77 (GL_LINK_WARNING ("mempcpy is unportable - " \
78 "use gnulib module mempcpy for portability"), \
82 /* Search backwards through a block for a byte (specified as an int). */
84 # if ! @HAVE_DECL_MEMRCHR@
85 extern void *memrchr (void const *, int, size_t)
86 __attribute__ ((__pure__));
88 #elif defined GNULIB_POSIXCHECK
90 # define memrchr(a,b,c) \
91 (GL_LINK_WARNING ("memrchr is unportable - " \
92 "use gnulib module memrchr for portability"), \
96 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
99 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
101 #elif defined GNULIB_POSIXCHECK
103 # define stpcpy(a,b) \
104 (GL_LINK_WARNING ("stpcpy is unportable - " \
105 "use gnulib module stpcpy for portability"), \
109 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
110 last non-NUL byte written into DST. */
112 # if ! @HAVE_STPNCPY@
113 # define stpncpy gnu_stpncpy
114 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
117 #elif defined GNULIB_POSIXCHECK
119 # define stpncpy(a,b,n) \
120 (GL_LINK_WARNING ("stpncpy is unportable - " \
121 "use gnulib module stpncpy for portability"), \
125 #if defined GNULIB_POSIXCHECK
126 /* strchr() does not work with multibyte strings if the locale encoding is
127 GB18030 and the character to be searched is a digit. */
129 # define strchr(s,c) \
130 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
131 "in some multibyte locales - " \
132 "use mbschr if you care about internationalization"), \
136 /* Find the first occurrence of C in S or the final NUL byte. */
137 #if @GNULIB_STRCHRNUL@
138 # if ! @HAVE_STRCHRNUL@
139 extern char *strchrnul (char const *__s, int __c_in)
140 __attribute__ ((__pure__));
142 #elif defined GNULIB_POSIXCHECK
144 # define strchrnul(a,b) \
145 (GL_LINK_WARNING ("strchrnul is unportable - " \
146 "use gnulib module strchrnul for portability"), \
150 /* Duplicate S, returning an identical malloc'd string. */
152 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
153 extern char *strdup (char const *__s);
155 #elif defined GNULIB_POSIXCHECK
158 (GL_LINK_WARNING ("strdup is unportable - " \
159 "use gnulib module strdup for portability"), \
163 /* Return a newly allocated copy of at most N bytes of STRING. */
165 # if ! @HAVE_STRNDUP@
167 # define strndup rpl_strndup
169 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
170 extern char *strndup (char const *__string, size_t __n);
172 #elif defined GNULIB_POSIXCHECK
174 # define strndup(a,n) \
175 (GL_LINK_WARNING ("strndup is unportable - " \
176 "use gnulib module strndup for portability"), \
180 /* Find the length (number of bytes) of STRING, but scan at most
181 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
184 # if ! @HAVE_DECL_STRNLEN@
185 extern size_t strnlen (char const *__string, size_t __maxlen)
186 __attribute__ ((__pure__));
188 #elif defined GNULIB_POSIXCHECK
190 # define strnlen(a,n) \
191 (GL_LINK_WARNING ("strnlen is unportable - " \
192 "use gnulib module strnlen for portability"), \
196 #if defined GNULIB_POSIXCHECK
197 /* strcspn() assumes the second argument is a list of single-byte characters.
198 Even in this simple case, it does not work with multibyte strings if the
199 locale encoding is GB18030 and one of the characters to be searched is a
202 # define strcspn(s,a) \
203 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
204 "in multibyte locales - " \
205 "use mbscspn if you care about internationalization"), \
209 /* Find the first occurrence in S of any character in ACCEPT. */
211 # if ! @HAVE_STRPBRK@
212 extern char *strpbrk (char const *__s, char const *__accept)
213 __attribute__ ((__pure__));
215 # if defined GNULIB_POSIXCHECK
216 /* strpbrk() assumes the second argument is a list of single-byte characters.
217 Even in this simple case, it does not work with multibyte strings if the
218 locale encoding is GB18030 and one of the characters to be searched is a
221 # define strpbrk(s,a) \
222 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
223 "in multibyte locales - " \
224 "use mbspbrk if you care about internationalization"), \
227 #elif defined GNULIB_POSIXCHECK
229 # define strpbrk(s,a) \
230 (GL_LINK_WARNING ("strpbrk is unportable - " \
231 "use gnulib module strpbrk for portability"), \
235 #if defined GNULIB_POSIXCHECK
236 /* strspn() assumes the second argument is a list of single-byte characters.
237 Even in this simple case, it cannot work with multibyte strings. */
239 # define strspn(s,a) \
240 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
241 "in multibyte locales - " \
242 "use mbsspn if you care about internationalization"), \
246 #if defined GNULIB_POSIXCHECK
247 /* strrchr() does not work with multibyte strings if the locale encoding is
248 GB18030 and the character to be searched is a digit. */
250 # define strrchr(s,c) \
251 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
252 "in some multibyte locales - " \
253 "use mbsrchr if you care about internationalization"), \
257 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
258 If one is found, overwrite it with a NUL, and advance *STRINGP
259 to point to the next char after it. Otherwise, set *STRINGP to NULL.
260 If *STRINGP was already NULL, nothing happens.
261 Return the old value of *STRINGP.
263 This is a variant of strtok() that is multithread-safe and supports
266 Caveat: It modifies the original string.
267 Caveat: These functions cannot be used on constant strings.
268 Caveat: The identity of the delimiting character is lost.
269 Caveat: It doesn't work with multibyte strings unless all of the delimiter
270 characters are ASCII characters < 0x30.
272 See also strtok_r(). */
275 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
277 # if defined GNULIB_POSIXCHECK
279 # define strsep(s,d) \
280 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
281 "in multibyte locales - " \
282 "use mbssep if you care about internationalization"), \
285 #elif defined GNULIB_POSIXCHECK
287 # define strsep(s,d) \
288 (GL_LINK_WARNING ("strsep is unportable - " \
289 "use gnulib module strsep for portability"), \
294 # if @REPLACE_STRSTR@
295 # define strstr rpl_strstr
296 char *strstr (const char *haystack, const char *needle)
297 __attribute__ ((__pure__));
299 #elif defined GNULIB_POSIXCHECK
300 /* strstr() does not work with multibyte strings if the locale encoding is
301 different from UTF-8:
302 POSIX says that it operates on "strings", and "string" in POSIX is defined
303 as a sequence of bytes, not of characters. */
305 # define strstr(a,b) \
306 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
307 "work correctly on character strings in most " \
308 "multibyte locales - " \
309 "use mbsstr if you care about internationalization, " \
310 "or use strstr if you care about speed"), \
314 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
316 #if ! @HAVE_STRCASESTR@
317 extern char *strcasestr (const char *haystack, const char *needle)
318 __attribute__ ((__pure__));
320 #if defined GNULIB_POSIXCHECK
321 /* strcasestr() does not work with multibyte strings:
322 It is a glibc extension, and glibc implements it only for unibyte
325 # define strcasestr(a,b) \
326 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
327 "in multibyte locales - " \
328 "use mbscasestr if you care about " \
329 "internationalization, or use c-strcasestr if you want " \
330 "a locale independent function"), \
334 /* Parse S into tokens separated by characters in DELIM.
335 If S is NULL, the saved pointer in SAVE_PTR is used as
336 the next starting point. For example:
337 char s[] = "-abc-=-def";
339 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
340 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
341 x = strtok_r(NULL, "=", &sp); // x = NULL
344 This is a variant of strtok() that is multithread-safe.
346 For the POSIX documentation for this function, see:
347 http://www.opengroup.org/susv3xsh/strtok.html
349 Caveat: It modifies the original string.
350 Caveat: These functions cannot be used on constant strings.
351 Caveat: The identity of the delimiting character is lost.
352 Caveat: It doesn't work with multibyte strings unless all of the delimiter
353 characters are ASCII characters < 0x30.
355 See also strsep(). */
356 #if @GNULIB_STRTOK_R@
357 # if ! @HAVE_DECL_STRTOK_R@
358 extern char *strtok_r (char *restrict s, char const *restrict delim,
359 char **restrict save_ptr);
361 # if defined GNULIB_POSIXCHECK
363 # define strtok_r(s,d,p) \
364 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
365 "in multibyte locales - " \
366 "use mbstok_r if you care about internationalization"), \
369 #elif defined GNULIB_POSIXCHECK
371 # define strtok_r(s,d,p) \
372 (GL_LINK_WARNING ("strtok_r is unportable - " \
373 "use gnulib module strtok_r for portability"), \
378 /* The following functions are not specified by POSIX. They are gnulib
382 /* Return the number of multibyte characters in the character string STRING.
383 This considers multibyte characters, unlike strlen, which counts bytes. */
384 extern size_t mbslen (const char *string);
388 /* Return the number of multibyte characters in the character string starting
389 at STRING and ending at STRING + LEN. */
390 extern size_t mbsnlen (const char *string, size_t len);
394 /* Locate the first 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 strchr(), this function works correctly in multibyte locales with
397 encodings such as GB18030. */
398 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
399 extern char * mbschr (const char *string, int c);
403 /* Locate the last single-byte character C in the character string STRING,
404 and return a pointer to it. Return NULL if C is not found in STRING.
405 Unlike strrchr(), this function works correctly in multibyte locales with
406 encodings such as GB18030. */
407 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
408 extern char * mbsrchr (const char *string, int c);
412 /* Find the first occurrence of the character string NEEDLE in the character
413 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
414 Unlike strstr(), this function works correctly in multibyte locales with
415 encodings different from UTF-8. */
416 extern char * mbsstr (const char *haystack, const char *needle);
419 #if @GNULIB_MBSCASECMP@
420 /* Compare the character strings S1 and S2, ignoring case, returning less than,
421 equal to or greater than zero if S1 is lexicographically less than, equal to
423 Note: This function may, in multibyte locales, return 0 for strings of
425 Unlike strcasecmp(), this function works correctly in multibyte locales. */
426 extern int mbscasecmp (const char *s1, const char *s2);
429 #if @GNULIB_MBSNCASECMP@
430 /* Compare the initial segment of the character string S1 consisting of at most
431 N characters with the initial segment of the character string S2 consisting
432 of at most N characters, ignoring case, returning less than, equal to or
433 greater than zero if the initial segment of S1 is lexicographically less
434 than, equal to or greater than the initial segment of S2.
435 Note: This function may, in multibyte locales, return 0 for initial segments
436 of different lengths!
437 Unlike strncasecmp(), this function works correctly in multibyte locales.
438 But beware that N is not a byte count but a character count! */
439 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
442 #if @GNULIB_MBSPCASECMP@
443 /* Compare the initial segment of the character string STRING consisting of
444 at most mbslen (PREFIX) characters with the character string PREFIX,
445 ignoring case, returning less than, equal to or greater than zero if this
446 initial segment is lexicographically less than, equal to or greater than
448 Note: This function may, in multibyte locales, return 0 if STRING is of
449 smaller length than PREFIX!
450 Unlike strncasecmp(), this function works correctly in multibyte
452 extern char * mbspcasecmp (const char *string, const char *prefix);
455 #if @GNULIB_MBSCASESTR@
456 /* Find the first occurrence of the character string NEEDLE in the character
457 string HAYSTACK, using case-insensitive comparison.
458 Note: This function may, in multibyte locales, return success even if
459 strlen (haystack) < strlen (needle) !
460 Unlike strcasestr(), this function works correctly in multibyte locales. */
461 extern char * mbscasestr (const char *haystack, const char *needle);
465 /* Find the first occurrence in the character string STRING of any character
466 in the character string ACCEPT. Return the number of bytes from the
467 beginning of the string to this occurrence, or to the end of the string
469 Unlike strcspn(), this function works correctly in multibyte locales. */
470 extern size_t mbscspn (const char *string, const char *accept);
474 /* Find the first occurrence in the character string STRING of any character
475 in the character string ACCEPT. Return the pointer to it, or NULL if none
477 Unlike strpbrk(), this function works correctly in multibyte locales. */
478 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
479 extern char * mbspbrk (const char *string, const char *accept);
483 /* Find the first occurrence in the character string STRING of any character
484 not in the character string REJECT. Return the number of bytes from the
485 beginning of the string to this occurrence, or to the end of the string
487 Unlike strspn(), this function works correctly in multibyte locales. */
488 extern size_t mbsspn (const char *string, const char *reject);
492 /* Search the next delimiter (multibyte character listed in the character
493 string DELIM) starting at the character string *STRINGP.
494 If one is found, overwrite it with a NUL, and advance *STRINGP to point
495 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
496 If *STRINGP was already NULL, nothing happens.
497 Return the old value of *STRINGP.
499 This is a variant of mbstok_r() that supports empty fields.
501 Caveat: It modifies the original string.
502 Caveat: These functions cannot be used on constant strings.
503 Caveat: The identity of the delimiting character is lost.
505 See also mbstok_r(). */
506 extern char * mbssep (char **stringp, const char *delim);
509 #if @GNULIB_MBSTOK_R@
510 /* Parse the character string STRING into tokens separated by characters in
511 the character string DELIM.
512 If STRING is NULL, the saved pointer in SAVE_PTR is used as
513 the next starting point. For example:
514 char s[] = "-abc-=-def";
516 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
517 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
518 x = mbstok_r(NULL, "=", &sp); // x = NULL
521 Caveat: It modifies the original string.
522 Caveat: These functions cannot be used on constant strings.
523 Caveat: The identity of the delimiting character is lost.
525 See also mbssep(). */
526 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
529 /* Map any int, typically from errno, into an error message. */
530 #if @GNULIB_STRERROR@
531 # if @REPLACE_STRERROR@
533 # define strerror rpl_strerror
534 extern char *strerror (int);
536 #elif defined GNULIB_POSIXCHECK
538 # define strerror(e) \
539 (GL_LINK_WARNING ("strerror is unportable - " \
540 "use gnulib module strerror to guarantee non-NULL result"), \
549 #endif /* _GL_STRING_H */
550 #endif /* _GL_STRING_H */