@@ -23,16 +23,7 @@ interface
23
23
HlpArrayUtils;
24
24
25
25
resourcestring
26
- SImpossibleRepresentationInt32 =
27
- ' Current Data Structure cannot be Represented as an "Int32" Type.' ;
28
- SImpossibleRepresentationUInt8 =
29
- ' Current Data Structure cannot be Represented as an "UInt8" Type.' ;
30
- SImpossibleRepresentationUInt16 =
31
- ' Current Data Structure cannot be Represented as an "UInt16" Type.' ;
32
- SImpossibleRepresentationUInt32 =
33
- ' Current Data Structure cannot be Represented as an "UInt32" Type.' ;
34
- SImpossibleRepresentationUInt64 =
35
- ' Current Data Structure cannot be Represented as an "UInt64" Type.' ;
26
+ SDifferingSizeOfByteArrayAndIntType = ' The size of the byte array (%0:d) and integer type (%1:d) have to match.' ;
36
27
37
28
type
38
29
THashResult = class sealed(TInterfacedObject, IHashResult)
@@ -184,51 +175,56 @@ function THashResult.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
184
175
185
176
function THashResult.GetInt32 : Int32;
186
177
begin
187
- if ( System.Length(FHash) <> 4 ) then
178
+ if System.Length(FHash) <> sizeof(Int32 ) then
188
179
begin
189
- raise EInvalidOperationHashLibException.CreateRes
190
- (@SImpossibleRepresentationInt32);
180
+ raise EInvalidOperationHashLibException.CreateResFmt(
181
+ @SDifferingSizeOfByteArrayAndIntType,
182
+ [System.Length(FHash), sizeof(Int32)]);
191
183
end ;
192
184
result := Int32((Int32(FHash[0 ]) shl 24 ) or (Int32(FHash[1 ]) shl 16 ) or
193
185
(Int32(FHash[2 ]) shl 8 ) or (Int32(FHash[3 ])));
194
186
end ;
195
187
196
188
function THashResult.GetUInt8 : UInt8;
197
189
begin
198
- if ( System.Length(FHash) <> 1 ) then
190
+ if System.Length(FHash) <> sizeof(UInt8 ) then
199
191
begin
200
- raise EInvalidOperationHashLibException.CreateRes
201
- (@SImpossibleRepresentationUInt8);
192
+ raise EInvalidOperationHashLibException.CreateResFmt(
193
+ @SDifferingSizeOfByteArrayAndIntType,
194
+ [System.Length(FHash), sizeof(UInt8)]);
202
195
end ;
203
196
result := (UInt8(FHash[0 ]));
204
197
end ;
205
198
206
199
function THashResult.GetUInt16 : UInt16;
207
200
begin
208
- if ( System.Length(FHash) <> 2 ) then
201
+ if System.Length(FHash) <> sizeof(UInt16 ) then
209
202
begin
210
- raise EInvalidOperationHashLibException.CreateRes
211
- (@SImpossibleRepresentationUInt16);
203
+ raise EInvalidOperationHashLibException.CreateResFmt(
204
+ @SDifferingSizeOfByteArrayAndIntType,
205
+ [System.Length(FHash), sizeof(UInt16)]);
212
206
end ;
213
207
result := (UInt16(FHash[0 ]) shl 8 ) or (UInt16(FHash[1 ]));
214
208
end ;
215
209
216
210
function THashResult.GetUInt32 : UInt32;
217
211
begin
218
- if ( System.Length(FHash) <> 4 ) then
212
+ if System.Length(FHash) <> sizeof(UInt32 ) then
219
213
begin
220
- raise EInvalidOperationHashLibException.CreateRes
221
- (@SImpossibleRepresentationUInt32);
214
+ raise EInvalidOperationHashLibException.CreateResFmt(
215
+ @SDifferingSizeOfByteArrayAndIntType,
216
+ [System.Length(FHash), sizeof(UInt32)]);
222
217
end ;
223
218
result := TConverters.ReadBytesAsUInt32BE(PByte(FHash), 0 );
224
219
end ;
225
220
226
221
function THashResult.GetUInt64 : UInt64;
227
222
begin
228
- if ( System.Length(FHash) <> 8 ) then
223
+ if System.Length(FHash) <> sizeof(UInt64 ) then
229
224
begin
230
- raise EInvalidOperationHashLibException.CreateRes
231
- (@SImpossibleRepresentationUInt64);
225
+ raise EInvalidOperationHashLibException.CreateResFmt(
226
+ @SDifferingSizeOfByteArrayAndIntType,
227
+ [System.Length(FHash), sizeof(UInt64)]);
232
228
end ;
233
229
result := TConverters.ReadBytesAsUInt64BE(PByte(FHash), 0 );
234
230
end ;
0 commit comments