From b634088b9efc244d31c20b7004fe6ef342cd717f Mon Sep 17 00:00:00 2001 From: maelh <42317414+maelh@users.noreply.github.com> Date: Sat, 1 May 2021 18:08:05 +0200 Subject: [PATCH] Reworded some error messages and switched to sentence case (#21) --- HashLib/src/Base/HlpHash.pas | 21 ++++++------- HashLib/src/Base/HlpHashResult.pas | 46 +++++++++++++---------------- HashLib/src/Utils/HlpConverters.pas | 6 ++-- 3 files changed, 35 insertions(+), 38 deletions(-) diff --git a/HashLib/src/Base/HlpHash.pas b/HashLib/src/Base/HlpHash.pas index d1ea943..f82b648 100644 --- a/HashLib/src/Base/HlpHash.pas +++ b/HashLib/src/Base/HlpHash.pas @@ -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) @@ -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; @@ -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; @@ -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 @@ -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); diff --git a/HashLib/src/Base/HlpHashResult.pas b/HashLib/src/Base/HlpHashResult.pas index 44ee0f8..8d86061 100644 --- a/HashLib/src/Base/HlpHashResult.pas +++ b/HashLib/src/Base/HlpHashResult.pas @@ -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) @@ -184,10 +175,11 @@ 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]))); @@ -195,40 +187,44 @@ function THashResult.GetInt32: Int32; 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; diff --git a/HashLib/src/Utils/HlpConverters.pas b/HashLib/src/Utils/HlpConverters.pas index 1ccaa6f..7c9dd7c 100644 --- a/HashLib/src/Utils/HlpConverters.pas +++ b/HashLib/src/Utils/HlpConverters.pas @@ -13,7 +13,7 @@ interface HlpBitConverter; resourcestring - SEncodingInstanceNil = 'Encoding Instance Cannot Be Nil'; + SAEncodingNilError = 'AEncoding cannot be nil'; type TConverters = class sealed(TObject) @@ -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} @@ -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}