1 /* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007,2008 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 # define __builtin_expect(expr, val) (expr)
32 /* Knuth-Morris-Pratt algorithm.
33 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
34 Return a boolean indicating success. */
37 knuth_morris_pratt (const unsigned char *haystack,
38 const unsigned char *last_haystack,
39 const unsigned char *needle, size_t m,
40 const unsigned char **resultp)
42 /* Allocate the table. */
43 size_t *table = (size_t *) nmalloca (m, sizeof (size_t));
48 0 < table[i] <= i is defined such that
49 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
50 and table[i] is as large as possible with this property.
54 needle[table[i]..i-1] = needle[0..i-1-table[i]].
56 rhaystack[0..i-1] == needle[0..i-1]
57 and exists h, i <= h < m: rhaystack[h] != needle[h]
59 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
60 table[0] remains uninitialized. */
64 /* i = 1: Nothing to verify for x = 0. */
68 for (i = 2; i < m; i++)
70 /* Here: j = i-1 - table[i-1].
71 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
72 for x < table[i-1], by induction.
73 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
74 unsigned char b = needle[i - 1];
78 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
79 is known to hold for x < i-1-j.
80 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
83 /* Set table[i] := i-1-j. */
87 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
88 for x = i-1-j, because
89 needle[i-1] != needle[j] = needle[i-1-x]. */
92 /* The inequality holds for all possible x. */
96 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
97 for i-1-j < x < i-1-j+table[j], because for these x:
99 = needle[x-(i-1-j)..j-1]
100 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
102 hence needle[x..i-1] != needle[0..i-1-x].
104 needle[i-1-j+table[j]..i-2]
105 = needle[table[j]..j-1]
106 = needle[0..j-1-table[j]] (by definition of table[j]). */
109 /* Here: j = i - table[i]. */
113 /* Search, using the table to accelerate the processing. */
116 const unsigned char *rhaystack;
117 const unsigned char *phaystack;
121 rhaystack = haystack;
122 phaystack = haystack;
123 /* Invariant: phaystack = rhaystack + j. */
124 while (phaystack != last_haystack)
125 if (needle[j] == *phaystack)
131 /* The entire needle has been found. */
132 *resultp = rhaystack;
138 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
139 rhaystack += table[j];
144 /* Found a mismatch at needle[0] already. */
154 /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
155 if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
158 memmem (const void *haystack_start, size_t haystack_len,
159 const void *needle_start, size_t needle_len)
161 /* Abstract memory is considered to be an array of 'unsigned char' values,
162 not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
163 const unsigned char *haystack = (const unsigned char *) haystack_start;
164 const unsigned char *needle = (const unsigned char *) needle_start;
165 const unsigned char *last_haystack = haystack + haystack_len;
166 const unsigned char *last_needle = needle + needle_len;
169 /* The first occurrence of the empty string is deemed to occur at
170 the beginning of the string. */
171 return (void *) haystack;
173 /* Sanity check, otherwise the loop might search through the whole
175 if (__builtin_expect (haystack_len < needle_len, 0))
178 /* Use optimizations in memchr when possible. */
179 if (__builtin_expect (needle_len == 1, 0))
180 return memchr (haystack, *needle, haystack_len);
182 /* Minimizing the worst-case complexity:
183 Let n = haystack_len, m = needle_len.
184 The naïve algorithm is O(n*m) worst-case.
185 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
187 To achieve linear complexity and yet amortize the cost of the
188 memory allocation, we activate the Knuth-Morris-Pratt algorithm
189 only once the naïve algorithm has already run for some time; more
191 - the outer loop count is >= 10,
192 - the average number of comparisons per outer loop is >= 5,
193 - the total number of comparisons is >= m.
194 But we try it only once. If the memory allocation attempt failed,
195 we don't retry it. */
198 size_t outer_loop_count = 0;
199 size_t comparison_count = 0;
201 /* Speed up the following searches of needle by caching its first
203 unsigned char b = *needle++;
207 if (haystack == last_haystack)
211 /* See whether it's advisable to use an asymptotically faster
214 && outer_loop_count >= 10
215 && comparison_count >= 5 * outer_loop_count)
217 /* See if needle + comparison_count now reaches the end of
219 if (comparison_count >= needle_len)
221 /* Try the Knuth-Morris-Pratt algorithm. */
222 const unsigned char *result;
223 if (knuth_morris_pratt (haystack, last_haystack,
224 needle - 1, needle_len, &result))
225 return (void *) result;
233 /* The first byte matches. */
235 const unsigned char *rhaystack = haystack + 1;
236 const unsigned char *rneedle = needle;
238 for (;; rhaystack++, rneedle++)
240 if (rneedle == last_needle)
242 return (void *) haystack;
243 if (rhaystack == last_haystack)
247 if (*rhaystack != *rneedle)
248 /* Nothing in this round. */