| 1 | /* Return the offset of one string within another. |
| 2 | Copyright (C) 1994-2019 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, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* This particular implementation was written by Eric Blake, 2008. */ |
| 20 | |
| 21 | #ifndef _LIBC |
| 22 | # include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | /* Specification of strstr. */ |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include <stdbool.h> |
| 29 | |
| 30 | #ifndef _LIBC |
| 31 | # define __builtin_expect(expr, val) (expr) |
| 32 | #endif |
| 33 | |
| 34 | #define RETURN_TYPE char * |
| 35 | #define AVAILABLE(h, h_l, j, n_l) \ |
| 36 | (((j) + (n_l) <= (h_l)) \ |
| 37 | || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \ |
| 38 | (j) + (n_l) <= (h_l))) |
| 39 | #define CHECK_EOL (1) |
| 40 | #define RET0_IF_0(a) if (!a) goto ret0 |
| 41 | #define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C)) |
| 42 | #include "str-two-way.h" |
| 43 | |
| 44 | #undef strstr |
| 45 | |
| 46 | #ifndef STRSTR |
| 47 | #define STRSTR strstr |
| 48 | #endif |
| 49 | |
| 50 | /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK |
| 51 | if NEEDLE is empty, otherwise NULL if NEEDLE is not found in |
| 52 | HAYSTACK. */ |
| 53 | char * |
| 54 | STRSTR (const char *haystack, const char *needle) |
| 55 | { |
| 56 | size_t needle_len; /* Length of NEEDLE. */ |
| 57 | size_t haystack_len; /* Known minimum length of HAYSTACK. */ |
| 58 | |
| 59 | /* Handle empty NEEDLE special case. */ |
| 60 | if (needle[0] == '\0') |
| 61 | return (char *) haystack; |
| 62 | |
| 63 | /* Skip until we find the first matching char from NEEDLE. */ |
| 64 | haystack = strchr (haystack, needle[0]); |
| 65 | if (haystack == NULL || needle[1] == '\0') |
| 66 | return (char *) haystack; |
| 67 | |
| 68 | /* Ensure HAYSTACK length is at least as long as NEEDLE length. |
| 69 | Since a match may occur early on in a huge HAYSTACK, use strnlen |
| 70 | and read ahead a few cachelines for improved performance. */ |
| 71 | needle_len = strlen (needle); |
| 72 | haystack_len = __strnlen (haystack, needle_len + 256); |
| 73 | if (haystack_len < needle_len) |
| 74 | return NULL; |
| 75 | |
| 76 | /* Check whether we have a match. This improves performance since we avoid |
| 77 | the initialization overhead of the two-way algorithm. */ |
| 78 | if (memcmp (haystack, needle, needle_len) == 0) |
| 79 | return (char *) haystack; |
| 80 | |
| 81 | /* Perform the search. Abstract memory is considered to be an array |
| 82 | of 'unsigned char' values, not an array of 'char' values. See |
| 83 | ISO C 99 section 6.2.6.1. */ |
| 84 | if (needle_len < LONG_NEEDLE_THRESHOLD) |
| 85 | return two_way_short_needle ((const unsigned char *) haystack, |
| 86 | haystack_len, |
| 87 | (const unsigned char *) needle, needle_len); |
| 88 | return two_way_long_needle ((const unsigned char *) haystack, haystack_len, |
| 89 | (const unsigned char *) needle, needle_len); |
| 90 | } |
| 91 | libc_hidden_builtin_def (strstr) |
| 92 | |
| 93 | #undef LONG_NEEDLE_THRESHOLD |
| 94 | |