chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / string / strstr.c
1 /* Return the offset of one string within another.
2    Copyright (C) 1994,1996,1997,2000,2001,2003,2008,2009
3    Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* This particular implementation was written by Eric Blake, 2008.  */
22
23 #ifndef _LIBC
24 # include <config.h>
25 #endif
26
27 /* Specification of strstr.  */
28 #include <string.h>
29
30 #include <stdbool.h>
31
32 #ifndef _LIBC
33 # define __builtin_expect(expr, val)   (expr)
34 #endif
35
36 #define RETURN_TYPE char *
37 #define AVAILABLE(h, h_l, j, n_l)                       \
38   (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
39    && ((h_l) = (j) + (n_l)))
40 #include "str-two-way.h"
41
42 #undef strstr
43
44 #ifndef STRSTR
45 #define STRSTR strstr
46 #endif
47
48 /* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
49    if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
50    HAYSTACK.  */
51 char *
52 STRSTR (const char *haystack_start, const char *needle_start)
53 {
54   const char *haystack = haystack_start;
55   const char *needle = needle_start;
56   size_t needle_len; /* Length of NEEDLE.  */
57   size_t haystack_len; /* Known minimum length of HAYSTACK.  */
58   bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
59
60   /* Determine length of NEEDLE, and in the process, make sure
61      HAYSTACK is at least as long (no point processing all of a long
62      NEEDLE if HAYSTACK is too short).  */
63   while (*haystack && *needle)
64     ok &= *haystack++ == *needle++;
65   if (*needle)
66     return NULL;
67   if (ok)
68     return (char *) haystack_start;
69
70   /* Reduce the size of haystack using strchr, since it has a smaller
71      linear coefficient than the Two-Way algorithm.  */
72   needle_len = needle - needle_start;
73   haystack = strchr (haystack_start + 1, *needle_start);
74   if (!haystack || __builtin_expect (needle_len == 1, 0))
75     return (char *) haystack;
76   needle -= needle_len;
77   haystack_len = (haystack > haystack_start + needle_len ? 1
78                   : needle_len + haystack_start - haystack);
79
80   /* Perform the search.  Abstract memory is considered to be an array
81      of 'unsigned char' values, not an array of 'char' values.  See
82      ISO C 99 section 6.2.6.1.  */
83   if (needle_len < LONG_NEEDLE_THRESHOLD)
84     return two_way_short_needle ((const unsigned char *) haystack,
85                                  haystack_len,
86                                  (const unsigned char *) needle, needle_len);
87   return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
88                               (const unsigned char *) needle, needle_len);
89 }
90 libc_hidden_builtin_def (strstr)
91
92 #undef LONG_NEEDLE_THRESHOLD