Recent

Author Topic: [SOLVED] Code cleanup at function ExtractFileExt  (Read 906 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
[SOLVED] Code cleanup at function ExtractFileExt
« on: January 06, 2024, 03:46:24 pm »
rtl/objpas/sysutils/fina.inc has
Code: Pascal  [Select][+][-]
  1. function ExtractFileExt(const FileName: PathStr): PathStr;
  2. var
  3.   i : longint;
  4.   EndSep : Set of AnsiChar;
  5.   SOF : Boolean; // Dot at Start of filename ?
  6.  
  7. begin
  8.   Result:='';
  9.   I := Length(FileName);
  10.   EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];
  11.   while (I > 0) and not CharInSet(FileName[I],EndSep) do
  12.     Dec(I);
  13.   if (I > 0) and (FileName[I] = ExtensionSeparator) then
  14.     begin
  15.           SOF:=(I=1) or (FileName[i-1] in AllowDirectorySeparators);
  16.           if (Not SOF) or FirstDotAtFileNameStartIsExtension then
  17.             Result := Copy(FileName, I, MaxInt);
  18.           end
  19.   else
  20.     Result := '';
  21. end;
  22.  
In addition to the initial "Result:='';" line, the function has a redundant empty string assignment at the "else" part.
The following patch removes the "else" part making the function become:
Code: Pascal  [Select][+][-]
  1. function ExtractFileExt(const FileName: PathStr): PathStr;
  2. var
  3.   i : longint;
  4.   EndSep : Set of AnsiChar;
  5.   SOF : Boolean; // Dot at Start of filename ?
  6.  
  7. begin
  8.   Result:='';
  9.   I := Length(FileName);
  10.   EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];
  11.   while (I > 0) and not CharInSet(FileName[I],EndSep) do
  12.     Dec(I);
  13.   if (I > 0) and (FileName[I] = ExtensionSeparator) then
  14.     begin
  15.           SOF:=(I=1) or (FileName[i-1] in AllowDirectorySeparators);
  16.           if (Not SOF) or FirstDotAtFileNameStartIsExtension then
  17.             Result := Copy(FileName, I, MaxInt);
  18.     end;
  19. end;
« Last Edit: January 07, 2024, 09:41:10 am by lagprogramming »

AlexTP

  • Hero Member
  • *****
  • Posts: 2574
    • UVviewsoft

 

TinyPortal © 2005-2018