OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [string/] [strstr.c] - Diff between revs 207 and 520

Only display areas with differences | Details | Blame | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.