Adjusted strings in Serializer.

This commit is contained in:
Arron David Nelson 2024-02-17 00:17:31 -08:00
parent d394ca8445
commit 54065f6ce3

View File

@ -401,9 +401,11 @@ namespace ehs
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)
{ {
if (sizeof(N) + sizeof(T) * str.Size() > size - offset) N inSize = sizeof(T) * (str.Size() + 1);
if (inSize > size - offset)
{ {
N remainder = sizeof(N) + sizeof(T) * str.Size() - (size - offset); N remainder = inSize - (size - offset);
Byte* r = new Byte[size + remainder]; Byte* r = new Byte[size + remainder];
@ -419,54 +421,38 @@ namespace ehs
{ {
if (endianness == Endianness::LE) if (endianness == Endianness::LE)
{ {
*(N*)&data[offset] = (N)str.Size(); Util::Copy(&data[offset], &str[0], str.Size(true));
offset += sizeof(N); *(T*)&data[offset + str.Size()] = 0;
for (N i = 0; i < str.Size(); ++i)
*(T*)&data[offset + i * sizeof(T)] = str[i];
} }
else else
{ {
N tmpSize = (N)str.Size();
for (N i = 0; i < sizeof(N); ++i)
data[offset + i] = ((Byte*)&tmpSize)[sizeof(N) - i - 1];
offset += sizeof(N);
for (N i = 0; i < str.Size(); ++i) for (N i = 0; i < str.Size(); ++i)
for (N b = 0; b < sizeof(T); ++b) for (N b = 0; b < sizeof(T); ++b)
data[offset + i * sizeof(T) + 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;
} }
} }
else else
{ {
if (endianness == Endianness::LE) if (endianness == Endianness::LE)
{ {
N tmpSize = (N)str.Size();
for (N i = 0; i < sizeof(N); ++i)
data[offset + i] = ((Byte*)&tmpSize)[sizeof(N) - i - 1];
offset += sizeof(N);
for (N i = 0; i < str.Size(); ++i) for (N i = 0; i < str.Size(); ++i)
for (N b = 0; b < sizeof(T); ++b) for (N b = 0; b < sizeof(T); ++b)
data[offset + i * sizeof(T) + 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;
} }
else else
{ {
*(N*)&data[offset] = (N)str.Size(); Util::Copy(&data[offset], &str[0], str.Size(true));
offset += sizeof(N); *(T*)&data[offset + str.Size()] = 0;
for (N i = 0; i < str.Size(); ++i)
*(T*)&data[offset + i * sizeof(T)] = str[i];
} }
} }
offset += str.Size() * sizeof(T); offset += inSize;
} }
void WriteVersion(const Version& value) void WriteVersion(const Version& value)
@ -1014,12 +1000,34 @@ namespace ehs
} }
template<typename T, typename O> template<typename T, typename O>
Str<T, O> ReadStr(const O size = 0) Str<T, O> ReadStr(O size = 0)
{ {
Str<T, O> result(size ? size : (O)Read<N>()); if (!size)
while (&data[offset + sizeof(T) * size])
size++;
for (O i = 0; i < result.Size(); ++i) Str<T, O> result(size);
result[i] = Read<T>();
if (CPU::GetEndianness() == Endianness::LE)
{
if (endianness == Endianness::LE)
Util::Copy(&result[0], &data[offset], result.Size(true));
else
for (N i = 0; i < size; ++i)
for (N b = 0; b < sizeof(T); ++b)
((Byte*)&result[i])[sizeof(T) - i - 1] = data[offset + sizeof(T) * i + b];
}
else
{
if (endianness == Endianness::LE)
for (N i = 0; i < size; ++i)
for (N b = 0; b < sizeof(T); ++b)
((Byte*)&result[i])[sizeof(T) - i - 1] = data[offset + sizeof(T) * i + b];
else
Util::Copy(&result[0], &data[offset], result.Size(true));
}
offset += result.Size(true) + 1;
return result; return result;
} }