[Home]
[Chapter]
[Contents]
[Previous Algorithm]
[Next Algorithm]


Boyer-Moore-Horspool text searching


function search( pat: PATTERN; text: TEXT ): integer; var i, j, k, m, n: integer; skip: array [0..MAXCHAR] of integer; found: boolean; begin found := FALSE; search := 0; m := length(pat); if m=0 then begin search := 1; found := TRUE; end; for k:=0 to MAXCHAR do skip[k] := m; {*** Preprocessing ***} for k:=1 to m-1 do skip[ord(pat[k])] := m-k; k := m; n := length(text); {*** Search ***} while not found and (k <= n) do begin i := k; j := m; while (j >= 1) do if text[i] <> pat[j] then j := -1 else begin j := j-1; i := i-1; end; if j = 0 then begin search := i+1; f ound := TRUE; end; k := k + skip[ord(text[k])]; end; end;

C source (713b.srch.c) Pascal source (713b.srch.p)



© Addison-Wesley Publishing Co. Inc.