1 /* A GNU-like <string.h>.
3 Copyright (C) 1995-1996, 2001-2008 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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 #ifndef _GL_STRING_H
21 #if __GNUC__ >= 3
22 @PRAGMA_SYSTEM_HEADER@
23 #endif
25 /* The include_next requires a split double-inclusion guard. */
26 #@INCLUDE_NEXT@ @NEXT_STRING_H@
28 #ifndef _GL_STRING_H
29 #define _GL_STRING_H
32 #ifndef __attribute__
33 /* This feature is available in gcc versions 2.5 and later. */
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
35 # define __attribute__(Spec) /* empty */
36 # endif
37 /* The attribute __pure__ was added in gcc 2.96. */
38 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
39 # define __pure__ /* empty */
40 # endif
41 #endif
44 /* The definition of GL_LINK_WARNING is copied here. */
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
52 /* Return the first occurrence of NEEDLE in HAYSTACK. */
53 #if @GNULIB_MEMMEM@
54 # if @REPLACE_MEMMEM@
55 # define memmem rpl_memmem
56 # endif
57 # if ! @HAVE_DECL_MEMMEM@ || @REPLACE_MEMMEM@
58 extern void *memmem (void const *__haystack, size_t __haystack_len,
59 void const *__needle, size_t __needle_len)
60 __attribute__ ((__pure__));
61 # endif
62 #elif defined GNULIB_POSIXCHECK
63 # undef memmem
64 # define memmem(a,al,b,bl) \
65 (GL_LINK_WARNING ("memmem is unportable and often quadratic - " \
66 "use gnulib module memmem-simple for portability, " \
67 "and module memmem for speed" ), \
68 memmem (a, al, b, bl))
69 #endif
71 /* Copy N bytes of SRC to DEST, return pointer to bytes after the
72 last written byte. */
73 #if @GNULIB_MEMPCPY@
74 # if ! @HAVE_MEMPCPY@
75 extern void *mempcpy (void *restrict __dest, void const *restrict __src,
76 size_t __n);
77 # endif
78 #elif defined GNULIB_POSIXCHECK
79 # undef mempcpy
80 # define mempcpy(a,b,n) \
81 (GL_LINK_WARNING ("mempcpy is unportable - " \
82 "use gnulib module mempcpy for portability"), \
83 mempcpy (a, b, n))
84 #endif
86 /* Search backwards through a block for a byte (specified as an int). */
87 #if @GNULIB_MEMRCHR@
88 # if ! @HAVE_DECL_MEMRCHR@
89 extern void *memrchr (void const *, int, size_t)
90 __attribute__ ((__pure__));
91 # endif
92 #elif defined GNULIB_POSIXCHECK
93 # undef memrchr
94 # define memrchr(a,b,c) \
95 (GL_LINK_WARNING ("memrchr is unportable - " \
96 "use gnulib module memrchr for portability"), \
97 memrchr (a, b, c))
98 #endif
100 /* Find the first occurrence of C in S. More efficient than
101 memchr(S,C,N), at the expense of undefined behavior if C does not
102 occur within N bytes. */
103 #if @GNULIB_RAWMEMCHR@
104 # if ! @HAVE_RAWMEMCHR@
105 extern void *rawmemchr (void const *__s, int __c_in)
106 __attribute__ ((__pure__));
107 # endif
108 #elif defined GNULIB_POSIXCHECK
109 # undef rawmemchr
110 # define rawmemchr(a,b) \
111 (GL_LINK_WARNING ("rawmemchr is unportable - " \
112 "use gnulib module rawmemchr for portability"), \
113 rawmemchr (a, b))
114 #endif
116 /* Copy SRC to DST, returning the address of the terminating '\0' in DST. */
117 #if @GNULIB_STPCPY@
118 # if ! @HAVE_STPCPY@
119 extern char *stpcpy (char *restrict __dst, char const *restrict __src);
120 # endif
121 #elif defined GNULIB_POSIXCHECK
122 # undef stpcpy
123 # define stpcpy(a,b) \
124 (GL_LINK_WARNING ("stpcpy is unportable - " \
125 "use gnulib module stpcpy for portability"), \
126 stpcpy (a, b))
127 #endif
129 /* Copy no more than N bytes of SRC to DST, returning a pointer past the
130 last non-NUL byte written into DST. */
131 #if @GNULIB_STPNCPY@
132 # if ! @HAVE_STPNCPY@
133 # define stpncpy gnu_stpncpy
134 extern char *stpncpy (char *restrict __dst, char const *restrict __src,
135 size_t __n);
136 # endif
137 #elif defined GNULIB_POSIXCHECK
138 # undef stpncpy
139 # define stpncpy(a,b,n) \
140 (GL_LINK_WARNING ("stpncpy is unportable - " \
141 "use gnulib module stpncpy for portability"), \
142 stpncpy (a, b, n))
143 #endif
145 #if defined GNULIB_POSIXCHECK
146 /* strchr() does not work with multibyte strings if the locale encoding is
147 GB18030 and the character to be searched is a digit. */
148 # undef strchr
149 # define strchr(s,c) \
150 (GL_LINK_WARNING ("strchr cannot work correctly on character strings " \
151 "in some multibyte locales - " \
152 "use mbschr if you care about internationalization"), \
153 strchr (s, c))
154 #endif
156 /* Find the first occurrence of C in S or the final NUL byte. */
157 #if @GNULIB_STRCHRNUL@
158 # if ! @HAVE_STRCHRNUL@
159 extern char *strchrnul (char const *__s, int __c_in)
160 __attribute__ ((__pure__));
161 # endif
162 #elif defined GNULIB_POSIXCHECK
163 # undef strchrnul
164 # define strchrnul(a,b) \
165 (GL_LINK_WARNING ("strchrnul is unportable - " \
166 "use gnulib module strchrnul for portability"), \
167 strchrnul (a, b))
168 #endif
170 /* Duplicate S, returning an identical malloc'd string. */
171 #if @GNULIB_STRDUP@
172 # if @REPLACE_STRDUP@
173 # undef strdup
174 # define strdup rpl_strdup
175 # endif
176 # if !(@HAVE_DECL_STRDUP@ || defined strdup) || @REPLACE_STRDUP@
177 extern char *strdup (char const *__s);
178 # endif
179 #elif defined GNULIB_POSIXCHECK
180 # undef strdup
181 # define strdup(a) \
182 (GL_LINK_WARNING ("strdup is unportable - " \
183 "use gnulib module strdup for portability"), \
184 strdup (a))
185 #endif
187 /* Return a newly allocated copy of at most N bytes of STRING. */
188 #if @GNULIB_STRNDUP@
189 # if ! @HAVE_STRNDUP@
190 # undef strndup
191 # define strndup rpl_strndup
192 # endif
193 # if ! @HAVE_STRNDUP@ || ! @HAVE_DECL_STRNDUP@
194 extern char *strndup (char const *__string, size_t __n);
195 # endif
196 #elif defined GNULIB_POSIXCHECK
197 # undef strndup
198 # define strndup(a,n) \
199 (GL_LINK_WARNING ("strndup is unportable - " \
200 "use gnulib module strndup for portability"), \
201 strndup (a, n))
202 #endif
204 /* Find the length (number of bytes) of STRING, but scan at most
205 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
206 return MAXLEN. */
207 #if @GNULIB_STRNLEN@
208 # if ! @HAVE_DECL_STRNLEN@
209 extern size_t strnlen (char const *__string, size_t __maxlen)
210 __attribute__ ((__pure__));
211 # endif
212 #elif defined GNULIB_POSIXCHECK
213 # undef strnlen
214 # define strnlen(a,n) \
215 (GL_LINK_WARNING ("strnlen is unportable - " \
216 "use gnulib module strnlen for portability"), \
217 strnlen (a, n))
218 #endif
220 #if defined GNULIB_POSIXCHECK
221 /* strcspn() assumes the second argument is a list of single-byte characters.
222 Even in this simple case, it does not work with multibyte strings if the
223 locale encoding is GB18030 and one of the characters to be searched is a
224 digit. */
225 # undef strcspn
226 # define strcspn(s,a) \
227 (GL_LINK_WARNING ("strcspn cannot work correctly on character strings " \
228 "in multibyte locales - " \
229 "use mbscspn if you care about internationalization"), \
230 strcspn (s, a))
231 #endif
233 /* Find the first occurrence in S of any character in ACCEPT. */
234 #if @GNULIB_STRPBRK@
235 # if ! @HAVE_STRPBRK@
236 extern char *strpbrk (char const *__s, char const *__accept)
237 __attribute__ ((__pure__));
238 # endif
239 # if defined GNULIB_POSIXCHECK
240 /* strpbrk() assumes the second argument is a list of single-byte characters.
241 Even in this simple case, it does not work with multibyte strings if the
242 locale encoding is GB18030 and one of the characters to be searched is a
243 digit. */
244 # undef strpbrk
245 # define strpbrk(s,a) \
246 (GL_LINK_WARNING ("strpbrk cannot work correctly on character strings " \
247 "in multibyte locales - " \
248 "use mbspbrk if you care about internationalization"), \
249 strpbrk (s, a))
250 # endif
251 #elif defined GNULIB_POSIXCHECK
252 # undef strpbrk
253 # define strpbrk(s,a) \
254 (GL_LINK_WARNING ("strpbrk is unportable - " \
255 "use gnulib module strpbrk for portability"), \
256 strpbrk (s, a))
257 #endif
259 #if defined GNULIB_POSIXCHECK
260 /* strspn() assumes the second argument is a list of single-byte characters.
261 Even in this simple case, it cannot work with multibyte strings. */
262 # undef strspn
263 # define strspn(s,a) \
264 (GL_LINK_WARNING ("strspn cannot work correctly on character strings " \
265 "in multibyte locales - " \
266 "use mbsspn if you care about internationalization"), \
267 strspn (s, a))
268 #endif
270 #if defined GNULIB_POSIXCHECK
271 /* strrchr() does not work with multibyte strings if the locale encoding is
272 GB18030 and the character to be searched is a digit. */
273 # undef strrchr
274 # define strrchr(s,c) \
275 (GL_LINK_WARNING ("strrchr cannot work correctly on character strings " \
276 "in some multibyte locales - " \
277 "use mbsrchr if you care about internationalization"), \
278 strrchr (s, c))
279 #endif
281 /* Search the next delimiter (char listed in DELIM) starting at *STRINGP.
282 If one is found, overwrite it with a NUL, and advance *STRINGP
283 to point to the next char after it. Otherwise, set *STRINGP to NULL.
284 If *STRINGP was already NULL, nothing happens.
285 Return the old value of *STRINGP.
287 This is a variant of strtok() that is multithread-safe and supports
288 empty fields.
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.
296 See also strtok_r(). */
297 #if @GNULIB_STRSEP@
298 # if ! @HAVE_STRSEP@
299 extern char *strsep (char **restrict __stringp, char const *restrict __delim);
300 # endif
301 # if defined GNULIB_POSIXCHECK
302 # undef strsep
303 # define strsep(s,d) \
304 (GL_LINK_WARNING ("strsep cannot work correctly on character strings " \
305 "in multibyte locales - " \
306 "use mbssep if you care about internationalization"), \
307 strsep (s, d))
308 # endif
309 #elif defined GNULIB_POSIXCHECK
310 # undef strsep
311 # define strsep(s,d) \
312 (GL_LINK_WARNING ("strsep is unportable - " \
313 "use gnulib module strsep for portability"), \
314 strsep (s, d))
315 #endif
317 #if @GNULIB_STRSTR@
318 # if @REPLACE_STRSTR@
319 # define strstr rpl_strstr
320 char *strstr (const char *haystack, const char *needle)
321 __attribute__ ((__pure__));
322 # endif
323 #elif defined GNULIB_POSIXCHECK
324 /* strstr() does not work with multibyte strings if the locale encoding is
325 different from UTF-8:
326 POSIX says that it operates on "strings", and "string" in POSIX is defined
327 as a sequence of bytes, not of characters. */
328 # undef strstr
329 # define strstr(a,b) \
330 (GL_LINK_WARNING ("strstr is quadratic on many systems, and cannot " \
331 "work correctly on character strings in most " \
332 "multibyte locales - " \
333 "use mbsstr if you care about internationalization, " \
334 "or use strstr if you care about speed"), \
335 strstr (a, b))
336 #endif
338 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
339 comparison. */
340 #if @GNULIB_STRCASESTR@
341 # if @REPLACE_STRCASESTR@
342 # define strcasestr rpl_strcasestr
343 # endif
344 # if ! @HAVE_STRCASESTR@ || @REPLACE_STRCASESTR@
345 extern char *strcasestr (const char *haystack, const char *needle)
346 __attribute__ ((__pure__));
347 # endif
348 #elif defined GNULIB_POSIXCHECK
349 /* strcasestr() does not work with multibyte strings:
350 It is a glibc extension, and glibc implements it only for unibyte
351 locales. */
352 # undef strcasestr
353 # define strcasestr(a,b) \
354 (GL_LINK_WARNING ("strcasestr does work correctly on character strings " \
355 "in multibyte locales - " \
356 "use mbscasestr if you care about " \
357 "internationalization, or use c-strcasestr if you want " \
358 "a locale independent function"), \
359 strcasestr (a, b))
360 #endif
362 /* Parse S into tokens separated by characters in DELIM.
363 If S is NULL, the saved pointer in SAVE_PTR is used as
364 the next starting point. For example:
365 char s[] = "-abc-=-def";
366 char *sp;
367 x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def"
368 x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL
369 x = strtok_r(NULL, "=", &sp); // x = NULL
370 // s = "abc\0-def\0"
372 This is a variant of strtok() that is multithread-safe.
374 For the POSIX documentation for this function, see:
375 http://www.opengroup.org/susv3xsh/strtok.html
377 Caveat: It modifies the original string.
378 Caveat: These functions cannot be used on constant strings.
379 Caveat: The identity of the delimiting character is lost.
380 Caveat: It doesn't work with multibyte strings unless all of the delimiter
381 characters are ASCII characters < 0x30.
383 See also strsep(). */
384 #if @GNULIB_STRTOK_R@
385 # if ! @HAVE_DECL_STRTOK_R@
386 extern char *strtok_r (char *restrict s, char const *restrict delim,
387 char **restrict save_ptr);
388 # endif
389 # if defined GNULIB_POSIXCHECK
390 # undef strtok_r
391 # define strtok_r(s,d,p) \
392 (GL_LINK_WARNING ("strtok_r cannot work correctly on character strings " \
393 "in multibyte locales - " \
394 "use mbstok_r if you care about internationalization"), \
395 strtok_r (s, d, p))
396 # endif
397 #elif defined GNULIB_POSIXCHECK
398 # undef strtok_r
399 # define strtok_r(s,d,p) \
400 (GL_LINK_WARNING ("strtok_r is unportable - " \
401 "use gnulib module strtok_r for portability"), \
402 strtok_r (s, d, p))
403 #endif
406 /* The following functions are not specified by POSIX. They are gnulib
407 extensions. */
409 #if @GNULIB_MBSLEN@
410 /* Return the number of multibyte characters in the character string STRING.
411 This considers multibyte characters, unlike strlen, which counts bytes. */
412 extern size_t mbslen (const char *string);
413 #endif
415 #if @GNULIB_MBSNLEN@
416 /* Return the number of multibyte characters in the character string starting
417 at STRING and ending at STRING + LEN. */
418 extern size_t mbsnlen (const char *string, size_t len);
419 #endif
421 #if @GNULIB_MBSCHR@
422 /* Locate the first single-byte character C in the character string STRING,
423 and return a pointer to it. Return NULL if C is not found in STRING.
424 Unlike strchr(), this function works correctly in multibyte locales with
425 encodings such as GB18030. */
426 # define mbschr rpl_mbschr /* avoid collision with HP-UX function */
427 extern char * mbschr (const char *string, int c);
428 #endif
430 #if @GNULIB_MBSRCHR@
431 /* Locate the last single-byte character C in the character string STRING,
432 and return a pointer to it. Return NULL if C is not found in STRING.
433 Unlike strrchr(), this function works correctly in multibyte locales with
434 encodings such as GB18030. */
435 # define mbsrchr rpl_mbsrchr /* avoid collision with HP-UX function */
436 extern char * mbsrchr (const char *string, int c);
437 #endif
439 #if @GNULIB_MBSSTR@
440 /* Find the first occurrence of the character string NEEDLE in the character
441 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK.
442 Unlike strstr(), this function works correctly in multibyte locales with
443 encodings different from UTF-8. */
444 extern char * mbsstr (const char *haystack, const char *needle);
445 #endif
447 #if @GNULIB_MBSCASECMP@
448 /* Compare the character strings S1 and S2, ignoring case, returning less than,
449 equal to or greater than zero if S1 is lexicographically less than, equal to
450 or greater than S2.
451 Note: This function may, in multibyte locales, return 0 for strings of
452 different lengths!
453 Unlike strcasecmp(), this function works correctly in multibyte locales. */
454 extern int mbscasecmp (const char *s1, const char *s2);
455 #endif
457 #if @GNULIB_MBSNCASECMP@
458 /* Compare the initial segment of the character string S1 consisting of at most
459 N characters with the initial segment of the character string S2 consisting
460 of at most N characters, ignoring case, returning less than, equal to or
461 greater than zero if the initial segment of S1 is lexicographically less
462 than, equal to or greater than the initial segment of S2.
463 Note: This function may, in multibyte locales, return 0 for initial segments
464 of different lengths!
465 Unlike strncasecmp(), this function works correctly in multibyte locales.
466 But beware that N is not a byte count but a character count! */
467 extern int mbsncasecmp (const char *s1, const char *s2, size_t n);
468 #endif
470 #if @GNULIB_MBSPCASECMP@
471 /* Compare the initial segment of the character string STRING consisting of
472 at most mbslen (PREFIX) characters with the character string PREFIX,
473 ignoring case, returning less than, equal to or greater than zero if this
474 initial segment is lexicographically less than, equal to or greater than
475 PREFIX.
476 Note: This function may, in multibyte locales, return 0 if STRING is of
477 smaller length than PREFIX!
478 Unlike strncasecmp(), this function works correctly in multibyte
479 locales. */
480 extern char * mbspcasecmp (const char *string, const char *prefix);
481 #endif
483 #if @GNULIB_MBSCASESTR@
484 /* Find the first occurrence of the character string NEEDLE in the character
485 string HAYSTACK, using case-insensitive comparison.
486 Note: This function may, in multibyte locales, return success even if
487 strlen (haystack) < strlen (needle) !
488 Unlike strcasestr(), this function works correctly in multibyte locales. */
489 extern char * mbscasestr (const char *haystack, const char *needle);
490 #endif
492 #if @GNULIB_MBSCSPN@
493 /* Find the first occurrence in the character string STRING of any character
494 in the character string ACCEPT. Return the number of bytes from the
495 beginning of the string to this occurrence, or to the end of the string
496 if none exists.
497 Unlike strcspn(), this function works correctly in multibyte locales. */
498 extern size_t mbscspn (const char *string, const char *accept);
499 #endif
501 #if @GNULIB_MBSPBRK@
502 /* Find the first occurrence in the character string STRING of any character
503 in the character string ACCEPT. Return the pointer to it, or NULL if none
504 exists.
505 Unlike strpbrk(), this function works correctly in multibyte locales. */
506 # define mbspbrk rpl_mbspbrk /* avoid collision with HP-UX function */
507 extern char * mbspbrk (const char *string, const char *accept);
508 #endif
510 #if @GNULIB_MBSSPN@
511 /* Find the first occurrence in the character string STRING of any character
512 not in the character string REJECT. Return the number of bytes from the
513 beginning of the string to this occurrence, or to the end of the string
514 if none exists.
515 Unlike strspn(), this function works correctly in multibyte locales. */
516 extern size_t mbsspn (const char *string, const char *reject);
517 #endif
519 #if @GNULIB_MBSSEP@
520 /* Search the next delimiter (multibyte character listed in the character
521 string DELIM) starting at the character string *STRINGP.
522 If one is found, overwrite it with a NUL, and advance *STRINGP to point
523 to the next multibyte character after it. Otherwise, set *STRINGP to NULL.
524 If *STRINGP was already NULL, nothing happens.
525 Return the old value of *STRINGP.
527 This is a variant of mbstok_r() that supports empty fields.
529 Caveat: It modifies the original string.
530 Caveat: These functions cannot be used on constant strings.
531 Caveat: The identity of the delimiting character is lost.
533 See also mbstok_r(). */
534 extern char * mbssep (char **stringp, const char *delim);
535 #endif
537 #if @GNULIB_MBSTOK_R@
538 /* Parse the character string STRING into tokens separated by characters in
539 the character string DELIM.
540 If STRING is NULL, the saved pointer in SAVE_PTR is used as
541 the next starting point. For example:
542 char s[] = "-abc-=-def";
543 char *sp;
544 x = mbstok_r(s, "-", &sp); // x = "abc", sp = "=-def"
545 x = mbstok_r(NULL, "-=", &sp); // x = "def", sp = NULL
546 x = mbstok_r(NULL, "=", &sp); // x = NULL
547 // s = "abc\0-def\0"
549 Caveat: It modifies the original string.
550 Caveat: These functions cannot be used on constant strings.
551 Caveat: The identity of the delimiting character is lost.
553 See also mbssep(). */
554 extern char * mbstok_r (char *string, const char *delim, char **save_ptr);
555 #endif
557 /* Map any int, typically from errno, into an error message. */
558 #if @GNULIB_STRERROR@
559 # if @REPLACE_STRERROR@
560 # undef strerror
561 # define strerror rpl_strerror
562 extern char *strerror (int);
563 # endif
564 #elif defined GNULIB_POSIXCHECK
565 # undef strerror
566 # define strerror(e) \
567 (GL_LINK_WARNING ("strerror is unportable - " \
568 "use gnulib module strerror to guarantee non-NULL result"), \
569 strerror (e))
570 #endif
572 #if @GNULIB_STRSIGNAL@
573 # if @REPLACE_STRSIGNAL@
574 # define strsignal rpl_strsignal
575 # endif
576 # if ! @HAVE_DECL_STRSIGNAL@ || @REPLACE_STRSIGNAL@
577 extern char *strsignal (int __sig);
578 # endif
579 #elif defined GNULIB_POSIXCHECK
580 # undef strsignal
581 # define strsignal(a) \
582 (GL_LINK_WARNING ("strsignal is unportable - " \
583 "use gnulib module strsignal for portability"), \
584 strsignal (a))
585 #endif
587 #if @GNULIB_STRVERSCMP@
588 # if !@HAVE_STRVERSCMP@
589 extern int strverscmp (const char *, const char *);
590 # endif
591 #elif defined GNULIB_POSIXCHECK
592 # undef strverscmp
593 # define strverscmp(a, b) \
594 (GL_LINK_WARNING ("strverscmp is unportable - " \
595 "use gnulib module strverscmp for portability"), \
596 strverscmp (a, b))
597 #endif
600 #ifdef __cplusplus
601 }
602 #endif
604 #endif /* _GL_STRING_H */
605 #endif /* _GL_STRING_H */