Mirror

Quick Search (Views: 726)

Problem/Question/Abstract:

Quick Search string searching

Answer:

procedure TForm1.QuickSearch(const AText, APattern: string);
var
i, k, N, M: integer;
v_found: boolean;
v_Shift: array[0..255] of byte;

procedure InitShift;
var
x: byte;
j, M: integer;
begin
M := Length(APattern);
x := 0;
while x <> 255 do
begin
v_Shift[x] := M + 1;
x := Succ(x);
end;
v_Shift[x] := M + 1;
j := 0;
while j < M do
begin
inc(j);
v_Shift[Ord(APattern[j])] := M + 1 - j;
end;
end;
begin
InitShift;
i := 0;
k := 0;
M := Length(APattern);
N := Length(AText);
while (i <= N - M + 1) and (k < M) do
begin
if AText[i + k] = APattern[1 + k] then
inc(k)
else
begin
i := i + v_Shift[ord(AText[i + M])];
k := 0;
end;
end;
v_found := (k = M);
//  if v_found then
//  begin
//    RichEdit1.SelStart := i - 1;
//    RichEdit1.SelLength := M;
//  end;
end;


<< Back to main page