[fpc-devel] Questions on TStringList.Find change (Mantis 28744)
Denis Kozlov
dezlov at gmail.com
Tue Mar 22 15:53:10 CET 2016
On 22 March 2016 at 14:43, Michael Van Canneyt wrote:
>
> On 3/22/16 2:01 AM, Michael Van Canneyt wrote:
>>
>>>
>>> There are now several options:
>>> 1. I change the code to raise an exception instead (as was my original
>>> plan)
>>> 2. I change the code to resort to a linear search if sorted is false.
>>> 3. I introduce a boolean property that determines whether Find does any
>>> check.
>>> By default the check will be done, to warn people they're doing
>>> something stupid. If they override the check, we can assume they know
>>> what
>>> they are doing.
>>> 4. Combine 1 and 3.
>>>
>>> Or,
>>>
>>> 5. You override sort so it does nothing. It is virtual.
>>> You can then set sorted to True, no sort will occur, and find will do
>>> it's job.
>>>
>>> 6. You use another TStringList where you change find to suit your needs.
>>> All it takes for you is to copy the implementation, you can keep the
>>> same
>>> name, and insert the unit after the classes unit in places where you
>>> need it.
>>>
>>
> That aside: I will probably go for 1 or 4.
>
> Michael.
>
Please consider the following implementation logic, I think it covers all
angles:
procedure Find(const S: string; out Index: Integer):Boolean;
begin
if Sorted then
Result := FindSorted(S, Index);
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/09a913b9/attachment.html>
More information about the fpc-devel
mailing list