Previous: strings misc, Up: strings [Index]
The library (vicare containers strings rabin-karp)
implements
the Rabin–Karp string search algorithm.
rabin-karp
Scheme object type, disjoint from all the other types, holding the state for repeated string searches using the same pattern over different texts.
Build and return a new rabin-karp
object. pattern must be
a Scheme string representing the pattern to search for. base must
be a fixnum used in the rolling hash algorithm (when in doubt use
256); it defaults to 256
. prime must be a “large”
prime fixnum (when in doubt use 472882049); it defaults to
472882049
.
Return #t
if obj is a rabin-karp
object, otherwise
return #f
.
Validation clause to be used with the facilities of the library
(vicare arguments validation)
. Succeed if obj satisfies
the predicate rabin-karp?
.
Perform the search of a pattern over text, which must be a Scheme
string. If successful: return the offset of the first character of the
first occurrence of pattern in text; else return #f
.
Examples:
#!r6rs (import (vicare) (vicare containers strings rabin-karp)) (define S (make-rabin-karp "ciao")) (rabin-karp-search S "012345ciao") ⇒ 6 (rabin-karp-search S "hello world") ⇒ #f