-
Notifications
You must be signed in to change notification settings - Fork 36
/
DelphiUiLib.Reflection.Strings.pas
397 lines (330 loc) · 8.35 KB
/
DelphiUiLib.Reflection.Strings.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
unit DelphiUiLib.Reflection.Strings;
{
This module provides various helper functions for preparing strings for
showing to the user.
}
interface
uses
System.TypInfo, DelphiApi.Reflection;
type
THintSection = record
Title: String;
Content: String;
class function New(Title, Content: String): THintSection; static;
end;
// Boolean state to string
function TrueFalseToString(Value: LongBool): String;
function EnabledDisabledToString(Value: LongBool): String;
function AllowedDisallowedToString(Value: LongBool): String;
function YesNoToString(Value: LongBool): String;
function CheckboxToString(Value: LongBool): String;
// Misc.
function BytesToString(Size: UInt64): String;
function TimeIntervalToString(Seconds: UInt64): String;
// Convert a set of bit flags to a string
function MapFlags(
Value: UInt64;
const Mapping: TArray<TFlagName>;
IncludeUnknown: Boolean = True;
const Default: String = '(none)';
ImportantBits: UInt64 = 0
): String;
function MapFlagsList(
Value: UInt64;
const Mapping: TArray<TFlagName>
): String;
// Create a hint from a set of sections
function BuildHint(const Sections: TArray<THintSection>): String; overload;
function BuildHint(const Title, Content: String): String; overload;
function BuildHint(const Titles, Contents: TArray<String>): String; overload;
// Convert a CamelCase-style enumeration value to a string
function PrettifyCamelCaseEnum(
TypeInfo: PTypeInfo;
Value: Integer;
[opt] const Prefix: String = '';
[opt] const Suffix: String = ''
): String;
// Convert a SNAKE_CASE-style enumeration value to a string
function PrettifySnakeCaseEnum(
TypeInfo: PTypeInfo;
Value: Integer;
[opt] const Prefix: String = '';
[opt] const Suffix: String = ''
): String;
// String to int conversion
function TryStrToUInt64Ex(
S: String;
out Value: UInt64
): Boolean;
function TryStrToUIntEx(
S: String;
out Value: Cardinal
): Boolean;
function StrToUIntEx(
const S: String;
const Comment: String
): Cardinal; inline;
function StrToUInt64Ex(
const S: String;
const Comment: String
): UInt64; inline;
implementation
uses
DelphiUiLib.Strings, SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function TrueFalseToString;
begin
if Value then
Result := 'True'
else
Result := 'False';
end;
function EnabledDisabledToString;
begin
if Value then
Result := 'Enabled'
else
Result := 'Disabled';
end;
function AllowedDisallowedToString;
begin
if Value then
Result := 'Allowed'
else
Result := 'Disallowed';
end;
function YesNoToString;
begin
if Value then
Result := 'Yes'
else
Result := 'No';
end;
function CheckboxToString;
begin
if Value then
Result := '☑'
else
Result := '☐';
end;
function BytesToString;
begin
if Size = UInt64(-1) then
Result := 'Infinite'
else if Size > 1 shl 30 then
Result := Format('%.2f GiB', [Size / (1 shl 30)])
else if Size > 1 shl 20 then
Result := Format('%.1f MiB', [Size / (1 shl 20)])
else if Size > 1 shl 10 then
Result := IntToStr(Size shr 10) + ' KiB'
else
Result := IntToStr(Size) + ' B';
end;
function TimeIntervalToString;
const
SecondsInDay = 86400;
SecondsInHour = 3600;
SecondsInMinute = 60;
var
Value: UInt64;
Strings: array of String;
i: Integer;
begin
SetLength(Strings, 4);
i := 0;
// Days
if Seconds >= SecondsInDay then
begin
Value := Seconds div SecondsInDay;
Seconds := Seconds mod SecondsInDay;
if Value = 1 then
Strings[i] := '1 day'
else
Strings[i] := IntToStr(Value) + ' days';
Inc(i);
end;
// Hours
if Seconds >= SecondsInHour then
begin
Value := Seconds div SecondsInHour;
Seconds := Seconds mod SecondsInHour;
if Value = 1 then
Strings[i] := '1 hour'
else
Strings[i] := IntToStr(Value) + ' hours';
Inc(i);
end;
// Minutes
if Seconds >= SecondsInMinute then
begin
Value := Seconds div SecondsInMinute;
Seconds := Seconds mod SecondsInMinute;
if Value = 1 then
Strings[i] := '1 minute'
else
Strings[i] := IntToStr(Value) + ' minutes';
Inc(i);
end;
// Seconds
if Seconds = 1 then
Strings[i] := '1 second'
else
Strings[i] := IntToStr(Seconds) + ' seconds';
Inc(i);
Result := String.Join(' ', Strings, 0, i);
end;
function MapFlags;
var
Strings: TArray<String>;
i, Count: Integer;
begin
if Value = 0 then
Exit(Default);
SetLength(Strings, Length(Mapping) + 2);
Count := 0;
// Include the default message if none of important bits present
if (ImportantBits <> 0) and (Value and ImportantBits = 0) then
begin
Strings[Count] := Default;
Inc(Count);
end;
// Map known bits
for i := 0 to High(Mapping) do
if Value and Mapping[i].Value = Mapping[i].Value then
begin
Strings[Count] := Mapping[i].Name;
Value := Value and not Mapping[i].Value;
Inc(Count);
end;
// Unknown bits
if IncludeUnknown and (Value <> 0) then
begin
Strings[Count] := IntToHexEx(Value);
Inc(Count);
end;
if Count = 0 then
Result := Default
else
Result := String.Join(', ', Strings, 0, Count);
end;
function MapFlagsList;
var
Strings: array of string;
i: Integer;
begin
SetLength(Strings, Length(Mapping));
for i := 0 to High(Mapping) do
begin
Strings[i] := CheckboxToString(Value and Mapping[i].Value =
Mapping[i].Value) + ' ' + Mapping[i].Name;
Value := Value and not Mapping[i].Value;
end;
Result := String.Join(#$D#$A, Strings);
end;
class function THintSection.New(Title, Content: String): THintSection;
begin
Result.Title := Title;
Result.Content := Content;
end;
function BuildHint(const Sections: TArray<THintSection>): String;
var
i, Count: Integer;
Items: TArray<String>;
begin
SetLength(Items, Length(Sections));
// Combine, skipping sections with empty content
Count := 0;
for i := Low(Sections) to High(Sections) do
if Sections[i].Content <> '' then
begin
Items[Count] := Format('%s: '#$D#$A' %s ', [Sections[i].Title,
Sections[i].Content]);
Inc(Count);
end;
if Count < Length(Sections) then
SetLength(Items, Count);
Result := String.Join(#$D#$A, Items);
end;
function BuildHint(const Title, Content: String): String;
begin
Result := BuildHint([THintSection.New(Title, Content)]);
end;
function BuildHint(const Titles, Contents: TArray<String>): String;
var
Sections: TArray<THintSection>;
i: Integer;
begin
if Length(Titles) <> Length(Contents) then
begin
Assert(False, 'Mismatched number of hint titles and contents');
Exit('');
end;
SetLength(Sections, Length(Titles));
for i := 0 to High(Sections) do
Sections[i] := THintSection.New(Titles[i], Contents[i]);
Result := BuildHint(Sections);
end;
function OutOfBound(Value: Integer): String;
begin
Result := IntToStr(Value) + ' (out of bound)';
end;
function PrettifyCamelCaseEnum;
begin
if (TypeInfo.Kind = tkEnumeration) and (Value >= TypeInfo.TypeData.MinValue)
and (Value <= TypeInfo.TypeData.MaxValue) then
Result := PrettifyCamelCase(GetEnumName(TypeInfo, Integer(Value)), Prefix,
Suffix)
else
Result := OutOfBound(Value);
end;
function PrettifySnakeCaseEnum;
begin
if (TypeInfo.Kind = tkEnumeration) and (Value >= TypeInfo.TypeData.MinValue)
and (Value <= TypeInfo.TypeData.MaxValue) then
Result := PrettifySnakeCase(GetEnumName(TypeInfo, Integer(Value)),
Prefix, Suffix)
else
Result := OutOfBound(Value);
end;
function TryStrToUInt64Ex;
var
E: Integer;
begin
if S.StartsWith('0x') then
S := S.Replace('0x', '$', []);
// Ignore space separators
S := S.Replace(' ', '', [rfReplaceAll]);
{$Q-}{$R-}
Val(S, Value, E);
{$IFDEF R+}{$R+}{$ENDIF}{$IFDEF Q+}{$Q+}{$ENDIF}
Result := (E = 0);
end;
function TryStrToUIntEx;
var
E: Integer;
begin
if S.StartsWith('0x') then
S := S.Replace('0x', '$', []);
// Ignore space separators
S := S.Replace(' ', '', [rfReplaceAll]);
{$Q-}{$R-}
Val(S, Value, E);
{$IFDEF R+}{$R+}{$ENDIF}{$IFDEF Q+}{$Q+}{$ENDIF}
Result := (E = 0);
end;
function StrToUInt64Ex;
const
E_DECHEX = 'Invalid %s. Please specify a decimal or a hexadecimal value.';
begin
if not TryStrToUInt64Ex(S, Result) then
raise EConvertError.Create(Format(E_DECHEX, [Comment]));
end;
function StrToUIntEx;
begin
{$Q-}{$R-}
Result := StrToUInt64Ex(S, Comment);
{$IFDEF R+}{$R+}{$ENDIF}{$IFDEF Q+}{$Q+}{$ENDIF}
end;
end.