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