1 /* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2011 Free Software
3 This file is part of the GNU C Library.
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)
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 along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* This particular implementation was written by Eric Blake, 2008. */
25 /* Specification of strstr. */
31 # define __builtin_expect(expr, val) (expr)
34 #define RETURN_TYPE char *
35 #define AVAILABLE(h, h_l, j, n_l) \
36 (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
37 && ((h_l) = (j) + (n_l)))
38 #include "str-two-way.h"
40 /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
41 if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
44 strstr (const char *haystack_start, const char *needle_start)
46 const char *haystack = haystack_start;
47 const char *needle = needle_start;
48 size_t needle_len; /* Length of NEEDLE. */
49 size_t haystack_len; /* Known minimum length of HAYSTACK. */
50 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
52 /* Determine length of NEEDLE, and in the process, make sure
53 HAYSTACK is at least as long (no point processing all of a long
54 NEEDLE if HAYSTACK is too short). */
55 while (*haystack && *needle)
56 ok &= *haystack++ == *needle++;
60 return (char *) haystack_start;
62 /* Reduce the size of haystack using strchr, since it has a smaller
63 linear coefficient than the Two-Way algorithm. */
64 needle_len = needle - needle_start;
65 haystack = strchr (haystack_start + 1, *needle_start);
66 if (!haystack || __builtin_expect (needle_len == 1, 0))
67 return (char *) haystack;
69 haystack_len = (haystack > haystack_start + needle_len ? 1
70 : needle_len + haystack_start - haystack);
72 /* Perform the search. Abstract memory is considered to be an array
73 of 'unsigned char' values, not an array of 'char' values. See
74 ISO C 99 section 6.2.6.1. */
75 if (needle_len < LONG_NEEDLE_THRESHOLD)
76 return two_way_short_needle ((const unsigned char *) haystack,
78 (const unsigned char *) needle, needle_len);
79 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
80 (const unsigned char *) needle, needle_len);
83 #undef LONG_NEEDLE_THRESHOLD