/
uutilsmore.pas
241 lines (188 loc) · 5.8 KB
/
uutilsmore.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
unit uUtilsMore;
{$mode objfpc}{$H+}
{$modeSwitch advancedRecords}
{$modeswitch TypeHelpers}
interface
uses
Classes, SysUtils;
type
{ TProgramVersion }
TProgramVersion = record
Major, Minor, Revision, Build: Cardinal;
class function Create(AMajor: Cardinal = 0; AMinor: Cardinal = 0;
ARevision: Cardinal = 0; ABuild: Cardinal = 0): TProgramVersion; static; overload;
class function Create(const AStr: String): TProgramVersion; static; overload;
function ToString(ADropTrailingZeros: Boolean = False): String;
end;
operator = (AVer1, AVer2: TProgramVersion): Boolean;
operator <> (AVer1, AVer2: TProgramVersion): Boolean;
operator > (AVer1, AVer2: TProgramVersion): Boolean;
operator < (AVer1, AVer2: TProgramVersion): Boolean;
type
{ THotKey }
THotKey = record
ShiftState: TShiftState;
Key: Word;
function ToString: String;
procedure Parse(const AString: String);
function IsEmpty: boolean;
end;
operator = (HotKey1, Hotkey2: THotKey): Boolean;
operator <> (HotKey1, Hotkey2: THotKey): Boolean;
implementation
uses
RegExpr, StrUtils, Menus {for ShortCutToKey}, LCLProc, LCLType;
type
{ TJoinInteger }
{ Thanks to Zvoni!
https://forum.lazarus.freepascal.org/index.php/topic,59519.msg443738.html#msg443738 }
TJoinInteger=Type helper(TStringHelper) for AnsiString
class function Join(const Separator: string; const Values: array of Integer): string; overload; static;
end;
operator = (AVer1, AVer2: TProgramVersion): Boolean;
begin
Result := (AVer1.Major = AVer2.Major) and (AVer1.Minor = AVer2.Minor)
and (AVer1.Revision = AVer2.Revision) and (AVer1.Build = AVer2.Build);
end;
operator <> (AVer1, AVer2: TProgramVersion): Boolean;
begin
Result := not (AVer1 = AVer2);
end;
operator>(AVer1, AVer2: TProgramVersion): Boolean;
begin
Result := False;
if AVer1.Major > AVer2.Major then
Result := True
else if AVer1.Major = AVer2.Major then
begin
if AVer1.Minor > AVer2.Minor then
Result := True
else if AVer1.Minor = AVer2.Minor then
begin
if AVer1.Revision > AVer2.Revision then
Result := True
else if AVer1.Revision = AVer2.Revision then
begin
if AVer1.Build > AVer2.Build then
Result := True;
end;
end;
end;
end;
operator<(AVer1, AVer2: TProgramVersion): Boolean;
begin
Result := (not (AVer1 > AVer2)) and (AVer1 <> AVer2);
end;
operator=(HotKey1, Hotkey2: THotKey): Boolean;
begin
Result := (HotKey1.ShiftState = Hotkey2.ShiftState)
and (HotKey1.Key = Hotkey2.Key);
end;
operator<>(HotKey1, Hotkey2: THotKey): Boolean;
begin
Result := not (HotKey1 = Hotkey2);
end;
{ THotKey }
function THotKey.ToString: String;
begin
Result := '';
if ssAlt in ShiftState then
Result := Result + 'Alt+';
if ssShift in ShiftState then
Result := Result + 'Shift+';
if ssCtrl in ShiftState then
Result := Result + 'Ctrl+';
// Other possible values are not implemented yet
//Result := Result + IntToStr(Key);
Result := Result + KeyAndShiftStateToKeyString(Key, []);
end;
procedure THotKey.Parse(const AString: String);
var
KeyNumPos: Integer;
KeyName: String;
ShortCut: TShortCut;
UnusedShiftState: TShiftState;
begin
ShiftState := [];
if ContainsText(AString, 'alt') then
Include(ShiftState, ssAlt);
if ContainsText(AString, 'shift') then
Include(ShiftState, ssShift);
if ContainsText(AString, 'ctrl') then
Include(ShiftState, ssCtrl);
// Other possible values are not implemented yet
KeyNumPos := RPos('+', AString) + 1;
KeyName := Copy(AString, KeyNumPos, Length(AString) - KeyNumPos + 1);
//Key := StrToInt(Copy(AString, KeyNumPos, Length(AString) - KeyNumPos + 1));
ShortCut := TextToShortCut(KeyName);
ShortCutToKey(ShortCut, Key, UnusedShiftState);
end;
function THotKey.IsEmpty: boolean;
begin
Result := Key = VK_UNKNOWN;
end;
{ TJoinInteger }
class function TJoinInteger.Join(const Separator: string;
const Values: array of Integer): string;
Var
SValues:Array Of String;
i:SizeInt;
begin
SetLength(SValues,System.Length(Values));
For i:=Low(SValues) To High(SValues) Do SValues[i]:=Values[i].ToString;
Result:=String.Join(Separator,SValues);
end;
{ TProgramVersion }
class function TProgramVersion.Create(AMajor: Cardinal; AMinor: Cardinal;
ARevision: Cardinal; ABuild: Cardinal): TProgramVersion; static;
begin
with Result do
begin
Major := AMajor;
Minor := AMinor;
Revision := ARevision;
Build := ABuild;
end;
end;
class function TProgramVersion.Create(const AStr: String): TProgramVersion;
var
Re: TRegExpr;
begin
// RegEx test: https://regex101.com/r/sEeva5/1
Result := TProgramVersion.Create(0, 0, 0, 0);
Re := TRegExpr.Create('^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?$');
Re.ModifierI := True;
if Re.Exec(AStr) then
begin
if Re.Match[1] <> '' then
Result.Major := StrToInt(Re.Match[1]);
if Re.Match[2] <> '' then
Result.Minor := StrToInt(Re.Match[2]);
if Re.Match[3] <> '' then
Result.Revision := StrToInt(Re.Match[3]);
if Re.Match[4] <> '' then
Result.Build := StrToInt(Re.Match[4]);
end;
Re.Free;
end;
function TProgramVersion.ToString(ADropTrailingZeros: Boolean): String;
type
TVersionNumbers = Array of {Cardinal} Integer;
var
Numbers: TVersionNumbers;
I: Integer;
begin
Numbers := TVersionNumbers.Create(Major, Minor, Revision, Build);
if ADropTrailingZeros then
begin
for I := High(Numbers) downto Low(Numbers) do
begin
if (Numbers[I] = 0) then
Delete(Numbers, I, 1)
else
Break;
end;
end;
Result := String.Join('.', Numbers);
end;
end.