1 /*****************************************************************************
2  * Copyright (c) 2014-2020 OpenRCT2 developers
3  *
4  * For a complete list of all authors, please refer to contributors.md
5  * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6  *
7  * OpenRCT2 is licensed under the GNU General Public License version 3.
8  *****************************************************************************/
9 
10 #include "StringBuilder.h"
11 
12 #include "String.hpp"
13 
14 #include <algorithm>
15 #include <iterator>
16 
StringBuilder(size_t capacity)17 StringBuilder::StringBuilder(size_t capacity)
18 {
19     _buffer.reserve(capacity);
20 }
21 
Append(int32_t codepoint)22 void StringBuilder::Append(int32_t codepoint)
23 {
24     Append(static_cast<codepoint_t>(codepoint));
25 }
26 
Append(codepoint_t codepoint)27 void StringBuilder::Append(codepoint_t codepoint)
28 {
29     size_t codepointLength = String::GetCodepointLength(codepoint);
30     std::basic_string<utf8> data(codepointLength, {});
31     String::WriteCodepoint(data.data(), codepoint);
32     _buffer.insert(_buffer.end(), data.begin(), data.end());
33 }
34 
Append(const utf8 * text)35 void StringBuilder::Append(const utf8* text)
36 {
37     size_t textLength = String::SizeOf(text);
38     Append(text, textLength);
39 }
40 
Append(const utf8 * text,size_t textLength)41 void StringBuilder::Append(const utf8* text, size_t textLength)
42 {
43     _buffer.insert(_buffer.end(), text, text + textLength);
44 }
45 
Append(const StringBuilder * sb)46 void StringBuilder::Append(const StringBuilder* sb)
47 {
48     Append(sb->GetBuffer(), sb->GetLength());
49 }
50 
Clear()51 void StringBuilder::Clear()
52 {
53     _buffer.clear();
54 }
55 
GetStdString() const56 std::string StringBuilder::GetStdString() const
57 {
58     return std::string(GetBuffer(), GetLength());
59 }
60 
GetBuffer() const61 const utf8* StringBuilder::GetBuffer() const
62 {
63     // buffer may be empty, so return an immutable empty string
64     if (_buffer.empty())
65         return "";
66     return _buffer.c_str();
67 }
68 
GetLength() const69 size_t StringBuilder::GetLength() const
70 {
71     return _buffer.size();
72 }
73