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