""" Horspool string searching, based on: http://www-igm.univ-mlv.fr/~lecroq/string/node18.html (Designed to run with ShedSkin) Command line parameters: filename pattern niter V. 1.0, 8 Feb 2006, by leonardo maffi """ from sys import argv class Horspool: def __init__(self, patt): MAXCHAR = 256 self.patt = patt self.m = len(patt) self.d = [self.m] * MAXCHAR for k in xrange(self.m - 1): self.d[ord(patt[k])] = self.m - k - 1 def find(self, text, n): j = 0 while j <= n - self.m: c = text[j + self.m - 1] if self.patt[self.m-1] == c and self.patt[:-1] == text[j:j+self.m-1]: return j j += self.d[ord(c)] return -1 def main(): data = file(argv[1], "rb").read() n = len(data) hp = Horspool(argv[2]) for i in xrange(int(argv[3])): pos = hp.find(data, n) print pos try: import psyco # Use Psyco if available except: pass else: psyco.full() main()