Repository navigation
Expand file tree
/
Copy pathUnsignedFunc.pas
More file actions
133 lines (115 loc) · 3.76 KB
/
Copy pathUnsignedFunc.pas
File metadata and controls
133 lines (115 loc) · 3.76 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
unit UnsignedFunc;
{
Inno Setup
Copyright (C) 1997-2026 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Provides unsigned alternatives to Delphi functions that use signed types for parameters or return
values where unsigned types would have been more appropriate
}
interface
{ FRAMEWORK_VCL is available as of Delphi 11.1, so define it manually here when we need it, to
support Delphi 10.4 }
{$IF Defined(ISIDEPROJ) or Defined(SETUPPROJ)}
{$DEFINE FRAMEWORK_VCL}
{$IFEND}
uses
{$IFDEF FRAMEWORK_VCL} Windows, UITypes, Controls, Graphics, {$ENDIF} SysUtils;
function ULength(const S: String): Cardinal; overload; inline;
function ULength(const S: RawByteString): Cardinal; overload; inline;
function ULength(const S: WideString): Cardinal; overload; inline;
function ULength(const S: TBytes): Cardinal; overload; inline;
procedure UMove(const Source; var Dest; Count: NativeUInt);
procedure UFillChar(var Dest; Count: NativeUInt; const Value: Integer);
function UCompareMem(P1, P2: Pointer; Length: NativeUInt): Boolean;
{$IFDEF FRAMEWORK_VCL}
function UColorToRGB(Color: TColor): TColorRef;
function UDrawTextBiDiModeFlags(const Control: TControl; const Flags: UINT): UINT;
{$ENDIF}
implementation
function ULength(const S: String): Cardinal;
begin
Result := Cardinal(Length(S));
end;
function ULength(const S: RawByteString): Cardinal;
begin
Result := Cardinal(Length(S));
end;
function ULength(const S: WideString): Cardinal;
begin
Result := Cardinal(Length(S));
end;
function ULength(const S: TBytes): Cardinal;
begin
Result := Cardinal(Length(S));
end;
procedure UMove(const Source; var Dest; Count: NativeUInt);
begin
var SourceBuf: PByte := @Source;
var DestBuf: PByte := @Dest;
const Backward = NativeUInt(DestBuf) > NativeUInt(SourceBuf);
if Backward then begin
Inc(SourceBuf, Count);
Inc(DestBuf, Count);
end;
while Count > 0 do begin
var SignedCount := High(NativeInt);
if Count < NativeUInt(SignedCount) then
SignedCount := NativeInt(Count);
if Backward then begin
Dec(SourceBuf, SignedCount);
Dec(DestBuf, SignedCount);
end;
Move(SourceBuf^, DestBuf^, SignedCount);
Dec(Count, SignedCount);
if not Backward then begin
Inc(SourceBuf, SignedCount);
Inc(DestBuf, SignedCount);
end;
end;
end;
procedure UFillChar(var Dest; Count: NativeUInt; const Value: Integer);
begin
var DestBuf: PByte := @Dest;
while Count > 0 do begin
var SignedCount := High(NativeInt);
if Count < NativeUInt(SignedCount) then
SignedCount := NativeInt(Count);
FillChar(DestBuf^, SignedCount, Value);
Dec(Count, SignedCount);
Inc(DestBuf, SignedCount);
end;
end;
function UCompareMem(P1, P2: Pointer; Length: NativeUInt): Boolean;
begin
Result := True;
if P1 <> P2 then begin
while Length > 0 do begin
{$IF RtlVersion >= 36.0}
var SignedLength := High(NativeInt);
if Length < NativeUInt(SignedLength) then
SignedLength := NativeInt(Length);
{$ELSE}
var SignedLength := High(Integer);
if Length < Cardinal(SignedLength) then
SignedLength := Integer(Length);
{$ENDIF}
if not CompareMem(P1, P2, SignedLength) then
Exit(False);
Dec(Length, SignedLength);
Inc(PByte(P1), SignedLength);
Inc(PByte(P2), SignedLength);
end;
end;
end;
{$IFDEF FRAMEWORK_VCL}
function UColorToRGB(Color: TColor): TColorRef;
begin
Result := TColorRef(ColorToRGB(Color));
end;
function UDrawTextBiDiModeFlags(const Control: TControl; const Flags: UINT): UINT;
begin
Result := UINT(Control.DrawTextBiDiModeFlags(Integer(Flags)));
end;
{$ENDIF}
end.