1 /* c-strcasestr.c -- case insensitive substring search in C locale
2 Copyright (C) 2005-2006 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
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. */
22 #include "c-strcasestr.h"
31 /* Knuth-Morris-Pratt algorithm.
32 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
33 Return a boolean indicating success. */
35 knuth_morris_pratt (const char *haystack, const char *needle,
38 size_t m = strlen (needle);
40 /* Allocate the table. */
41 size_t *table = (size_t *) allocsa (m * sizeof (size_t));
46 0 < table[i] <= i is defined such that
47 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
49 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
50 and table[i] is as large as possible with this property.
51 table[0] remains uninitialized. */
57 for (i = 2; i < m; i++)
59 unsigned char b = c_tolower ((unsigned char) needle[i - 1]);
63 if (b == c_tolower ((unsigned char) needle[j]))
78 /* Search, using the table to accelerate the processing. */
81 const char *rhaystack;
82 const char *phaystack;
88 /* Invariant: phaystack = rhaystack + j. */
89 while (*phaystack != '\0')
90 if (c_tolower ((unsigned char) needle[j])
91 == c_tolower ((unsigned char) *phaystack))
97 /* The entire needle has been found. */
104 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
105 rhaystack += table[j];
110 /* Found a mismatch at needle[0] already. */
120 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
122 Note: This function may, in multibyte locales, return success even if
123 strlen (haystack) < strlen (needle) ! */
125 c_strcasestr (const char *haystack, const char *needle)
127 /* Be careful not to look at the entire extent of haystack or needle
128 until needed. This is useful because of these two cases:
129 - haystack may be very long, and a match of needle found early,
130 - needle may be very long, and not even a short initial segment of
131 needle may be found in haystack. */
134 /* Minimizing the worst-case complexity:
135 Let n = strlen(haystack), m = strlen(needle).
136 The naïve algorithm is O(n*m) worst-case.
137 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
139 To achieve linear complexity and yet amortize the cost of the memory
140 allocation, we activate the Knuth-Morris-Pratt algorithm only once
141 the naïve algorithm has already run for some time; more precisely,
143 - the outer loop count is >= 10,
144 - the average number of comparisons per outer loop is >= 5,
145 - the total number of comparisons is >= m.
146 But we try it only once. If the memory allocation attempt failed,
147 we don't retry it. */
149 size_t outer_loop_count = 0;
150 size_t comparison_count = 0;
151 size_t last_ccount = 0; /* last comparison count */
152 const char *needle_last_ccount = needle; /* = needle + last_ccount */
154 /* Speed up the following searches of needle by caching its first
156 unsigned char b = c_tolower ((unsigned char) *needle);
161 if (*haystack == '\0')
165 /* See whether it's advisable to use an asymptotically faster
168 && outer_loop_count >= 10
169 && comparison_count >= 5 * outer_loop_count)
171 /* See if needle + comparison_count now reaches the end of
173 if (needle_last_ccount != NULL)
175 needle_last_ccount +=
176 strnlen (needle_last_ccount, comparison_count - last_ccount);
177 if (*needle_last_ccount == '\0')
178 needle_last_ccount = NULL;
179 last_ccount = comparison_count;
181 if (needle_last_ccount == NULL)
183 /* Try the Knuth-Morris-Pratt algorithm. */
186 knuth_morris_pratt (haystack, needle - 1, &result);
188 return (char *) result;
195 if (c_tolower ((unsigned char) *haystack) == b)
196 /* The first character matches. */
198 const char *rhaystack = haystack + 1;
199 const char *rneedle = needle;
201 for (;; rhaystack++, rneedle++)
203 if (*rneedle == '\0')
205 return (char *) haystack;
206 if (*rhaystack == '\0')
210 if (c_tolower ((unsigned char) *rhaystack)
211 != c_tolower ((unsigned char) *rneedle))
212 /* Nothing in this round. */
219 return (char *) haystack;