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