Compare and round more carefully
[emacs.git] / lib / string.in.h
blob9a6b311d00799cce6dfe71e39f33f9eacd91bd28
1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2017 Free Software Foundation, Inc.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #if __GNUC__ >= 3
19 @PRAGMA_SYSTEM_HEADER@
20 #endif
21 @PRAGMA_COLUMNS@
23 #if defined _GL_ALREADY_INCLUDING_STRING_H
24 /* Special invocation convention:
25 - On OS X/NetBSD we have a sequence of nested includes
26 <string.h> -> <strings.h> -> "string.h"
27 In this situation system _chk variants due to -D_FORTIFY_SOURCE
28 might be used after any replacements defined here. */
30 #@INCLUDE_NEXT@ @NEXT_STRING_H@
32 #else
33 /* Normal invocation convention. */
35 #ifndef _@GUARD_PREFIX@_STRING_H
37 #define _GL_ALREADY_INCLUDING_STRING_H
39 /* The include_next requires a split double-inclusion guard. */
40 #@INCLUDE_NEXT@ @NEXT_STRING_H@
42 #undef _GL_ALREADY_INCLUDING_STRING_H
44 #ifndef _@GUARD_PREFIX@_STRING_H
45 #define _@GUARD_PREFIX@_STRING_H
47 /* NetBSD 5.0 mis-defines NULL. */
48 #include <stddef.h>
50 /* MirBSD defines mbslen as a macro. */
51 #if @GNULIB_MBSLEN@ && defined __MirBSD__
52 # include <wchar.h>
53 #endif
55 /* The __attribute__ feature is available in gcc versions 2.5 and later.
56 The attribute __pure__ was added in gcc 2.96. */
57 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96)
58 # define _GL_ATTRIBUTE_PURE __attribute__ ((__pure__))
59 #else
60 # define _GL_ATTRIBUTE_PURE /* empty */
61 #endif
63 /* NetBSD 5.0 declares strsignal in <unistd.h>, not in <string.h>. */
64 /* But in any case avoid namespace pollution on glibc systems. */
65 #if (@GNULIB_STRSIGNAL@ || defined GNULIB_POSIXCHECK) && defined __NetBSD__ \
66 && ! defined __GLIBC__
67 # include <unistd.h>
68 #endif
70 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
72 /* The definition of _GL_ARG_NONNULL is copied here. */
74 /* The definition of _GL_WARN_ON_USE is copied here. */
77 /* Find the index of the least-significant set bit. */
78 #if @GNULIB_FFSL@
79 # if !@HAVE_FFSL@
80 _GL_FUNCDECL_SYS (ffsl, int, (long int i));
81 # endif
82 _GL_CXXALIAS_SYS (ffsl, int, (long int i));
83 _GL_CXXALIASWARN (ffsl);
84 #elif defined GNULIB_POSIXCHECK
85 # undef ffsl
86 # if HAVE_RAW_DECL_FFSL
87 _GL_WARN_ON_USE (ffsl, "ffsl is not portable - use the ffsl module");
88 # endif
89 #endif
92 /* Find the index of the least-significant set bit. */
93 #if @GNULIB_FFSLL@
94 # if !@HAVE_FFSLL@
95 _GL_FUNCDECL_SYS (ffsll, int, (long long int i));
96 # endif
97 _GL_CXXALIAS_SYS (ffsll, int, (long long int i));
98 _GL_CXXALIASWARN (ffsll);
99 #elif defined GNULIB_POSIXCHECK
100 # undef ffsll
101 # if HAVE_RAW_DECL_FFSLL
102 _GL_WARN_ON_USE (ffsll, "ffsll is not portable - use the ffsll module");
103 # endif
104 #endif
107 /* Return the first instance of C within N bytes of S, or NULL. */
108 #if @GNULIB_MEMCHR@
109 # if @REPLACE_MEMCHR@
110 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
111 # define memchr rpl_memchr
112 # endif
113 _GL_FUNCDECL_RPL (memchr, void *, (void const *__s, int __c, size_t __n)
114 _GL_ATTRIBUTE_PURE
115 _GL_ARG_NONNULL ((1)));
116 _GL_CXXALIAS_RPL (memchr, void *, (void const *__s, int __c, size_t __n));
117 # else
118 # if ! @HAVE_MEMCHR@
119 _GL_FUNCDECL_SYS (memchr, void *, (void const *__s, int __c, size_t __n)
120 _GL_ATTRIBUTE_PURE
121 _GL_ARG_NONNULL ((1)));
122 # endif
123 /* On some systems, this function is defined as an overloaded function:
124 extern "C" { const void * std::memchr (const void *, int, size_t); }
125 extern "C++" { void * std::memchr (void *, int, size_t); } */
126 _GL_CXXALIAS_SYS_CAST2 (memchr,
127 void *, (void const *__s, int __c, size_t __n),
128 void const *, (void const *__s, int __c, size_t __n));
129 # endif
130 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
131 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
132 _GL_CXXALIASWARN1 (memchr, void *, (void *__s, int __c, size_t __n));
133 _GL_CXXALIASWARN1 (memchr, void const *,
134 (void const *__s, int __c, size_t __n));
135 # else
136 _GL_CXXALIASWARN (memchr);
137 # endif
138 #elif defined GNULIB_POSIXCHECK
139 # undef memchr
140 /* Assume memchr is always declared. */
141 _GL_WARN_ON_USE (memchr, "memchr has platform-specific bugs - "
142 "use gnulib module memchr for portability" );
143 #endif
145 /* Return the first occurrence of NEEDLE in HAYSTACK. */
146 #if @GNULIB_MEMMEM@
147 # if @REPLACE_MEMMEM@
148 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
149 # define memmem rpl_memmem
150 # endif
151 _GL_FUNCDECL_RPL (memmem, void *,
152 (void const *__haystack, size_t __haystack_len,
153 void const *__needle, size_t __needle_len)
154 _GL_ATTRIBUTE_PURE
155 _GL_ARG_NONNULL ((1, 3)));
156 _GL_CXXALIAS_RPL (memmem, void *,
157 (void const *__haystack, size_t __haystack_len,
158 void const *__needle, size_t __needle_len));
159 # else
160 # if ! @HAVE_DECL_MEMMEM@
161 _GL_FUNCDECL_SYS (memmem, void *,
162 (void const *__haystack, size_t __haystack_len,
163 void const *__needle, size_t __needle_len)
164 _GL_ATTRIBUTE_PURE
165 _GL_ARG_NONNULL ((1, 3)));
166 # endif
167 _GL_CXXALIAS_SYS (memmem, void *,
168 (void const *__haystack, size_t __haystack_len,
169 void const *__needle, size_t __needle_len));
170 # endif
171 _GL_CXXALIASWARN (memmem);
172 #elif defined GNULIB_POSIXCHECK
173 # undef memmem
174 # if HAVE_RAW_DECL_MEMMEM
175 _GL_WARN_ON_USE (memmem, "memmem is unportable and often quadratic - "
176 "use gnulib module memmem-simple for portability, "
177 "and module memmem for speed" );
178 # endif
179 #endif
181 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
182 last written byte. */
183 #if @GNULIB_MEMPCPY@
184 # if ! @HAVE_MEMPCPY@
185 _GL_FUNCDECL_SYS (mempcpy, void *,
186 (void *restrict __dest, void const *restrict __src,
187 size_t __n)
188 _GL_ARG_NONNULL ((1, 2)));
189 # endif
190 _GL_CXXALIAS_SYS (mempcpy, void *,
191 (void *restrict __dest, void const *restrict __src,
192 size_t __n));
193 _GL_CXXALIASWARN (mempcpy);
194 #elif defined GNULIB_POSIXCHECK
195 # undef mempcpy
196 # if HAVE_RAW_DECL_MEMPCPY
197 _GL_WARN_ON_USE (mempcpy, "mempcpy is unportable - "
198 "use gnulib module mempcpy for portability");
199 # endif
200 #endif
202 /* Search backwards through a block for a byte (specified as an int). */
203 #if @GNULIB_MEMRCHR@
204 # if ! @HAVE_DECL_MEMRCHR@
205 _GL_FUNCDECL_SYS (memrchr, void *, (void const *, int, size_t)
206 _GL_ATTRIBUTE_PURE
207 _GL_ARG_NONNULL ((1)));
208 # endif
209 /* On some systems, this function is defined as an overloaded function:
210 extern "C++" { const void * std::memrchr (const void *, int, size_t); }
211 extern "C++" { void * std::memrchr (void *, int, size_t); } */
212 _GL_CXXALIAS_SYS_CAST2 (memrchr,
213 void *, (void const *, int, size_t),
214 void const *, (void const *, int, size_t));
215 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
216 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
217 _GL_CXXALIASWARN1 (memrchr, void *, (void *, int, size_t));
218 _GL_CXXALIASWARN1 (memrchr, void const *, (void const *, int, size_t));
219 # else
220 _GL_CXXALIASWARN (memrchr);
221 # endif
222 #elif defined GNULIB_POSIXCHECK
223 # undef memrchr
224 # if HAVE_RAW_DECL_MEMRCHR
225 _GL_WARN_ON_USE (memrchr, "memrchr is unportable - "
226 "use gnulib module memrchr for portability");
227 # endif
228 #endif
230 /* Find the first occurrence of C in S. More efficient than
231 memchr(S,C,N), at the expense of undefined behavior if C does not
232 occur within N bytes. */
233 #if @GNULIB_RAWMEMCHR@
234 # if ! @HAVE_RAWMEMCHR@
235 _GL_FUNCDECL_SYS (rawmemchr, void *, (void const *__s, int __c_in)
236 _GL_ATTRIBUTE_PURE
237 _GL_ARG_NONNULL ((1)));
238 # endif
239 /* On some systems, this function is defined as an overloaded function:
240 extern "C++" { const void * std::rawmemchr (const void *, int); }
241 extern "C++" { void * std::rawmemchr (void *, int); } */
242 _GL_CXXALIAS_SYS_CAST2 (rawmemchr,
243 void *, (void const *__s, int __c_in),
244 void const *, (void const *__s, int __c_in));
245 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
246 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
247 _GL_CXXALIASWARN1 (rawmemchr, void *, (void *__s, int __c_in));
248 _GL_CXXALIASWARN1 (rawmemchr, void const *, (void const *__s, int __c_in));
249 # else
250 _GL_CXXALIASWARN (rawmemchr);
251 # endif
252 #elif defined GNULIB_POSIXCHECK
253 # undef rawmemchr
254 # if HAVE_RAW_DECL_RAWMEMCHR
255 _GL_WARN_ON_USE (rawmemchr, "rawmemchr is unportable - "
256 "use gnulib module rawmemchr for portability");
257 # endif
258 #endif
260 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
261 #if @GNULIB_STPCPY@
262 # if ! @HAVE_STPCPY@
263 _GL_FUNCDECL_SYS (stpcpy, char *,
264 (char *restrict __dst, char const *restrict __src)
265 _GL_ARG_NONNULL ((1, 2)));
266 # endif
267 _GL_CXXALIAS_SYS (stpcpy, char *,
268 (char *restrict __dst, char const *restrict __src));
269 _GL_CXXALIASWARN (stpcpy);
270 #elif defined GNULIB_POSIXCHECK
271 # undef stpcpy
272 # if HAVE_RAW_DECL_STPCPY
273 _GL_WARN_ON_USE (stpcpy, "stpcpy is unportable - "
274 "use gnulib module stpcpy for portability");
275 # endif
276 #endif
278 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
279 last non-NUL byte written into DST. */
280 #if @GNULIB_STPNCPY@
281 # if @REPLACE_STPNCPY@
282 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
283 # undef stpncpy
284 # define stpncpy rpl_stpncpy
285 # endif
286 _GL_FUNCDECL_RPL (stpncpy, char *,
287 (char *restrict __dst, char const *restrict __src,
288 size_t __n)
289 _GL_ARG_NONNULL ((1, 2)));
290 _GL_CXXALIAS_RPL (stpncpy, char *,
291 (char *restrict __dst, char const *restrict __src,
292 size_t __n));
293 # else
294 # if ! @HAVE_STPNCPY@
295 _GL_FUNCDECL_SYS (stpncpy, char *,
296 (char *restrict __dst, char const *restrict __src,
297 size_t __n)
298 _GL_ARG_NONNULL ((1, 2)));
299 # endif
300 _GL_CXXALIAS_SYS (stpncpy, char *,
301 (char *restrict __dst, char const *restrict __src,
302 size_t __n));
303 # endif
304 _GL_CXXALIASWARN (stpncpy);
305 #elif defined GNULIB_POSIXCHECK
306 # undef stpncpy
307 # if HAVE_RAW_DECL_STPNCPY
308 _GL_WARN_ON_USE (stpncpy, "stpncpy is unportable - "
309 "use gnulib module stpncpy for portability");
310 # endif
311 #endif
313 #if defined GNULIB_POSIXCHECK
314 /* strchr() does not work with multibyte strings if the locale encoding is
315 GB18030 and the character to be searched is a digit. */
316 # undef strchr
317 /* Assume strchr is always declared. */
318 _GL_WARN_ON_USE (strchr, "strchr cannot work correctly on character strings "
319 "in some multibyte locales - "
320 "use mbschr if you care about internationalization");
321 #endif
323 /* Find the first occurrence of C in S or the final NUL byte. */
324 #if @GNULIB_STRCHRNUL@
325 # if @REPLACE_STRCHRNUL@
326 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
327 # define strchrnul rpl_strchrnul
328 # endif
329 _GL_FUNCDECL_RPL (strchrnul, char *, (const char *__s, int __c_in)
330 _GL_ATTRIBUTE_PURE
331 _GL_ARG_NONNULL ((1)));
332 _GL_CXXALIAS_RPL (strchrnul, char *,
333 (const char *str, int ch));
334 # else
335 # if ! @HAVE_STRCHRNUL@
336 _GL_FUNCDECL_SYS (strchrnul, char *, (char const *__s, int __c_in)
337 _GL_ATTRIBUTE_PURE
338 _GL_ARG_NONNULL ((1)));
339 # endif
340 /* On some systems, this function is defined as an overloaded function:
341 extern "C++" { const char * std::strchrnul (const char *, int); }
342 extern "C++" { char * std::strchrnul (char *, int); } */
343 _GL_CXXALIAS_SYS_CAST2 (strchrnul,
344 char *, (char const *__s, int __c_in),
345 char const *, (char const *__s, int __c_in));
346 # endif
347 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
348 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
349 _GL_CXXALIASWARN1 (strchrnul, char *, (char *__s, int __c_in));
350 _GL_CXXALIASWARN1 (strchrnul, char const *, (char const *__s, int __c_in));
351 # else
352 _GL_CXXALIASWARN (strchrnul);
353 # endif
354 #elif defined GNULIB_POSIXCHECK
355 # undef strchrnul
356 # if HAVE_RAW_DECL_STRCHRNUL
357 _GL_WARN_ON_USE (strchrnul, "strchrnul is unportable - "
358 "use gnulib module strchrnul for portability");
359 # endif
360 #endif
362 /* Duplicate S, returning an identical malloc'd string. */
363 #if @GNULIB_STRDUP@
364 # if @REPLACE_STRDUP@
365 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
366 # undef strdup
367 # define strdup rpl_strdup
368 # endif
369 _GL_FUNCDECL_RPL (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
370 _GL_CXXALIAS_RPL (strdup, char *, (char const *__s));
371 # else
372 # if defined __cplusplus && defined GNULIB_NAMESPACE && defined strdup
373 /* strdup exists as a function and as a macro. Get rid of the macro. */
374 # undef strdup
375 # endif
376 # if !(@HAVE_DECL_STRDUP@ || defined strdup)
377 _GL_FUNCDECL_SYS (strdup, char *, (char const *__s) _GL_ARG_NONNULL ((1)));
378 # endif
379 _GL_CXXALIAS_SYS (strdup, char *, (char const *__s));
380 # endif
381 _GL_CXXALIASWARN (strdup);
382 #elif defined GNULIB_POSIXCHECK
383 # undef strdup
384 # if HAVE_RAW_DECL_STRDUP
385 _GL_WARN_ON_USE (strdup, "strdup is unportable - "
386 "use gnulib module strdup for portability");
387 # endif
388 #endif
390 /* Append no more than N characters from SRC onto DEST. */
391 #if @GNULIB_STRNCAT@
392 # if @REPLACE_STRNCAT@
393 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
394 # undef strncat
395 # define strncat rpl_strncat
396 # endif
397 _GL_FUNCDECL_RPL (strncat, char *, (char *dest, const char *src, size_t n)
398 _GL_ARG_NONNULL ((1, 2)));
399 _GL_CXXALIAS_RPL (strncat, char *, (char *dest, const char *src, size_t n));
400 # else
401 _GL_CXXALIAS_SYS (strncat, char *, (char *dest, const char *src, size_t n));
402 # endif
403 _GL_CXXALIASWARN (strncat);
404 #elif defined GNULIB_POSIXCHECK
405 # undef strncat
406 # if HAVE_RAW_DECL_STRNCAT
407 _GL_WARN_ON_USE (strncat, "strncat is unportable - "
408 "use gnulib module strncat for portability");
409 # endif
410 #endif
412 /* Return a newly allocated copy of at most N bytes of STRING. */
413 #if @GNULIB_STRNDUP@
414 # if @REPLACE_STRNDUP@
415 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
416 # undef strndup
417 # define strndup rpl_strndup
418 # endif
419 _GL_FUNCDECL_RPL (strndup, char *, (char const *__s, size_t __n)
420 _GL_ARG_NONNULL ((1)));
421 _GL_CXXALIAS_RPL (strndup, char *, (char const *__s, size_t __n));
422 # else
423 # if ! @HAVE_DECL_STRNDUP@
424 _GL_FUNCDECL_SYS (strndup, char *, (char const *__s, size_t __n)
425 _GL_ARG_NONNULL ((1)));
426 # endif
427 _GL_CXXALIAS_SYS (strndup, char *, (char const *__s, size_t __n));
428 # endif
429 _GL_CXXALIASWARN (strndup);
430 #elif defined GNULIB_POSIXCHECK
431 # undef strndup
432 # if HAVE_RAW_DECL_STRNDUP
433 _GL_WARN_ON_USE (strndup, "strndup is unportable - "
434 "use gnulib module strndup for portability");
435 # endif
436 #endif
438 /* Find the length (number of bytes) of STRING, but scan at most
439 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
440 return MAXLEN. */
441 #if @GNULIB_STRNLEN@
442 # if @REPLACE_STRNLEN@
443 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
444 # undef strnlen
445 # define strnlen rpl_strnlen
446 # endif
447 _GL_FUNCDECL_RPL (strnlen, size_t, (char const *__s, size_t __maxlen)
448 _GL_ATTRIBUTE_PURE
449 _GL_ARG_NONNULL ((1)));
450 _GL_CXXALIAS_RPL (strnlen, size_t, (char const *__s, size_t __maxlen));
451 # else
452 # if ! @HAVE_DECL_STRNLEN@
453 _GL_FUNCDECL_SYS (strnlen, size_t, (char const *__s, size_t __maxlen)
454 _GL_ATTRIBUTE_PURE
455 _GL_ARG_NONNULL ((1)));
456 # endif
457 _GL_CXXALIAS_SYS (strnlen, size_t, (char const *__s, size_t __maxlen));
458 # endif
459 _GL_CXXALIASWARN (strnlen);
460 #elif defined GNULIB_POSIXCHECK
461 # undef strnlen
462 # if HAVE_RAW_DECL_STRNLEN
463 _GL_WARN_ON_USE (strnlen, "strnlen is unportable - "
464 "use gnulib module strnlen for portability");
465 # endif
466 #endif
468 #if defined GNULIB_POSIXCHECK
469 /* strcspn() assumes the second argument is a list of single-byte characters.
470 Even in this simple case, it does not work with multibyte strings if the
471 locale encoding is GB18030 and one of the characters to be searched is a
472 digit. */
473 # undef strcspn
474 /* Assume strcspn is always declared. */
475 _GL_WARN_ON_USE (strcspn, "strcspn cannot work correctly on character strings "
476 "in multibyte locales - "
477 "use mbscspn if you care about internationalization");
478 #endif
480 /* Find the first occurrence in S of any character in ACCEPT. */
481 #if @GNULIB_STRPBRK@
482 # if ! @HAVE_STRPBRK@
483 _GL_FUNCDECL_SYS (strpbrk, char *, (char const *__s, char const *__accept)
484 _GL_ATTRIBUTE_PURE
485 _GL_ARG_NONNULL ((1, 2)));
486 # endif
487 /* On some systems, this function is defined as an overloaded function:
488 extern "C" { const char * strpbrk (const char *, const char *); }
489 extern "C++" { char * strpbrk (char *, const char *); } */
490 _GL_CXXALIAS_SYS_CAST2 (strpbrk,
491 char *, (char const *__s, char const *__accept),
492 const char *, (char const *__s, char const *__accept));
493 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
494 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
495 _GL_CXXALIASWARN1 (strpbrk, char *, (char *__s, char const *__accept));
496 _GL_CXXALIASWARN1 (strpbrk, char const *,
497 (char const *__s, char const *__accept));
498 # else
499 _GL_CXXALIASWARN (strpbrk);
500 # endif
501 # if defined GNULIB_POSIXCHECK
502 /* strpbrk() assumes the second argument is a list of single-byte characters.
503 Even in this simple case, it does not work with multibyte strings if the
504 locale encoding is GB18030 and one of the characters to be searched is a
505 digit. */
506 # undef strpbrk
507 _GL_WARN_ON_USE (strpbrk, "strpbrk cannot work correctly on character strings "
508 "in multibyte locales - "
509 "use mbspbrk if you care about internationalization");
510 # endif
511 #elif defined GNULIB_POSIXCHECK
512 # undef strpbrk
513 # if HAVE_RAW_DECL_STRPBRK
514 _GL_WARN_ON_USE (strpbrk, "strpbrk is unportable - "
515 "use gnulib module strpbrk for portability");
516 # endif
517 #endif
519 #if defined GNULIB_POSIXCHECK
520 /* strspn() assumes the second argument is a list of single-byte characters.
521 Even in this simple case, it cannot work with multibyte strings. */
522 # undef strspn
523 /* Assume strspn is always declared. */
524 _GL_WARN_ON_USE (strspn, "strspn cannot work correctly on character strings "
525 "in multibyte locales - "
526 "use mbsspn if you care about internationalization");
527 #endif
529 #if defined GNULIB_POSIXCHECK
530 /* strrchr() does not work with multibyte strings if the locale encoding is
531 GB18030 and the character to be searched is a digit. */
532 # undef strrchr
533 /* Assume strrchr is always declared. */
534 _GL_WARN_ON_USE (strrchr, "strrchr cannot work correctly on character strings "
535 "in some multibyte locales - "
536 "use mbsrchr if you care about internationalization");
537 #endif
539 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
540 If one is found, overwrite it with a NUL, and advance *STRINGP
541 to point to the next char after it. Otherwise, set *STRINGP to NULL.
542 If *STRINGP was already NULL, nothing happens.
543 Return the old value of *STRINGP.
545 This is a variant of strtok() that is multithread-safe and supports
546 empty fields.
548 Caveat: It modifies the original string.
549 Caveat: These functions cannot be used on constant strings.
550 Caveat: The identity of the delimiting character is lost.
551 Caveat: It doesn't work with multibyte strings unless all of the delimiter
552 characters are ASCII characters < 0x30.
554 See also strtok_r(). */
555 #if @GNULIB_STRSEP@
556 # if ! @HAVE_STRSEP@
557 _GL_FUNCDECL_SYS (strsep, char *,
558 (char **restrict __stringp, char const *restrict __delim)
559 _GL_ARG_NONNULL ((1, 2)));
560 # endif
561 _GL_CXXALIAS_SYS (strsep, char *,
562 (char **restrict __stringp, char const *restrict __delim));
563 _GL_CXXALIASWARN (strsep);
564 # if defined GNULIB_POSIXCHECK
565 # undef strsep
566 _GL_WARN_ON_USE (strsep, "strsep cannot work correctly on character strings "
567 "in multibyte locales - "
568 "use mbssep if you care about internationalization");
569 # endif
570 #elif defined GNULIB_POSIXCHECK
571 # undef strsep
572 # if HAVE_RAW_DECL_STRSEP
573 _GL_WARN_ON_USE (strsep, "strsep is unportable - "
574 "use gnulib module strsep for portability");
575 # endif
576 #endif
578 #if @GNULIB_STRSTR@
579 # if @REPLACE_STRSTR@
580 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
581 # define strstr rpl_strstr
582 # endif
583 _GL_FUNCDECL_RPL (strstr, char *, (const char *haystack, const char *needle)
584 _GL_ATTRIBUTE_PURE
585 _GL_ARG_NONNULL ((1, 2)));
586 _GL_CXXALIAS_RPL (strstr, char *, (const char *haystack, const char *needle));
587 # else
588 /* On some systems, this function is defined as an overloaded function:
589 extern "C++" { const char * strstr (const char *, const char *); }
590 extern "C++" { char * strstr (char *, const char *); } */
591 _GL_CXXALIAS_SYS_CAST2 (strstr,
592 char *, (const char *haystack, const char *needle),
593 const char *, (const char *haystack, const char *needle));
594 # endif
595 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
596 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
597 _GL_CXXALIASWARN1 (strstr, char *, (char *haystack, const char *needle));
598 _GL_CXXALIASWARN1 (strstr, const char *,
599 (const char *haystack, const char *needle));
600 # else
601 _GL_CXXALIASWARN (strstr);
602 # endif
603 #elif defined GNULIB_POSIXCHECK
604 /* strstr() does not work with multibyte strings if the locale encoding is
605 different from UTF-8:
606 POSIX says that it operates on "strings", and "string" in POSIX is defined
607 as a sequence of bytes, not of characters. */
608 # undef strstr
609 /* Assume strstr is always declared. */
610 _GL_WARN_ON_USE (strstr, "strstr is quadratic on many systems, and cannot "
611 "work correctly on character strings in most "
612 "multibyte locales - "
613 "use mbsstr if you care about internationalization, "
614 "or use strstr if you care about speed");
615 #endif
617 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
618 comparison. */
619 #if @GNULIB_STRCASESTR@
620 # if @REPLACE_STRCASESTR@
621 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
622 # define strcasestr rpl_strcasestr
623 # endif
624 _GL_FUNCDECL_RPL (strcasestr, char *,
625 (const char *haystack, const char *needle)
626 _GL_ATTRIBUTE_PURE
627 _GL_ARG_NONNULL ((1, 2)));
628 _GL_CXXALIAS_RPL (strcasestr, char *,
629 (const char *haystack, const char *needle));
630 # else
631 # if ! @HAVE_STRCASESTR@
632 _GL_FUNCDECL_SYS (strcasestr, char *,
633 (const char *haystack, const char *needle)
634 _GL_ATTRIBUTE_PURE
635 _GL_ARG_NONNULL ((1, 2)));
636 # endif
637 /* On some systems, this function is defined as an overloaded function:
638 extern "C++" { const char * strcasestr (const char *, const char *); }
639 extern "C++" { char * strcasestr (char *, const char *); } */
640 _GL_CXXALIAS_SYS_CAST2 (strcasestr,
641 char *, (const char *haystack, const char *needle),
642 const char *, (const char *haystack, const char *needle));
643 # endif
644 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10) && !defined __UCLIBC__) \
645 && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
646 _GL_CXXALIASWARN1 (strcasestr, char *, (char *haystack, const char *needle));
647 _GL_CXXALIASWARN1 (strcasestr, const char *,
648 (const char *haystack, const char *needle));
649 # else
650 _GL_CXXALIASWARN (strcasestr);
651 # endif
652 #elif defined GNULIB_POSIXCHECK
653 /* strcasestr() does not work with multibyte strings:
654 It is a glibc extension, and glibc implements it only for unibyte
655 locales. */
656 # undef strcasestr
657 # if HAVE_RAW_DECL_STRCASESTR
658 _GL_WARN_ON_USE (strcasestr, "strcasestr does work correctly on character "
659 "strings in multibyte locales - "
660 "use mbscasestr if you care about "
661 "internationalization, or use c-strcasestr if you want "
662 "a locale independent function");
663 # endif
664 #endif
666 /* Parse S into tokens separated by characters in DELIM.
667 If S is NULL, the saved pointer in SAVE_PTR is used as
668 the next starting point. For example:
669 char s[] = "-abc-=-def";
670 char *sp;
671 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
672 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
673 x = strtok_r(NULL, "=", &sp); // x = NULL
674 // s = "abc\0-def\0"
676 This is a variant of strtok() that is multithread-safe.
678 For the POSIX documentation for this function, see:
679 http://www.opengroup.org/susv3xsh/strtok.html
681 Caveat: It modifies the original string.
682 Caveat: These functions cannot be used on constant strings.
683 Caveat: The identity of the delimiting character is lost.
684 Caveat: It doesn't work with multibyte strings unless all of the delimiter
685 characters are ASCII characters < 0x30.
687 See also strsep(). */
688 #if @GNULIB_STRTOK_R@
689 # if @REPLACE_STRTOK_R@
690 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
691 # undef strtok_r
692 # define strtok_r rpl_strtok_r
693 # endif
694 _GL_FUNCDECL_RPL (strtok_r, char *,
695 (char *restrict s, char const *restrict delim,
696 char **restrict save_ptr)
697 _GL_ARG_NONNULL ((2, 3)));
698 _GL_CXXALIAS_RPL (strtok_r, char *,
699 (char *restrict s, char const *restrict delim,
700 char **restrict save_ptr));
701 # else
702 # if @UNDEFINE_STRTOK_R@ || defined GNULIB_POSIXCHECK
703 # undef strtok_r
704 # endif
705 # if ! @HAVE_DECL_STRTOK_R@
706 _GL_FUNCDECL_SYS (strtok_r, char *,
707 (char *restrict s, char const *restrict delim,
708 char **restrict save_ptr)
709 _GL_ARG_NONNULL ((2, 3)));
710 # endif
711 _GL_CXXALIAS_SYS (strtok_r, char *,
712 (char *restrict s, char const *restrict delim,
713 char **restrict save_ptr));
714 # endif
715 _GL_CXXALIASWARN (strtok_r);
716 # if defined GNULIB_POSIXCHECK
717 _GL_WARN_ON_USE (strtok_r, "strtok_r cannot work correctly on character "
718 "strings in multibyte locales - "
719 "use mbstok_r if you care about internationalization");
720 # endif
721 #elif defined GNULIB_POSIXCHECK
722 # undef strtok_r
723 # if HAVE_RAW_DECL_STRTOK_R
724 _GL_WARN_ON_USE (strtok_r, "strtok_r is unportable - "
725 "use gnulib module strtok_r for portability");
726 # endif
727 #endif
730 /* The following functions are not specified by POSIX. They are gnulib
731 extensions. */
733 #if @GNULIB_MBSLEN@
734 /* Return the number of multibyte characters in the character string STRING.
735 This considers multibyte characters, unlike strlen, which counts bytes. */
736 # ifdef __MirBSD__ /* MirBSD defines mbslen as a macro. Override it. */
737 # undef mbslen
738 # endif
739 # if @HAVE_MBSLEN@ /* AIX, OSF/1, MirBSD define mbslen already in libc. */
740 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
741 # define mbslen rpl_mbslen
742 # endif
743 _GL_FUNCDECL_RPL (mbslen, size_t, (const char *string)
744 _GL_ATTRIBUTE_PURE
745 _GL_ARG_NONNULL ((1)));
746 _GL_CXXALIAS_RPL (mbslen, size_t, (const char *string));
747 # else
748 _GL_FUNCDECL_SYS (mbslen, size_t, (const char *string)
749 _GL_ATTRIBUTE_PURE
750 _GL_ARG_NONNULL ((1)));
751 _GL_CXXALIAS_SYS (mbslen, size_t, (const char *string));
752 # endif
753 _GL_CXXALIASWARN (mbslen);
754 #endif
756 #if @GNULIB_MBSNLEN@
757 /* Return the number of multibyte characters in the character string starting
758 at STRING and ending at STRING + LEN. */
759 _GL_EXTERN_C size_t mbsnlen (const char *string, size_t len)
760 _GL_ATTRIBUTE_PURE
761 _GL_ARG_NONNULL ((1));
762 #endif
764 #if @GNULIB_MBSCHR@
765 /* Locate the first single-byte character C in the character string STRING,
766 and return a pointer to it. Return NULL if C is not found in STRING.
767 Unlike strchr(), this function works correctly in multibyte locales with
768 encodings such as GB18030. */
769 # if defined __hpux
770 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
771 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
772 # endif
773 _GL_FUNCDECL_RPL (mbschr, char *, (const char *string, int c)
774 _GL_ATTRIBUTE_PURE
775 _GL_ARG_NONNULL ((1)));
776 _GL_CXXALIAS_RPL (mbschr, char *, (const char *string, int c));
777 # else
778 _GL_FUNCDECL_SYS (mbschr, char *, (const char *string, int c)
779 _GL_ATTRIBUTE_PURE
780 _GL_ARG_NONNULL ((1)));
781 _GL_CXXALIAS_SYS (mbschr, char *, (const char *string, int c));
782 # endif
783 _GL_CXXALIASWARN (mbschr);
784 #endif
786 #if @GNULIB_MBSRCHR@
787 /* Locate the last single-byte character C in the character string STRING,
788 and return a pointer to it. Return NULL if C is not found in STRING.
789 Unlike strrchr(), this function works correctly in multibyte locales with
790 encodings such as GB18030. */
791 # if defined __hpux || defined __INTERIX
792 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
793 # define mbsrchr rpl_mbsrchr /* avoid collision with system function */
794 # endif
795 _GL_FUNCDECL_RPL (mbsrchr, char *, (const char *string, int c)
796 _GL_ATTRIBUTE_PURE
797 _GL_ARG_NONNULL ((1)));
798 _GL_CXXALIAS_RPL (mbsrchr, char *, (const char *string, int c));
799 # else
800 _GL_FUNCDECL_SYS (mbsrchr, char *, (const char *string, int c)
801 _GL_ATTRIBUTE_PURE
802 _GL_ARG_NONNULL ((1)));
803 _GL_CXXALIAS_SYS (mbsrchr, char *, (const char *string, int c));
804 # endif
805 _GL_CXXALIASWARN (mbsrchr);
806 #endif
808 #if @GNULIB_MBSSTR@
809 /* Find the first occurrence of the character string NEEDLE in the character
810 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
811 Unlike strstr(), this function works correctly in multibyte locales with
812 encodings different from UTF-8. */
813 _GL_EXTERN_C char * mbsstr (const char *haystack, const char *needle)
814 _GL_ATTRIBUTE_PURE
815 _GL_ARG_NONNULL ((1, 2));
816 #endif
818 #if @GNULIB_MBSCASECMP@
819 /* Compare the character strings S1 and S2, ignoring case, returning less than,
820 equal to or greater than zero if S1 is lexicographically less than, equal to
821 or greater than S2.
822 Note: This function may, in multibyte locales, return 0 for strings of
823 different lengths!
824 Unlike strcasecmp(), this function works correctly in multibyte locales. */
825 _GL_EXTERN_C int mbscasecmp (const char *s1, const char *s2)
826 _GL_ATTRIBUTE_PURE
827 _GL_ARG_NONNULL ((1, 2));
828 #endif
830 #if @GNULIB_MBSNCASECMP@
831 /* Compare the initial segment of the character string S1 consisting of at most
832 N characters with the initial segment of the character string S2 consisting
833 of at most N characters, ignoring case, returning less than, equal to or
834 greater than zero if the initial segment of S1 is lexicographically less
835 than, equal to or greater than the initial segment of S2.
836 Note: This function may, in multibyte locales, return 0 for initial segments
837 of different lengths!
838 Unlike strncasecmp(), this function works correctly in multibyte locales.
839 But beware that N is not a byte count but a character count! */
840 _GL_EXTERN_C int mbsncasecmp (const char *s1, const char *s2, size_t n)
841 _GL_ATTRIBUTE_PURE
842 _GL_ARG_NONNULL ((1, 2));
843 #endif
845 #if @GNULIB_MBSPCASECMP@
846 /* Compare the initial segment of the character string STRING consisting of
847 at most mbslen (PREFIX) characters with the character string PREFIX,
848 ignoring case. If the two match, return a pointer to the first byte
849 after this prefix in STRING. Otherwise, return NULL.
850 Note: This function may, in multibyte locales, return non-NULL if STRING
851 is of smaller length than PREFIX!
852 Unlike strncasecmp(), this function works correctly in multibyte
853 locales. */
854 _GL_EXTERN_C char * mbspcasecmp (const char *string, const char *prefix)
855 _GL_ATTRIBUTE_PURE
856 _GL_ARG_NONNULL ((1, 2));
857 #endif
859 #if @GNULIB_MBSCASESTR@
860 /* Find the first occurrence of the character string NEEDLE in the character
861 string HAYSTACK, using case-insensitive comparison.
862 Note: This function may, in multibyte locales, return success even if
863 strlen (haystack) < strlen (needle) !
864 Unlike strcasestr(), this function works correctly in multibyte locales. */
865 _GL_EXTERN_C char * mbscasestr (const char *haystack, const char *needle)
866 _GL_ATTRIBUTE_PURE
867 _GL_ARG_NONNULL ((1, 2));
868 #endif
870 #if @GNULIB_MBSCSPN@
871 /* Find the first occurrence in the character string STRING of any character
872 in the character string ACCEPT. Return the number of bytes from the
873 beginning of the string to this occurrence, or to the end of the string
874 if none exists.
875 Unlike strcspn(), this function works correctly in multibyte locales. */
876 _GL_EXTERN_C size_t mbscspn (const char *string, const char *accept)
877 _GL_ATTRIBUTE_PURE
878 _GL_ARG_NONNULL ((1, 2));
879 #endif
881 #if @GNULIB_MBSPBRK@
882 /* Find the first occurrence in the character string STRING of any character
883 in the character string ACCEPT. Return the pointer to it, or NULL if none
884 exists.
885 Unlike strpbrk(), this function works correctly in multibyte locales. */
886 # if defined __hpux
887 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
888 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
889 # endif
890 _GL_FUNCDECL_RPL (mbspbrk, char *, (const char *string, const char *accept)
891 _GL_ATTRIBUTE_PURE
892 _GL_ARG_NONNULL ((1, 2)));
893 _GL_CXXALIAS_RPL (mbspbrk, char *, (const char *string, const char *accept));
894 # else
895 _GL_FUNCDECL_SYS (mbspbrk, char *, (const char *string, const char *accept)
896 _GL_ATTRIBUTE_PURE
897 _GL_ARG_NONNULL ((1, 2)));
898 _GL_CXXALIAS_SYS (mbspbrk, char *, (const char *string, const char *accept));
899 # endif
900 _GL_CXXALIASWARN (mbspbrk);
901 #endif
903 #if @GNULIB_MBSSPN@
904 /* Find the first occurrence in the character string STRING of any character
905 not in the character string REJECT. Return the number of bytes from the
906 beginning of the string to this occurrence, or to the end of the string
907 if none exists.
908 Unlike strspn(), this function works correctly in multibyte locales. */
909 _GL_EXTERN_C size_t mbsspn (const char *string, const char *reject)
910 _GL_ATTRIBUTE_PURE
911 _GL_ARG_NONNULL ((1, 2));
912 #endif
914 #if @GNULIB_MBSSEP@
915 /* Search the next delimiter (multibyte character listed in the character
916 string DELIM) starting at the character string *STRINGP.
917 If one is found, overwrite it with a NUL, and advance *STRINGP to point
918 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
919 If *STRINGP was already NULL, nothing happens.
920 Return the old value of *STRINGP.
922 This is a variant of mbstok_r() that supports empty fields.
924 Caveat: It modifies the original string.
925 Caveat: These functions cannot be used on constant strings.
926 Caveat: The identity of the delimiting character is lost.
928 See also mbstok_r(). */
929 _GL_EXTERN_C char * mbssep (char **stringp, const char *delim)
930 _GL_ARG_NONNULL ((1, 2));
931 #endif
933 #if @GNULIB_MBSTOK_R@
934 /* Parse the character string STRING into tokens separated by characters in
935 the character string DELIM.
936 If STRING is NULL, the saved pointer in SAVE_PTR is used as
937 the next starting point. For example:
938 char s[] = "-abc-=-def";
939 char *sp;
940 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
941 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
942 x = mbstok_r(NULL, "=", &sp); // x = NULL
943 // s = "abc\0-def\0"
945 Caveat: It modifies the original string.
946 Caveat: These functions cannot be used on constant strings.
947 Caveat: The identity of the delimiting character is lost.
949 See also mbssep(). */
950 _GL_EXTERN_C char * mbstok_r (char *string, const char *delim, char **save_ptr)
951 _GL_ARG_NONNULL ((2, 3));
952 #endif
954 /* Map any int, typically from errno, into an error message. */
955 #if @GNULIB_STRERROR@
956 # if @REPLACE_STRERROR@
957 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
958 # undef strerror
959 # define strerror rpl_strerror
960 # endif
961 _GL_FUNCDECL_RPL (strerror, char *, (int));
962 _GL_CXXALIAS_RPL (strerror, char *, (int));
963 # else
964 _GL_CXXALIAS_SYS (strerror, char *, (int));
965 # endif
966 _GL_CXXALIASWARN (strerror);
967 #elif defined GNULIB_POSIXCHECK
968 # undef strerror
969 /* Assume strerror is always declared. */
970 _GL_WARN_ON_USE (strerror, "strerror is unportable - "
971 "use gnulib module strerror to guarantee non-NULL result");
972 #endif
974 /* Map any int, typically from errno, into an error message. Multithread-safe.
975 Uses the POSIX declaration, not the glibc declaration. */
976 #if @GNULIB_STRERROR_R@
977 # if @REPLACE_STRERROR_R@
978 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
979 # undef strerror_r
980 # define strerror_r rpl_strerror_r
981 # endif
982 _GL_FUNCDECL_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen)
983 _GL_ARG_NONNULL ((2)));
984 _GL_CXXALIAS_RPL (strerror_r, int, (int errnum, char *buf, size_t buflen));
985 # else
986 # if !@HAVE_DECL_STRERROR_R@
987 _GL_FUNCDECL_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen)
988 _GL_ARG_NONNULL ((2)));
989 # endif
990 _GL_CXXALIAS_SYS (strerror_r, int, (int errnum, char *buf, size_t buflen));
991 # endif
992 # if @HAVE_DECL_STRERROR_R@
993 _GL_CXXALIASWARN (strerror_r);
994 # endif
995 #elif defined GNULIB_POSIXCHECK
996 # undef strerror_r
997 # if HAVE_RAW_DECL_STRERROR_R
998 _GL_WARN_ON_USE (strerror_r, "strerror_r is unportable - "
999 "use gnulib module strerror_r-posix for portability");
1000 # endif
1001 #endif
1003 #if @GNULIB_STRSIGNAL@
1004 # if @REPLACE_STRSIGNAL@
1005 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
1006 # define strsignal rpl_strsignal
1007 # endif
1008 _GL_FUNCDECL_RPL (strsignal, char *, (int __sig));
1009 _GL_CXXALIAS_RPL (strsignal, char *, (int __sig));
1010 # else
1011 # if ! @HAVE_DECL_STRSIGNAL@
1012 _GL_FUNCDECL_SYS (strsignal, char *, (int __sig));
1013 # endif
1014 /* Need to cast, because on Cygwin 1.5.x systems, the return type is
1015 'const char *'. */
1016 _GL_CXXALIAS_SYS_CAST (strsignal, char *, (int __sig));
1017 # endif
1018 _GL_CXXALIASWARN (strsignal);
1019 #elif defined GNULIB_POSIXCHECK
1020 # undef strsignal
1021 # if HAVE_RAW_DECL_STRSIGNAL
1022 _GL_WARN_ON_USE (strsignal, "strsignal is unportable - "
1023 "use gnulib module strsignal for portability");
1024 # endif
1025 #endif
1027 #if @GNULIB_STRVERSCMP@
1028 # if !@HAVE_STRVERSCMP@
1029 _GL_FUNCDECL_SYS (strverscmp, int, (const char *, const char *)
1030 _GL_ATTRIBUTE_PURE
1031 _GL_ARG_NONNULL ((1, 2)));
1032 # endif
1033 _GL_CXXALIAS_SYS (strverscmp, int, (const char *, const char *));
1034 _GL_CXXALIASWARN (strverscmp);
1035 #elif defined GNULIB_POSIXCHECK
1036 # undef strverscmp
1037 # if HAVE_RAW_DECL_STRVERSCMP
1038 _GL_WARN_ON_USE (strverscmp, "strverscmp is unportable - "
1039 "use gnulib module strverscmp for portability");
1040 # endif
1041 #endif
1044 #endif /* _@GUARD_PREFIX@_STRING_H */
1045 #endif /* _@GUARD_PREFIX@_STRING_H */
1046 #endif