Back to index

glibc  2.9
strstr.c
Go to the documentation of this file.
00001 /* Return the offset of one string within another.
00002    Copyright (C) 1994,1996,1997,2000,2001,2003,2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 /* This particular implementation was written by Eric Blake, 2008.  */
00021 
00022 #ifndef _LIBC
00023 # include <config.h>
00024 #endif
00025 
00026 /* Specification of strstr.  */
00027 #include <string.h>
00028 
00029 #include <stdbool.h>
00030 
00031 #ifndef _LIBC
00032 # define __builtin_expect(expr, val)   (expr)
00033 #endif
00034 
00035 #define RETURN_TYPE char *
00036 #define AVAILABLE(h, h_l, j, n_l)                \
00037   (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
00038    && ((h_l) = (j) + (n_l)))
00039 #include "str-two-way.h"
00040 
00041 #undef strstr
00042 
00043 /* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
00044    if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
00045    HAYSTACK.  */
00046 char *
00047 strstr (const char *haystack_start, const char *needle_start)
00048 {
00049   const char *haystack = haystack_start;
00050   const char *needle = needle_start;
00051   size_t needle_len; /* Length of NEEDLE.  */
00052   size_t haystack_len; /* Known minimum length of HAYSTACK.  */
00053   bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
00054 
00055   /* Determine length of NEEDLE, and in the process, make sure
00056      HAYSTACK is at least as long (no point processing all of a long
00057      NEEDLE if HAYSTACK is too short).  */
00058   while (*haystack && *needle)
00059     ok &= *haystack++ == *needle++;
00060   if (*needle)
00061     return NULL;
00062   if (ok)
00063     return (char *) haystack_start;
00064 
00065   /* Reduce the size of haystack using strchr, since it has a smaller
00066      linear coefficient than the Two-Way algorithm.  */
00067   needle_len = needle - needle_start;
00068   haystack = strchr (haystack_start + 1, *needle_start);
00069   if (!haystack || __builtin_expect (needle_len == 1, 0))
00070     return (char *) haystack;
00071   needle -= needle_len;
00072   haystack_len = (haystack > haystack_start + needle_len ? 1
00073                 : needle_len + haystack_start - haystack);
00074 
00075   /* Perform the search.  Abstract memory is considered to be an array
00076      of 'unsigned char' values, not an array of 'char' values.  See
00077      ISO C 99 section 6.2.6.1.  */
00078   if (needle_len < LONG_NEEDLE_THRESHOLD)
00079     return two_way_short_needle ((const unsigned char *) haystack,
00080                              haystack_len,
00081                              (const unsigned char *) needle, needle_len);
00082   return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
00083                            (const unsigned char *) needle, needle_len);
00084 }
00085 libc_hidden_builtin_def (strstr)
00086 
00087 #undef LONG_NEEDLE_THRESHOLD