]> sjero.net Git - wget/blob - lib/string.in.h
Merging heads.
[wget] / lib / string.in.h
1 /* A GNU-like <string.h>.
2
3    Copyright (C) 1995-1996, 2001-2008 Free Software Foundation, Inc.
4
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 3, or (at your option)
8    any later version.
9
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.
14
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.  */
18
19 #ifndef _GL_STRING_H
20
21 /* The include_next requires a split double-inclusion guard.  */
22 #@INCLUDE_NEXT@ @NEXT_STRING_H@
23
24 #ifndef _GL_STRING_H
25 #define _GL_STRING_H
26
27
28 #ifndef __attribute__
29 /* This feature is available in gcc versions 2.5 and later.  */
30 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
31 #  define __attribute__(Spec) /* empty */
32 # endif
33 /* The attribute __pure__ was added in gcc 2.96.  */
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
35 #  define __pure__ /* empty */
36 # endif
37 #endif
38
39
40 /* The definition of GL_LINK_WARNING is copied here.  */
41
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47
48 /* Return the first occurrence of NEEDLE in HAYSTACK.  */
49 #if @GNULIB_MEMMEM@
50 # if @REPLACE_MEMMEM@
51 #  define memmem rpl_memmem
52 # endif
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__));
57 # endif
58 #elif defined GNULIB_POSIXCHECK
59 # undef memmem
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))
65 #endif
66
67 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
68    last written byte.  */
69 #if @GNULIB_MEMPCPY@
70 # if ! @HAVE_MEMPCPY@
71 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
72                       size_t __n);
73 # endif
74 #elif defined GNULIB_POSIXCHECK
75 # undef mempcpy
76 # define mempcpy(a,b,n) \
77     (GL_LINK_WARNING ("mempcpy is unportable - " \
78                       "use gnulib module mempcpy for portability"), \
79      mempcpy (a, b, n))
80 #endif
81
82 /* Search backwards through a block for a byte (specified as an int).  */
83 #if @GNULIB_MEMRCHR@
84 # if ! @HAVE_DECL_MEMRCHR@
85 extern void *memrchr (void const *, int, size_t)
86   __attribute__ ((__pure__));
87 # endif
88 #elif defined GNULIB_POSIXCHECK
89 # undef memrchr
90 # define memrchr(a,b,c) \
91     (GL_LINK_WARNING ("memrchr is unportable - " \
92                       "use gnulib module memrchr for portability"), \
93      memrchr (a, b, c))
94 #endif
95
96 /* Find the first occurrence of C in S.  More efficient than
97    memchr(S,C,N), at the expense of undefined behavior if C does not
98    occur within N bytes.  */
99 #if @GNULIB_RAWMEMCHR@
100 # if ! @HAVE_RAWMEMCHR@
101 extern void *rawmemchr (void const *__s, int __c_in)
102   __attribute__ ((__pure__));
103 # endif
104 #elif defined GNULIB_POSIXCHECK
105 # undef rawmemchr
106 # define rawmemchr(a,b) \
107     (GL_LINK_WARNING ("rawmemchr is unportable - " \
108                       "use gnulib module rawmemchr for portability"), \
109      rawmemchr (a, b))
110 #endif
111
112 /* Copy SRC to DST, returning the address of the terminating '\0' in DST.  */
113 #if @GNULIB_STPCPY@
114 # if ! @HAVE_STPCPY@
115 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
116 # endif
117 #elif defined GNULIB_POSIXCHECK
118 # undef stpcpy
119 # define stpcpy(a,b) \
120     (GL_LINK_WARNING ("stpcpy is unportable - " \
121                       "use gnulib module stpcpy for portability"), \
122      stpcpy (a, b))
123 #endif
124
125 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
126    last non-NUL byte written into DST.  */
127 #if @GNULIB_STPNCPY@
128 # if ! @HAVE_STPNCPY@
129 #  define stpncpy gnu_stpncpy
130 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
131                       size_t __n);
132 # endif
133 #elif defined GNULIB_POSIXCHECK
134 # undef stpncpy
135 # define stpncpy(a,b,n) \
136     (GL_LINK_WARNING ("stpncpy is unportable - " \
137                       "use gnulib module stpncpy for portability"), \
138      stpncpy (a, b, n))
139 #endif
140
141 #if defined GNULIB_POSIXCHECK
142 /* strchr() does not work with multibyte strings if the locale encoding is
143    GB18030 and the character to be searched is a digit.  */
144 # undef strchr
145 # define strchr(s,c) \
146     (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
147                       "in some multibyte locales - " \
148                       "use mbschr if you care about internationalization"), \
149      strchr (s, c))
150 #endif
151
152 /* Find the first occurrence of C in S or the final NUL byte.  */
153 #if @GNULIB_STRCHRNUL@
154 # if ! @HAVE_STRCHRNUL@
155 extern char *strchrnul (char const *__s, int __c_in)
156   __attribute__ ((__pure__));
157 # endif
158 #elif defined GNULIB_POSIXCHECK
159 # undef strchrnul
160 # define strchrnul(a,b) \
161     (GL_LINK_WARNING ("strchrnul is unportable - " \
162                       "use gnulib module strchrnul for portability"), \
163      strchrnul (a, b))
164 #endif
165
166 /* Duplicate S, returning an identical malloc'd string.  */
167 #if @GNULIB_STRDUP@
168 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
169 extern char *strdup (char const *__s);
170 # endif
171 #elif defined GNULIB_POSIXCHECK
172 # undef strdup
173 # define strdup(a) \
174     (GL_LINK_WARNING ("strdup is unportable - " \
175                       "use gnulib module strdup for portability"), \
176      strdup (a))
177 #endif
178
179 /* Return a newly allocated copy of at most N bytes of STRING.  */
180 #if @GNULIB_STRNDUP@
181 # if ! @HAVE_STRNDUP@
182 #  undef strndup
183 #  define strndup rpl_strndup
184 # endif
185 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
186 extern char *strndup (char const *__string, size_t __n);
187 # endif
188 #elif defined GNULIB_POSIXCHECK
189 # undef strndup
190 # define strndup(a,n) \
191     (GL_LINK_WARNING ("strndup is unportable - " \
192                       "use gnulib module strndup for portability"), \
193      strndup (a, n))
194 #endif
195
196 /* Find the length (number of bytes) of STRING, but scan at most
197    MAXLEN bytes.  If no '\0' terminator is found in that many bytes,
198    return MAXLEN.  */
199 #if @GNULIB_STRNLEN@
200 # if ! @HAVE_DECL_STRNLEN@
201 extern size_t strnlen (char const *__string, size_t __maxlen)
202   __attribute__ ((__pure__));
203 # endif
204 #elif defined GNULIB_POSIXCHECK
205 # undef strnlen
206 # define strnlen(a,n) \
207     (GL_LINK_WARNING ("strnlen is unportable - " \
208                       "use gnulib module strnlen for portability"), \
209      strnlen (a, n))
210 #endif
211
212 #if defined GNULIB_POSIXCHECK
213 /* strcspn() assumes the second argument is a list of single-byte characters.
214    Even in this simple case, it does not work with multibyte strings if the
215    locale encoding is GB18030 and one of the characters to be searched is a
216    digit.  */
217 # undef strcspn
218 # define strcspn(s,a) \
219     (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
220                       "in multibyte locales - " \
221                       "use mbscspn if you care about internationalization"), \
222      strcspn (s, a))
223 #endif
224
225 /* Find the first occurrence in S of any character in ACCEPT.  */
226 #if @GNULIB_STRPBRK@
227 # if ! @HAVE_STRPBRK@
228 extern char *strpbrk (char const *__s, char const *__accept)
229   __attribute__ ((__pure__));
230 # endif
231 # if defined GNULIB_POSIXCHECK
232 /* strpbrk() assumes the second argument is a list of single-byte characters.
233    Even in this simple case, it does not work with multibyte strings if the
234    locale encoding is GB18030 and one of the characters to be searched is a
235    digit.  */
236 #  undef strpbrk
237 #  define strpbrk(s,a) \
238      (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
239                        "in multibyte locales - " \
240                        "use mbspbrk if you care about internationalization"), \
241       strpbrk (s, a))
242 # endif
243 #elif defined GNULIB_POSIXCHECK
244 # undef strpbrk
245 # define strpbrk(s,a) \
246     (GL_LINK_WARNING ("strpbrk is unportable - " \
247                       "use gnulib module strpbrk for portability"), \
248      strpbrk (s, a))
249 #endif
250
251 #if defined GNULIB_POSIXCHECK
252 /* strspn() assumes the second argument is a list of single-byte characters.
253    Even in this simple case, it cannot work with multibyte strings.  */
254 # undef strspn
255 # define strspn(s,a) \
256     (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
257                       "in multibyte locales - " \
258                       "use mbsspn if you care about internationalization"), \
259      strspn (s, a))
260 #endif
261
262 #if defined GNULIB_POSIXCHECK
263 /* strrchr() does not work with multibyte strings if the locale encoding is
264    GB18030 and the character to be searched is a digit.  */
265 # undef strrchr
266 # define strrchr(s,c) \
267     (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
268                       "in some multibyte locales - " \
269                       "use mbsrchr if you care about internationalization"), \
270      strrchr (s, c))
271 #endif
272
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.
278
279    This is a variant of strtok() that is multithread-safe and supports
280    empty fields.
281
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.
287
288    See also strtok_r().  */
289 #if @GNULIB_STRSEP@
290 # if ! @HAVE_STRSEP@
291 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
292 # endif
293 # if defined GNULIB_POSIXCHECK
294 #  undef strsep
295 #  define strsep(s,d) \
296      (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
297                        "in multibyte locales - " \
298                        "use mbssep if you care about internationalization"), \
299       strsep (s, d))
300 # endif
301 #elif defined GNULIB_POSIXCHECK
302 # undef strsep
303 # define strsep(s,d) \
304     (GL_LINK_WARNING ("strsep is unportable - " \
305                       "use gnulib module strsep for portability"), \
306      strsep (s, d))
307 #endif
308
309 #if @GNULIB_STRSTR@
310 # if @REPLACE_STRSTR@
311 #  define strstr rpl_strstr
312 char *strstr (const char *haystack, const char *needle)
313   __attribute__ ((__pure__));
314 # endif
315 #elif defined GNULIB_POSIXCHECK
316 /* strstr() does not work with multibyte strings if the locale encoding is
317    different from UTF-8:
318    POSIX says that it operates on "strings", and "string" in POSIX is defined
319    as a sequence of bytes, not of characters.  */
320 # undef strstr
321 # define strstr(a,b) \
322     (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
323                       "work correctly on character strings in most "    \
324                       "multibyte locales - " \
325                       "use mbsstr if you care about internationalization, " \
326                       "or use strstr if you care about speed"), \
327      strstr (a, b))
328 #endif
329
330 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
331    comparison.  */
332 #if @GNULIB_STRCASESTR@
333 # if @REPLACE_STRCASESTR@
334 #  define strcasestr rpl_strcasestr
335 # endif
336 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
337 extern char *strcasestr (const char *haystack, const char *needle)
338   __attribute__ ((__pure__));
339 # endif
340 #elif defined GNULIB_POSIXCHECK
341 /* strcasestr() does not work with multibyte strings:
342    It is a glibc extension, and glibc implements it only for unibyte
343    locales.  */
344 # undef strcasestr
345 # define strcasestr(a,b) \
346     (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
347                       "in multibyte locales - " \
348                       "use mbscasestr if you care about " \
349                       "internationalization, or use c-strcasestr if you want " \
350                       "a locale independent function"), \
351      strcasestr (a, b))
352 #endif
353
354 /* Parse S into tokens separated by characters in DELIM.
355    If S is NULL, the saved pointer in SAVE_PTR is used as
356    the next starting point.  For example:
357         char s[] = "-abc-=-def";
358         char *sp;
359         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
360         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
361         x = strtok_r(NULL, "=", &sp);   // x = NULL
362                 // s = "abc\0-def\0"
363
364    This is a variant of strtok() that is multithread-safe.
365
366    For the POSIX documentation for this function, see:
367    http://www.opengroup.org/susv3xsh/strtok.html
368
369    Caveat: It modifies the original string.
370    Caveat: These functions cannot be used on constant strings.
371    Caveat: The identity of the delimiting character is lost.
372    Caveat: It doesn't work with multibyte strings unless all of the delimiter
373            characters are ASCII characters < 0x30.
374
375    See also strsep().  */
376 #if @GNULIB_STRTOK_R@
377 # if ! @HAVE_DECL_STRTOK_R@
378 extern char *strtok_r (char *restrict s, char const *restrict delim,
379                        char **restrict save_ptr);
380 # endif
381 # if defined GNULIB_POSIXCHECK
382 #  undef strtok_r
383 #  define strtok_r(s,d,p) \
384      (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
385                        "in multibyte locales - " \
386                        "use mbstok_r if you care about internationalization"), \
387       strtok_r (s, d, p))
388 # endif
389 #elif defined GNULIB_POSIXCHECK
390 # undef strtok_r
391 # define strtok_r(s,d,p) \
392     (GL_LINK_WARNING ("strtok_r is unportable - " \
393                       "use gnulib module strtok_r for portability"), \
394      strtok_r (s, d, p))
395 #endif
396
397
398 /* The following functions are not specified by POSIX.  They are gnulib
399    extensions.  */
400
401 #if @GNULIB_MBSLEN@
402 /* Return the number of multibyte characters in the character string STRING.
403    This considers multibyte characters, unlike strlen, which counts bytes.  */
404 extern size_t mbslen (const char *string);
405 #endif
406
407 #if @GNULIB_MBSNLEN@
408 /* Return the number of multibyte characters in the character string starting
409    at STRING and ending at STRING + LEN.  */
410 extern size_t mbsnlen (const char *string, size_t len);
411 #endif
412
413 #if @GNULIB_MBSCHR@
414 /* Locate the first single-byte character C in the character string STRING,
415    and return a pointer to it.  Return NULL if C is not found in STRING.
416    Unlike strchr(), this function works correctly in multibyte locales with
417    encodings such as GB18030.  */
418 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
419 extern char * mbschr (const char *string, int c);
420 #endif
421
422 #if @GNULIB_MBSRCHR@
423 /* Locate the last single-byte character C in the character string STRING,
424    and return a pointer to it.  Return NULL if C is not found in STRING.
425    Unlike strrchr(), this function works correctly in multibyte locales with
426    encodings such as GB18030.  */
427 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
428 extern char * mbsrchr (const char *string, int c);
429 #endif
430
431 #if @GNULIB_MBSSTR@
432 /* Find the first occurrence of the character string NEEDLE in the character
433    string HAYSTACK.  Return NULL if NEEDLE is not found in HAYSTACK.
434    Unlike strstr(), this function works correctly in multibyte locales with
435    encodings different from UTF-8.  */
436 extern char * mbsstr (const char *haystack, const char *needle);
437 #endif
438
439 #if @GNULIB_MBSCASECMP@
440 /* Compare the character strings S1 and S2, ignoring case, returning less than,
441    equal to or greater than zero if S1 is lexicographically less than, equal to
442    or greater than S2.
443    Note: This function may, in multibyte locales, return 0 for strings of
444    different lengths!
445    Unlike strcasecmp(), this function works correctly in multibyte locales.  */
446 extern int mbscasecmp (const char *s1, const char *s2);
447 #endif
448
449 #if @GNULIB_MBSNCASECMP@
450 /* Compare the initial segment of the character string S1 consisting of at most
451    N characters with the initial segment of the character string S2 consisting
452    of at most N characters, ignoring case, returning less than, equal to or
453    greater than zero if the initial segment of S1 is lexicographically less
454    than, equal to or greater than the initial segment of S2.
455    Note: This function may, in multibyte locales, return 0 for initial segments
456    of different lengths!
457    Unlike strncasecmp(), this function works correctly in multibyte locales.
458    But beware that N is not a byte count but a character count!  */
459 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
460 #endif
461
462 #if @GNULIB_MBSPCASECMP@
463 /* Compare the initial segment of the character string STRING consisting of
464    at most mbslen (PREFIX) characters with the character string PREFIX,
465    ignoring case, returning less than, equal to or greater than zero if this
466    initial segment is lexicographically less than, equal to or greater than
467    PREFIX.
468    Note: This function may, in multibyte locales, return 0 if STRING is of
469    smaller length than PREFIX!
470    Unlike strncasecmp(), this function works correctly in multibyte
471    locales.  */
472 extern char * mbspcasecmp (const char *string, const char *prefix);
473 #endif
474
475 #if @GNULIB_MBSCASESTR@
476 /* Find the first occurrence of the character string NEEDLE in the character
477    string HAYSTACK, using case-insensitive comparison.
478    Note: This function may, in multibyte locales, return success even if
479    strlen (haystack) < strlen (needle) !
480    Unlike strcasestr(), this function works correctly in multibyte locales.  */
481 extern char * mbscasestr (const char *haystack, const char *needle);
482 #endif
483
484 #if @GNULIB_MBSCSPN@
485 /* Find the first occurrence in the character string STRING of any character
486    in the character string ACCEPT.  Return the number of bytes from the
487    beginning of the string to this occurrence, or to the end of the string
488    if none exists.
489    Unlike strcspn(), this function works correctly in multibyte locales.  */
490 extern size_t mbscspn (const char *string, const char *accept);
491 #endif
492
493 #if @GNULIB_MBSPBRK@
494 /* Find the first occurrence in the character string STRING of any character
495    in the character string ACCEPT.  Return the pointer to it, or NULL if none
496    exists.
497    Unlike strpbrk(), this function works correctly in multibyte locales.  */
498 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
499 extern char * mbspbrk (const char *string, const char *accept);
500 #endif
501
502 #if @GNULIB_MBSSPN@
503 /* Find the first occurrence in the character string STRING of any character
504    not in the character string REJECT.  Return the number of bytes from the
505    beginning of the string to this occurrence, or to the end of the string
506    if none exists.
507    Unlike strspn(), this function works correctly in multibyte locales.  */
508 extern size_t mbsspn (const char *string, const char *reject);
509 #endif
510
511 #if @GNULIB_MBSSEP@
512 /* Search the next delimiter (multibyte character listed in the character
513    string DELIM) starting at the character string *STRINGP.
514    If one is found, overwrite it with a NUL, and advance *STRINGP to point
515    to the next multibyte character after it.  Otherwise, set *STRINGP to NULL.
516    If *STRINGP was already NULL, nothing happens.
517    Return the old value of *STRINGP.
518
519    This is a variant of mbstok_r() that supports empty fields.
520
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.
524
525    See also mbstok_r().  */
526 extern char * mbssep (char **stringp, const char *delim);
527 #endif
528
529 #if @GNULIB_MBSTOK_R@
530 /* Parse the character string STRING into tokens separated by characters in
531    the character string DELIM.
532    If STRING is NULL, the saved pointer in SAVE_PTR is used as
533    the next starting point.  For example:
534         char s[] = "-abc-=-def";
535         char *sp;
536         x = mbstok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
537         x = mbstok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
538         x = mbstok_r(NULL, "=", &sp);   // x = NULL
539                 // s = "abc\0-def\0"
540
541    Caveat: It modifies the original string.
542    Caveat: These functions cannot be used on constant strings.
543    Caveat: The identity of the delimiting character is lost.
544
545    See also mbssep().  */
546 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
547 #endif
548
549 /* Map any int, typically from errno, into an error message.  */
550 #if @GNULIB_STRERROR@
551 # if @REPLACE_STRERROR@
552 #  undef strerror
553 #  define strerror rpl_strerror
554 extern char *strerror (int);
555 # endif
556 #elif defined GNULIB_POSIXCHECK
557 # undef strerror
558 # define strerror(e) \
559     (GL_LINK_WARNING ("strerror is unportable - " \
560                       "use gnulib module strerror to guarantee non-NULL result"), \
561      strerror (e))
562 #endif
563
564 #if @GNULIB_STRSIGNAL@
565 # if @REPLACE_STRSIGNAL@
566 #  define strsignal rpl_strsignal
567 # endif
568 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
569 extern char *strsignal (int __sig);
570 # endif
571 #elif defined GNULIB_POSIXCHECK
572 # undef strsignal
573 # define strsignal(a) \
574     (GL_LINK_WARNING ("strsignal is unportable - " \
575                       "use gnulib module strsignal for portability"), \
576      strsignal (a))
577 #endif
578
579
580 #ifdef __cplusplus
581 }
582 #endif
583
584 #endif /* _GL_STRING_H */
585 #endif /* _GL_STRING_H */