-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPasfmt.FormatCore.pas
110 lines (90 loc) · 2.87 KB
/
Pasfmt.FormatCore.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
unit Pasfmt.FormatCore;
interface
type
TFormatResult = record
Output: UTF8String;
Cursors: TArray<Integer>;
ExitCode: Cardinal;
ErrorInfo: string;
end;
TFormatter = record
Executable: string;
WorkingDirectory: string;
Timeout: Integer;
function Format(Input: UTF8String; Cursors: TArray<Integer>): TFormatResult;
end;
implementation
uses
Pasfmt.Subprocess,
System.SysUtils,
System.StrUtils;
//______________________________________________________________________________________________________________________
function ExtractCursorTag(Data: string; out TagValue: string): string;
function IsValueChar(const Char: Char): Boolean;
begin
Result := ((Char >= '0') and (Char <= '9')) or (Char = ',');
end;
const
CTag: string = 'CURSOR=';
var
TagPos: Integer;
StartPos: Integer;
EndPos: Integer;
begin
TagPos := System.Pos(CTag, Data);
if TagPos > 0 then begin
StartPos := TagPos + Length(CTag);
EndPos := StartPos;
while (EndPos <= Length(Data)) and IsValueChar(Data[EndPos]) do
Inc(EndPos);
TagValue := Copy(Data, StartPos, EndPos - StartPos);
Result := Copy(Data, 1, TagPos - 1) + Copy(Data, EndPos + 1);
end;
end;
//______________________________________________________________________________________________________________________
function SerializeCursors(const Cursors: TArray<Integer>): string;
var
I: Integer;
begin
Result := '';
for I := 0 to Length(Cursors) - 1 do begin
if I = 0 then
Result := IntToStr(Cursors[I])
else
Result := Result + ',' + IntToStr(Cursors[I]);
end;
end;
//______________________________________________________________________________________________________________________
function DeserializeCursors(TagValue: string): TArray<Integer>;
var
CursorStrs: TArray<string>;
I: Integer;
begin
CursorStrs := SplitString(TagValue, ',');
SetLength(Result, Length(CursorStrs));
for I := 0 to Length(CursorStrs) - 1 do begin
if not TryStrToInt(CursorStrs[I], Result[I]) then begin
Result[I] := -1;
end;
end;
end;
//______________________________________________________________________________________________________________________
function TFormatter.Format(Input: UTF8String; Cursors: TArray<Integer>): TFormatResult;
var
CommandLine: string;
StdErr: UTF8String;
CursorTagValue: string;
EffectiveExe: string;
begin
EffectiveExe := Executable;
if EffectiveExe = '' then begin
EffectiveExe := 'pasfmt.exe';
end;
CommandLine :=
System.SysUtils.Format('"%s" -C encoding=utf-8 --cursor=%s', [EffectiveExe, SerializeCursors(Cursors)]);
Result := Default(TFormatResult);
Result.ExitCode := RunProcess(CommandLine, Input, Result.Output, StdErr, WorkingDirectory, Timeout);
Result.ErrorInfo := ExtractCursorTag(Trim(string(StdErr)), CursorTagValue);
Result.Cursors := DeserializeCursors(CursorTagValue);
end;
end.