|Maintainer||Daniel Fischer <firstname.lastname@example.org>|
Deprecated: Use the new interface instead
Fast overlapping Boyer-Moore search of both strict and lazy
Descriptions of the algorithm can be found at http://www-igm.univ-mlv.fr/~lecroq/string/node14.html#SECTION00140 and http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm
Original authors: Daniel Fischer (daniel.is.fischer at googlemail.com) and Chris Kuklewicz (haskell at list.mightyreason.com).
This module exists only for backwards compatibility. Nevertheless
there have been small changes in the behaviour of the functions.
The module exports four search functions:
matchSS. All of them return the list of all
starting positions of possibly overlapping occurrences of a pattern
in a string.
Formerly, all four functions returned an empty list when passed
an empty pattern. Now, in accordance with the functions from the other
matchXY "" target = [0 .. .
Parameter and return types
The first parameter is always the pattern string. The second parameter is always the target string to be searched. The returned list contains the offsets of all overlapping patterns.
Int64 is an index into the target string
which is aligned to the head of the pattern string. Strict targets
Int indices and lazy targets return
Int64 indices. All
returned lists are computed and returned in a lazy fashion.
matchLS take lazy bytestrings as patterns. For
performance, if the pattern is not a single strict chunk then all
the the pattern chunks will copied into a concatenated strict
bytestring. This limits the patterns to a length of (maxBound ::
matchSL take lazy bytestrings as targets.
These are written so that while they work they will not retain a
reference to all the earlier parts of the the lazy bytestring.
This means the garbage collector would be able to keep only a small
amount of the target string and free the rest.
In general, the Boyer-Moore algorithm is the most efficient method to search for a pattern inside a string. The advantage over other algorithms (e.g. Naïve, Knuth-Morris-Pratt, Horspool, Sunday) can be made arbitrarily large for specially selected patterns and targets, but usually, it's a factor of 2–3 versus Knuth-Morris-Pratt and of 6–10 versus the naïve algorithm. The Horspool and Sunday algorithms, which are simplified variants of the Boyer-Moore algorithm, typically have performance between Boyer-Moore and Knuth-Morris-Pratt, mostly closer to Boyer-Moore. The advantage of the Boyer-moore variants over other algorithms generally becomes larger for longer patterns. For very short patterns (or patterns with a very short period), other algorithms, e.g. Data.ByteString.Search.DFA can be faster (my tests suggest that "very short" means two, maybe three bytes).
In general, searching in a strict
ByteString is slightly faster
than searching in a lazy
ByteString, but for long targets the
smaller memory footprint of lazy
ByteStrings can make searching
those (sometimes much) faster. On the other hand, there are cases
where searching in a strict target is much faster, even for long targets.
On 32-bit systems,
Int-arithmetic is much faster than
so when there are many matches, that can make a significant difference.
Also, the modification to ameliorate the case of periodic patterns
is defeated by chunk-boundaries, so long patterns with a short period
and many matches exhibit poor behaviour (consider using
Data.ByteString.Lazy.Search.DFA or Data.ByteString.Lazy.Search.KMP
in those cases, the former for medium-length patterns, the latter for
long patterns; only
matchSL suffer from
this problem, though).
Preprocessing the pattern string is O(
patternLength). The search
performance is O(
patternLength) in the best case,
allowing it to go faster than a Knuth-Morris-Pratt algorithm. With
a non-periodic pattern the worst case uses O(3*
comparisons. The periodic pattern worst case is quadratic
patternLength) complexity for the original
The searching functions in this module contain a modification which
drastically improves the performance for periodic patterns.
I believe that for strict target strings, the worst case is now
targetLength) also for periodic patterns and for lazy target
strings, my semi-educated guess is
targetLength * (1 +
These functions can all be usefully partially applied.
Given only a pattern the partially applied version will compute
the supporting lookup tables only once, allowing for efficient re-use.
Similarly, the partially applied
matchLS will compute
the concatenated pattern only once.
The current code uses
Int to keep track of the locations in the
target string. If the length of the pattern plus the length of any
strict chunk of the target string is greater or equal to
then this will overflow causing an error. We try
to detect this and call
error before a segfault occurs.