Disable x87 inline functions for x86-64
[glibc.git] / string / strstr.c
blob10e6fdc7f98cc3cc4f5ae27fb05e6098cf2e348a
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.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 /* This particular implementation was written by Eric Blake, 2008. */
22 #ifndef _LIBC
23 # include <config.h>
24 #endif
26 /* Specification of strstr. */
27 #include <string.h>
29 #include <stdbool.h>
31 #ifndef _LIBC
32 # define __builtin_expect(expr, val) (expr)
33 #endif
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"
41 #undef strstr
43 #ifndef STRSTR
44 #define STRSTR strstr
45 #endif
47 /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
48 if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
49 HAYSTACK. */
50 char *
51 STRSTR (const char *haystack_start, const char *needle_start)
53 const char *haystack = haystack_start;
54 const char *needle = needle_start;
55 size_t needle_len; /* Length of NEEDLE. */
56 size_t haystack_len; /* Known minimum length of HAYSTACK. */
57 bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
59 /* Determine length of NEEDLE, and in the process, make sure
60 HAYSTACK is at least as long (no point processing all of a long
61 NEEDLE if HAYSTACK is too short). */
62 while (*haystack && *needle)
63 ok &= *haystack++ == *needle++;
64 if (*needle)
65 return NULL;
66 if (ok)
67 return (char *) haystack_start;
69 /* Reduce the size of haystack using strchr, since it has a smaller
70 linear coefficient than the Two-Way algorithm. */
71 needle_len = needle - needle_start;
72 haystack = strchr (haystack_start + 1, *needle_start);
73 if (!haystack || __builtin_expect (needle_len == 1, 0))
74 return (char *) haystack;
75 needle -= needle_len;
76 haystack_len = (haystack > haystack_start + needle_len ? 1
77 : needle_len + haystack_start - haystack);
79 /* Perform the search. Abstract memory is considered to be an array
80 of 'unsigned char' values, not an array of 'char' values. See
81 ISO C 99 section 6.2.6.1. */
82 if (needle_len < LONG_NEEDLE_THRESHOLD)
83 return two_way_short_needle ((const unsigned char *) haystack,
84 haystack_len,
85 (const unsigned char *) needle, needle_len);
86 return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
87 (const unsigned char *) needle, needle_len);
89 libc_hidden_builtin_def (strstr)
91 #undef LONG_NEEDLE_THRESHOLD