* lib/string_.h (strncasecmp): Fix typo: this macro takes 3
[gnulib.git] / lib / string_.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 #define _GL_STRING_H
21
22 #include @ABSOLUTE_STRING_H@
23
24
25 /* GL_LINK_WARNING("literal string") arranges to emit the literal string as
26    a linker warning on most glibc systems.
27    We use a linker warning rather than a preprocessor warning, because
28    #warning cannot be used inside macros.  */
29 #ifndef GL_LINK_WARNING
30   /* This works on platforms with GNU ld and ELF object format.
31      Testing __GLIBC__ is sufficient for asserting that GNU ld is in use.
32      Testing __ELF__ guarantees the ELF object format.
33      Testing __GNUC__ is necessary for the compound expression syntax.  */
34 # if defined __GLIBC__ && defined __ELF__ && defined __GNUC__
35 #  define GL_LINK_WARNING(message) \
36      GL_LINK_WARNING1 (__FILE__, __LINE__, message)
37 #  define GL_LINK_WARNING1(file, line, message) \
38      GL_LINK_WARNING2 (file, line, message)  /* macroexpand file and line */
39 #  define GL_LINK_WARNING2(file, line, message) \
40      GL_LINK_WARNING3 (file ":" #line ": warning: " message)
41 #  define GL_LINK_WARNING3(message) \
42      ({ static const char warning[sizeof (message)]             \
43           __attribute__ ((__unused__,                           \
44                           __section__ (".gnu.warning"),         \
45                           __aligned__ (1)))                     \
46           = message "\n";                                       \
47         (void)0;                                                \
48      })
49 # else
50 #  define GL_LINK_WARNING(message) ((void) 0)
51 # endif
52 #endif
53
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 /* Return the first occurrence of NEEDLE in HAYSTACK.  */
60 #if @GNULIB_MEMMEM@
61 # if ! @HAVE_DECL_MEMMEM@
62 extern void *memmem (void const *__haystack, size_t __haystack_len,
63                      void const *__needle, size_t __needle_len);
64 # endif
65 #elif defined GNULIB_POSIXCHECK
66 # undef memmem
67 # define memmem memmem_is_unportable__use_gnulib_module_memmem_for_portability
68 #endif
69
70 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
71    last written byte.  */
72 #if @GNULIB_MEMPCPY@
73 # if ! @HAVE_MEMPCPY@
74 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
75                       size_t __n);
76 # endif
77 #elif defined GNULIB_POSIXCHECK
78 # undef mempcpy
79 # define mempcpy mempcpy_is_unportable__use_gnulib_module_mempcpy_for_portability
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 # endif
87 #elif defined GNULIB_POSIXCHECK
88 # undef memrchr
89 # define memrchr memrchr_is_unportable__use_gnulib_module_memrchr_for_portability
90 #endif
91
92 /* Copy SRC to DST, returning the address of the terminating '\0' in DST.  */
93 #if @GNULIB_STPCPY@
94 # if ! @HAVE_STPCPY@
95 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
96 # endif
97 #elif defined GNULIB_POSIXCHECK
98 # undef stpcpy
99 # define stpcpy stpcpy_is_unportable__use_gnulib_module_stpcpy_for_portability
100 #endif
101
102 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
103    last non-NUL byte written into DST.  */
104 #if @GNULIB_STPNCPY@
105 # if ! @HAVE_STPNCPY@
106 #  define stpncpy gnu_stpncpy
107 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
108                       size_t __n);
109 # endif
110 #elif defined GNULIB_POSIXCHECK
111 # undef stpncpy
112 # define stpncpy stpncpy_is_unportable__use_gnulib_module_stpncpy_for_portability
113 #endif
114
115 /* Compare strings S1 and S2, ignoring case, returning less than, equal to or
116    greater than zero if S1 is lexicographically less than, equal to or greater
117    than S2.
118    Note: This function does not work in multibyte locales.  */
119 #if ! @HAVE_STRCASECMP@
120 extern int strcasecmp (char const *s1, char const *s2);
121 #endif
122 #if defined GNULIB_POSIXCHECK
123 /* strcasecmp() does not work with multibyte strings:
124    POSIX says that it operates on "strings", and "string" in POSIX is defined
125    as a sequence of bytes, not of characters.   */
126 # undef strcasecmp
127 # define strcasecmp(a,b) \
128     (GL_LINK_WARNING ("strcasecmp cannot work correctly on character strings in multibyte locales - use mbscasecmp if you care about internationalization, or use c_strcasecmp (from gnulib module c-strcase) if you want a locale independent function"), \
129      strcasecmp (a, b))
130 #endif
131
132 /* Compare no more than N bytes of strings S1 and S2, ignoring case,
133    returning less than, equal to or greater than zero if S1 is
134    lexicographically less than, equal to or greater than S2.
135    Note: This function cannot work correctly in multibyte locales.  */
136 #if ! @HAVE_DECL_STRNCASECMP@
137 extern int strncasecmp (char const *s1, char const *s2, size_t n);
138 #endif
139 #if defined GNULIB_POSIXCHECK
140 /* strncasecmp() does not work with multibyte strings:
141    POSIX says that it operates on "strings", and "string" in POSIX is defined
142    as a sequence of bytes, not of characters.  */
143 # undef strncasecmp
144 # define strncasecmp(a,b,n) \
145     (GL_LINK_WARNING ("strncasecmp cannot work correctly on character strings in multibyte locales - don't use it if you care about internationalization; use c_strncasecmp (from gnulib module c-strcase) if you want a locale independent function"), \
146      strncasecmp (a, b, n))
147 #endif
148
149 #if defined GNULIB_POSIXCHECK
150 /* strchr() does not work with multibyte strings if the locale encoding is
151    GB18030 and the character to be searched is a digit.  */
152 # undef strchr
153 # define strchr(s,c) \
154     (GL_LINK_WARNING ("strchr cannot work correctly on character strings in some multibyte locales - use mbschr if you care about internationalization"), \
155      strchr (s, c))
156 #endif
157
158 /* Find the first occurrence of C in S or the final NUL byte.  */
159 #if @GNULIB_STRCHRNUL@
160 # if ! @HAVE_STRCHRNUL@
161 extern char *strchrnul (char const *__s, int __c_in);
162 # endif
163 #elif defined GNULIB_POSIXCHECK
164 # undef strchrnul
165 # define strchrnul strchrnul_is_unportable__use_gnulib_module_strchrnul_for_portability
166 #endif
167
168 /* Duplicate S, returning an identical malloc'd string.  */
169 #if @GNULIB_STRDUP@
170 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
171 extern char *strdup (char const *__s);
172 # endif
173 #elif defined GNULIB_POSIXCHECK
174 # undef strdup
175 # define strdup strdup_is_unportable__use_gnulib_module_strdup_for_portability
176 #endif
177
178 /* Return a newly allocated copy of at most N bytes of STRING.  */
179 #if @GNULIB_STRNDUP@
180 # if ! @HAVE_STRNDUP@
181 #  undef strndup
182 #  define strndup rpl_strndup
183 #  if ! @HAVE_DECL_STRNDUP@
184 extern char *strndup (char const *__string, size_t __n);
185 #  endif
186 # endif
187 #elif defined GNULIB_POSIXCHECK
188 # undef strndup
189 # define strndup strndup_is_unportable__use_gnulib_module_strndup_for_portability
190 #endif
191
192 /* Find the length (number of bytes) of STRING, but scan at most
193    MAXLEN bytes.  If no '\0' terminator is found in that many bytes,
194    return MAXLEN.  */
195 #if @GNULIB_STRNLEN@
196 # if ! @HAVE_DECL_STRNLEN@
197 extern size_t strnlen (char const *__string, size_t __maxlen);
198 # endif
199 #elif defined GNULIB_POSIXCHECK
200 # undef strnlen
201 # define strnlen strnlen_is_unportable__use_gnulib_module_strnlen_for_portability
202 #endif
203
204 #if defined GNULIB_POSIXCHECK
205 /* strcspn() assumes the second argument is a list of single-byte characters.
206    Even in this simple case, it does not work with multibyte strings if the
207    locale encoding is GB18030 and one of the characters to be searched is a
208    digit.  */
209 # undef strcspn
210 # define strcspn(s,a) \
211     (GL_LINK_WARNING ("strcspn cannot work correctly on character strings in multibyte locales - use mbscspn if you care about internationalization"), \
212      strcspn (s, a))
213 #endif
214
215 /* Find the first occurrence in S of any character in ACCEPT.  */
216 #if @GNULIB_STRPBRK@
217 # if ! @HAVE_STRPBRK@
218 extern char *strpbrk (char const *__s, char const *__accept);
219 # endif
220 # if defined GNULIB_POSIXCHECK
221 /* strpbrk() assumes the second argument is a list of single-byte characters.
222    Even in this simple case, it does not work with multibyte strings if the
223    locale encoding is GB18030 and one of the characters to be searched is a
224    digit.  */
225 #  undef strpbrk
226 #  define strpbrk(s,a) \
227      (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings in multibyte locales - use mbspbrk if you care about internationalization"), \
228       strpbrk (s, a))
229 # endif
230 #elif defined GNULIB_POSIXCHECK
231 # undef strpbrk
232 # define strpbrk strpbrk_is_unportable__use_gnulib_module_strpbrk_for_portability
233 #endif
234
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.  */
238 # undef strspn
239 # define strspn(s,a) \
240     (GL_LINK_WARNING ("strspn cannot work correctly on character strings in multibyte locales - use mbsspn if you care about internationalization"), \
241      strspn (s, a))
242 #endif
243
244 #if defined GNULIB_POSIXCHECK
245 /* strrchr() does not work with multibyte strings if the locale encoding is
246    GB18030 and the character to be searched is a digit.  */
247 # undef strrchr
248 # define strrchr(s,c) \
249     (GL_LINK_WARNING ("strrchr cannot work correctly on character strings in some multibyte locales - use mbsrchr if you care about internationalization"), \
250      strrchr (s, c))
251 #endif
252
253 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
254    If one is found, overwrite it with a NUL, and advance *STRINGP
255    to point to the next char after it.  Otherwise, set *STRINGP to NULL.
256    If *STRINGP was already NULL, nothing happens.
257    Return the old value of *STRINGP.
258
259    This is a variant of strtok() that is multithread-safe and supports
260    empty fields.
261
262    Caveat: It modifies the original string.
263    Caveat: These functions cannot be used on constant strings.
264    Caveat: The identity of the delimiting character is lost.
265    Caveat: It doesn't work with multibyte strings unless all of the delimiter
266            characters are ASCII characters < 0x30.
267
268    See also strtok_r().  */
269 #if @GNULIB_STRSEP@
270 # if ! @HAVE_STRSEP@
271 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
272 # endif
273 # if defined GNULIB_POSIXCHECK
274 #  undef strsep
275 #  define strsep(s,d) \
276      (GL_LINK_WARNING ("strsep cannot work correctly on character strings in multibyte locales - use mbssep if you care about internationalization"), \
277       strsep (s, d))
278 # endif
279 #elif defined GNULIB_POSIXCHECK
280 # undef strsep
281 # define strsep strsep_is_unportable__use_gnulib_module_strsep_for_portability
282 #endif
283
284 #if defined GNULIB_POSIXCHECK
285 /* strstr() does not work with multibyte strings if the locale encoding is
286    different from UTF-8:
287    POSIX says that it operates on "strings", and "string" in POSIX is defined
288    as a sequence of bytes, not of characters.  */
289 # undef strstr
290 # define strstr(a,b) \
291     (GL_LINK_WARNING ("strstr cannot work correctly on character strings in most multibyte locales - use mbsstr if you care about internationalization"), \
292      strstr (a, b))
293 #endif
294
295 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
296    comparison.  */
297 #if ! @HAVE_STRCASESTR@
298 extern char *strcasestr (const char *haystack, const char *needle);
299 #endif
300 #if defined GNULIB_POSIXCHECK
301 /* strcasestr() does not work with multibyte strings:
302    It is a glibc extension, and glibc implements it only for unibyte
303    locales.  */
304 # undef strcasestr
305 # define strcasestr(a,b) \
306     (GL_LINK_WARNING ("strcasestr does work correctly on character strings in multibyte locales - use mbscasestr if you care about internationalization, or use c-strcasestr if you want a locale independent function"), \
307      strcasestr (a, b))
308 #endif
309
310 /* Parse S into tokens separated by characters in DELIM.
311    If S is NULL, the saved pointer in SAVE_PTR is used as
312    the next starting point.  For example:
313         char s[] = "-abc-=-def";
314         char *sp;
315         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
316         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
317         x = strtok_r(NULL, "=", &sp);   // x = NULL
318                 // s = "abc\0-def\0"
319
320    This is a variant of strtok() that is multithread-safe.
321
322    For the POSIX documentation for this function, see:
323    http://www.opengroup.org/susv3xsh/strtok.html
324
325    Caveat: It modifies the original string.
326    Caveat: These functions cannot be used on constant strings.
327    Caveat: The identity of the delimiting character is lost.
328    Caveat: It doesn't work with multibyte strings unless all of the delimiter
329            characters are ASCII characters < 0x30.
330
331    See also strsep().  */
332 #if @GNULIB_STRTOK_R@
333 # if ! @HAVE_DECL_STRTOK_R@
334 extern char *strtok_r (char *restrict s, char const *restrict delim,
335                        char **restrict save_ptr);
336 # endif
337 # if defined GNULIB_POSIXCHECK
338 #  undef strtok_r
339 #  define strtok_r(s,d,p) \
340      (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings in multibyte locales - use mbstok_r if you care about internationalization"), \
341       strtok_r (s, d, p))
342 # endif
343 #elif defined GNULIB_POSIXCHECK
344 # undef strtok_r
345 # define strtok_r strtok_r_is_unportable__use_gnulib_module_strtok_r_for_portability
346 #endif
347
348
349 /* The following functions are not specified by POSIX.  They are gnulib
350    extensions.  */
351
352 #if @GNULIB_MBSLEN@
353 /* Return the number of multibyte characters in the character string STRING.
354    This considers multibyte characters, unlike strlen, which counts bytes.  */
355 extern size_t mbslen (const char *string);
356 #endif
357
358 #if @GNULIB_MBSCHR@
359 /* Locate the first single-byte character C in the character string STRING,
360    and return a pointer to it.  Return NULL if C is not found in STRING.
361    Unlike strchr(), this function works correctly in multibyte locales with
362    encodings such as GB18030.  */
363 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
364 extern char * mbschr (const char *string, int c);
365 #endif
366
367 #if @GNULIB_MBSRCHR@
368 /* Locate the last single-byte character C in the character string STRING,
369    and return a pointer to it.  Return NULL if C is not found in STRING.
370    Unlike strrchr(), this function works correctly in multibyte locales with
371    encodings such as GB18030.  */
372 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
373 extern char * mbsrchr (const char *string, int c);
374 #endif
375
376 #if @GNULIB_MBSSTR@
377 /* Find the first occurrence of the character string NEEDLE in the character
378    string HAYSTACK.  Return NULL if NEEDLE is not found in HAYSTACK.
379    Unlike strstr(), this function works correctly in multibyte locales with
380    encodings different from UTF-8.  */
381 extern char * mbsstr (const char *haystack, const char *needle);
382 #endif
383
384 #if @GNULIB_MBSCASECMP@
385 /* Compare the character strings S1 and S2, ignoring case, returning less than,
386    equal to or greater than zero if S1 is lexicographically less than, equal to
387    or greater than S2.
388    Note: This function may, in multibyte locales, return 0 for strings of
389    different lengths!
390    Unlike strcasecmp(), this function works correctly in multibyte locales.  */
391 extern int mbscasecmp (const char *s1, const char *s2);
392 #endif
393
394 #if @GNULIB_MBSCASESTR@
395 /* Find the first occurrence of the character string NEEDLE in the character
396    string HAYSTACK, using case-insensitive comparison.
397    Note: This function may, in multibyte locales, return success even if
398    strlen (haystack) < strlen (needle) !
399    Unlike strcasestr(), this function works correctly in multibyte locales.  */
400 extern char * mbscasestr (const char *haystack, const char *needle);
401 #endif
402
403 #if @GNULIB_MBSCSPN@
404 /* Find the first occurrence in the character string STRING of any character
405    in the character string ACCEPT.  Return the number of bytes from the
406    beginning of the string to this occurrence, or to the end of the string
407    if none exists.
408    Unlike strcspn(), this function works correctly in multibyte locales.  */
409 extern size_t mbscspn (const char *string, const char *accept);
410 #endif
411
412 #if @GNULIB_MBSPBRK@
413 /* Find the first occurrence in the character string STRING of any character
414    in the character string ACCEPT.  Return the pointer to it, or NULL if none
415    exists.
416    Unlike strpbrk(), this function works correctly in multibyte locales.  */
417 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
418 extern char * mbspbrk (const char *string, const char *accept);
419 #endif
420
421 #if @GNULIB_MBSSPN@
422 /* Find the first occurrence in the character string STRING of any character
423    not in the character string REJECT.  Return the number of bytes from the
424    beginning of the string to this occurrence, or to the end of the string
425    if none exists.
426    Unlike strspn(), this function works correctly in multibyte locales.  */
427 extern size_t mbsspn (const char *string, const char *reject);
428 #endif
429
430 #if @GNULIB_MBSSEP@
431 /* Search the next delimiter (multibyte character listed in the character
432    string DELIM) starting at the character string *STRINGP.
433    If one is found, overwrite it with a NUL, and advance *STRINGP to point
434    to the next multibyte character after it.  Otherwise, set *STRINGP to NULL.
435    If *STRINGP was already NULL, nothing happens.
436    Return the old value of *STRINGP.
437
438    This is a variant of mbstok_r() that supports empty fields.
439
440    Caveat: It modifies the original string.
441    Caveat: These functions cannot be used on constant strings.
442    Caveat: The identity of the delimiting character is lost.
443
444    See also mbstok_r().  */
445 extern char * mbssep (char **stringp, const char *delim);
446 #endif
447
448 #if @GNULIB_MBSTOK_R@
449 /* Parse the character string STRING into tokens separated by characters in
450    the character string DELIM.
451    If STRING is NULL, the saved pointer in SAVE_PTR is used as
452    the next starting point.  For example:
453         char s[] = "-abc-=-def";
454         char *sp;
455         x = mbstok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
456         x = mbstok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
457         x = mbstok_r(NULL, "=", &sp);   // x = NULL
458                 // s = "abc\0-def\0"
459
460    Caveat: It modifies the original string.
461    Caveat: These functions cannot be used on constant strings.
462    Caveat: The identity of the delimiting character is lost.
463
464    See also mbssep().  */
465 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
466 #endif
467
468
469 #ifdef __cplusplus
470 }
471 #endif
472
473 #endif