Blame string/memmem.c

Packit Service 82fcde
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/* This particular implementation was written by Eric Blake, 2008.  */
Packit Service 82fcde
Packit Service 82fcde
#ifndef _LIBC
Packit Service 82fcde
# include <config.h>
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
/* Specification of memmem.  */
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
Packit Service 82fcde
#ifndef _LIBC
Packit Service 82fcde
# define __builtin_expect(expr, val)   (expr)
Packit Service 82fcde
# define __memmem	memmem
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
#define RETURN_TYPE void *
Packit Service 82fcde
#define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
Packit Service 82fcde
#define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
Packit Service 82fcde
#include "str-two-way.h"
Packit Service 82fcde
Packit Service 82fcde
#undef memmem
Packit Service 82fcde
Packit Service 82fcde
/* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
Packit Service 82fcde
   if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
Packit Service 82fcde
   HAYSTACK.  */
Packit Service 82fcde
void *
Packit Service 82fcde
__memmem (const void *haystack_start, size_t haystack_len,
Packit Service 82fcde
	  const void *needle_start, size_t needle_len)
Packit Service 82fcde
{
Packit Service 82fcde
  /* Abstract memory is considered to be an array of 'unsigned char' values,
Packit Service 82fcde
     not an array of 'char' values.  See ISO C 99 section 6.2.6.1.  */
Packit Service 82fcde
  const unsigned char *haystack = (const unsigned char *) haystack_start;
Packit Service 82fcde
  const unsigned char *needle = (const unsigned char *) needle_start;
Packit Service 82fcde
Packit Service 82fcde
  if (needle_len == 0)
Packit Service 82fcde
    /* The first occurrence of the empty string is deemed to occur at
Packit Service 82fcde
       the beginning of the string.  */
Packit Service 82fcde
    return (void *) haystack;
Packit Service 82fcde
Packit Service 82fcde
  /* Sanity check, otherwise the loop might search through the whole
Packit Service 82fcde
     memory.  */
Packit Service 82fcde
  if (__glibc_unlikely (haystack_len < needle_len))
Packit Service 82fcde
    return NULL;
Packit Service 82fcde
Packit Service 82fcde
  /* Use optimizations in memchr when possible, to reduce the search
Packit Service 82fcde
     size of haystack using a linear algorithm with a smaller
Packit Service 82fcde
     coefficient.  However, avoid memchr for long needles, since we
Packit Service 82fcde
     can often achieve sublinear performance.  */
Packit Service 82fcde
  if (needle_len < LONG_NEEDLE_THRESHOLD)
Packit Service 82fcde
    {
Packit Service 82fcde
      haystack = memchr (haystack, *needle, haystack_len);
Packit Service 82fcde
      if (!haystack || __builtin_expect (needle_len == 1, 0))
Packit Service 82fcde
	return (void *) haystack;
Packit Service 82fcde
      haystack_len -= haystack - (const unsigned char *) haystack_start;
Packit Service 82fcde
      if (haystack_len < needle_len)
Packit Service 82fcde
	return NULL;
Packit Service 82fcde
      return two_way_short_needle (haystack, haystack_len, needle, needle_len);
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    return two_way_long_needle (haystack, haystack_len, needle, needle_len);
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_def (__memmem)
Packit Service 82fcde
weak_alias (__memmem, memmem)
Packit Service 82fcde
libc_hidden_weak (memmem)
Packit Service 82fcde
Packit Service 82fcde
#undef LONG_NEEDLE_THRESHOLD