run copyright.sh for 2011.
[deliverable/binutils-gdb.git] / gdb / gnulib / memmem.c
CommitLineData
f434ba03 1/* Copyright (C) 1991, 1992, 1993, 1994, 1996, 1997, 1998, 2000, 2004, 2007,
7b6bb8da 2 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
f6ea5628
DJ
3 This file is part of the GNU C Library.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
4c38e0a4 7 the Free Software Foundation; either version 3, or (at your option)
f6ea5628
DJ
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19/* This particular implementation was written by Eric Blake, 2008. */
20
21#ifndef _LIBC
22# include <config.h>
23#endif
24
25/* Specification of memmem. */
26#include <string.h>
27
28#ifndef _LIBC
29# define __builtin_expect(expr, val) (expr)
30#endif
31
32#define RETURN_TYPE void *
33#define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
34#include "str-two-way.h"
35
36/* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
37 if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
38 HAYSTACK. */
39void *
40memmem (const void *haystack_start, size_t haystack_len,
f434ba03 41 const void *needle_start, size_t needle_len)
f6ea5628
DJ
42{
43 /* Abstract memory is considered to be an array of 'unsigned char' values,
44 not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
45 const unsigned char *haystack = (const unsigned char *) haystack_start;
46 const unsigned char *needle = (const unsigned char *) needle_start;
47
48 if (needle_len == 0)
49 /* The first occurrence of the empty string is deemed to occur at
50 the beginning of the string. */
51 return (void *) haystack;
52
53 /* Sanity check, otherwise the loop might search through the whole
54 memory. */
55 if (__builtin_expect (haystack_len < needle_len, 0))
56 return NULL;
57
58 /* Use optimizations in memchr when possible, to reduce the search
59 size of haystack using a linear algorithm with a smaller
60 coefficient. However, avoid memchr for long needles, since we
61 can often achieve sublinear performance. */
62 if (needle_len < LONG_NEEDLE_THRESHOLD)
63 {
64 haystack = memchr (haystack, *needle, haystack_len);
65 if (!haystack || __builtin_expect (needle_len == 1, 0))
f434ba03 66 return (void *) haystack;
f6ea5628
DJ
67 haystack_len -= haystack - (const unsigned char *) haystack_start;
68 if (haystack_len < needle_len)
f434ba03 69 return NULL;
f6ea5628
DJ
70 return two_way_short_needle (haystack, haystack_len, needle, needle_len);
71 }
72 else
73 return two_way_long_needle (haystack, haystack_len, needle, needle_len);
74}
75
76#undef LONG_NEEDLE_THRESHOLD
This page took 0.231806 seconds and 4 git commands to generate.