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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 /* Knuth-Morris-Pratt algorithm.
29 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
30 Return a boolean indicating success. */
32 knuth_morris_pratt (const char *haystack, const char *needle,
35 size_t m = strlen (needle);
37 /* Allocate the table. */
38 size_t *table = (size_t *) malloc (m * sizeof (size_t));
43 0 < table[i] <= i is defined such that
44 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
46 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
47 and table[i] is as large as possible with this property.
48 table[0] remains uninitialized. */
54 for (i = 2; i < m; i++)
56 unsigned char b = (unsigned char) needle[i - 1];
60 if (b == (unsigned char) needle[j])
75 /* Search, using the table to accelerate the processing. */
78 const char *rhaystack;
79 const char *phaystack;
85 /* Invariant: phaystack = rhaystack + j. */
86 while (*phaystack != '\0')
87 if ((unsigned char) needle[j] == (unsigned char) *phaystack)
93 /* The entire needle has been found. */
100 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
101 rhaystack += table[j];
106 /* Found a mismatch at needle[0] already. */
116 /* Find the first occurrence of NEEDLE in HAYSTACK. */
118 c_strstr (const char *haystack, const char *needle)
120 /* Be careful not to look at the entire extent of haystack or needle
121 until needed. This is useful because of these two cases:
122 - haystack may be very long, and a match of needle found early,
123 - needle may be very long, and not even a short initial segment of
124 needle may be found in haystack. */
127 /* Minimizing the worst-case complexity:
128 Let n = strlen(haystack), m = strlen(needle).
129 The naïve algorithm is O(n*m) worst-case.
130 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
132 To achieve linear complexity and yet amortize the cost of the memory
133 allocation, we activate the Knuth-Morris-Pratt algorithm only once
134 the naïve algorithm has already run for some time; more precisely,
136 - the outer loop count is >= 10,
137 - the average number of comparisons per outer loop is >= 5,
138 - the total number of comparisons is >= m.
139 But we try it only once. If the memory allocation attempt failed,
140 we don't retry it. */
142 size_t outer_loop_count = 0;
143 size_t comparison_count = 0;
144 size_t last_ccount = 0; /* last comparison count */
145 const char *needle_last_ccount = needle; /* = needle + last_ccount */
147 /* Speed up the following searches of needle by caching its first
149 unsigned char b = (unsigned char) *needle;
154 if (*haystack == '\0')
158 /* See whether it's advisable to use an asymptotically faster
161 && outer_loop_count >= 10
162 && comparison_count >= 5 * outer_loop_count)
164 /* See if needle + comparison_count now reaches the end of
166 if (needle_last_ccount != NULL)
168 needle_last_ccount +=
169 strnlen (needle_last_ccount, comparison_count - last_ccount);
170 if (*needle_last_ccount == '\0')
171 needle_last_ccount = NULL;
172 last_ccount = comparison_count;
174 if (needle_last_ccount == NULL)
176 /* Try the Knuth-Morris-Pratt algorithm. */
179 knuth_morris_pratt (haystack, needle - 1, &result);
181 return (char *) result;
188 if ((unsigned char) *haystack == b)
189 /* The first character matches. */
191 const char *rhaystack = haystack + 1;
192 const char *rneedle = needle;
194 for (;; rhaystack++, rneedle++)
196 if (*rneedle == '\0')
198 return (char *) haystack;
199 if (*rhaystack == '\0')
203 if ((unsigned char) *rhaystack != (unsigned char) *rneedle)
204 /* Nothing in this round. */
211 return (char *) haystack;