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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [libiberty/] [memmem.c] - Blame information for rev 104

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 khays
/* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007,2011 Free Software Foundation, Inc.
2
   This file is part of the GNU C Library.
3
 
4
   This program is free software; you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation; either version 2, or (at your option)
7
   any later version.
8
 
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
 
14
   You should have received a copy of the GNU General Public License along
15
   with this program; if not, write to the Free Software Foundation,
16
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
 
18
/*
19
 
20
@deftypefn Supplemental void* memmem (const void *@var{haystack}, @
21
  size_t @var{haystack_len} const void *@var{needle}, size_t @var{needle_len})
22
 
23
Returns a pointer to the first occurrence of @var{needle} (length
24
@var{needle_len}) in @var{haystack} (length @var{haystack_len}).
25
Returns @code{NULL} if not found.
26
 
27
@end deftypefn
28
 
29
*/
30
 
31
#ifndef _LIBC
32
# include <config.h>
33
#endif
34
 
35
#include <stddef.h>
36
#include <string.h>
37
 
38
#ifndef _LIBC
39
# define __builtin_expect(expr, val)   (expr)
40
#endif
41
 
42
#undef memmem
43
 
44
/* Return the first occurrence of NEEDLE in HAYSTACK.  */
45
void *
46
memmem (const void *haystack, size_t haystack_len, const void *needle,
47
        size_t needle_len)
48
{
49
  const char *begin;
50
  const char *const last_possible
51
    = (const char *) haystack + haystack_len - needle_len;
52
 
53
  if (needle_len == 0)
54
    /* The first occurrence of the empty string is deemed to occur at
55
       the beginning of the string.  */
56
    return (void *) haystack;
57
 
58
  /* Sanity check, otherwise the loop might search through the whole
59
     memory.  */
60
  if (__builtin_expect (haystack_len < needle_len, 0))
61
    return NULL;
62
 
63
  for (begin = (const char *) haystack; begin <= last_possible; ++begin)
64
    if (begin[0] == ((const char *) needle)[0] &&
65
        !memcmp ((const void *) &begin[1],
66
                 (const void *) ((const char *) needle + 1),
67
                 needle_len - 1))
68
      return (void *) begin;
69
 
70
  return NULL;
71
}

powered by: WebSVN 2.1.0

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