Skip to content

Commit

Permalink
Reworded some error messages and switched to sentence case (Xor-el#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelh authored May 1, 2021
1 parent fa78952 commit b634088
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
21 changes: 11 additions & 10 deletions HashLib/src/Base/HlpHash.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ interface
HlpIHashResult;

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

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

function THash.Clone(): IHash;
begin
raise ENotImplementedHashLibException.CreateResFmt
(@SCloneNotYetImplemented, [Name]);
raise ENotImplementedHashLibException.CreateResFmt(@SCloneNotImplemented,
[Name]);
end;

function THash.ComputeBytes(const AData: THashLibByteArray): IHashResult;
Expand Down Expand Up @@ -264,7 +264,8 @@ procedure THash.TransformStream(const AStream: TStream; ALength: Int64);
begin
if ((AStream.Position + ALength) > AStream.Size) then
begin
raise EIndexOutOfRangeHashLibException.CreateRes(@SIndexOutOfRange);
raise EIndexOutOfRangeHashLibException.CreateRes(
@SReadBeyondStreamEndError);
end;
end;

Expand All @@ -275,7 +276,7 @@ procedure THash.TransformStream(const AStream: TStream; ALength: Int64);
end
else
begin
raise EArgumentNilHashLibException.CreateRes(@SUnAssignedStream);
raise EArgumentNilHashLibException.CreateRes(@SAStreamNilError);
end;

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

LFileStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
Expand Down
46 changes: 21 additions & 25 deletions HashLib/src/Base/HlpHashResult.pas
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ interface
HlpArrayUtils;

resourcestring
SImpossibleRepresentationInt32 =
'Current Data Structure cannot be Represented as an "Int32" Type.';
SImpossibleRepresentationUInt8 =
'Current Data Structure cannot be Represented as an "UInt8" Type.';
SImpossibleRepresentationUInt16 =
'Current Data Structure cannot be Represented as an "UInt16" Type.';
SImpossibleRepresentationUInt32 =
'Current Data Structure cannot be Represented as an "UInt32" Type.';
SImpossibleRepresentationUInt64 =
'Current Data Structure cannot be Represented as an "UInt64" Type.';
SDifferingSizeOfByteArrayAndIntType = 'The size of the byte array (%0:d) and integer type (%1:d) have to match.';

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

function THashResult.GetInt32: Int32;
begin
if (System.Length(FHash) <> 4) then
if System.Length(FHash) <> sizeof(Int32) then
begin
raise EInvalidOperationHashLibException.CreateRes
(@SImpossibleRepresentationInt32);
raise EInvalidOperationHashLibException.CreateResFmt(
@SDifferingSizeOfByteArrayAndIntType,
[System.Length(FHash), sizeof(Int32)]);
end;
result := Int32((Int32(FHash[0]) shl 24) or (Int32(FHash[1]) shl 16) or
(Int32(FHash[2]) shl 8) or (Int32(FHash[3])));
end;

function THashResult.GetUInt8: UInt8;
begin
if (System.Length(FHash) <> 1) then
if System.Length(FHash) <> sizeof(UInt8) then
begin
raise EInvalidOperationHashLibException.CreateRes
(@SImpossibleRepresentationUInt8);
raise EInvalidOperationHashLibException.CreateResFmt(
@SDifferingSizeOfByteArrayAndIntType,
[System.Length(FHash), sizeof(UInt8)]);
end;
result := (UInt8(FHash[0]));
end;

function THashResult.GetUInt16: UInt16;
begin
if (System.Length(FHash) <> 2) then
if System.Length(FHash) <> sizeof(UInt16) then
begin
raise EInvalidOperationHashLibException.CreateRes
(@SImpossibleRepresentationUInt16);
raise EInvalidOperationHashLibException.CreateResFmt(
@SDifferingSizeOfByteArrayAndIntType,
[System.Length(FHash), sizeof(UInt16)]);
end;
result := (UInt16(FHash[0]) shl 8) or (UInt16(FHash[1]));
end;

function THashResult.GetUInt32: UInt32;
begin
if (System.Length(FHash) <> 4) then
if System.Length(FHash) <> sizeof(UInt32) then
begin
raise EInvalidOperationHashLibException.CreateRes
(@SImpossibleRepresentationUInt32);
raise EInvalidOperationHashLibException.CreateResFmt(
@SDifferingSizeOfByteArrayAndIntType,
[System.Length(FHash), sizeof(UInt32)]);
end;
result := TConverters.ReadBytesAsUInt32BE(PByte(FHash), 0);
end;

function THashResult.GetUInt64: UInt64;
begin
if (System.Length(FHash) <> 8) then
if System.Length(FHash) <> sizeof(UInt64) then
begin
raise EInvalidOperationHashLibException.CreateRes
(@SImpossibleRepresentationUInt64);
raise EInvalidOperationHashLibException.CreateResFmt(
@SDifferingSizeOfByteArrayAndIntType,
[System.Length(FHash), sizeof(UInt64)]);
end;
result := TConverters.ReadBytesAsUInt64BE(PByte(FHash), 0);
end;
Expand Down
6 changes: 3 additions & 3 deletions HashLib/src/Utils/HlpConverters.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface
HlpBitConverter;

resourcestring
SEncodingInstanceNil = 'Encoding Instance Cannot Be Nil';
SAEncodingNilError = 'AEncoding cannot be nil';

type
TConverters = class sealed(TObject)
Expand Down Expand Up @@ -491,7 +491,7 @@ class function TConverters.ConvertStringToBytes(const AInput: String;
begin
if AEncoding = Nil then
begin
raise EArgumentNilHashLibException.CreateRes(@SEncodingInstanceNil);
raise EArgumentNilHashLibException.CreateRes(@SAEncodingNilError);
end;

{$IFDEF FPC}
Expand All @@ -506,7 +506,7 @@ class function TConverters.ConvertBytesToString(const AInput: THashLibByteArray;
begin
if AEncoding = Nil then
begin
raise EArgumentNilHashLibException.CreateRes(@SEncodingInstanceNil);
raise EArgumentNilHashLibException.CreateRes(@SAEncodingNilError);
end;

{$IFDEF FPC}
Expand Down

0 comments on commit b634088

Please sign in to comment.