From 332e9e76d5cd976ffc4453032018a5bf584929fc Mon Sep 17 00:00:00 2001 From: Gabriel Guedes Date: Sat, 29 Aug 2020 10:14:44 -0300 Subject: [PATCH 1/2] Part of #11159: Removing unused functions from StringBuilder.hpp --- src/openrct2/core/StringBuilder.hpp | 46 ----------------------------- 1 file changed, 46 deletions(-) diff --git a/src/openrct2/core/StringBuilder.hpp b/src/openrct2/core/StringBuilder.hpp index 3a9a81f003..aa5b36e2dd 100644 --- a/src/openrct2/core/StringBuilder.hpp +++ b/src/openrct2/core/StringBuilder.hpp @@ -94,44 +94,6 @@ public: } } - /** - * Like Clear, only will guarantee freeing of the underlying buffer. - */ - void Reset() - { - _length = 0; - _capacity = 0; - SafeFree(_buffer); - } - - /** - * Resets the StringBuilder and returns the working buffer (resized to the string size). - */ - utf8* StealString() - { - utf8* result = _buffer; - result = Memory::ReallocateArray(result, _length + 1); - result[_length] = 0; - - _length = 0; - _capacity = 0; - _buffer = nullptr; - - return result; - } - - /** - * Returns the current string buffer as a new fire-and-forget string. - */ - utf8* GetString() const - { - // If buffer is null, length should be 0 which will create a new one byte memory block containing a null terminator - utf8* result = Memory::AllocateArray(_length + 1); - std::copy_n(_buffer, _length, result); - result[_length] = 0; - return result; - } - /** * Returns the current string buffer as a standard string. */ @@ -152,14 +114,6 @@ public: return _buffer; } - /** - * Gets the amount of allocated memory for the string buffer. - */ - size_t GetCapacity() const - { - return _capacity; - } - /** * Gets the length of the current string. */ From 95ae589750a6cc0102eafecd740f321f2dd3e57e Mon Sep 17 00:00:00 2001 From: Gabriel Guedes Date: Sat, 29 Aug 2020 11:16:05 -0300 Subject: [PATCH 2/2] Part of #11159: Prefer std::basic_string over raw pointer Usage of std::basic_string to improve readability and maintainability. --- src/openrct2/core/StringBuilder.hpp | 55 ++++++++--------------------- 1 file changed, 14 insertions(+), 41 deletions(-) diff --git a/src/openrct2/core/StringBuilder.hpp b/src/openrct2/core/StringBuilder.hpp index aa5b36e2dd..3b259581bf 100644 --- a/src/openrct2/core/StringBuilder.hpp +++ b/src/openrct2/core/StringBuilder.hpp @@ -10,10 +10,10 @@ #pragma once #include "../common.h" -#include "Memory.hpp" #include "String.hpp" #include +#include #include /** @@ -26,13 +26,10 @@ public: StringBuilder() = default; explicit StringBuilder(size_t capacity) { - EnsureCapacity(capacity); + _buffer.reserve(capacity); } - ~StringBuilder() - { - Memory::Free(_buffer); - } + ~StringBuilder() = default; void Append(int32_t codepoint) { @@ -45,10 +42,9 @@ public: void Append(codepoint_t codepoint) { size_t codepointLength = String::GetCodepointLength(codepoint); - EnsureCapacity(_length + codepointLength + 1); - String::WriteCodepoint(_buffer + _length, codepoint); - _length += codepointLength; - _buffer[_length] = 0; + std::basic_string data(codepointLength, {}); + String::WriteCodepoint(data.data(), codepoint); + _buffer.insert(_buffer.end(), data.begin(), data.end()); } /** @@ -68,10 +64,7 @@ public: */ void Append(const utf8* text, size_t textLength) { - EnsureCapacity(_length + textLength + 1); - std::copy_n(text, textLength, _buffer + _length); - _length += textLength; - _buffer[_length] = 0; + _buffer.insert(_buffer.end(), text, text + textLength); } /** @@ -87,11 +80,7 @@ public: */ void Clear() { - _length = 0; - if (_capacity >= 1) - { - _buffer[_length] = 0; - } + _buffer.clear(); } /** @@ -99,7 +88,7 @@ public: */ std::string GetStdString() const { - return std::string(_buffer, _length); + return std::string(GetBuffer(), GetLength()); } /** @@ -108,10 +97,10 @@ public: */ const utf8* GetBuffer() const { - // buffer may be null, so return an immutable empty string - if (_buffer == nullptr) + // buffer may be empty, so return an immutable empty string + if (_buffer.empty()) return ""; - return _buffer; + return _buffer.c_str(); } /** @@ -119,25 +108,9 @@ public: */ size_t GetLength() const { - return _length; + return _buffer.size(); } private: - utf8* _buffer = nullptr; - size_t _capacity = 0; - size_t _length = 0; - - void EnsureCapacity(size_t capacity) - { - if (_capacity > capacity) - return; - - _capacity = std::max(static_cast(8), _capacity); - while (_capacity < capacity) - { - _capacity *= 2; - } - - _buffer = Memory::ReallocateArray(_buffer, _capacity); - } + std::basic_string _buffer; };