I needed to create a comma string for a Trect to store values in text format and noticed the TRect
helper does not have those...
After looking around on the net I found the FMX.utls in Delphi has such functions close to that idea..
so I added this as an helper to the already helper in one of my common units for a program I have.
TRectHelper = Record Helper for Trect
Function FromString(S:String):TRect;
Function ToString:String;
End;
Implementation
Function ParseForInt(S:String;Var Index:Integer;Out NoMore:Boolean):Integer;
Var
W:String;
Begin
While (Index <= Length(S))and ( S[Index] in['-',' ','0'..'9'] ) Do
Begin
if S[Index] <> ' ' Then W := W+S[Index];
Inc(Index);
End;
Inc(Index);
NoMore := (Index > Length(S));
Result := W.ToInteger;
End;
Function TRectHelper.FromString(S:String):TRect;
Var
I:Integer;
NoMore :Boolean;
begin
Result :=TRect.Create(0,0,100,100);
I := 1;
Result.Left := ParseForInt(S,I,NoMore);
If Not NoMore Then
Result.Top := ParseForInt(S,I,NoMore);
If Not NoMore Then
result.Right := ParseForInt(S,I,NoMore);
if Not NoMore then
Result.Bottom := ParseForInt(S,I,NoMore);
end;
Function TRectHelper.ToSTring:String;
Begin
Result := Left.Tostring+','+Top.Tostring+','+Right.ToString+','+Bottom.Tostring;
end;
I am sure I missed something somewhere but as it stands it seems to work.
currenly I am using it to read and write a string in a IniFile but it has other usages.
I plan on doing this to Tpoint also
Question:
Is there any way to detect if SELF is valid ?
because If I attempt to set Self while in there it crashes of course if I do this from the Record and not
from an instance.
Example
BoundsRect := Trect.fromString(S);
Inside the Trect.FromString(S); I also tried to set the SELF along with the Results so it would act as a
double duty function, but I can't see how I can tell the difference between TRect and a RectName.FromString..
Currently it looks like it is using the SELF from the caller if I do the TRect....
I thought maybe the SELF parameter would be Nil in this case but it is not..