标题可能有点令人困惑,但基本上,我想要一个可以缩小 Delphi 代码的函数。
类似这样的:
function MinifyDelphiCode(delphi: String): String;
begin
// Returns the minified Delphi (.pas) code String
end;
有没有内置函数或库可以实现这一点?或者有人有已经做到这一点的功能吗?
这是我期望的 input 和 output 的示例:
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Skia,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.Skia;
type
TForm2 = class(TForm)
SkSvg1: TSkSvg;
Button1: TButton;
RadioButton1: TRadioButton;
CheckBox1: TCheckBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.fmx}
{
gas
dgsgdsdgs
gdsdgs
dgssdgdg
}
procedure TForm2.Button1Click(Sender: TObject);
// Some comment
begin
ShowMessage('Hello World'); // Random comment that I put here
end;
end.
unit Unit2; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Skia, FMX.Controls.Presentation, FMX.StdCtrls, FMX.Skia; type TForm2 = class(TForm) SkSvg1: TSkSvg; Button1: TButton; RadioButton1: TRadioButton; CheckBox1: TCheckBox; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation {$R *.fmx} { gas dgsgdsdgs gdsdgs dgssdgdg } procedure TForm2.Button1Click(Sender: TObject); begin ShowMessage('Hello World'); end; end.
它不需要与我的输出完全相同。我的输出只是一个例子。
我决定为此编写自己的函数。这是代码:
function MinifyDelphi(delphi: String): String;
function RemoveComments(MyLine: String): String;
begin
if (MyLine.Contains('//')) then
begin
if (MyLine[1] = '/') AND (MyLine[2] = '/') then
begin
MyLine := ''; // This whole line is a comment that can't be minified, so remove it.
end else
begin
var StringCharCount := MyLine.CountChar('''');
var StringCharFirst := MyLine.IndexOf('''');
var StringCharSecond := MyLine.Substring(StringCharFirst+1).IndexOf('''') + StringCharFirst + 1;
var CommentChar := MyLine.IndexOf('//');
if (StringCharCount > 1) then
begin
if (StringCharFirst > -1) AND (StringCharSecond > -1) AND (StringCharFirst <> StringCharSecond) then
if (StringCharFirst < CommentChar) AND (StringCharSecond > CommentChar) AND (StringCharCount mod 2 = 0) then
begin
// We found //, but it is within a string (quotes ' '), let's leave it.
var CommentCharAnother := MyLine.LastIndexOf('//');
if (CommentChar <> CommentCharAnother) then
MyLine := MyLine.Remove(CommentCharAnother); // Somewhere else is a comment, remove it.
end else
begin
if (StringCharFirst < CommentChar) AND (StringCharSecond > CommentChar) then
CommentChar := MyLine.Substring(CommentChar+2).IndexOf('//') + CommentChar + 1;
MyLine := MyLine.Remove(CommentChar); // Somewhere else is a comment, remove it.
end;
end else
MyLine := MyLine.Remove(CommentChar); // There's no strings, remove this comment.
end;
end;
Result := MyLine;
end;
begin
var sLine := '';
for var I in delphi.Split([sLineBreak]) do
begin
var TrimmedLine := I.Trim([' ', #09, #10, #13]);
TrimmedLine := RemoveComments(TrimmedLine);
if ((sLine.Length + TrimmedLine.Length + 1) >= 1023) then
begin
Result := Result + sLine + sLineBreak;
sLine := TrimmedLine + ' ';
end else
sLine := sLine + TrimmedLine + ' ';
end;
if (sLine.Length > 0) then Result := Result + sLine;
end;
此函数缩小了 Delphi 代码,但请注意,它删除了单行注释 (
//
) 并保留多行注释 ({}
)。我删除了单行注释 (//
),因为它们无法缩小。
我在各种 Delphi 代码上测试了它,它似乎可以很好地满足我的缩小需求。如果有任何问题,请随时发表评论,我会尝试修复它们或改进此功能以更好地缩小😁