1
/* A GNU-like <string.h>.
3
Copyright (C) 1995-1996, 2001-2009 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)
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, write to the Free Software Foundation,
17
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22
@PRAGMA_SYSTEM_HEADER@
25
/* The include_next requires a split double-inclusion guard. */
26
#@INCLUDE_NEXT@ @NEXT_STRING_H@
31
/* NetBSD 5.0 mis-defines NULL. */
35
/* This feature is available in gcc versions 2.5 and later. */
36
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
37
# define __attribute__(Spec) /* empty */
39
/* The attribute __pure__ was added in gcc 2.96. */
40
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
41
# define __pure__ /* empty */
46
/* The definition of GL_LINK_WARNING is copied here. */
54
/* Return the first instance of C within N bytes of S, or NULL. */
57
# define memchr rpl_memchr
58
extern void *memchr (void const *__s, int __c, size_t __n)
59
__attribute__ ((__pure__));
61
#elif defined GNULIB_POSIXCHECK
63
# define memchr(s,c,n) \
64
(GL_LINK_WARNING ("memchr has platform-specific bugs - " \
65
"use gnulib module memchr for portability" ), \
69
/* Return the first occurrence of NEEDLE in HAYSTACK. */
72
# define memmem rpl_memmem
74
# if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
75
extern void *memmem (void const *__haystack, size_t __haystack_len,
76
void const *__needle, size_t __needle_len)
77
__attribute__ ((__pure__));
79
#elif defined GNULIB_POSIXCHECK
81
# define memmem(a,al,b,bl) \
82
(GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
83
"use gnulib module memmem-simple for portability, " \
84
"and module memmem for speed" ), \
85
memmem (a, al, b, bl))
88
/* Copy N bytes of SRC to DEST, return pointer to bytes after the
92
extern void *mempcpy (void *restrict __dest, void const *restrict __src,
95
#elif defined GNULIB_POSIXCHECK
97
# define mempcpy(a,b,n) \
98
(GL_LINK_WARNING ("mempcpy is unportable - " \
99
"use gnulib module mempcpy for portability"), \
103
/* Search backwards through a block for a byte (specified as an int). */
105
# if ! @HAVE_DECL_MEMRCHR@
106
extern void *memrchr (void const *, int, size_t)
107
__attribute__ ((__pure__));
109
#elif defined GNULIB_POSIXCHECK
111
# define memrchr(a,b,c) \
112
(GL_LINK_WARNING ("memrchr is unportable - " \
113
"use gnulib module memrchr for portability"), \
117
/* Find the first occurrence of C in S. More efficient than
118
memchr(S,C,N), at the expense of undefined behavior if C does not
119
occur within N bytes. */
120
#if @GNULIB_RAWMEMCHR@
121
# if ! @HAVE_RAWMEMCHR@
122
extern void *rawmemchr (void const *__s, int __c_in)
123
__attribute__ ((__pure__));
125
#elif defined GNULIB_POSIXCHECK
127
# define rawmemchr(a,b) \
128
(GL_LINK_WARNING ("rawmemchr is unportable - " \
129
"use gnulib module rawmemchr for portability"), \
133
/* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
136
extern char *stpcpy (char *restrict __dst, char const *restrict __src);
138
#elif defined GNULIB_POSIXCHECK
140
# define stpcpy(a,b) \
141
(GL_LINK_WARNING ("stpcpy is unportable - " \
142
"use gnulib module stpcpy for portability"), \
146
/* Copy no more than N bytes of SRC to DST, returning a pointer past the
147
last non-NUL byte written into DST. */
149
# if ! @HAVE_STPNCPY@
150
# define stpncpy gnu_stpncpy
151
extern char *stpncpy (char *restrict __dst, char const *restrict __src,
154
#elif defined GNULIB_POSIXCHECK
156
# define stpncpy(a,b,n) \
157
(GL_LINK_WARNING ("stpncpy is unportable - " \
158
"use gnulib module stpncpy for portability"), \
162
#if defined GNULIB_POSIXCHECK
163
/* strchr() does not work with multibyte strings if the locale encoding is
164
GB18030 and the character to be searched is a digit. */
166
# define strchr(s,c) \
167
(GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
168
"in some multibyte locales - " \
169
"use mbschr if you care about internationalization"), \
173
/* Find the first occurrence of C in S or the final NUL byte. */
174
#if @GNULIB_STRCHRNUL@
175
# if ! @HAVE_STRCHRNUL@
176
extern char *strchrnul (char const *__s, int __c_in)
177
__attribute__ ((__pure__));
179
#elif defined GNULIB_POSIXCHECK
181
# define strchrnul(a,b) \
182
(GL_LINK_WARNING ("strchrnul is unportable - " \
183
"use gnulib module strchrnul for portability"), \
187
/* Duplicate S, returning an identical malloc'd string. */
189
# if @REPLACE_STRDUP@
191
# define strdup rpl_strdup
193
# if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
194
extern char *strdup (char const *__s);
196
#elif defined GNULIB_POSIXCHECK
199
(GL_LINK_WARNING ("strdup is unportable - " \
200
"use gnulib module strdup for portability"), \
204
/* Return a newly allocated copy of at most N bytes of STRING. */
206
# if ! @HAVE_STRNDUP@
208
# define strndup rpl_strndup
210
# if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
211
extern char *strndup (char const *__string, size_t __n);
213
#elif defined GNULIB_POSIXCHECK
215
# define strndup(a,n) \
216
(GL_LINK_WARNING ("strndup is unportable - " \
217
"use gnulib module strndup for portability"), \
221
/* Find the length (number of bytes) of STRING, but scan at most
222
MAXLEN bytes. If no '\0' terminator is found in that many bytes,
225
# if ! @HAVE_DECL_STRNLEN@
226
extern size_t strnlen (char const *__string, size_t __maxlen)
227
__attribute__ ((__pure__));
229
#elif defined GNULIB_POSIXCHECK
231
# define strnlen(a,n) \
232
(GL_LINK_WARNING ("strnlen is unportable - " \
233
"use gnulib module strnlen for portability"), \
237
#if defined GNULIB_POSIXCHECK
238
/* strcspn() assumes the second argument is a list of single-byte characters.
239
Even in this simple case, it does not work with multibyte strings if the
240
locale encoding is GB18030 and one of the characters to be searched is a
243
# define strcspn(s,a) \
244
(GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
245
"in multibyte locales - " \
246
"use mbscspn if you care about internationalization"), \
250
/* Find the first occurrence in S of any character in ACCEPT. */
252
# if ! @HAVE_STRPBRK@
253
extern char *strpbrk (char const *__s, char const *__accept)
254
__attribute__ ((__pure__));
256
# if defined GNULIB_POSIXCHECK
257
/* strpbrk() assumes the second argument is a list of single-byte characters.
258
Even in this simple case, it does not work with multibyte strings if the
259
locale encoding is GB18030 and one of the characters to be searched is a
262
# define strpbrk(s,a) \
263
(GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
264
"in multibyte locales - " \
265
"use mbspbrk if you care about internationalization"), \
268
#elif defined GNULIB_POSIXCHECK
270
# define strpbrk(s,a) \
271
(GL_LINK_WARNING ("strpbrk is unportable - " \
272
"use gnulib module strpbrk for portability"), \
276
#if defined GNULIB_POSIXCHECK
277
/* strspn() assumes the second argument is a list of single-byte characters.
278
Even in this simple case, it cannot work with multibyte strings. */
280
# define strspn(s,a) \
281
(GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
282
"in multibyte locales - " \
283
"use mbsspn if you care about internationalization"), \
287
#if defined GNULIB_POSIXCHECK
288
/* strrchr() does not work with multibyte strings if the locale encoding is
289
GB18030 and the character to be searched is a digit. */
291
# define strrchr(s,c) \
292
(GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
293
"in some multibyte locales - " \
294
"use mbsrchr if you care about internationalization"), \
298
/* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
299
If one is found, overwrite it with a NUL, and advance *STRINGP
300
to point to the next char after it. Otherwise, set *STRINGP to NULL.
301
If *STRINGP was already NULL, nothing happens.
302
Return the old value of *STRINGP.
304
This is a variant of strtok() that is multithread-safe and supports
307
Caveat: It modifies the original string.
308
Caveat: These functions cannot be used on constant strings.
309
Caveat: The identity of the delimiting character is lost.
310
Caveat: It doesn't work with multibyte strings unless all of the delimiter
311
characters are ASCII characters < 0x30.
313
See also strtok_r(). */
316
extern char *strsep (char **restrict __stringp, char const *restrict __delim);
318
# if defined GNULIB_POSIXCHECK
320
# define strsep(s,d) \
321
(GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
322
"in multibyte locales - " \
323
"use mbssep if you care about internationalization"), \
326
#elif defined GNULIB_POSIXCHECK
328
# define strsep(s,d) \
329
(GL_LINK_WARNING ("strsep is unportable - " \
330
"use gnulib module strsep for portability"), \
335
# if @REPLACE_STRSTR@
336
# define strstr rpl_strstr
337
char *strstr (const char *haystack, const char *needle)
338
__attribute__ ((__pure__));
340
#elif defined GNULIB_POSIXCHECK
341
/* strstr() does not work with multibyte strings if the locale encoding is
342
different from UTF-8:
343
POSIX says that it operates on "strings", and "string" in POSIX is defined
344
as a sequence of bytes, not of characters. */
346
# define strstr(a,b) \
347
(GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
348
"work correctly on character strings in most " \
349
"multibyte locales - " \
350
"use mbsstr if you care about internationalization, " \
351
"or use strstr if you care about speed"), \
355
/* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
357
#if @GNULIB_STRCASESTR@
358
# if @REPLACE_STRCASESTR@
359
# define strcasestr rpl_strcasestr
361
# if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
362
extern char *strcasestr (const char *haystack, const char *needle)
363
__attribute__ ((__pure__));
365
#elif defined GNULIB_POSIXCHECK
366
/* strcasestr() does not work with multibyte strings:
367
It is a glibc extension, and glibc implements it only for unibyte
370
# define strcasestr(a,b) \
371
(GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
372
"in multibyte locales - " \
373
"use mbscasestr if you care about " \
374
"internationalization, or use c-strcasestr if you want " \
375
"a locale independent function"), \
379
/* Parse S into tokens separated by characters in DELIM.
380
If S is NULL, the saved pointer in SAVE_PTR is used as
381
the next starting point. For example:
382
char s[] = "-abc-=-def";
384
x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
385
x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
386
x = strtok_r(NULL, "=", &sp); // x = NULL
389
This is a variant of strtok() that is multithread-safe.
391
For the POSIX documentation for this function, see:
392
http://www.opengroup.org/susv3xsh/strtok.html
394
Caveat: It modifies the original string.
395
Caveat: These functions cannot be used on constant strings.
396
Caveat: The identity of the delimiting character is lost.
397
Caveat: It doesn't work with multibyte strings unless all of the delimiter
398
characters are ASCII characters < 0x30.
400
See also strsep(). */
401
#if @GNULIB_STRTOK_R@
402
# if ! @HAVE_DECL_STRTOK_R@
403
extern char *strtok_r (char *restrict s, char const *restrict delim,
404
char **restrict save_ptr);
406
# if defined GNULIB_POSIXCHECK
408
# define strtok_r(s,d,p) \
409
(GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
410
"in multibyte locales - " \
411
"use mbstok_r if you care about internationalization"), \
414
#elif defined GNULIB_POSIXCHECK
416
# define strtok_r(s,d,p) \
417
(GL_LINK_WARNING ("strtok_r is unportable - " \
418
"use gnulib module strtok_r for portability"), \
423
/* The following functions are not specified by POSIX. They are gnulib
427
/* Return the number of multibyte characters in the character string STRING.
428
This considers multibyte characters, unlike strlen, which counts bytes. */
429
extern size_t mbslen (const char *string);
433
/* Return the number of multibyte characters in the character string starting
434
at STRING and ending at STRING + LEN. */
435
extern size_t mbsnlen (const char *string, size_t len);
439
/* Locate the first single-byte character C in the character string STRING,
440
and return a pointer to it. Return NULL if C is not found in STRING.
441
Unlike strchr(), this function works correctly in multibyte locales with
442
encodings such as GB18030. */
443
# define mbschr rpl_mbschr /* avoid collision with HP-UX function */
444
extern char * mbschr (const char *string, int c);
448
/* Locate the last single-byte character C in the character string STRING,
449
and return a pointer to it. Return NULL if C is not found in STRING.
450
Unlike strrchr(), this function works correctly in multibyte locales with
451
encodings such as GB18030. */
452
# define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
453
extern char * mbsrchr (const char *string, int c);
457
/* Find the first occurrence of the character string NEEDLE in the character
458
string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
459
Unlike strstr(), this function works correctly in multibyte locales with
460
encodings different from UTF-8. */
461
extern char * mbsstr (const char *haystack, const char *needle);
464
#if @GNULIB_MBSCASECMP@
465
/* Compare the character strings S1 and S2, ignoring case, returning less than,
466
equal to or greater than zero if S1 is lexicographically less than, equal to
468
Note: This function may, in multibyte locales, return 0 for strings of
470
Unlike strcasecmp(), this function works correctly in multibyte locales. */
471
extern int mbscasecmp (const char *s1, const char *s2);
474
#if @GNULIB_MBSNCASECMP@
475
/* Compare the initial segment of the character string S1 consisting of at most
476
N characters with the initial segment of the character string S2 consisting
477
of at most N characters, ignoring case, returning less than, equal to or
478
greater than zero if the initial segment of S1 is lexicographically less
479
than, equal to or greater than the initial segment of S2.
480
Note: This function may, in multibyte locales, return 0 for initial segments
481
of different lengths!
482
Unlike strncasecmp(), this function works correctly in multibyte locales.
483
But beware that N is not a byte count but a character count! */
484
extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
487
#if @GNULIB_MBSPCASECMP@
488
/* Compare the initial segment of the character string STRING consisting of
489
at most mbslen (PREFIX) characters with the character string PREFIX,
490
ignoring case, returning less than, equal to or greater than zero if this
491
initial segment is lexicographically less than, equal to or greater than
493
Note: This function may, in multibyte locales, return 0 if STRING is of
494
smaller length than PREFIX!
495
Unlike strncasecmp(), this function works correctly in multibyte
497
extern char * mbspcasecmp (const char *string, const char *prefix);
500
#if @GNULIB_MBSCASESTR@
501
/* Find the first occurrence of the character string NEEDLE in the character
502
string HAYSTACK, using case-insensitive comparison.
503
Note: This function may, in multibyte locales, return success even if
504
strlen (haystack) < strlen (needle) !
505
Unlike strcasestr(), this function works correctly in multibyte locales. */
506
extern char * mbscasestr (const char *haystack, const char *needle);
510
/* Find the first occurrence in the character string STRING of any character
511
in the character string ACCEPT. Return the number of bytes from the
512
beginning of the string to this occurrence, or to the end of the string
514
Unlike strcspn(), this function works correctly in multibyte locales. */
515
extern size_t mbscspn (const char *string, const char *accept);
519
/* Find the first occurrence in the character string STRING of any character
520
in the character string ACCEPT. Return the pointer to it, or NULL if none
522
Unlike strpbrk(), this function works correctly in multibyte locales. */
523
# define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
524
extern char * mbspbrk (const char *string, const char *accept);
528
/* Find the first occurrence in the character string STRING of any character
529
not in the character string REJECT. Return the number of bytes from the
530
beginning of the string to this occurrence, or to the end of the string
532
Unlike strspn(), this function works correctly in multibyte locales. */
533
extern size_t mbsspn (const char *string, const char *reject);
537
/* Search the next delimiter (multibyte character listed in the character
538
string DELIM) starting at the character string *STRINGP.
539
If one is found, overwrite it with a NUL, and advance *STRINGP to point
540
to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
541
If *STRINGP was already NULL, nothing happens.
542
Return the old value of *STRINGP.
544
This is a variant of mbstok_r() that supports empty fields.
546
Caveat: It modifies the original string.
547
Caveat: These functions cannot be used on constant strings.
548
Caveat: The identity of the delimiting character is lost.
550
See also mbstok_r(). */
551
extern char * mbssep (char **stringp, const char *delim);
554
#if @GNULIB_MBSTOK_R@
555
/* Parse the character string STRING into tokens separated by characters in
556
the character string DELIM.
557
If STRING is NULL, the saved pointer in SAVE_PTR is used as
558
the next starting point. For example:
559
char s[] = "-abc-=-def";
561
x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
562
x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
563
x = mbstok_r(NULL, "=", &sp); // x = NULL
566
Caveat: It modifies the original string.
567
Caveat: These functions cannot be used on constant strings.
568
Caveat: The identity of the delimiting character is lost.
570
See also mbssep(). */
571
extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
574
/* Map any int, typically from errno, into an error message. */
575
#if @GNULIB_STRERROR@
576
# if @REPLACE_STRERROR@
578
# define strerror rpl_strerror
579
extern char *strerror (int);
581
#elif defined GNULIB_POSIXCHECK
583
# define strerror(e) \
584
(GL_LINK_WARNING ("strerror is unportable - " \
585
"use gnulib module strerror to guarantee non-NULL result"), \
589
#if @GNULIB_STRSIGNAL@
590
# if @REPLACE_STRSIGNAL@
591
# define strsignal rpl_strsignal
593
# if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
594
extern char *strsignal (int __sig);
596
#elif defined GNULIB_POSIXCHECK
598
# define strsignal(a) \
599
(GL_LINK_WARNING ("strsignal is unportable - " \
600
"use gnulib module strsignal for portability"), \
604
#if @GNULIB_STRVERSCMP@
605
# if !@HAVE_STRVERSCMP@
606
extern int strverscmp (const char *, const char *);
608
#elif defined GNULIB_POSIXCHECK
610
# define strverscmp(a, b) \
611
(GL_LINK_WARNING ("strverscmp is unportable - " \
612
"use gnulib module strverscmp for portability"), \
621
#endif /* _GL_STRING_H */
622
#endif /* _GL_STRING_H */