1 /* Byte-wise substring search, using the Two-Way algorithm.
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Eric Blake <ebb9@byu.net>, 2008.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 /* Before including this file, you need to include <string.h> (and
21 <config.h> before that, if not part of libc), and define:
22 RETURN_TYPE A macro that expands to the return type.
23 AVAILABLE(h, h_l, j, n_l)
24 A macro that returns nonzero if there are
25 at least N_L bytes left starting at H[J].
26 H is 'unsigned char *', H_L, J, and N_L
27 are 'size_t'; H_L is an lvalue. For
28 NUL-terminated searches, H_L can be
29 modified each iteration to avoid having
30 to compute the end of H up front.
32 For case-insensitivity, you may optionally define:
33 CMP_FUNC(p1, p2, l) A macro that returns 0 iff the first L
34 characters of P1 and P2 are equal.
35 CANON_ELEMENT(c) A macro that canonicalizes an element right after
36 it has been fetched from one of the two strings.
37 The argument is an 'unsigned char'; the result
38 must be an 'unsigned char' as well.
40 Other macros you may optionally define:
41 RET0_IF_0(a) Documented below at default definition.
44 This file undefines the macros listed above, and defines
45 LONG_NEEDLE_THRESHOLD.
50 #include <sys/param.h> /* Defines MAX. */
52 /* We use the Two-Way string matching algorithm, which guarantees
53 linear complexity with constant space. Additionally, for long
54 needles, we also use a bad character shift table similar to the
55 Boyer-Moore algorithm to achieve improved (potentially sub-linear)
58 See http://www-igm.univ-mlv.fr/~lecroq/string/node26.html#SECTION00260
59 and http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm
62 /* Point at which computing a bad-byte shift table is likely to be
63 worthwhile. Small needles should not compute a table, since it
64 adds (1 << CHAR_BIT) + NEEDLE_LEN computations of preparation for a
65 speedup no greater than a factor of NEEDLE_LEN. The larger the
66 needle, the better the potential performance gain. On the other
67 hand, on non-POSIX systems with CHAR_BIT larger than eight, the
68 memory required for the table is prohibitive. */
70 # define LONG_NEEDLE_THRESHOLD 32U
72 # define LONG_NEEDLE_THRESHOLD SIZE_MAX
76 # define CANON_ELEMENT(c) c
79 # define CMP_FUNC memcmp
82 /* Check for end-of-line in strstr and strcasestr routines.
83 We piggy-back matching procedure for detecting EOL where possible,
84 and use AVAILABLE macro otherwise. */
86 # define CHECK_EOL (0)
89 /* Return NULL if argument is '\0'. */
91 # define RET0_IF_0(a) /* nothing */
94 /* Perform a critical factorization of NEEDLE, of length NEEDLE_LEN.
95 Return the index of the first byte in the right half, and set
96 *PERIOD to the global period of the right half.
98 The global period of a string is the smallest index (possibly its
99 length) at which all remaining bytes in the string are repetitions
100 of the prefix (the last repetition may be a subset of the prefix).
102 When NEEDLE is factored into two halves, a local period is the
103 length of the smallest word that shares a suffix with the left half
104 and shares a prefix with the right half. All factorizations of a
105 non-empty NEEDLE have a local period of at least 1 and no greater
108 A critical factorization has the property that the local period
109 equals the global period. All strings have at least one critical
110 factorization with the left half smaller than the global period.
112 Given an ordered alphabet, a critical factorization can be computed
113 in linear time, with 2 * NEEDLE_LEN comparisons, by computing the
114 larger of two ordered maximal suffixes. The ordered maximal
115 suffixes are determined by lexicographic comparison of
118 critical_factorization (const unsigned char *needle
, size_t needle_len
,
121 /* Index of last byte of left half, or SIZE_MAX. */
122 size_t max_suffix
, max_suffix_rev
;
123 size_t j
; /* Index into NEEDLE for current candidate suffix. */
124 size_t k
; /* Offset into current period. */
125 size_t p
; /* Intermediate period. */
126 unsigned char a
, b
; /* Current comparison bytes. */
129 0 <= j < NEEDLE_LEN - 1
130 -1 <= max_suffix{,_rev} < j (treating SIZE_MAX as if it were signed)
131 min(max_suffix, max_suffix_rev) < global period of NEEDLE
132 1 <= p <= global period of NEEDLE
133 p == global period of the substring NEEDLE[max_suffix{,_rev}+1...j]
137 /* Perform lexicographic search. */
138 max_suffix
= SIZE_MAX
;
141 while (j
+ k
< needle_len
)
143 a
= CANON_ELEMENT (needle
[j
+ k
]);
144 b
= CANON_ELEMENT (needle
[max_suffix
+ k
]);
147 /* Suffix is smaller, period is entire prefix so far. */
154 /* Advance through repetition of the current period. */
165 /* Suffix is larger, start over from current location. */
172 /* Perform reverse lexicographic search. */
173 max_suffix_rev
= SIZE_MAX
;
176 while (j
+ k
< needle_len
)
178 a
= CANON_ELEMENT (needle
[j
+ k
]);
179 b
= CANON_ELEMENT (needle
[max_suffix_rev
+ k
]);
182 /* Suffix is smaller, period is entire prefix so far. */
185 p
= j
- max_suffix_rev
;
189 /* Advance through repetition of the current period. */
200 /* Suffix is larger, start over from current location. */
201 max_suffix_rev
= j
++;
206 /* Choose the longer suffix. Return the first byte of the right
207 half, rather than the last byte of the left half. */
208 if (max_suffix_rev
+ 1 < max_suffix
+ 1)
209 return max_suffix
+ 1;
211 return max_suffix_rev
+ 1;
214 /* Return the first location of non-empty NEEDLE within HAYSTACK, or
215 NULL. HAYSTACK_LEN is the minimum known length of HAYSTACK. This
216 method is optimized for NEEDLE_LEN < LONG_NEEDLE_THRESHOLD.
217 Performance is guaranteed to be linear, with an initialization cost
218 of 2 * NEEDLE_LEN comparisons.
220 If AVAILABLE does not modify HAYSTACK_LEN (as in memmem), then at
221 most 2 * HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching.
222 If AVAILABLE modifies HAYSTACK_LEN (as in strstr), then at most 3 *
223 HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching. */
225 two_way_short_needle (const unsigned char *haystack
, size_t haystack_len
,
226 const unsigned char *needle
, size_t needle_len
)
228 size_t i
; /* Index into current byte of NEEDLE. */
229 size_t j
; /* Index into current window of HAYSTACK. */
230 size_t period
; /* The period of the right half of needle. */
231 size_t suffix
; /* The index of the right half of needle. */
233 /* Factor the needle into two halves, such that the left half is
234 smaller than the global period, and the right half is
235 periodic (with a period as large as NEEDLE_LEN - suffix). */
236 suffix
= critical_factorization (needle
, needle_len
, &period
);
238 /* Perform the search. Each iteration compares the right half
240 if (CMP_FUNC (needle
, needle
+ period
, suffix
) == 0)
242 /* Entire needle is periodic; a mismatch can only advance by the
243 period, so use memory to avoid rescanning known occurrences
247 while (AVAILABLE (haystack
, haystack_len
, j
, needle_len
))
249 const unsigned char *pneedle
;
250 const unsigned char *phaystack
;
252 /* Scan for matches in right half. */
253 i
= MAX (suffix
, memory
);
254 pneedle
= &needle
[i
];
255 phaystack
= &haystack
[i
+ j
];
256 while (i
< needle_len
&& (CANON_ELEMENT (*pneedle
++)
257 == CANON_ELEMENT (*phaystack
++)))
261 /* Scan for matches in left half. */
263 pneedle
= &needle
[i
];
264 phaystack
= &haystack
[i
+ j
];
265 while (memory
< i
+ 1 && (CANON_ELEMENT (*pneedle
--)
266 == CANON_ELEMENT (*phaystack
--)))
268 if (i
+ 1 < memory
+ 1)
269 return (RETURN_TYPE
) (haystack
+ j
);
270 /* No match, so remember how many repetitions of period
271 on the right half were scanned. */
273 memory
= needle_len
- period
;
284 const unsigned char *phaystack
= &haystack
[suffix
];
285 /* The comparison always starts from needle[suffix], so cache it
286 and use an optimized first-character loop. */
287 unsigned char needle_suffix
= CANON_ELEMENT (needle
[suffix
]);
290 /* We start matching from the SUFFIX'th element, so make sure we
291 don't hit '\0' before that. */
292 if (haystack_len
< suffix
+ 1
293 && !AVAILABLE (haystack
, haystack_len
, 0, suffix
+ 1))
297 /* The two halves of needle are distinct; no extra memory is
298 required, and any mismatch results in a maximal shift. */
299 period
= MAX (suffix
, needle_len
- suffix
) + 1;
303 && AVAILABLE (haystack
, haystack_len
, j
, needle_len
)
307 unsigned char haystack_char
;
308 const unsigned char *pneedle
;
310 /* TODO: The first-character loop can be sped up by adapting
311 longword-at-a-time implementation of memchr/strchr. */
313 != (haystack_char
= CANON_ELEMENT (*phaystack
++)))
315 RET0_IF_0 (haystack_char
);
323 /* Calculate J if it wasn't kept up-to-date in the first-character
325 j
= phaystack
- &haystack
[suffix
] - 1;
328 /* Scan for matches in right half. */
330 pneedle
= &needle
[i
];
331 while (i
< needle_len
)
333 if (CANON_ELEMENT (*pneedle
++)
334 != (haystack_char
= CANON_ELEMENT (*phaystack
++)))
336 RET0_IF_0 (haystack_char
);
343 /* Scan for matches in left half. */
345 pneedle
= &needle
[i
];
346 phaystack
= &haystack
[i
+ j
];
347 while (i
!= SIZE_MAX
)
349 if (CANON_ELEMENT (*pneedle
--)
350 != (haystack_char
= CANON_ELEMENT (*phaystack
--)))
352 RET0_IF_0 (haystack_char
);
358 return (RETURN_TYPE
) (haystack
+ j
);
365 if (!AVAILABLE (haystack
, haystack_len
, j
, needle_len
))
369 phaystack
= &haystack
[suffix
+ j
];
372 ret0
: __attribute__ ((unused
))
376 /* Return the first location of non-empty NEEDLE within HAYSTACK, or
377 NULL. HAYSTACK_LEN is the minimum known length of HAYSTACK. This
378 method is optimized for LONG_NEEDLE_THRESHOLD <= NEEDLE_LEN.
379 Performance is guaranteed to be linear, with an initialization cost
380 of 3 * NEEDLE_LEN + (1 << CHAR_BIT) operations.
382 If AVAILABLE does not modify HAYSTACK_LEN (as in memmem), then at
383 most 2 * HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching,
384 and sublinear performance O(HAYSTACK_LEN / NEEDLE_LEN) is possible.
385 If AVAILABLE modifies HAYSTACK_LEN (as in strstr), then at most 3 *
386 HAYSTACK_LEN - NEEDLE_LEN comparisons occur in searching, and
387 sublinear performance is not possible. */
389 two_way_long_needle (const unsigned char *haystack
, size_t haystack_len
,
390 const unsigned char *needle
, size_t needle_len
)
392 size_t i
; /* Index into current byte of NEEDLE. */
393 size_t j
; /* Index into current window of HAYSTACK. */
394 size_t period
; /* The period of the right half of needle. */
395 size_t suffix
; /* The index of the right half of needle. */
396 size_t shift_table
[1U << CHAR_BIT
]; /* See below. */
398 /* Factor the needle into two halves, such that the left half is
399 smaller than the global period, and the right half is
400 periodic (with a period as large as NEEDLE_LEN - suffix). */
401 suffix
= critical_factorization (needle
, needle_len
, &period
);
403 /* Populate shift_table. For each possible byte value c,
404 shift_table[c] is the distance from the last occurrence of c to
405 the end of NEEDLE, or NEEDLE_LEN if c is absent from the NEEDLE.
406 shift_table[NEEDLE[NEEDLE_LEN - 1]] contains the only 0. */
407 for (i
= 0; i
< 1U << CHAR_BIT
; i
++)
408 shift_table
[i
] = needle_len
;
409 for (i
= 0; i
< needle_len
; i
++)
410 shift_table
[CANON_ELEMENT (needle
[i
])] = needle_len
- i
- 1;
412 /* Perform the search. Each iteration compares the right half
414 if (CMP_FUNC (needle
, needle
+ period
, suffix
) == 0)
416 /* Entire needle is periodic; a mismatch can only advance by the
417 period, so use memory to avoid rescanning known occurrences
422 while (AVAILABLE (haystack
, haystack_len
, j
, needle_len
))
424 const unsigned char *pneedle
;
425 const unsigned char *phaystack
;
427 /* Check the last byte first; if it does not match, then
428 shift to the next possible match location. */
429 shift
= shift_table
[CANON_ELEMENT (haystack
[j
+ needle_len
- 1])];
432 if (memory
&& shift
< period
)
434 /* Since needle is periodic, but the last period has
435 a byte out of place, there can be no match until
436 after the mismatch. */
437 shift
= needle_len
- period
;
443 /* Scan for matches in right half. The last byte has
444 already been matched, by virtue of the shift table. */
445 i
= MAX (suffix
, memory
);
446 pneedle
= &needle
[i
];
447 phaystack
= &haystack
[i
+ j
];
448 while (i
< needle_len
- 1 && (CANON_ELEMENT (*pneedle
++)
449 == CANON_ELEMENT (*phaystack
++)))
451 if (needle_len
- 1 <= i
)
453 /* Scan for matches in left half. */
455 pneedle
= &needle
[i
];
456 phaystack
= &haystack
[i
+ j
];
457 while (memory
< i
+ 1 && (CANON_ELEMENT (*pneedle
--)
458 == CANON_ELEMENT (*phaystack
--)))
460 if (i
+ 1 < memory
+ 1)
461 return (RETURN_TYPE
) (haystack
+ j
);
462 /* No match, so remember how many repetitions of period
463 on the right half were scanned. */
465 memory
= needle_len
- period
;
476 /* The two halves of needle are distinct; no extra memory is
477 required, and any mismatch results in a maximal shift. */
479 period
= MAX (suffix
, needle_len
- suffix
) + 1;
481 while (AVAILABLE (haystack
, haystack_len
, j
, needle_len
))
483 const unsigned char *pneedle
;
484 const unsigned char *phaystack
;
486 /* Check the last byte first; if it does not match, then
487 shift to the next possible match location. */
488 shift
= shift_table
[CANON_ELEMENT (haystack
[j
+ needle_len
- 1])];
494 /* Scan for matches in right half. The last byte has
495 already been matched, by virtue of the shift table. */
497 pneedle
= &needle
[i
];
498 phaystack
= &haystack
[i
+ j
];
499 while (i
< needle_len
- 1 && (CANON_ELEMENT (*pneedle
++)
500 == CANON_ELEMENT (*phaystack
++)))
502 if (needle_len
- 1 <= i
)
504 /* Scan for matches in left half. */
506 pneedle
= &needle
[i
];
507 phaystack
= &haystack
[i
+ j
];
508 while (i
!= SIZE_MAX
&& (CANON_ELEMENT (*pneedle
--)
509 == CANON_ELEMENT (*phaystack
--)))
512 return (RETURN_TYPE
) (haystack
+ j
);