Optimize to use the same vector repeatedly and return a reference to it instead of copying.

master
sigonasr2 1 year ago
parent e516dbf65a
commit d0cc51f224
  1. 20
      SHNFileDecryptor/SHNFileDecryptor.h

@ -81,6 +81,8 @@ private:
std::vector<Column>columns; std::vector<Column>columns;
std::vector<std::vector<Data>>contents; std::vector<std::vector<Data>>contents;
std::string filename; std::string filename;
std::vector<std::byte>readArr;
std::byte*fileMarker=0;
public: public:
void Load(std::string file); void Load(std::string file);
void Save(); void Save();
@ -140,8 +142,8 @@ void SHNFile::Decrypt(){
num = num3; num = num3;
} }
} }
std::vector<std::byte>SHNFile::ReadBytes(int bytes){ std::vector<std::byte>&SHNFile::ReadBytes(int bytes){
std::vector<std::byte>byteArr={}; readArr.clear();
for(int i=0;i<bytes;i++){ for(int i=0;i<bytes;i++){
if(marker<data.size()){ if(marker<data.size()){
byteArr.push_back(data[marker]); byteArr.push_back(data[marker]);
@ -153,7 +155,7 @@ std::vector<std::byte>SHNFile::ReadBytes(int bytes){
return byteArr; return byteArr;
} }
std::byte SHNFile::ReadByte(){ std::byte SHNFile::ReadByte(){
std::vector<std::byte>b=ReadBytes(1); std::vector<std::byte>&b=ReadBytes(1);
if(b.size()>0){ if(b.size()>0){
return b[0]; return b[0];
} else { } else {
@ -167,7 +169,7 @@ void SHNFile::WriteSByte(std::ofstream&f,sbyte b){
f<<char(b); f<<char(b);
} }
uint16_t SHNFile::ReadUInt16(){ uint16_t SHNFile::ReadUInt16(){
std::vector<std::byte>intBytes=ReadBytes(2); std::vector<std::byte>&intBytes=ReadBytes(2);
uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]); uint16_t numb = uint16_t(intBytes[1])<<8|uint16_t(intBytes[0]);
return numb; return numb;
} }
@ -175,7 +177,7 @@ void SHNFile::WriteUInt16(std::ofstream&f,uint16_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF);
} }
int16_t SHNFile::ReadInt16(){ int16_t SHNFile::ReadInt16(){
std::vector<std::byte>intBytes=ReadBytes(2); std::vector<std::byte>&intBytes=ReadBytes(2);
int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]); int16_t numb = int16_t(intBytes[1])<<8|int16_t(intBytes[0]);
return numb; return numb;
} }
@ -183,7 +185,7 @@ void SHNFile::WriteInt16(std::ofstream&f,int16_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF);
} }
uint32_t SHNFile::ReadUInt32(){ uint32_t SHNFile::ReadUInt32(){
std::vector<std::byte>intBytes=ReadBytes(4); std::vector<std::byte>&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]); uint32_t numb = uint32_t(intBytes[3])<<24|uint32_t(intBytes[2])<<16|uint32_t(intBytes[1])<<8|uint32_t(intBytes[0]);
return numb; return numb;
} }
@ -191,12 +193,12 @@ void SHNFile::WriteUInt32(std::ofstream&f,uint32_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF);
} }
int SHNFile::Read(){ int SHNFile::Read(){
std::vector<std::byte>intBytes=ReadBytes(4); std::vector<std::byte>&intBytes=ReadBytes(4);
int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]); int numb = int(intBytes[3])<<24|int(intBytes[2])<<16|int(intBytes[1])<<8|int(intBytes[0]);
return numb; return numb;
} }
int32_t SHNFile::ReadInt32(){ int32_t SHNFile::ReadInt32(){
std::vector<std::byte>intBytes=ReadBytes(4); std::vector<std::byte>&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]); int32_t numb = int32_t(intBytes[3])<<24|int32_t(intBytes[2])<<16|int32_t(intBytes[1])<<8|uint32_t(intBytes[0]);
return numb; return numb;
} }
@ -204,7 +206,7 @@ void SHNFile::WriteInt32(std::ofstream&f,int32_t val){
f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF); f<<unsigned char(val&0xFF)<<unsigned char((val>>8)&0xFF)<<unsigned char((val>>16)&0xFF)<<unsigned char((val>>24)&0xFF);
} }
std::string SHNFile::ReadString(int bytes){ std::string SHNFile::ReadString(int bytes){
std::vector<std::byte>strBytes=ReadBytes(bytes); std::vector<std::byte>&strBytes=ReadBytes(bytes);
std::string str=""; std::string str="";
for(int i=0;i<strBytes.size();i++){ for(int i=0;i<strBytes.size();i++){
if(strBytes[i]!=std::byte(0)){ if(strBytes[i]!=std::byte(0)){

Loading…
Cancel
Save