-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSystem.pas
More file actions
435 lines (360 loc) · 11.7 KB
/
System.pas
File metadata and controls
435 lines (360 loc) · 11.7 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
namespace RemObjects.Elements.RTL.Delphi;
interface
uses
RemObjects.Elements.RTL;
{$GLOBALS ON}
type
THandle = public Int64; // Since native types are not supported on Java, THandle stores a 64 bit integer always
TArray<T> = array of T;
TDateTime = public record
public
Value: Double;
operator Implicit(AValue: Double): TDateTime;
operator Implicit(ADateTime: TDateTime): Double;
operator &Add(ALeft, ARight: TDateTime): TDateTime;
operator &Add(ALeft: TDateTime; ARight: Double): TDateTime;
operator &Add(ALeft: TDateTime; ARight: Integer): TDateTime;
operator Subtract(ALeft, ARight: TDateTime): TDateTime;
operator Subtract(ALeft: TDateTime; ARight: Double): TDateTime;
operator Subtract(ALeft: TDateTime; ARight: Integer): TDateTime;
operator Equal(ALeft, ARight: TDateTime): Boolean;
operator Equal(ALeft: TDateTime; ARight: Double): Boolean;
operator NotEqual(ALeft, ARight: TDateTime): Boolean;
operator NotEqual(ALeft: TDateTime; ARight: Double): Boolean;
operator Less(ALeft, ARight: TDateTime): Boolean;
operator LessOrEqual(ALeft, ARight: TDateTime): Boolean;
operator Greater(ALeft, ARight: TDateTime): Boolean;
operator GreaterOrEqual(ALeft, ARight: TDateTime): Boolean;
operator Multiply(ALeft: TDateTime; ARight: Integer): Double;
operator Multiply(ALeft: TDateTime; ARight: Double): Double;
operator Divide(ALeft: TDateTime; ARight: Integer): Double;
operator Divide(ALeft: TDateTime; ARight: Double): Double;
{$IF ECHOES}
operator Implicit(ADateTime: TDateTime): System.DateTime;
operator Implicit(ADateTime: System.DateTime): TDateTime;
{$ENDIF}
end;
TDate = public TDateTime;
TTime = public TDateTime;
{$IF NOT COOPER}
TCustomAttribute = public Attribute;
{$ENDIF}
function Pos(SubStr: DelphiString; S: DelphiString; aOffset: Integer := 1): Integer; inline;
function Pos(SubStr: PlatformString; S: PlatformString; aOffset: Integer := 1): Integer; inline;
function Pos(SubStr: PlatformString; S: DelphiString; aOffset: Integer := 1): Integer; inline;
function Pos(SubStr: DelphiString; S: PlatformString; aOffset: Integer := 1): Integer; inline;
procedure Insert(aSource: DelphiString; var aTarget: DelphiString; aOffset: Integer); inline;
procedure Insert(aSource: PlatformString; var aTarget: PlatformString; aOffset: Integer); inline;
procedure Delete(var S: DelphiString; aIndex: Integer; aCount: Integer); inline;
function &Copy(S: DelphiString; aIndex: Integer; aCount: Integer): DelphiString; inline;
function &Copy(S: PlatformString; aIndex: Integer; aCount: Integer): DelphiString; inline;
{$IF NOT COOPER}
function &Copy<T>(B: array of T; aIndex: Integer; aCount: Integer): array of T; public;
function &Copy<T>(B: TArray<T>; aIndex: Integer): TArray<T>; inline; public;
procedure Delete<T>(var Arr: TArray<T>; Start: Integer; Count: Integer);
{$ENDIF}
procedure FillChar(var Dest: DelphiString; aCount: Integer; aValue: Char);
{$IF NOT COOPER}
procedure FillChar(var Dest; aCount: Integer; aValue: Byte); unsafe;
procedure Move(const ASource; var Dest; Count: NativeInt); unsafe;
function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; unsafe;
{$ENDIF COOPER}
function StringOfChar(aCh: Char; aCount: Integer): DelphiString; inline;
function Trunc(Val: Double): Integer; inline;
function Abs(Val: Double): Double; inline;
function Abs(Val: Integer): Integer; inline;
function Abs(Val: Int64): Int64; inline;
function Round(Val: Double): Int64; inline;
function Sqrt(X: Double): Double; inline;
function Frac(X: Double): Double; inline;
function Exp(X: Double): Double; inline;
function Cos(X: Double): Double; inline;
function Sin(X: Double): Double; inline;
//function Ln(const X: Double): Double; inline;
function ArcTan(X: Double): Double; inline;
function Tangent(X: Double): Double; inline;
procedure SineCosine(X: Double; var aSin, aCos: Double);
{$IF NOT COOPER}
function InterlockedIncrement(var Addend: Integer): Integer;
function InterlockedDecrement(var Addend: Integer): Integer;
{$ENDIF}
implementation
function Pos(SubStr: DelphiString; S: DelphiString; aOffset: Integer := 1): Integer;
begin
result := S.IndexOf(SubStr, aOffset) + 1;
end;
function Pos(SubStr: PlatformString; S: PlatformString; aOffset: Integer := 1): Integer;
begin
result := Pos(DelphiString(SubStr), DelphiString(S));
end;
function Pos(SubStr: PlatformString; S: DelphiString; aOffset: Integer := 1): Integer;
begin
result := Pos(DelphiString(SubStr), S);
end;
function Pos(SubStr: DelphiString; S: PlatformString; aOffset: Integer := 1): Integer;
begin
result := Pos(SubStr, DelphiString(S));
end;
procedure Insert(aSource: DelphiString; var aTarget: DelphiString; aOffset: Integer);
begin
aTarget.Insert(aOffset - 1, aSource);
end;
procedure Insert(aSource: PlatformString; var aTarget: PlatformString; aOffset: Integer);
begin
DelphiString(aTarget).Insert(aOffset - 1, aSource);
end;
procedure Delete(var S: DelphiString; aIndex: Integer; aCount: Integer);
begin
S.Remove(aIndex - 1, aCount);
end;
function &Copy(S: DelphiString; aIndex: Integer; aCount: Integer): DelphiString;
begin
result := S.SubString(aIndex - 1, aCount);
end;
function &Copy(S: PlatformString; aIndex: Integer; aCount: Integer): DelphiString;
begin
result := DelphiString(S).SubString(aIndex - 1, aCount);
end;
{$IF NOT COOPER}
function &Copy<T>(B: array of T; aIndex: Integer; aCount: Integer): array of T;
begin
result := new T[aCount];
{$IF TOFFEE}
for i: Integer := 0 to aCount do
result[i] := B[aIndex + i];
{$ELSE}
system.Array.Copy(B, aIndex, result, 0, aCount);
{$ENDIF}
end;
function &Copy<T>(B: TArray<T>; aIndex: Integer): TArray<T>; inline;
begin
result := &Copy(B, aIndex, B.length);
end;
{$ENDIF}
procedure FillChar(var Dest: DelphiString; aCount: Integer; aValue: Char);
begin
for i: Integer := 0 to Dest.Length - 1 do
Dest.Chars[i] := aValue;
end;
{$IF NOT COOPER}
procedure Delete<T>(var Arr: TArray<T>; Start: Integer; Count: Integer);
begin
var Source := Arr;
Arr := new T[Arr.Length - Count];
for Index: Integer := 0 to Start - 1 do
Arr[Index] := Source[Index];
for Index: Integer := Start + Count to Source.Length - 1 do
Arr[Index - Count] := Source[Index];
end;
{$ENDIF}
{$IF NOT COOPER}
procedure FillChar(var Dest; aCount: Integer; aValue: Byte);
begin
var Ptr := PByte(@Dest); pinned;
for Idx: Integer := 0 to aCount - 1 do
begin
Ptr^ := aValue;
inc(Ptr);
end;
end;
procedure Move(const ASource; var Dest; Count: NativeInt); unsafe;
begin
var SourcePtr := PByte(@ASource); pinned;
var DestPtr := PByte(@Dest); pinned;
for Idx: Integer := 0 to Count - 1 do
begin
DestPtr^ := SourcePtr^;
inc(SourcePtr);
inc(DestPtr);
end;
//System.Buffer.MemoryCopy(@Source, @Dest, Count, Count);
end;
function CompareMem(P1, P2: Pointer; Length: Integer): Boolean;
begin
var Left := PByte(P1); pinned;
var Right := PByte(P2); pinned;
for Idx: Integer := 0 to Length - 1 do
begin
if Left^ <> Right^ then
exit(False);
inc(Left);
inc(Right);
end;
exit(True);
end;
{$ENDIF COOPER}
function StringOfChar(aCh: Char; aCount: Integer): DelphiString;
begin
result := DelphiString.Create(aCh, aCount);
end;
function Trunc(Val: Double): Integer;
begin
result := Integer(Math.Truncate(Val));
end;
function Abs(Val: Double): Double;
begin
result := Math.Abs(Val);
end;
function Abs(Val: Integer): Integer;
begin
result := Math.Abs(Val);
end;
function Abs(Val: Int64): Int64;
begin
result := Math.Abs(Val);
end;
function Round(Val: Double): Int64;
begin
result := Math.Round(Val)
end;
function Sqrt(X: Double): Double;
begin
result := Math.Sqrt(X);
end;
function Frac(X: Double): Double;
begin
// TODO
end;
function Exp(X: Double): Double;
begin
result := Math.Exp(X);
end;
function Cos(X: Double): Double;
begin
result := Math.Cos(X);
end;
function Sin(X: Double): Double;
begin
result := Math.Sin(X);
end;
function ArcTan(X: Double): Double;
begin
result := Math.Atan(X);
end;
function Tangent(X: Double): Double;
begin
result := Math.Tan(X);
end;
procedure SineCosine(X: Double; var aSin, aCos: Double);
begin
aSin := Math.Sin(X);
aCos := Math.Cos(X);
end;
{$IF NOT COOPER}
function InterlockedIncrement(var Addend: Integer): Integer;
begin
interlockedInc(var Addend);
end;
function InterlockedDecrement(var Addend: Integer): Integer;
begin
interlockedDec(var Addend);
end;
{$ENDIF}
operator TDateTime.Implicit(AValue: Double): TDateTime;
begin
result.Value := AValue;
end;
operator TDateTime.Implicit(ADateTime: TDateTime): Double;
begin
result := ADateTime.Value;
end;
operator TDateTime.&Add(ALeft, ARight: TDateTime): TDateTime;
begin
result.Value := ALeft.Value + ARight.Value;
end;
operator TDateTime.&Add(ALeft: TDateTime; ARight: Double): TDateTime;
begin
result.Value := ALeft.Value + ARight;
end;
operator TDateTime.&Add(ALeft: TDateTime; ARight: Integer): TDateTime;
begin
result.Value := ALeft.Value + ARight;
end;
operator TDateTime.Subtract(ALeft, ARight: TDateTime): TDateTime;
begin
result.Value := ALeft.Value - ARight.Value;
end;
operator TDateTime.Subtract(ALeft: TDateTime; ARight: Double): TDateTime;
begin
result.Value := ALeft.Value - ARight;
end;
operator TDateTime.Subtract(ALeft: TDateTime; ARight: Integer): TDateTime;
begin
result.Value := ALeft.Value - ARight;
end;
operator TDateTime.Equal(ALeft, ARight: TDateTime): Boolean;
begin
result := ALeft.Value = ARight.Value;
end;
operator TDateTime.Equal(ALeft: TDateTime; ARight: Double): Boolean;
begin
result := ALeft.Value = ARight;
end;
operator TDateTime.NotEqual(ALeft, ARight: TDateTime): Boolean;
begin
result := ALeft.Value <> ARight.Value;
end;
operator TDateTime.NotEqual(ALeft: TDateTime; ARight: Double): Boolean;
begin
result := ALeft.Value <> ARight;
end;
operator TDateTime.Less(ALeft, ARight: TDateTime): Boolean;
begin
result := ALeft.Value < ARight.Value;
end;
operator TDateTime.LessOrEqual(ALeft, ARight: TDateTime): Boolean;
begin
result := ALeft.Value <= ARight.Value;
end;
operator TDateTime.Greater(ALeft, ARight: TDateTime): Boolean;
begin
result := ALeft.Value > ARight.Value;
end;
operator TDateTime.GreaterOrEqual(ALeft, ARight: TDateTime): Boolean;
begin
result := ALeft.Value >= ARight.Value;
end;
operator TDateTime.Multiply(ALeft: TDateTime; ARight: Integer): Double;
begin
result := ALeft.Value * ARight;
end;
operator TDateTime.Multiply(ALeft: TDateTime; ARight: Double): Double;
begin
result := ALeft.Value * ARight;
end;
operator TDateTime.Divide(ALeft: TDateTime; ARight: Integer): Double;
begin
result := ALeft.Value / ARight;
end;
operator TDateTime.Divide(ALeft: TDateTime; ARight: Double): Double;
begin
result := ALeft.Value / ARight;
end;
{$IF ECHOES}
const
DateTimeOrigin: System.DateTime = new System.DateTime(1899, 12, 30);
operator TDateTime.Implicit(ADateTime: TDateTime): System.DateTime;
begin
// FromOADate may be slow and imprecise, so use our own conversion
// References :
// https://stackoverflow.com/questions/13919641/how-to-convert-a-double-value-to-a-datetime-in-c#answer-36453145
// https://stackoverflow.com/questions/3724355/net-datetime-different-resolution-when-converting-to-and-from-oadate/13922172#13922172
//
if Double.IsNaN(ADateTime.Value) or (-1 < ADateTime.Value < 0) then
raise new ArgumentOutOfRangeException; // NaN or ]-1..0[ ADateTime.Value not supported
var integerPart := System.Math.Truncate(ADateTime.Value);
var decimalPart := ADateTime.Value - integerPart;
result :=
DateTimeOrigin +
System.TimeSpan.FromTicks(
Convert.ToInt64(
(integerPart + System.Math.Abs(decimalPart)) * System.TimeSpan.TicksPerDay
)
);
end;
operator TDateTime.Implicit(ADateTime: System.DateTime): TDateTime;
begin
result.Value := ADateTime.ToOADate;
end;
{$ENDIF}
end.