From d0cc51f22417b9c39518b4726ca862a0a1361dc4 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sat, 5 Aug 2023 17:50:12 -0500 Subject: [PATCH] Optimize to use the same vector repeatedly and return a reference to it instead of copying. --- SHNFileDecryptor/SHNFileDecryptor.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/SHNFileDecryptor/SHNFileDecryptor.h b/SHNFileDecryptor/SHNFileDecryptor.h index 8d6e2b6..af6a18d 100644 --- a/SHNFileDecryptor/SHNFileDecryptor.h +++ b/SHNFileDecryptor/SHNFileDecryptor.h @@ -81,6 +81,8 @@ private: std::vectorcolumns; std::vector>contents; std::string filename; + std::vectorreadArr; + std::byte*fileMarker=0; public: void Load(std::string file); void Save(); @@ -140,8 +142,8 @@ void SHNFile::Decrypt(){ num = num3; } } -std::vectorSHNFile::ReadBytes(int bytes){ - std::vectorbyteArr={}; +std::vector&SHNFile::ReadBytes(int bytes){ + readArr.clear(); for(int i=0;iSHNFile::ReadBytes(int bytes){ return byteArr; } std::byte SHNFile::ReadByte(){ - std::vectorb=ReadBytes(1); + std::vector&b=ReadBytes(1); if(b.size()>0){ return b[0]; } else { @@ -167,7 +169,7 @@ void SHNFile::WriteSByte(std::ofstream&f,sbyte b){ f<intBytes=ReadBytes(2); + std::vector&intBytes=ReadBytes(2); uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]); return numb; } @@ -175,7 +177,7 @@ void SHNFile::WriteUInt16(std::ofstream&f,uint16_t val){ f<>8)&0xFF); } int16_t SHNFile::ReadInt16(){ - std::vectorintBytes=ReadBytes(2); + std::vector&intBytes=ReadBytes(2); int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]); return numb; } @@ -183,7 +185,7 @@ void SHNFile::WriteInt16(std::ofstream&f,int16_t val){ f<>8)&0xFF); } uint32_t SHNFile::ReadUInt32(){ - std::vectorintBytes=ReadBytes(4); + std::vector&intBytes=ReadBytes(4); uint32_t numb = uint32_t(intBytes[3])<<24|uint32_t(intBytes[2])<<16|uint32_t(intBytes[1])<<8|uint32_t(intBytes[0]); return numb; } @@ -191,12 +193,12 @@ void SHNFile::WriteUInt32(std::ofstream&f,uint32_t val){ f<>8)&0xFF)<>16)&0xFF)<>24)&0xFF); } int SHNFile::Read(){ - std::vectorintBytes=ReadBytes(4); + std::vector&intBytes=ReadBytes(4); int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); return numb; } int32_t SHNFile::ReadInt32(){ - std::vectorintBytes=ReadBytes(4); + std::vector&intBytes=ReadBytes(4); int32_t numb = int32_t(intBytes[3])<<24|int32_t(intBytes[2])<<16|int32_t(intBytes[1])<<8|uint32_t(intBytes[0]); return numb; } @@ -204,7 +206,7 @@ void SHNFile::WriteInt32(std::ofstream&f,int32_t val){ f<>8)&0xFF)<>16)&0xFF)<>24)&0xFF); } std::string SHNFile::ReadString(int bytes){ - std::vectorstrBytes=ReadBytes(bytes); + std::vector&strBytes=ReadBytes(bytes); std::string str=""; for(int i=0;i