OpenTTD Source 20250529-master-g10c159a79f
string_builder.hpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
12#ifndef STRING_BUILDER_HPP
13#define STRING_BUILDER_HPP
14
15#include <charconv>
16
21public:
22 using size_type = std::string_view::size_type;
23
24 virtual ~BaseStringBuilder() = default;
25
29 virtual void PutBuffer(std::span<const char> str) = 0;
30
34 void Put(std::string_view str) { this->PutBuffer(str); }
35
36 void PutUint8(uint8_t value);
37 void PutSint8(int8_t value);
38 void PutUint16LE(uint16_t value);
39 void PutSint16LE(int16_t value);
40 void PutUint32LE(uint32_t value);
41 void PutSint32LE(int32_t value);
42 void PutUint64LE(uint64_t value);
43 void PutSint64LE(int64_t value);
44
45 void PutChar(char c);
46 void PutUtf8(char32_t c);
47
51 template <class T>
52 void PutIntegerBase(T value, int base)
53 {
54 std::array<char, 32> buf;
55 auto result = std::to_chars(buf.data(), buf.data() + buf.size(), value, base);
56 if (result.ec != std::errc{}) return;
57 size_type len = result.ptr - buf.data();
58 this->PutBuffer({buf.data(), len});
59 }
60};
61
65class StringBuilder final : public BaseStringBuilder {
66 std::string *dest;
67public:
72 StringBuilder(std::string &dest) : dest(&dest) {}
73
77 [[nodiscard]] bool AnyBytesWritten() const noexcept { return !this->dest->empty(); }
81 [[nodiscard]] size_type GetBytesWritten() const noexcept { return this->dest->size(); }
85 [[nodiscard]] const std::string &GetWrittenData() const noexcept { return *dest; }
89 [[nodiscard]] std::string &GetString() noexcept { return *dest; }
90
91 void PutBuffer(std::span<const char> str) override;
92
96 StringBuilder& operator+=(std::string_view str)
97 {
98 this->Put(str);
99 return *this;
100 }
101
102 using back_insert_iterator = std::back_insert_iterator<std::string>;
106 back_insert_iterator back_inserter()
107 {
108 return back_insert_iterator(*this->dest);
109 }
110};
111
112#endif /* STRING_BUILDER_HPP */
Compose data into a string / buffer.
virtual void PutBuffer(std::span< const char > str)=0
Append buffer.
void PutUint32LE(uint32_t value)
Append binary uint32 using little endian.
void PutSint8(int8_t value)
Append binary int8.
void PutUint64LE(uint64_t value)
Append binary uint64 using little endian.
void PutSint64LE(int64_t value)
Append binary int64 using little endian.
void PutUtf8(char32_t c)
Append UTF.8 char.
void PutUint16LE(uint16_t value)
Append binary uint16 using little endian.
void PutSint32LE(int32_t value)
Append binary int32 using little endian.
void Put(std::string_view str)
Append string.
void PutIntegerBase(T value, int base)
Append integer 'value' in given number 'base'.
void PutChar(char c)
Append 8-bit char.
void PutSint16LE(int16_t value)
Append binary int16 using little endian.
void PutUint8(uint8_t value)
Append binary uint8.
Compose data into a growing std::string.
const std::string & GetWrittenData() const noexcept
Get already written data.
std::string & GetString() noexcept
Get mutable already written data.
back_insert_iterator back_inserter()
Create a back-insert-iterator.
StringBuilder & operator+=(std::string_view str)
Append string.
void PutBuffer(std::span< const char > str) override
Append buffer.
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
StringBuilder(std::string &dest)
Construct StringBuilder into destination string.
bool AnyBytesWritten() const noexcept
Check whether any bytes have been written.