Recent

Author Topic: [SOLVED] Improvement of function TCustomApplication.FindOptionIndex  (Read 542 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 405
packages/fcl-base/src/custapp.pp has
Code: Pascal  [Select][+][-]
  1. function TCustomApplication.FindOptionIndex(const S: String;
  2.   var Longopt: Boolean; StartAt : Integer = -1): Integer;
  3.  
  4. Var
  5.   SO,O : String;
  6.   I,P : Integer;
  7.  
  8. begin
  9.   If Not CaseSensitiveOptions then
  10.     SO:=UpperCase(S)
  11.   else
  12.     SO:=S;
  13.   Result:=-1;
  14.   I:=StartAt;
  15.   if (I=-1) then
  16.     I:=ParamCount;
  17.   While (Result=-1) and (I>0) do
  18.     begin
  19.     O:=Params[i];
  20.     // - must be seen as an option value
  21.     If (Length(O)>1) and (O[1]=FOptionChar) then
  22.       begin
  23.       Delete(O,1,1);
  24.       LongOpt:=(Length(O)>0) and (O[1]=FOptionChar);
  25.       If LongOpt then
  26.         begin
  27.         Delete(O,1,1);
  28.         P:=Pos('=',O);
  29.         If (P<>0) then
  30.           O:=Copy(O,1,P-1);
  31.         end;
  32.       If Not CaseSensitiveOptions then
  33.         O:=UpperCase(O);
  34.       If (O=SO) then
  35.         Result:=i;
  36.       end;
  37.     Dec(i);
  38.     end;
  39. end;

1/2 The following lines contain a useless condition
Code: Pascal  [Select][+][-]
  1.     If (Length(O)>1) and (O[1]=FOptionChar) then
  2.       begin
  3.       Delete(O,1,1);
  4.       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
« Last Edit: February 27, 2024, 09:44:13 am by lagprogramming »


Thaddy

  • Hero Member
  • *****
  • Posts: 14357
  • Sensorship about opinions does not belong here.
Re: Improvement of function TCustomApplication.FindOptionIndex
« Reply #2 on: February 23, 2024, 04:06:54 pm »
You know I like micro optimizations, but in this case (Assuming {$B-} state) it does not seem to matter in the generated assembler. Although your logic is correct.
« Last Edit: February 23, 2024, 04:09:18 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018