[fpc-pascal] function String Replication

Peter J. Haas fpc.ml at pjh2.de
Sun Mar 20 18:27:59 CET 2005


Hi Sergio,

on 2005-03-04T22:21:15+01:00 you wrote:

> I am looking for the name of the native fpc function for:

DupeString, unit StrUtils.

Take a look in the Run-Time library reference guide:
http://www.freepascal.org/docs.html (Standard units reference manual).


> function ReplicateStr(s:string;x:Integer): String;
> var
>   i:Integer;
> Begin

Here you should init the result. Certainly FPC do this, if you have
enabled huge strings {$H+}, but in other cases your code can failed.

    Result := '';
    
>   For i:=0 to x-1 do
>     Result += s;
> end;

If you have enabled huge strings {$H+} you should modify the source to
increase the performance:

function ReplicateStr(s: String; x: Integer): String;
var
  Len, i, DstIdx, SrcIdx: Integer;
begin
  if x < 0 then
    x := 0;
  Len := Length(s);
  SetLength(Result, Len * x);
  DstIdx := 0;
  for i := 1 to x do begin
    for SrcIdx := 1 to Len do
      Result[DstIdx + SrcIdx] := s[SrcIdx];
    Inc(DstIdx. Len);
  end;
end;

wkr Peter.





More information about the fpc-pascal mailing list