X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fstrstr.c;h=5399ff6feab592a6e2593863a4ea7d92199d177c;hb=fac94349f25305566277d15b965fa8f4efe66613;hp=5bd7cb4658890734141a59c4e4f791f743b52876;hpb=103b7cd6e6cc45a5aa570ad8c4d0aa6b0188b602;p=gnulib.git diff --git a/lib/strstr.c b/lib/strstr.c index 5bd7cb465..5399ff6fe 100644 --- a/lib/strstr.c +++ b/lib/strstr.c @@ -1,6 +1,6 @@ -/* Searching in a string. - Copyright (C) 2005 Free Software Foundation, Inc. - Written by Bruno Haible , 2005. +/* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2011 Free Software + Foundation, Inc. + This file is part of the GNU C Library. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -12,115 +12,72 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#if HAVE_CONFIG_H +/* This particular implementation was written by Eric Blake, 2008. */ + +#ifndef _LIBC # include #endif -/* Specification. */ -#include "strstr.h" +/* Specification of strstr. */ +#include + +#include -#if HAVE_MBRTOWC -# include "mbuiter.h" +#ifndef _LIBC +# define __builtin_expect(expr, val) (expr) #endif -/* Find the first occurrence of NEEDLE in HAYSTACK. */ +#define RETURN_TYPE char * +#define AVAILABLE(h, h_l, j, n_l) \ + (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l)) \ + && ((h_l) = (j) + (n_l))) +#include "str-two-way.h" + +/* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK + if NEEDLE is empty, otherwise NULL if NEEDLE is not found in + HAYSTACK. */ char * -strstr (const char *haystack, const char *needle) +strstr (const char *haystack_start, const char *needle_start) { - /* Be careful not to look at the entire extent of haystack or needle - until needed. This is useful because of these two cases: - - haystack may be very long, and a match of needle found early, - - needle may be very long, and not even a short initial segment of - needle may be found in haystack. */ -#if HAVE_MBRTOWC - if (MB_CUR_MAX > 1) - { - mbui_iterator_t iter_needle; - - mbui_init (iter_needle, needle); - if (mbui_avail (iter_needle)) - { - mbui_iterator_t iter_haystack; - - mbui_init (iter_haystack, haystack); - for (;; mbui_advance (iter_haystack)) - { - if (!mbui_avail (iter_haystack)) - /* No match. */ - return NULL; + const char *haystack = haystack_start; + const char *needle = needle_start; + size_t needle_len; /* Length of NEEDLE. */ + size_t haystack_len; /* Known minimum length of HAYSTACK. */ + bool ok = true; /* True if NEEDLE is prefix of HAYSTACK. */ - if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle))) - /* The first character matches. */ - { - mbui_iterator_t rhaystack; - mbui_iterator_t rneedle; + /* Determine length of NEEDLE, and in the process, make sure + HAYSTACK is at least as long (no point processing all of a long + NEEDLE if HAYSTACK is too short). */ + while (*haystack && *needle) + ok &= *haystack++ == *needle++; + if (*needle) + return NULL; + if (ok) + return (char *) haystack_start; - memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t)); - mbui_advance (rhaystack); + /* Reduce the size of haystack using strchr, since it has a smaller + linear coefficient than the Two-Way algorithm. */ + needle_len = needle - needle_start; + haystack = strchr (haystack_start + 1, *needle_start); + if (!haystack || __builtin_expect (needle_len == 1, 0)) + return (char *) haystack; + needle -= needle_len; + haystack_len = (haystack > haystack_start + needle_len ? 1 + : needle_len + haystack_start - haystack); - mbui_init (rneedle, needle); - if (!mbui_avail (rneedle)) - abort (); - mbui_advance (rneedle); - - for (;; mbui_advance (rhaystack), mbui_advance (rneedle)) - { - if (!mbui_avail (rneedle)) - /* Found a match. */ - return (char *) haystack; - if (!mbui_avail (rhaystack)) - /* No match. */ - return NULL; - if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle))) - /* Nothing in this round. */ - break; - } - } - } - } - else - return (char *) haystack; - } - else -#endif - { - if (*needle != '\0') - { - /* Speed up the following searches of needle by caching its first - character. */ - char b = *needle++; - - for (;; haystack++) - { - if (*haystack == '\0') - /* No match. */ - return NULL; - if (*haystack == b) - /* The first character matches. */ - { - const char *rhaystack = haystack + 1; - const char *rneedle = needle; - - for (;; rhaystack++, rneedle++) - { - if (*rneedle == '\0') - /* Found a match. */ - return (char *) haystack; - if (*rhaystack == '\0') - /* No match. */ - return NULL; - if (*rhaystack != *rneedle) - /* Nothing in this round. */ - break; - } - } - } - } - else - return (char *) haystack; - } + /* Perform the search. 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. */ + if (needle_len < LONG_NEEDLE_THRESHOLD) + return two_way_short_needle ((const unsigned char *) haystack, + haystack_len, + (const unsigned char *) needle, needle_len); + return two_way_long_needle ((const unsigned char *) haystack, haystack_len, + (const unsigned char *) needle, needle_len); } + +#undef LONG_NEEDLE_THRESHOLD