Author: Attila T.P. Quick Search string searching Answer: 1 2 procedure TForm1.QuickSearch(const AText, APattern: string); 3 var 4 i, k, N, M: integer; 5 v_found: boolean; 6 v_Shift: array[0..255] of byte; 7 8 procedure InitShift; 9 var 10 x: byte; 11 j, M: integer; 12 begin 13 M := Length(APattern); 14 x := 0; 15 while x <> 255 do 16 begin 17 v_Shift[x] := M + 1; 18 x := Succ(x); 19 end; 20 v_Shift[x] := M + 1; 21 j := 0; 22 while j < M do 23 begin 24 inc(j); 25 v_Shift[Ord(APattern[j])] := M + 1 - j; 26 end; 27 end; 28 begin 29 InitShift; 30 i := 0; 31 k := 0; 32 M := Length(APattern); 33 N := Length(AText); 34 while (i <= N - M + 1) and (k < M) do 35 begin 36 if AText[i + k] = APattern[1 + k] then 37 inc(k) 38 else 39 begin 40 i := i + v_Shift[ord(AText[i + M])]; 41 k := 0; 42 end; 43 end; 44 v_found := (k = M); 45 // if v_found then 46 // begin 47 // RichEdit1.SelStart := i - 1; 48 // RichEdit1.SelLength := M; 49 // end; 50 end;