Skip to content

Commit b634088

Browse files
authored
Reworded some error messages and switched to sentence case (Xor-el#21)
1 parent fa78952 commit b634088

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

HashLib/src/Base/HlpHash.pas

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ interface
1313
HlpIHashResult;
1414

1515
resourcestring
16-
SIndexOutOfRange = 'Current Index Is Out Of Range';
17-
SInvalidBufferSize = '"BufferSize" Must Be Greater Than Zero';
18-
SUnAssignedStream = 'Input Stream Is Unassigned';
19-
SFileNotExist = 'Specified File Not Found';
20-
SCloneNotYetImplemented = 'Clone Not Yet Implemented For "%s"';
16+
SReadBeyondStreamEndError = 'Cannot read beyond stream end';
17+
SInvalidBufferSize = 'BufferSize must be greater than zero';
18+
SAStreamNilError = 'AStream cannot be nil';
19+
SFileNotFound = 'File "%s" not found';
20+
SCloneNotImplemented = 'Clone function not implemented for "%s" hash';
2121

2222
type
2323
THash = class abstract(TInterfacedObject, IHash)
@@ -211,8 +211,8 @@ function THash.ComputeFile(const AFileName: String; AFrom, ALength: Int64)
211211

212212
function THash.Clone(): IHash;
213213
begin
214-
raise ENotImplementedHashLibException.CreateResFmt
215-
(@SCloneNotYetImplemented, [Name]);
214+
raise ENotImplementedHashLibException.CreateResFmt(@SCloneNotImplemented,
215+
[Name]);
216216
end;
217217

218218
function THash.ComputeBytes(const AData: THashLibByteArray): IHashResult;
@@ -264,7 +264,8 @@ procedure THash.TransformStream(const AStream: TStream; ALength: Int64);
264264
begin
265265
if ((AStream.Position + ALength) > AStream.Size) then
266266
begin
267-
raise EIndexOutOfRangeHashLibException.CreateRes(@SIndexOutOfRange);
267+
raise EIndexOutOfRangeHashLibException.CreateRes(
268+
@SReadBeyondStreamEndError);
268269
end;
269270
end;
270271

@@ -275,7 +276,7 @@ procedure THash.TransformStream(const AStream: TStream; ALength: Int64);
275276
end
276277
else
277278
begin
278-
raise EArgumentNilHashLibException.CreateRes(@SUnAssignedStream);
279+
raise EArgumentNilHashLibException.CreateRes(@SAStreamNilError);
279280
end;
280281

281282
if BufferSize > AStream.Size then // Sanity Check
@@ -342,7 +343,7 @@ procedure THash.TransformFile(const AFileName: String; AFrom, ALength: Int64);
342343
{$ENDIF DEBUG}
343344
if not FileExists(AFileName) then
344345
begin
345-
raise EArgumentHashLibException.CreateRes(@SFileNotExist);
346+
raise EArgumentHashLibException.CreateResFmt(@SFileNotFound, [AFileName]);
346347
end;
347348

348349
LFileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);

HashLib/src/Base/HlpHashResult.pas

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@ interface
2323
HlpArrayUtils;
2424

2525
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.';
3627

3728
type
3829
THashResult = class sealed(TInterfacedObject, IHashResult)
@@ -184,51 +175,56 @@ function THashResult.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
184175

185176
function THashResult.GetInt32: Int32;
186177
begin
187-
if (System.Length(FHash) <> 4) then
178+
if System.Length(FHash) <> sizeof(Int32) then
188179
begin
189-
raise EInvalidOperationHashLibException.CreateRes
190-
(@SImpossibleRepresentationInt32);
180+
raise EInvalidOperationHashLibException.CreateResFmt(
181+
@SDifferingSizeOfByteArrayAndIntType,
182+
[System.Length(FHash), sizeof(Int32)]);
191183
end;
192184
result := Int32((Int32(FHash[0]) shl 24) or (Int32(FHash[1]) shl 16) or
193185
(Int32(FHash[2]) shl 8) or (Int32(FHash[3])));
194186
end;
195187

