function TStringList.Find(const S: string; out Index: Integer): Boolean;
var
L, R, I: Integer;
CompareRes: PtrInt;
begin
Result := false;
Index:=-1;
if Not Sorted then
Raise EListError.Create(SErrFindNeedsSortedList);
// Use binary search.
L := 0;
R := Pred(Count); // Count - 1;
if L<=R then // while (L<=R) do
repeat // repeat is 5-10% faster than while, so
I := L + (R - L) shr 1; // div 2; shr is faster than div...
CompareRes := DoCompareText(S, Flist^[I].FString);
if (CompareRes>0) then
L := Succ(I); // I+1; instead of adding, Succ or Pred only checks... so faster code.
else begin
R := Pred(I); // I-1;
if (CompareRes=0) then begin
Result := true;
if (Duplicates<>dupAccept) then
L := I; // forces end of while loop
end;
end;
until L>R;
Index := L;
end;