Need help porting function to Free Pascal
In article <Pine.SOL.3.91.990125235123.18789B-100...@fan1.fan.nb.ca>,
Quote
Jeff Patterson <aa...@fan.nb.ca> wrote:
> know that the Mem[] is referencing Real-Mode memory, but i want it to
> reference the variable I am trying to reference with Seg(),Ofs() (which
> of course don't work with FPC).
> function Stz(var Start; max:Byte):String;
> var I : Byte;
> S : String;
> C : Char;
> begin
> If Max<1 then Max := 1;
> Dec(Max,1);
> S := '';
> I := 0;
> repeat
> C := Chr(Mem[Seg(Start):Ofs(Start)+I]);
> If (C<>#0) and (C<>#26) then S := S + C;
> Inc(I);
> until ((C = #0) or (C = #26)) or (Length(S)>Max);
> Stz := S;
> end;
> I know it could be written better (not using repeat..until, etc) but I'll
> get around to that later (unless someone wants to optimize it or something).
Use PChar instead (also better in TP, much faster than your mem
construct)... I'd do something like
Function Stz(var Start; max: Byte): String;
Var I: Byte;
P: PChar;
Begin
If Max < 1 Then Max := 1;
Dec(Max);
Stz := '';
I := 0;
P := @Start;
While Not(P[I] in [#0,#26]) And (Length(Stz) < Max) Do
Begin
Stz := Stz + P[I];
Inc(I)
End
End;
Some remarks:
a) this is written for FPC, it won't work in TP (you'll have to use a
temporary string, like you did first)
b) my version will continue at most until Length(S) = Max, yours will
continue until Length(S) = Max + 1, I don't know whether this was
intended or not
c) it may contain bugs (I've tested whether it compiles, but not the
functionality)
Jonas