1 /* c-strstr.c -- substring search in C locale
2 Copyright (C) 2005-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2005, 2007.
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
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 /* Knuth-Morris-Pratt algorithm. */
30 #define CANON_ELEMENT(c) c
33 /* Find the first occurrence of NEEDLE in HAYSTACK. */
35 c_strstr (const char *haystack, const char *needle)
37 /* Be careful not to look at the entire extent of haystack or needle
38 until needed. This is useful because of these two cases:
39 - haystack may be very long, and a match of needle found early,
40 - needle may be very long, and not even a short initial segment of
41 needle may be found in haystack. */
44 /* Minimizing the worst-case complexity:
45 Let n = strlen(haystack), m = strlen(needle).
46 The naïve algorithm is O(n*m) worst-case.
47 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
49 To achieve linear complexity and yet amortize the cost of the memory
50 allocation, we activate the Knuth-Morris-Pratt algorithm only once
51 the naïve algorithm has already run for some time; more precisely,
53 - the outer loop count is >= 10,
54 - the average number of comparisons per outer loop is >= 5,
55 - the total number of comparisons is >= m.
56 But we try it only once. If the memory allocation attempt failed,
59 size_t outer_loop_count = 0;
60 size_t comparison_count = 0;
61 size_t last_ccount = 0; /* last comparison count */
62 const char *needle_last_ccount = needle; /* = needle + last_ccount */
64 /* Speed up the following searches of needle by caching its first
66 unsigned char b = (unsigned char) *needle;
71 if (*haystack == '\0')
75 /* See whether it's advisable to use an asymptotically faster
78 && outer_loop_count >= 10
79 && comparison_count >= 5 * outer_loop_count)
81 /* See if needle + comparison_count now reaches the end of
83 if (needle_last_ccount != NULL)
86 strnlen (needle_last_ccount, comparison_count - last_ccount);
87 if (*needle_last_ccount == '\0')
88 needle_last_ccount = NULL;
89 last_ccount = comparison_count;
91 if (needle_last_ccount == NULL)
93 /* Try the Knuth-Morris-Pratt algorithm. */
96 knuth_morris_pratt_unibyte (haystack, needle - 1, &result);
98 return (char *) result;
105 if ((unsigned char) *haystack == b)
106 /* The first character matches. */
108 const char *rhaystack = haystack + 1;
109 const char *rneedle = needle;
111 for (;; rhaystack++, rneedle++)
113 if (*rneedle == '\0')
115 return (char *) haystack;
116 if (*rhaystack == '\0')
120 if ((unsigned char) *rhaystack != (unsigned char) *rneedle)
121 /* Nothing in this round. */
128 return (char *) haystack;