Boyer-Moore string search: Difference between revisions

m
(added Pascal example)
Line 581:
var
Needle: rawbytestring;
const
MatchInitLen = 4;
begin
if aPattern <> '' then begin
Line 593 ⟶ 591:
Result := function(const aHaystack: rawbytestring): TIntArray
var
Matches: TIntArray;
pNeedle: PByte absolute Needle;
pHaystack: PByte absolute aHaystack;
I, J, NeedleLast, MatchPosOutPos, OldPfxEnd: SizeInt;
const
OutInitLen = 4;
begin
MatchesResult := nil;
if (Needle = '') or (Length(aHaystack) < Length(Needle)) then exit(Matches);
SetLength(MatchesResult, MatchInitLenOutInitLen);
MatchPosOutPos := 0;
NeedleLast := Pred(Length(Needle));
I := NeedleLast;
Line 611 ⟶ 610:
end;
if J < OldPfxEnd then begin
if MatchPosOutPos = Length(MatchesResult) then SetLength(MatchesResult, MatchPosOutPos * 2);
MatchesResult[MatchPosOutPos] := I - OldPfxEnd + 2;
Inc(MatchPosOutPos);
Inc(I, Succ(GsTable[0] - OldPfxEnd));
OldPfxEnd := Length(Needle)*2 - GsTable[0];
Line 621 ⟶ 620:
end;
end;
SetLength(MatchesResult, MatchPosOutPos);
Result := Matches;
end;
end;
73

edits