Skip to content

Improvement of function TCustomApplication.FindOptionIndex

from 'lagprogramming'.

packages/fcl-base/src/custapp.pp has

    function TCustomApplication.FindOptionIndex(const S: String;
      var Longopt: Boolean; StartAt : Integer = -1): Integer;
     
    Var
      SO,O : String;
      I,P : Integer;
     
    begin
      If Not CaseSensitiveOptions then
        SO:=UpperCase(S)
      else
        SO:=S;
      Result:=-1;
      I:=StartAt;
      if (I=-1) then
        I:=ParamCount;
      While (Result=-1) and (I>0) do
        begin
        O:=Params[i];
        // - must be seen as an option value
        If (Length(O)>1) and (O[1]=FOptionChar) then
          begin
          Delete(O,1,1);
          LongOpt:=(Length(O)>0) and (O[1]=FOptionChar);
          If LongOpt then
            begin
            Delete(O,1,1);
            P:=Pos('=',O);
            If (P<>0) then
              O:=Copy(O,1,P-1);
            end;
          If Not CaseSensitiveOptions then
            O:=UpperCase(O);
          If (O=SO) then
            Result:=i;
          end;
        Dec(i);
        end;
    end;

1/2 The following lines contain a useless condition

        If (Length(O)>1) and (O[1]=FOptionChar) then
          begin
          Delete(O,1,1);
          LongOpt:=(Length(O)>0) and (O[1]=FOptionChar);

The condition (Length(O)>0) is always true because before deleting the first character using "Delete(O,1,1);" the length of variable O was at least 2(due to the "If (Length(O)>1)" condition). So, "LongOpt:=(Length(O)>0) and (O[1]=FOptionChar);" has been replaced with "LongOpt:=(O[1]=FOptionChar);".

2/2 After that, "If (P<>0) then O:=Copy(O,1,P-1);" has been replaced with "If (P<>0) then SetLength(O,P-1);".

Here is a patch

patch.diff

Edited by Alexey Torgashin
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information