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