Fixed error in Serializer::WriteStr.

This commit is contained in:
Arron David Nelson 2024-02-17 00:42:24 -08:00
parent 7a79fa4d76
commit 8e88412deb

View File

@ -395,6 +395,67 @@ namespace ehs
offset += sizeof(T) * value.Size(); offset += sizeof(T) * value.Size();
} }
template<typename T, typename O = UInt_64>
void WriteStr(const T* str, N inSize = 0)
{
if (!inSize)
while (str[inSize])
inSize++;
N bSize = sizeof(T) * inSize;
N bSizeN = sizeof(T) * (inSize + 1);
if (bSizeN > size - offset)
{
N remainder = bSizeN - (size - offset);
Byte* r = new Byte[size + remainder];
Util::Copy(r, data, size);
delete[] data;
data = r;
size += remainder;
}
if (CPU::GetEndianness() == Endianness::LE)
{
if (endianness == Endianness::LE)
{
Util::Copy(&data[offset], &str[0], bSize);
*(T*)&data[offset + inSize] = 0;
}
else
{
for (N i = 0; i < inSize; ++i)
for (N b = 0; b < sizeof(T); ++b)
data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1];
*(T*)&data[offset + bSize] = 0;
}
}
else
{
if (endianness == Endianness::LE)
{
for (N i = 0; i < inSize; ++i)
for (N b = 0; b < sizeof(T); ++b)
data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1];
*(T*)&data[offset + bSize] = 0;
}
else
{
Util::Copy(&data[offset], &str[0], bSize);
*(T*)&data[offset + bSize] = 0;
}
}
offset += bSizeN;
}
template<typename T, typename O = UInt_64> template<typename T, typename O = UInt_64>
void WriteStr(const Str<T, O>& str) void WriteStr(const Str<T, O>& str)
{ {
@ -419,7 +480,7 @@ namespace ehs
{ {
Util::Copy(&data[offset], &str[0], str.Size(true)); Util::Copy(&data[offset], &str[0], str.Size(true));
*(T*)&data[offset + str.Size()] = 0; *(T*)&data[offset + str.Size(true)] = 0;
} }
else else
{ {
@ -427,7 +488,7 @@ namespace ehs
for (N b = 0; b < sizeof(T); ++b) for (N b = 0; b < sizeof(T); ++b)
data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1];
*(T*)&data[offset + str.Size()] = 0; *(T*)&data[offset + str.Size(true)] = 0;
} }
} }
else else
@ -438,13 +499,13 @@ namespace ehs
for (N b = 0; b < sizeof(T); ++b) for (N b = 0; b < sizeof(T); ++b)
data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1]; data[offset + sizeof(T) * i + b] = ((Byte*)&str[i])[sizeof(T) - i - 1];
*(T*)&data[offset + str.Size()] = 0; *(T*)&data[offset + str.Size(true)] = 0;
} }
else else
{ {
Util::Copy(&data[offset], &str[0], str.Size(true)); Util::Copy(&data[offset], &str[0], str.Size(true));
*(T*)&data[offset + str.Size()] = 0; *(T*)&data[offset + str.Size(true)] = 0;
} }
} }