Skip to content

Commit

Permalink
Even more improvements to the Delphi Minify Function
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunroselt committed Jul 27, 2023
1 parent cb986a5 commit de56927
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Roselt.CodeFormatting.pas
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class function TCodeFormatter.MinifyCSS(css: String): String;
class function TCodeFormatter.MinifyDelphi(delphi: String; RemoveComments: Boolean): String;
function RemoveCommentsFromLine(MyLine: String): String;
begin
var MultiLineComment := '';
if (MyLine.Contains('//')) then
begin
if (MyLine[1] = '/') AND (MyLine[2] = '/') then
Expand All @@ -77,7 +78,18 @@ class function TCodeFormatter.MinifyDelphi(delphi: String; RemoveComments: Boole
var StringCharFirst := MyLine.IndexOf('''');
var StringCharSecond := MyLine.Substring(StringCharFirst+1).IndexOf('''') + StringCharFirst + 1;
var CommentChar := MyLine.IndexOf('//');
if (StringCharCount > 1) then
if (MyLine.CountChar('{') > 0) AND (MyLine.CountChar('}') > 0) then
if (MyLine.IndexOf('}') > MyLine.IndexOf('{')) then
begin
MultiLineComment := MyLine.Remove(MyLine.IndexOf('}')+1);
MyLine := MyLine.Substring(MultiLineComment.Length);
StringCharCount := MyLine.CountChar('''');
StringCharFirst := MyLine.IndexOf('''');
StringCharSecond := MyLine.Substring(StringCharFirst+1).IndexOf('''') + StringCharFirst + 1;
CommentChar := MyLine.IndexOf('//');
end;

if (StringCharCount > 1) AND (MyLine.Contains('//')) then
begin
if (StringCharFirst > -1) AND (StringCharSecond > -1) AND (StringCharFirst <> StringCharSecond) then
if (StringCharFirst < CommentChar) AND (StringCharSecond > CommentChar) AND (StringCharCount mod 2 = 0) then
Expand All @@ -96,7 +108,7 @@ class function TCodeFormatter.MinifyDelphi(delphi: String; RemoveComments: Boole
MyLine := MyLine.Remove(CommentChar); // There's no strings, remove this comment.
end;
end;
Result := MyLine;
Result := MultiLineComment + MyLine;
end;
begin
var sLine := '';
Expand Down

0 comments on commit de56927

Please sign in to comment.