[fpc-devel] Questions on TStringList.Find change (Mantis 28744)

Denis Kozlov dezlov at gmail.com
Wed Mar 23 00:19:22 CET 2016


On 22/03/2016 17:56, Michael Van Canneyt wrote:
> or better something concise like ltAuto,ltUser,ltNone.

It may make more sense to call it ListSortType (as opposed to ListType):
   TListSortType = (lstNone, lstAuto, lstManual);

Something like this could work (prototype code):

=======================================================
function TStringList.GetSorted: Boolean;
begin
   Result := (FSortType = lstAuto);
end;

procedure TStringList.SetSorted(Value: Boolean);
begin
   if Value xor (FSortType= lstAuto)then
   begin
     if Value then
begin
       Sort;
       FSortType:= lstAuto;
     end
     else
FSortType:= lstNone;
   end;
end;

procedure TStringList.SetSortType(Value: TListSortType);
begin
   if FSortType<> Value then
   begin
     if Value = lstAutothen
Sort;
     FSorted := Value;
   end;
end;

procedure TStringList.Find(const S: string; out Index: Integer):Boolean;
begin
   if FSortType <> lstNonethen
     Result := FindSorted(S, Index);
   else
   begin
     Index := IndexOf(S);
     Result := (Index >= 0);
   end;
end;

procedure TStringList.FindSorted(const S: string; out Index: 
Integer):Boolean;
begin
   ifFSortType= lstNonethen
     raise Exception.Create('List must be sorted');
   // sorted search logic here...
end;
=======================================================

An alternative solution, without adding TListSortType:

=======================================================
procedure Find(const S: string; out Index: Integer; *AssumeSorted: 
Boolean = False*):Boolean;
begin
   if *AssumeSorted* or Sorted then
     Result := FindSorted(S, Index, False);
   else
   begin
     Index := IndexOf(S);
     Result := (Index >= 0);
   end;
end;

procedure FindSorted(const S: string; out Index: Integer; *CheckSorted: 
Boolean = True*):Boolean;
begin
   if *CheckSorted* and not Sorted then
     raise Exception.Create('List must be sorted');
   // search logic here...
end;
=======================================================

Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160322/13f931ce/attachment.html>


More information about the fpc-devel mailing list