Move declarations of str{,n}casecmp from <string.h> to <strings.h>.
[gnulib.git] / lib / string.in.h
1 /* A GNU-like <string.h>.
2
3    Copyright (C) 1995-1996, 2001-2007 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 2, 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 /* The definition of GL_LINK_WARNING is copied here.  */
29
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35
36 /* Return the first occurrence of NEEDLE in HAYSTACK.  */
37 #if @GNULIB_MEMMEM@
38 # if ! @HAVE_DECL_MEMMEM@
39 extern void *memmem (void const *__haystack, size_t __haystack_len,
40                      void const *__needle, size_t __needle_len);
41 # endif
42 #elif defined GNULIB_POSIXCHECK
43 # undef memmem
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))
48 #endif
49
50 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
51    last written byte.  */
52 #if @GNULIB_MEMPCPY@
53 # if ! @HAVE_MEMPCPY@
54 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
55                       size_t __n);
56 # endif
57 #elif defined GNULIB_POSIXCHECK
58 # undef mempcpy
59 # define mempcpy(a,b,n) \
60     (GL_LINK_WARNING ("mempcpy is unportable - " \
61                       "use gnulib module mempcpy for portability"), \
62      mempcpy (a, b, n))
63 #endif
64
65 /* Search backwards through a block for a byte (specified as an int).  */
66 #if @GNULIB_MEMRCHR@
67 # if ! @HAVE_DECL_MEMRCHR@
68 extern void *memrchr (void const *, int, size_t);
69 # endif
70 #elif defined GNULIB_POSIXCHECK
71 # undef memrchr
72 # define memrchr(a,b,c) \
73     (GL_LINK_WARNING ("memrchr is unportable - " \
74                       "use gnulib module memrchr for portability"), \
75      memrchr (a, b, c))
76 #endif
77
78 /* Copy SRC to DST, returning the address of the terminating '\0' in DST.  */
79 #if @GNULIB_STPCPY@
80 # if ! @HAVE_STPCPY@
81 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
82 # endif
83 #elif defined GNULIB_POSIXCHECK
84 # undef stpcpy
85 # define stpcpy(a,b) \
86     (GL_LINK_WARNING ("stpcpy is unportable - " \
87                       "use gnulib module stpcpy for portability"), \
88      stpcpy (a, b))
89 #endif
90
91 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
92    last non-NUL byte written into DST.  */
93 #if @GNULIB_STPNCPY@
94 # if ! @HAVE_STPNCPY@
95 #  define stpncpy gnu_stpncpy
96 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
97                       size_t __n);
98 # endif
99 #elif defined GNULIB_POSIXCHECK
100 # undef stpncpy
101 # define stpncpy(a,b,n) \
102     (GL_LINK_WARNING ("stpncpy is unportable - " \
103                       "use gnulib module stpncpy for portability"), \
104      stpncpy (a, b, n))
105 #endif
106
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.  */
110 # undef strchr
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"), \
115      strchr (s, c))
116 #endif
117
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);
122 # endif
123 #elif defined GNULIB_POSIXCHECK
124 # undef strchrnul
125 # define strchrnul(a,b) \
126     (GL_LINK_WARNING ("strchrnul is unportable - " \
127                       "use gnulib module strchrnul for portability"), \
128      strchrnul (a, b))
129 #endif
130
131 /* Duplicate S, returning an identical malloc'd string.  */
132 #if @GNULIB_STRDUP@
133 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
134 extern char *strdup (char const *__s);
135 # endif
136 #elif defined GNULIB_POSIXCHECK
137 # undef strdup
138 # define strdup(a) \
139     (GL_LINK_WARNING ("strdup is unportable - " \
140                       "use gnulib module strdup for portability"), \
141      strdup (a))
142 #endif
143
144 /* Return a newly allocated copy of at most N bytes of STRING.  */
145 #if @GNULIB_STRNDUP@
146 # if ! @HAVE_STRNDUP@
147 #  undef strndup
148 #  define strndup rpl_strndup
149 # endif
150 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
151 extern char *strndup (char const *__string, size_t __n);
152 # endif
153 #elif defined GNULIB_POSIXCHECK
154 # undef strndup
155 # define strndup(a,n) \
156     (GL_LINK_WARNING ("strndup is unportable - " \
157                       "use gnulib module strndup for portability"), \
158      strndup (a, n))
159 #endif
160
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,
163    return MAXLEN.  */
164 #if @GNULIB_STRNLEN@
165 # if ! @HAVE_DECL_STRNLEN@
166 extern size_t strnlen (char const *__string, size_t __maxlen);
167 # endif
168 #elif defined GNULIB_POSIXCHECK
169 # undef strnlen
170 # define strnlen(a,n) \
171     (GL_LINK_WARNING ("strnlen is unportable - " \
172                       "use gnulib module strnlen for portability"), \
173      strnlen (a, n))
174 #endif
175
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
180    digit.  */
181 # undef strcspn
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"), \
186      strcspn (s, a))
187 #endif
188
189 /* Find the first occurrence in S of any character in ACCEPT.  */
190 #if @GNULIB_STRPBRK@
191 # if ! @HAVE_STRPBRK@
192 extern char *strpbrk (char const *__s, char const *__accept);
193 # endif
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
198    digit.  */
199 #  undef strpbrk
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"), \
204       strpbrk (s, a))
205 # endif
206 #elif defined GNULIB_POSIXCHECK
207 # undef strpbrk
208 # define strpbrk(s,a) \
209     (GL_LINK_WARNING ("strpbrk is unportable - " \
210                       "use gnulib module strpbrk for portability"), \
211      strpbrk (s, a))
212 #endif
213
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.  */
217 # undef strspn
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"), \
222      strspn (s, a))
223 #endif
224
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.  */
228 # undef strrchr
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"), \
233      strrchr (s, c))
234 #endif
235
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.
241
242    This is a variant of strtok() that is multithread-safe and supports
243    empty fields.
244
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.
250
251    See also strtok_r().  */
252 #if @GNULIB_STRSEP@
253 # if ! @HAVE_STRSEP@
254 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
255 # endif
256 # if defined GNULIB_POSIXCHECK
257 #  undef strsep
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"), \
262       strsep (s, d))
263 # endif
264 #elif defined GNULIB_POSIXCHECK
265 # undef strsep
266 # define strsep(s,d) \
267     (GL_LINK_WARNING ("strsep is unportable - " \
268                       "use gnulib module strsep for portability"), \
269      strsep (s, d))
270 #endif
271
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.  */
277 # undef strstr
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"), \
282      strstr (a, b))
283 #endif
284
285 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
286    comparison.  */
287 #if ! @HAVE_STRCASESTR@
288 extern char *strcasestr (const char *haystack, const char *needle);
289 #endif
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
293    locales.  */
294 # undef strcasestr
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"), \
301      strcasestr (a, b))
302 #endif
303
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";
308         char *sp;
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
312                 // s = "abc\0-def\0"
313
314    This is a variant of strtok() that is multithread-safe.
315
316    For the POSIX documentation for this function, see:
317    http://www.opengroup.org/susv3xsh/strtok.html
318
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.
324
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);
330 # endif
331 # if defined GNULIB_POSIXCHECK
332 #  undef strtok_r
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"), \
337       strtok_r (s, d, p))
338 # endif
339 #elif defined GNULIB_POSIXCHECK
340 # undef strtok_r
341 # define strtok_r(s,d,p) \
342     (GL_LINK_WARNING ("strtok_r is unportable - " \
343                       "use gnulib module strtok_r for portability"), \
344      strtok_r (s, d, p))
345 #endif
346
347
348 /* The following functions are not specified by POSIX.  They are gnulib
349    extensions.  */
350
351 #if @GNULIB_MBSLEN@
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);
355 #endif
356
357 #if @GNULIB_MBSNLEN@
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);
361 #endif
362
363 #if @GNULIB_MBSCHR@
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);
370 #endif
371
372 #if @GNULIB_MBSRCHR@
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);
379 #endif
380
381 #if @GNULIB_MBSSTR@
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);
387 #endif
388
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
392    or greater than S2.
393    Note: This function may, in multibyte locales, return 0 for strings of
394    different lengths!
395    Unlike strcasecmp(), this function works correctly in multibyte locales.  */
396 extern int mbscasecmp (const char *s1, const char *s2);
397 #endif
398
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);
410 #endif
411
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
417    PREFIX.
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
421    locales.  */
422 extern char * mbspcasecmp (const char *string, const char *prefix);
423 #endif
424
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);
432 #endif
433
434 #if @GNULIB_MBSCSPN@
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
438    if none exists.
439    Unlike strcspn(), this function works correctly in multibyte locales.  */
440 extern size_t mbscspn (const char *string, const char *accept);
441 #endif
442
443 #if @GNULIB_MBSPBRK@
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
446    exists.
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);
450 #endif
451
452 #if @GNULIB_MBSSPN@
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
456    if none exists.
457    Unlike strspn(), this function works correctly in multibyte locales.  */
458 extern size_t mbsspn (const char *string, const char *reject);
459 #endif
460
461 #if @GNULIB_MBSSEP@
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.
468
469    This is a variant of mbstok_r() that supports empty fields.
470
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.
474
475    See also mbstok_r().  */
476 extern char * mbssep (char **stringp, const char *delim);
477 #endif
478
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";
485         char *sp;
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
489                 // s = "abc\0-def\0"
490
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.
494
495    See also mbssep().  */
496 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
497 #endif
498
499 /* Map any int, typically from errno, into an error message.  */
500 #if @GNULIB_STRERROR@
501 # if @REPLACE_STRERROR@
502 #  undef strerror
503 #  define strerror rpl_strerror
504 extern char *strerror (int);
505 # endif
506 #elif defined GNULIB_POSIXCHECK
507 # undef strerror
508 # define strerror(e) \
509     (GL_LINK_WARNING ("strerror is unportable - " \
510                       "use gnulib module strerror to guarantee non-NULL result"), \
511      strerror (e))
512 #endif
513
514
515 #ifdef __cplusplus
516 }
517 #endif
518
519 #endif /* _GL_STRING_H */
520 #endif /* _GL_STRING_H */