Blame string/memmem.c

Packit 6c4009
/* Copyright (C) 1991-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 memmem.  */
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#ifndef _LIBC
Packit 6c4009
# define __builtin_expect(expr, val)   (expr)
Packit 6c4009
# define __memmem	memmem
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#define RETURN_TYPE void *
Packit 6c4009
#define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
Packit 6c4009
#define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
Packit 6c4009
#include "str-two-way.h"
Packit 6c4009
Packit 6c4009
#undef memmem
Packit 6c4009
Packit 6c4009
/* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
Packit 6c4009
   if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
Packit 6c4009
   HAYSTACK.  */
Packit 6c4009
void *
Packit 6c4009
__memmem (const void *haystack_start, size_t haystack_len,
Packit 6c4009
	  const void *needle_start, size_t needle_len)
Packit 6c4009
{
Packit 6c4009
  /* Abstract memory is considered to be an array of 'unsigned char' values,
Packit 6c4009
     not an array of 'char' values.  See ISO C 99 section 6.2.6.1.  */
Packit 6c4009
  const unsigned char *haystack = (const unsigned char *) haystack_start;
Packit 6c4009
  const unsigned char *needle = (const unsigned char *) needle_start;
Packit 6c4009
Packit 6c4009
  if (needle_len == 0)
Packit 6c4009
    /* The first occurrence of the empty string is deemed to occur at
Packit 6c4009
       the beginning of the string.  */
Packit 6c4009
    return (void *) haystack;
Packit 6c4009
Packit 6c4009
  /* Sanity check, otherwise the loop might search through the whole
Packit 6c4009
     memory.  */
Packit 6c4009
  if (__glibc_unlikely (haystack_len < needle_len))
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  /* Use optimizations in memchr when possible, to reduce the search
Packit 6c4009
     size of haystack using a linear algorithm with a smaller
Packit 6c4009
     coefficient.  However, avoid memchr for long needles, since we
Packit 6c4009
     can often achieve sublinear performance.  */
Packit 6c4009
  if (needle_len < LONG_NEEDLE_THRESHOLD)
Packit 6c4009
    {
Packit 6c4009
      haystack = memchr (haystack, *needle, haystack_len);
Packit 6c4009
      if (!haystack || __builtin_expect (needle_len == 1, 0))
Packit 6c4009
	return (void *) haystack;
Packit 6c4009
      haystack_len -= haystack - (const unsigned char *) haystack_start;
Packit 6c4009
      if (haystack_len < needle_len)
Packit 6c4009
	return NULL;
Packit 6c4009
      return two_way_short_needle (haystack, haystack_len, needle, needle_len);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    return two_way_long_needle (haystack, haystack_len, needle, needle_len);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__memmem)
Packit 6c4009
weak_alias (__memmem, memmem)
Packit 6c4009
libc_hidden_weak (memmem)
Packit 6c4009
Packit 6c4009
#undef LONG_NEEDLE_THRESHOLD