196188
function THashResult.GetUInt8: UInt8;
197189
begin
198-
if (System.Length(FHash) <> 1) then
190+
if System.Length(FHash) <> sizeof(UInt8) then
199191
begin
200-
raise EInvalidOperationHashLibException.CreateRes
201-
(@SImpossibleRepresentationUInt8);
192+
raise EInvalidOperationHashLibException.CreateResFmt(
193+
@SDifferingSizeOfByteArrayAndIntType,
194+
[System.Length(FHash), sizeof(UInt8)]);
202195
end;
203196
result := (UInt8(FHash[0]));
204197
end;
205198

206199
function THashResult.GetUInt16: UInt16;
207200
begin
208-
if (System.Length(FHash) <> 2) then
201+
if System.Length(FHash) <> sizeof(UInt16) then
209202
begin
210-
raise EInvalidOperationHashLibException.CreateRes
211-
(@SImpossibleRepresentationUInt16);
203+
raise EInvalidOperationHashLibException.CreateResFmt(
204+
@SDifferingSizeOfByteArrayAndIntType,
205+
[System.Length(FHash), sizeof(UInt16)]);
212206
end;
213207
result := (UInt16(FHash[0]) shl 8) or (UInt16(FHash[1]));
214208
end;
215209

216210
function THashResult.GetUInt32: UInt32;
217211
begin
218-
if (System.Length(FHash) <> 4) then
212+
if System.Length(FHash) <> sizeof(UInt32) then
219213
begin
220-
raise EInvalidOperationHashLibException.CreateRes
221-
(@SImpossibleRepresentationUInt32);
214+
raise EInvalidOperationHashLibException.CreateResFmt(
215+
@SDifferingSizeOfByteArrayAndIntType,
216+
[System.Length(FHash), sizeof(UInt32)]);
222217
end;
223218
result := TConverters.ReadBytesAsUInt32BE(PByte(FHash), 0);
224219
end;
225220

226221
function THashResult.GetUInt64: UInt64;
227222
begin
228-
if (System.Length(FHash) <> 8) then
223+
if System.Length(FHash) <> sizeof(UInt64) then
229224
begin
230-
raise EInvalidOperationHashLibException.CreateRes
231-
(@SImpossibleRepresentationUInt64);
225+
raise EInvalidOperationHashLibException.CreateResFmt(
226+
@SDifferingSizeOfByteArrayAndIntType,
227+
[System.Length(FHash), sizeof(UInt64)]);
232228
end;
233229
result := TConverters.ReadBytesAsUInt64BE(PByte(FHash), 0);
234230
end;

HashLib/src/Utils/HlpConverters.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface
1313
HlpBitConverter;
1414

1515
resourcestring
16-
SEncodingInstanceNil = 'Encoding Instance Cannot Be Nil';
16+
SAEncodingNilError = 'AEncoding cannot be nil';
1717

1818
type
1919
TConverters = class sealed(TObject)
@@ -491,7 +491,7 @@ class function TConverters.ConvertStringToBytes(const AInput: String;
491491
begin
492492
if AEncoding = Nil then
493493
begin
494-
raise EArgumentNilHashLibException.CreateRes(@SEncodingInstanceNil);
494+
raise EArgumentNilHashLibException.CreateRes(@SAEncodingNilError);
495495
end;
496496

497497
{$IFDEF FPC}
@@ -506,7 +506,7 @@ class function TConverters.ConvertBytesToString(const AInput: THashLibByteArray;
506506
begin
507507
if AEncoding = Nil then
508508
begin
509-
raise EArgumentNilHashLibException.CreateRes(@SEncodingInstanceNil);
509+
raise EArgumentNilHashLibException.CreateRes(@SAEncodingNilError);
510510
end;
511511

512512
{$IFDEF FPC}

0 commit comments

Comments
 (0)