|
Packit |
6c4009 |
/* Return the offset of one string within another.
|
|
Packit |
6c4009 |
Copyright (C) 1994-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This particular implementation was written by Eric Blake, 2008. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef _LIBC
|
|
Packit |
6c4009 |
# include <config.h>
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Specification of strstr. */
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef _LIBC
|
|
Packit |
6c4009 |
# define __builtin_expect(expr, val) (expr)
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define RETURN_TYPE char *
|
|
Packit |
6c4009 |
#define AVAILABLE(h, h_l, j, n_l) \
|
|
Packit |
051679 |
(((j) + (n_l) <= (h_l)) \
|
|
Packit |
051679 |
|| ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
|
|
Packit |
051679 |
(j) + (n_l) <= (h_l)))
|
|
Packit |
6c4009 |
#define CHECK_EOL (1)
|
|
Packit |
6c4009 |
#define RET0_IF_0(a) if (!a) goto ret0
|
|
Packit |
6c4009 |
#define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
|
|
Packit |
6c4009 |
#include "str-two-way.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#undef strstr
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef STRSTR
|
|
Packit |
6c4009 |
#define STRSTR strstr
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
|
|
Packit |
6c4009 |
if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
|
|
Packit |
6c4009 |
HAYSTACK. */
|
|
Packit |
6c4009 |
char *
|
|
Packit |
6c4009 |
STRSTR (const char *haystack_start, const char *needle_start)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const char *haystack = haystack_start;
|
|
Packit |
6c4009 |
const char *needle = needle_start;
|
|
Packit |
6c4009 |
size_t needle_len; /* Length of NEEDLE. */
|
|
Packit |
6c4009 |
size_t haystack_len; /* Known minimum length of HAYSTACK. */
|
|
Packit |
6c4009 |
bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Determine length of NEEDLE, and in the process, make sure
|
|
Packit |
6c4009 |
HAYSTACK is at least as long (no point processing all of a long
|
|
Packit |
6c4009 |
NEEDLE if HAYSTACK is too short). */
|
|
Packit |
6c4009 |
while (*haystack && *needle)
|
|
Packit |
6c4009 |
ok &= *haystack++ == *needle++;
|
|
Packit |
6c4009 |
if (*needle)
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
if (ok)
|
|
Packit |
6c4009 |
return (char *) haystack_start;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Reduce the size of haystack using strchr, since it has a smaller
|
|
Packit |
6c4009 |
linear coefficient than the Two-Way algorithm. */
|
|
Packit |
6c4009 |
needle_len = needle - needle_start;
|
|
Packit |
6c4009 |
haystack = strchr (haystack_start + 1, *needle_start);
|
|
Packit |
6c4009 |
if (!haystack || __builtin_expect (needle_len == 1, 0))
|
|
Packit |
6c4009 |
return (char *) haystack;
|
|
Packit |
6c4009 |
needle -= needle_len;
|
|
Packit |
6c4009 |
haystack_len = (haystack > haystack_start + needle_len ? 1
|
|
Packit |
6c4009 |
: needle_len + haystack_start - haystack);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Perform the search. Abstract memory is considered to be an array
|
|
Packit |
6c4009 |
of 'unsigned char' values, not an array of 'char' values. See
|
|
Packit |
6c4009 |
ISO C 99 section 6.2.6.1. */
|
|
Packit |
6c4009 |
if (needle_len < LONG_NEEDLE_THRESHOLD)
|
|
Packit |
6c4009 |
return two_way_short_needle ((const unsigned char *) haystack,
|
|
Packit |
6c4009 |
haystack_len,
|
|
Packit |
6c4009 |
(const unsigned char *) needle, needle_len);
|
|
Packit |
6c4009 |
return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
|
|
Packit |
6c4009 |
(const unsigned char *) needle, needle_len);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
libc_hidden_builtin_def (strstr)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#undef LONG_NEEDLE_THRESHOLD
|