2000-02-18 03:39:52 +08:00
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<strstr>>---find string segment
|
|
|
|
|
|
|
|
INDEX
|
|
|
|
strstr
|
|
|
|
|
2017-11-30 16:20:06 +08:00
|
|
|
SYNOPSIS
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <string.h>
|
|
|
|
char *strstr(const char *<[s1]>, const char *<[s2]>);
|
|
|
|
|
|
|
|
DESCRIPTION
|
2008-01-12 12:25:55 +08:00
|
|
|
Locates the first occurrence in the string pointed to by <[s1]> of
|
2000-02-18 03:39:52 +08:00
|
|
|
the sequence of characters in the string pointed to by <[s2]>
|
2008-01-12 12:25:55 +08:00
|
|
|
(excluding the terminating null character).
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
RETURNS
|
|
|
|
Returns a pointer to the located string segment, or a null
|
|
|
|
pointer if the string <[s2]> is not found. If <[s2]> points to
|
2008-01-12 12:25:55 +08:00
|
|
|
a string with zero length, <[s1]> is returned.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
<<strstr>> is ANSI C.
|
|
|
|
|
|
|
|
<<strstr>> requires no supporting OS subroutines.
|
|
|
|
|
|
|
|
QUICKREF
|
|
|
|
strstr ansi pure
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2008-01-12 12:25:55 +08:00
|
|
|
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
|
|
|
|
# define RETURN_TYPE char *
|
|
|
|
# define AVAILABLE(h, h_l, j, n_l) \
|
|
|
|
(!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \
|
|
|
|
&& ((h_l) = (j) + (n_l)))
|
|
|
|
# include "str-two-way.h"
|
|
|
|
#endif
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
strstr (const char *searchee,
|
2017-12-04 10:25:16 +08:00
|
|
|
const char *lookfor)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2008-01-12 12:25:55 +08:00
|
|
|
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
|
|
|
|
|
|
|
|
/* Less code size, but quadratic performance in the worst case. */
|
2000-02-18 03:39:52 +08:00
|
|
|
if (*searchee == 0)
|
|
|
|
{
|
|
|
|
if (*lookfor)
|
|
|
|
return (char *) NULL;
|
|
|
|
return (char *) searchee;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*searchee)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (lookfor[i] == 0)
|
|
|
|
{
|
|
|
|
return (char *) searchee;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lookfor[i] != searchee[i])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
searchee++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char *) NULL;
|
2008-01-12 12:25:55 +08:00
|
|
|
|
|
|
|
#else /* compilation for speed */
|
|
|
|
|
|
|
|
/* Larger code size, but guaranteed linear performance. */
|
|
|
|
const char *haystack = searchee;
|
|
|
|
const char *needle = lookfor;
|
|
|
|
size_t needle_len; /* Length of NEEDLE. */
|
|
|
|
size_t haystack_len; /* Known minimum length of HAYSTACK. */
|
|
|
|
int ok = 1; /* True if NEEDLE is prefix of HAYSTACK. */
|
|
|
|
|
|
|
|
/* Determine length of NEEDLE, and in the process, make sure
|
|
|
|
HAYSTACK is at least as long (no point processing all of a long
|
|
|
|
NEEDLE if HAYSTACK is too short). */
|
|
|
|
while (*haystack && *needle)
|
|
|
|
ok &= *haystack++ == *needle++;
|
|
|
|
if (*needle)
|
|
|
|
return NULL;
|
|
|
|
if (ok)
|
|
|
|
return (char *) searchee;
|
|
|
|
|
|
|
|
/* Reduce the size of haystack using strchr, since it has a smaller
|
|
|
|
linear coefficient than the Two-Way algorithm. */
|
|
|
|
needle_len = needle - lookfor;
|
|
|
|
haystack = strchr (searchee + 1, *lookfor);
|
|
|
|
if (!haystack || needle_len == 1)
|
|
|
|
return (char *) haystack;
|
|
|
|
haystack_len = (haystack > searchee + needle_len ? 1
|
|
|
|
: needle_len + searchee - haystack);
|
|
|
|
|
|
|
|
/* Perform the search. */
|
|
|
|
if (needle_len < LONG_NEEDLE_THRESHOLD)
|
|
|
|
return two_way_short_needle ((const unsigned char *) haystack,
|
|
|
|
haystack_len,
|
|
|
|
(const unsigned char *) lookfor, needle_len);
|
|
|
|
return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
|
|
|
|
(const unsigned char *) lookfor, needle_len);
|
|
|
|
#endif /* compilation for speed */
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|