59a14ca24b9198565bbde2a20c87dae99fcf4719
[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 ": " 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 @GNULIB_STRCASE@
137 # if ! @HAVE_DECL_STRNCASECMP@
138 extern int strncasecmp (char const *__s1, char const *__s2, size_t __n);
139 # endif
140 #endif
141 #if defined GNULIB_POSIXCHECK
142 # undef strncasecmp
143 # define strncasecmp(a,b) \
144     (GL_LINK_WARNING ("strncasecmp cannot work correctly in multibyte locales - don't use it if you care about internationalization"), \
145      strncasecmp (a, b))
146 #endif
147
148 #if defined GNULIB_POSIXCHECK
149 /* strchr() does not work with multibyte strings if the locale encoding is
150    GB18030 and the character to be searched is a digit.  */
151 # undef strchr
152 # define strchr(s,c) \
153     (GL_LINK_WARNING ("strchr cannot work correctly on character strings in some multibyte locales - use mbschr if you care about internationalization"), \
154      strchr (s, c))
155 #endif
156
157 /* Find the first occurrence of C in S or the final NUL byte.  */
158 #if @GNULIB_STRCHRNUL@
159 # if ! @HAVE_STRCHRNUL@
160 extern char *strchrnul (char const *__s, int __c_in);
161 # endif
162 #elif defined GNULIB_POSIXCHECK
163 # undef strchrnul
164 # define strchrnul strchrnul_is_unportable__use_gnulib_module_strchrnul_for_portability
165 #endif
166
167 /* Duplicate S, returning an identical malloc'd string.  */
168 #if @GNULIB_STRDUP@
169 # if ! @HAVE_DECL_STRDUP@ && ! defined strdup
170 extern char *strdup (char const *__s);
171 # endif
172 #elif defined GNULIB_POSIXCHECK
173 # undef strdup
174 # define strdup strdup_is_unportable__use_gnulib_module_strdup_for_portability
175 #endif
176
177 /* Return a newly allocated copy of at most N bytes of STRING.  */
178 #if @GNULIB_STRNDUP@
179 # if ! @HAVE_STRNDUP@
180 #  undef strndup
181 #  define strndup rpl_strndup
182 #  if ! @HAVE_DECL_STRNDUP@
183 extern char *strndup (char const *__string, size_t __n);
184 #  endif
185 # endif
186 #elif defined GNULIB_POSIXCHECK
187 # undef strndup
188 # define strndup strndup_is_unportable__use_gnulib_module_strndup_for_portability
189 #endif
190
191 /* Find the length (number of bytes) of STRING, but scan at most
192    MAXLEN bytes.  If no '\0' terminator is found in that many bytes,
193    return MAXLEN.  */
194 #if @GNULIB_STRNLEN@
195 # if ! @HAVE_DECL_STRNLEN@
196 extern size_t strnlen (char const *__string, size_t __maxlen);
197 # endif
198 #elif defined GNULIB_POSIXCHECK
199 # undef strnlen
200 # define strnlen strnlen_is_unportable__use_gnulib_module_strnlen_for_portability
201 #endif
202
203 /* Find the first occurrence in S of any character in ACCEPT.  */
204 #if @GNULIB_STRPBRK@
205 # if ! @HAVE_STRPBRK@
206 extern char *strpbrk (char const *__s, char const *__accept);
207 # endif
208 #elif defined GNULIB_POSIXCHECK
209 # undef strpbrk
210 # define strpbrk strpbrk_is_unportable__use_gnulib_module_strpbrk_for_portability
211 #endif
212
213 #if defined GNULIB_POSIXCHECK
214 /* strrchr() does not work with multibyte strings if the locale encoding is
215    GB18030 and the character to be searched is a digit.  */
216 # undef strrchr
217 # define strrchr(s,c) \
218     (GL_LINK_WARNING ("strrchr cannot work correctly on character strings in some multibyte locales - use mbsrchr if you care about internationalization"), \
219      strrchr (s, c))
220 #endif
221
222 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
223    If one is found, overwrite it with a NUL, and advance *STRINGP
224    to point to the next char after it.  Otherwise, set *STRINGP to NULL.
225    If *STRINGP was already NULL, nothing happens.
226    Return the old value of *STRINGP.
227
228    This is a variant of strtok() that is multithread-safe and supports
229    empty fields.
230
231    Caveat: It modifies the original string.
232    Caveat: These functions cannot be used on constant strings.
233    Caveat: The identity of the delimiting character is lost.
234    Caveat: It doesn't work with multibyte strings unless all of the delimiter
235            characters are ASCII characters < 0x30.
236
237    See also strtok_r().  */
238 #if @GNULIB_STRSEP@
239 # if ! @HAVE_STRSEP@
240 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
241 # endif
242 #elif defined GNULIB_POSIXCHECK
243 # undef strsep
244 # define strsep strsep_is_unportable__use_gnulib_module_strsep_for_portability
245 #endif
246
247 #if defined GNULIB_POSIXCHECK
248 /* strstr() does not work with multibyte strings if the locale encoding is
249    different from UTF-8:
250    POSIX says that it operates on "strings", and "string" in POSIX is defined
251    as a sequence of bytes, not of characters.  */
252 # undef strstr
253 # define strstr(a,b) \
254     (GL_LINK_WARNING ("strstr cannot work correctly on character strings in most multibyte locales - use mbsstr if you care about internationalization"), \
255      strstr (a, b))
256 #endif
257
258 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
259    comparison.
260    Note: This function may, in multibyte locales, return success even if
261    strlen (haystack) < strlen (needle) !  */
262 #if @GNULIB_STRCASESTR@
263 # if @REPLACE_STRCASESTR@
264 #  undef strcasestr
265 #  define strcasestr rpl_strcasestr
266 extern char *strcasestr (const char *haystack, const char *needle);
267 # endif
268 #elif defined GNULIB_POSIXCHECK
269 # undef strcasestr
270 # define strcasestr(a,b) \
271     (GL_LINK_WARNING ("strcasestr is often incorrectly implemented for multibyte locales - use gnulib module 'strcasestr' for correct and portable internationalization"), \
272      strcasestr (a, b))
273 #endif
274
275 /* Parse S into tokens separated by characters in DELIM.
276    If S is NULL, the saved pointer in SAVE_PTR is used as
277    the next starting point.  For example:
278         char s[] = "-abc-=-def";
279         char *sp;
280         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
281         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
282         x = strtok_r(NULL, "=", &sp);   // x = NULL
283                 // s = "abc\0-def\0"
284
285    This is a variant of strtok() that is multithread-safe.
286
287    For the POSIX documentation for this function, see:
288    http://www.opengroup.org/susv3xsh/strtok.html
289
290    Caveat: It modifies the original string.
291    Caveat: These functions cannot be used on constant strings.
292    Caveat: The identity of the delimiting character is lost.
293    Caveat: It doesn't work with multibyte strings unless all of the delimiter
294            characters are ASCII characters < 0x30.
295
296    See also strsep().  */
297 #if @GNULIB_STRTOK_R@
298 # if ! @HAVE_DECL_STRTOK_R@
299 extern char *strtok_r (char *restrict __s, char const *restrict __sep,
300                        char **restrict __lasts);
301 # endif
302 #elif defined GNULIB_POSIXCHECK
303 # undef strtok_r
304 # define strtok_r strtok_r_is_unportable__use_gnulib_module_strtok_r_for_portability
305 #endif
306
307
308 /* The following functions are not specified by POSIX.  They are gnulib
309    extensions.  */
310
311 #if @GNULIB_MBSCHR@
312 /* Locate the first single-byte character C in the character string STRING,
313    and return a pointer to it.  Return NULL if C is not found in STRING.
314    Unlike strchr(), this function works correctly in multibyte locales with
315    encodings such as GB18030.  */
316 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
317 extern char * mbschr (const char *string, int c);
318 #endif
319
320 #if @GNULIB_MBSRCHR@
321 /* Locate the last single-byte character C in the character string STRING,
322    and return a pointer to it.  Return NULL if C is not found in STRING.
323    Unlike strrchr(), this function works correctly in multibyte locales with
324    encodings such as GB18030.  */
325 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
326 extern char * mbsrchr (const char *string, int c);
327 #endif
328
329 #if @GNULIB_MBSSTR@
330 /* Find the first occurrence of the character string NEEDLE in the character
331    string HAYSTACK.  Return NULL if NEEDLE is not found in HAYSTACK.
332    Unlike strstr(), this function works correctly in multibyte locales with
333    encodings different from UTF-8.  */
334 extern char * mbsstr (const char *haystack, const char *needle);
335 #endif
336
337 #if @GNULIB_MBSCASECMP@
338 /* Compare the character strings S1 and S2, ignoring case, returning less than,
339    equal to or greater than zero if S1 is lexicographically less than, equal to
340    or greater than S2.
341    Note: This function may, in multibyte locales, return 0 for strings of
342    different lengths!
343    Unlike strcasecmp(), this function works correctly in multibyte locales.  */
344 extern int mbscasecmp (const char *s1, const char *s2);
345 #endif
346
347
348 #ifdef __cplusplus
349 }
350 #endif
351
352 #endif