Fix String::Resize to support buffer growing

This commit is contained in:
Wojtek Figat
2021-05-03 21:04:40 +02:00
parent c4c1655f76
commit b57c0a9db1

View File

@@ -497,18 +497,19 @@ public:
} }
/// <summary> /// <summary>
/// Resizes string contents, works like: *this = Left(length) but is faster. /// Resizes string contents.
/// </summary> /// </summary>
/// <param name="length">New length of the string (amount of characters from left side to preserve).</param> /// <param name="length">New length of the string.</param>
void Resize(int32 length) void Resize(int32 length)
{ {
ASSERT(length >= 0 && length <= _length); ASSERT(length >= 0);
if (_length != length) if (_length != length)
{ {
const auto oldData = _data; const auto oldData = _data;
const auto minLength = _length < length ? _length : length;
_length = length; _length = length;
_data = (T*)Platform::Allocate((length + 1) * sizeof(T), 16); _data = (T*)Platform::Allocate((length + 1) * sizeof(T), 16);
Platform::MemoryCopy(_data, oldData, length * sizeof(T)); Platform::MemoryCopy(_data, oldData, minLength * sizeof(T));
_data[length] = 0; _data[length] = 0;
Platform::Free(oldData); Platform::Free(oldData);
} }