1 /* Copyright (C) 1991,92,93,94,96,97,98,2000,2004,2007,2008 Free Software
3 This file is part of the GNU C Library.
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 along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* This particular implementation was written by Eric Blake, 2008. */
25 /* Specification of memmem. */
33 # define __builtin_expect(expr, val) (expr)
36 /* We use the Two-Way string matching algorithm, which guarantees
37 linear complexity with constant space. Additionally, for long
38 needles, we also use a bad character shift table similar to the
39 Boyer-Moore algorithm to achieve sub-linear performance.
41 See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
42 and http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm
45 /* Point at which computing a bad-byte shift table is likely to be
46 worthwhile. Small needles should not compute a table, since it
47 adds (1 << CHAR_BIT) + NEEDLE_LEN computations of preparation for a
48 speedup no greater than a factor of NEEDLE_LEN. The larger the
49 needle, the better the potential performance gain. On the other
50 hand, on non-POSIX systems with CHAR_BIT larger than eight, the
51 memory required for the table is prohibitive. */
53 # define LONG_NEEDLE_THRESHOLD 32U
55 # define LONG_NEEDLE_THRESHOLD SIZE_MAX
58 #define MAX(a, b) ((a < b) ? (b) : (a))
60 /* Perform a critical factorization of NEEDLE, of length NEEDLE_LEN.
61 Return the index of the first byte in the right half, and set
62 *PERIOD to the global period of the right half.
64 The global period of a string is the smallest index (possibly its
65 length) at which all remaining bytes in the string are repetitions
66 of the prefix (the last repetition may be a subset of the prefix).
68 When NEEDLE is factored into two halves, a local period is the
69 length of the smallest word that shares a suffix with the left half
70 and shares a prefix with the right half. All factorizations of a
71 non-empty NEEDLE have a local period of at least 1 and no greater
74 A critical factorization has the property that the local period
75 equals the global period. All strings have at least one critical
76 factorization with the left half smaller than the global period.
78 Given an ordered alphabet, a critical factorization can be computed
79 in linear time, with 2 * NEEDLE_LEN comparisons, by computing the
80 larger of two ordered maximal suffixes. The ordered maximal
81 suffixes are determined by lexicographic comparison of
84 critical_factorization (const unsigned char *needle, size_t needle_len,
87 /* Index of last byte of left half, or SIZE_MAX. */
88 size_t max_suffix, max_suffix_rev;
89 size_t j; /* Index into NEEDLE for current candidate suffix. */
90 size_t k; /* Offset into current period. */
91 size_t p; /* Intermediate period. */
92 unsigned char a, b; /* Current comparison bytes. */
95 0 <= j < NEEDLE_LEN - 1
96 -1 <= max_suffix{,_rev} < j (treating SIZE_MAX as if it were signed)
97 min(max_suffix, max_suffix_rev) < global period of NEEDLE
98 1 <= p <= global period of NEEDLE
99 p == global period of the substring NEEDLE[max_suffix{,_rev}+1...j]
103 /* Perform lexicographic search. */
104 max_suffix = SIZE_MAX;
107 while (j + k < needle_len)
110 b = needle[max_suffix + k];
113 /* Suffix is smaller, period is entire prefix so far. */
120 /* Advance through repetition of the current period. */
131 /* Suffix is larger, start over from current location. */
138 /* Perform reverse lexicographic search. */
139 max_suffix_rev = SIZE_MAX;
142 while (j + k < needle_len)
145 b = needle[max_suffix_rev + k];
148 /* Suffix is smaller, period is entire prefix so far. */
151 p = j - max_suffix_rev;
155 /* Advance through repetition of the current period. */
166 /* Suffix is larger, start over from current location. */
167 max_suffix_rev = j++;
172 /* Choose the longer suffix. Return the first byte of the right
173 half, rather than the last byte of the left half. */
174 if (max_suffix_rev + 1 < max_suffix + 1)
175 return max_suffix + 1;
177 return max_suffix_rev + 1;
180 /* Return the first location of NEEDLE within HAYSTACK, or NULL. This
181 method requires 0 < NEEDLE_LEN <= HAYSTACK_LEN, and is optimized
182 for NEEDLE_LEN < LONG_NEEDLE_THRESHOLD. Performance is linear,
183 with 2 * NEEDLE_LEN comparisons in preparation, and at most 2 *
184 HAYSTACK_LEN - NEEDLE_LEN comparisons in searching. */
186 two_way_short_needle (const unsigned char *haystack, size_t haystack_len,
187 const unsigned char *needle, size_t needle_len)
189 size_t i; /* Index into current byte of NEEDLE. */
190 size_t j; /* Index into current window of HAYSTACK. */
191 size_t period; /* The period of the right half of needle. */
192 size_t suffix; /* The index of the right half of needle. */
194 /* Factor the needle into two halves, such that the left half is
195 smaller than the global period, and the right half is
196 periodic (with a period as large as NEEDLE_LEN - suffix). */
197 suffix = critical_factorization (needle, needle_len, &period);
199 /* Perform the search. Each iteration compares the right half
201 if (memcmp (needle, needle + period, suffix) == 0)
203 /* Entire needle is periodic; a mismatch can only advance by the
204 period, so use memory to avoid rescanning known occurrences
208 while (j <= haystack_len - needle_len)
210 /* Scan for matches in right half. */
211 i = MAX (suffix, memory);
212 while (i < needle_len && needle[i] == haystack[i + j])
216 /* Scan for matches in left half. */
218 while (memory < i + 1 && needle[i] == haystack[i + j])
220 if (i + 1 < memory + 1)
221 return (void *) (haystack + j);
222 /* No match, so remember how many repetitions of period
223 on the right half were scanned. */
225 memory = needle_len - period;
236 /* The two halves of needle are distinct; no extra memory is
237 required, and any mismatch results in a maximal shift. */
238 period = MAX (suffix, needle_len - suffix) + 1;
240 while (j <= haystack_len - needle_len)
242 /* Scan for matches in right half. */
244 while (i < needle_len && needle[i] == haystack[i + j])
248 /* Scan for matches in left half. */
250 while (i != SIZE_MAX && needle[i] == haystack[i + j])
253 return (void *) (haystack + j);
263 /* Return the first location of NEEDLE within HAYSTACK, or NULL. This
264 method requires 0 < NEEDLE_LEN <= HAYSTACK_LEN, and is optimized
265 for LONG_NEEDLE_THRESHOLD <= NEEDLE_LEN. Performance is linear,
266 with 3 * NEEDLE_LEN + (1U << CHAR_BIT) operations in preparation,
267 and at most 2 * HAYSTACK_LEN - NEEDLE_LEN comparisons in searching.
268 The extra initialization cost allows for potential sublinear
269 performance O(HAYSTACK_LEN / NEEDLE_LEN). */
271 two_way_long_needle (const unsigned char *haystack, size_t haystack_len,
272 const unsigned char *needle, size_t needle_len)
274 size_t i; /* Index into current byte of NEEDLE. */
275 size_t j; /* Index into current window of HAYSTACK. */
276 size_t period; /* The period of the right half of needle. */
277 size_t suffix; /* The index of the right half of needle. */
278 size_t shift_table[1U << CHAR_BIT]; /* See below. */
280 /* Factor the needle into two halves, such that the left half is
281 smaller than the global period, and the right half is
282 periodic (with a period as large as NEEDLE_LEN - suffix). */
283 suffix = critical_factorization (needle, needle_len, &period);
285 /* Populate shift_table. For each possible byte value c,
286 shift_table[c] is the distance from the last occurrence of c to
287 the end of NEEDLE, or NEEDLE_LEN if c is absent from the NEEDLE.
288 shift_table[NEEDLE[NEEDLE_LEN - 1]] contains the only 0. */
289 for (i = 0; i < 1U << CHAR_BIT; i++)
290 shift_table[i] = needle_len;
291 for (i = 0; i < needle_len; i++)
292 shift_table[needle[i]] = needle_len - i - 1;
294 /* Perform the search. Each iteration compares the right half
296 if (memcmp (needle, needle + period, suffix) == 0)
298 /* Entire needle is periodic; a mismatch can only advance by the
299 period, so use memory to avoid rescanning known occurrences
303 while (j <= haystack_len - needle_len)
305 /* Check the last byte first; if it does not match, then
306 shift to the next possible match location. */
307 size_t shift = shift_table[haystack[j + needle_len - 1]];
310 if (memory && shift < period)
312 /* Since needle is periodic, but the last period has
313 a byte out of place, there can be no match until
314 after the mismatch. */
315 shift = needle_len - period;
321 /* Scan for matches in right half. The last byte has
322 already been matched, by virtue of the shift table. */
323 i = MAX (suffix, memory);
324 while (i < needle_len - 1 && needle[i] == haystack[i + j])
326 if (needle_len - 1 <= i)
328 /* Scan for matches in left half. */
330 while (memory < i + 1 && needle[i] == haystack[i + j])
332 if (i + 1 < memory + 1)
333 return (void *) (haystack + j);
334 /* No match, so remember how many repetitions of period
335 on the right half were scanned. */
337 memory = needle_len - period;
348 /* The two halves of needle are distinct; no extra memory is
349 required, and any mismatch results in a maximal shift. */
350 period = MAX (suffix, needle_len - suffix) + 1;
352 while (j <= haystack_len - needle_len)
354 /* Check the last byte first; if it does not match, then
355 shift to the next possible match location. */
356 size_t shift = shift_table[haystack[j + needle_len - 1]];
362 /* Scan for matches in right half. The last byte has
363 already been matched, by virtue of the shift table. */
365 while (i < needle_len - 1 && needle[i] == haystack[i + j])
367 if (needle_len - 1 <= i)
369 /* Scan for matches in left half. */
371 while (i != SIZE_MAX && needle[i] == haystack[i + j])
374 return (void *) (haystack + j);
384 /* Return the first occurrence of NEEDLE in HAYSTACK. Return HAYSTACK
385 if NEEDLE_LEN is 0, otherwise NULL if NEEDLE is not found in
388 memmem (const void *haystack_start, size_t haystack_len,
389 const void *needle_start, size_t needle_len)
391 /* Abstract memory is considered to be an array of 'unsigned char' values,
392 not an array of 'char' values. See ISO C 99 section 6.2.6.1. */
393 const unsigned char *haystack = (const unsigned char *) haystack_start;
394 const unsigned char *needle = (const unsigned char *) needle_start;
397 /* The first occurrence of the empty string is deemed to occur at
398 the beginning of the string. */
399 return (void *) haystack;
401 /* Sanity check, otherwise the loop might search through the whole
403 if (__builtin_expect (haystack_len < needle_len, 0))
406 /* Use optimizations in memchr when possible, to reduce the search
407 size of haystack using a linear algorithm with a smaller
408 coefficient. However, avoid memchr for long needles, since we
409 can often achieve sublinear performance. */
410 if (needle_len < LONG_NEEDLE_THRESHOLD)
412 haystack = memchr (haystack, *needle, haystack_len);
413 if (!haystack || __builtin_expect (needle_len == 1, 0))
414 return (void *) haystack;
415 haystack_len -= haystack - (const unsigned char *) haystack_start;
416 if (haystack_len < needle_len)
418 return two_way_short_needle (haystack, haystack_len, needle, needle_len);
421 return two_way_long_needle (haystack, haystack_len, needle, needle_len);
424 #undef LONG_NEEDLE_THRESHOLD