Back to index

glibc  2.9
memmem.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 /* This particular implementation was written by Eric Blake, 2008.  */
00020 
00021 #ifndef _LIBC
00022 # include <config.h>
00023 #endif
00024 
00025 /* Specification of memmem.  */
00026 #include <string.h>
00027 
00028 #ifndef _LIBC
00029 # define __builtin_expect(expr, val)   (expr)
00030 #endif
00031 
00032 #define RETURN_TYPE void *
00033 #define AVAILABLE(h, h_l, j, n_l) ((j) <= (h_l) - (n_l))
00034 #include "str-two-way.h"
00035 
00036 #undef memmem
00037 
00038 /* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
00039    if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
00040    HAYSTACK.  */
00041 void *
00042 memmem (const void *haystack_start, size_t haystack_len,
00043        const void *needle_start, size_t needle_len)
00044 {
00045   /* Abstract memory is considered to be an array of 'unsigned char' values,
00046      not an array of 'char' values.  See ISO C 99 section 6.2.6.1.  */
00047   const unsigned char *haystack = (const unsigned char *) haystack_start;
00048   const unsigned char *needle = (const unsigned char *) needle_start;
00049 
00050   if (needle_len == 0)
00051     /* The first occurrence of the empty string is deemed to occur at
00052        the beginning of the string.  */
00053     return (void *) haystack;
00054 
00055   /* Sanity check, otherwise the loop might search through the whole
00056      memory.  */
00057   if (__builtin_expect (haystack_len < needle_len, 0))
00058     return NULL;
00059 
00060   /* Use optimizations in memchr when possible, to reduce the search
00061      size of haystack using a linear algorithm with a smaller
00062      coefficient.  However, avoid memchr for long needles, since we
00063      can often achieve sublinear performance.  */
00064   if (needle_len < LONG_NEEDLE_THRESHOLD)
00065     {
00066       haystack = memchr (haystack, *needle, haystack_len);
00067       if (!haystack || __builtin_expect (needle_len == 1, 0))
00068        return (void *) haystack;
00069       haystack_len -= haystack - (const unsigned char *) haystack_start;
00070       if (haystack_len < needle_len)
00071        return NULL;
00072       return two_way_short_needle (haystack, haystack_len, needle, needle_len);
00073     }
00074   else
00075     return two_way_long_needle (haystack, haystack_len, needle, needle_len);
00076 }
00077 
00078 #undef LONG_NEEDLE_THRESHOLD