Files
glibc/string/memmem.c
T

87 lines
3.1 KiB
C
Raw Normal View History

/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
1997-04-12 23:41:36 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1997-04-12 23:41:36 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1997-04-12 23:41:36 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
2012-02-09 23:18:22 +00:00
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
1995-02-18 01:27:10 +00:00
/* This particular implementation was written by Eric Blake, 2008. */
#ifndef _LIBC
# include <config.h>
#endif
/* Specification of memmem. */
1995-02-18 01:27:10 +00:00
#include <string.h>
2004-10-01 22:49:36 +00:00
#ifndef _LIBC
# define __builtin_expect(expr, val) (expr)
2014-11-12 22:41:03 +00:00
# define __memmem memmem
2004-10-01 22:49:36 +00:00
#endif
#define RETURN_TYPE void *
#define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
2018-07-16 17:50:09 +01:00
#define FASTSEARCH(S,C,N) (void*) memchr ((void *)(S), (C), (N))
#include "str-two-way.h"
1997-09-11 12:09:10 +00:00
#undef memmem
1995-02-18 01:27:10 +00:00
/* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
HAYSTACK. */
1997-04-12 23:41:36 +00:00
void *
2014-11-12 22:41:03 +00:00
__memmem (const void *haystack_start, size_t haystack_len,
const void *needle_start, size_t needle_len)
1995-02-18 01:27:10 +00:00
{
/* Abstract memory is considered to be an array of 'unsigned char' values,
not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
const unsigned char *haystack = (const unsigned char *) haystack_start;
const unsigned char *needle = (const unsigned char *) needle_start;
1995-02-18 01:27:10 +00:00
if (needle_len == 0)
/* The first occurrence of the empty string is deemed to occur at
1998-01-30 12:53:20 +00:00
the beginning of the string. */
1998-01-30 13:40:43 +00:00
return (void *) haystack;
1995-02-18 01:27:10 +00:00
2000-05-18 05:35:38 +00:00
/* Sanity check, otherwise the loop might search through the whole
memory. */
2014-02-10 14:45:42 +01:00
if (__glibc_unlikely (haystack_len < needle_len))
2000-05-18 05:35:38 +00:00
return NULL;
/* Use optimizations in memchr when possible, to reduce the search
size of haystack using a linear algorithm with a smaller
coefficient. However, avoid memchr for long needles, since we
can often achieve sublinear performance. */
if (needle_len < LONG_NEEDLE_THRESHOLD)
{
haystack = memchr (haystack, *needle, haystack_len);
if (!haystack || __builtin_expect (needle_len == 1, 0))
return (void *) haystack;
haystack_len -= haystack - (const unsigned char *) haystack_start;
if (haystack_len < needle_len)
return NULL;
2018-08-28 12:42:19 +05:30
/* Check whether we have a match. This improves performance since we
avoid the initialization overhead of the two-way algorithm. */
if (memcmp (haystack, needle, needle_len) == 0)
return (void *) haystack;
return two_way_short_needle (haystack, haystack_len, needle, needle_len);
}
else
return two_way_long_needle (haystack, haystack_len, needle, needle_len);
1995-02-18 01:27:10 +00:00
}
2014-11-12 22:41:03 +00:00
libc_hidden_def (__memmem)
weak_alias (__memmem, memmem)
libc_hidden_weak (memmem)
#undef LONG_NEEDLE_THRESHOLD