From bda71c067bb67d93a1362fc13bdf933e9bb40fbe Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 13 Jun 2023 15:04:30 +0200 Subject: [PATCH] Port #1166 to `StringAnsi` --- Source/Engine/Core/Types/String.cpp | 35 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Source/Engine/Core/Types/String.cpp b/Source/Engine/Core/Types/String.cpp index cd88239c5..cee60c07c 100644 --- a/Source/Engine/Core/Types/String.cpp +++ b/Source/Engine/Core/Types/String.cpp @@ -43,15 +43,15 @@ void String::Set(const Char* chars, int32 length) } else { - Char* alloc = nullptr; + Char* data = nullptr; if (length != 0) { - alloc = (Char*)Platform::Allocate((length + 1) * sizeof(Char), 16); - alloc[length] = 0; - Platform::MemoryCopy(alloc, chars, length * sizeof(Char)); + data = (Char*)Platform::Allocate((length + 1) * sizeof(Char), 16); + Platform::MemoryCopy(data, chars, length * sizeof(Char)); + data[length] = 0; } Platform::Free(_data); - _data = alloc; + _data = data; _length = length; } } @@ -363,23 +363,26 @@ StringAnsi::StringAnsi(const StringAnsiView& str) void StringAnsi::Set(const char* chars, int32 length) { - if (length != _length) + ASSERT(length >= 0); + if (length == _length) { - ASSERT(length >= 0); - Platform::Free(_data); + if (_data == chars) + return; + Platform::MemoryCopy(_data, chars, length * sizeof(char)); + } + else + { + char* data = nullptr; if (length != 0) { - _data = (char*)Platform::Allocate((length + 1) * sizeof(char), 16); - _data[length] = 0; - } - else - { - _data = nullptr; + data = (char*)Platform::Allocate((length + 1) * sizeof(char), 16); + Platform::MemoryCopy(data, chars, length * sizeof(char)); + data[length] = 0; } + Platform::Free(_data); + _data = data; _length = length; } - - Platform::MemoryCopy(_data, chars, length * sizeof(char)); } void StringAnsi::Set(const Char* chars, int32 length)