Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function SubStrings(const Text: String; const Ptrn:String): TIntegerArray;
- var
- hits,maxhits,h,q,i: Integer;
- begin
- MaxHits := Length(Ptrn);
- Hits := 1;
- h := 0;
- q := 1;
- SetLength(Result, q);
- for i:=1 to Length(Text) do
- begin
- if Text[i] = Ptrn[Hits] then
- begin
- Inc(Hits);
- if (Hits > MaxHits) then
- begin
- if q <= h then
- begin
- q := q+q;
- SetLength(Result, q);
- end;
- Result[h] := (i - Hits) + 1;
- Inc(h);
- Hits := 1;
- end;
- end else
- Hits := 1;
- end;
- SetLength(Result, h);
- end;
- function StrReplace(Text, Ptrn, Rep: String; Flags:TReplaceFlags): String; Cdecl;
- var
- Hi,HiPtrn,HiRep,i,j,k: Integer;
- Prev,Curr:Integer;
- Subs: TIntegerArray;
- begin
- Hi := Length(Text);
- if Hi = 0 then Exit;
- case (rfIgnoreCase in flags) of
- True: Subs := SubStrings(LowerCase(Text), LowerCase(Ptrn));
- False:Subs := SubStrings(Text, Ptrn);
- end;
- if Length(Subs) = 0 then
- Exit(Copy(Text, 1,Hi));
- HiRep := Length(Rep);
- HiPtrn := Length(Ptrn);
- SetLength(Result, Hi + (Length(Subs) * (HiRep-HiPtrn)));
- k := 1;
- Prev := 1;
- case (rfReplaceAll in Flags) of
- True:
- begin
- for i:=0 to High(Subs) do
- begin
- Curr := Subs[i];
- j := (Curr-Prev) + 1;
- Move(Text[Prev], Result[k], j);
- k := k + j;
- Move(Rep[1], Result[k], HiRep);
- k := k+HiRep;
- Prev := Curr + HiPtrn + 1;
- end;
- Move(Text[Prev], Result[k], Hi-Prev+1);
- end;
- False:
- begin
- Curr := Subs[0];
- j := (Curr-Prev) + 1;
- Move(Text[Prev], Result[k], j);
- k := k + j;
- Move(Rep[1], Result[k], HiRep);
- k := k+HiRep;
- Prev := Curr + HiPtrn + 1;
- Move(Text[Prev], Result[k], Hi-Prev+1);
- SetLength(Result, k+(Hi-Prev)+1);
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement