OpenTTD Source 15.0-beta2
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(const char *str, size_type len) = 0;
30
34 void PutBuffer(std::span<const char> str) { this->PutBuffer(str.data(), str.size()); }
35
39 void Put(std::string_view str) { this->PutBuffer(str.data(), str.size()); }
40
41 void PutUint8(uint8_t value);
42 void PutSint8(int8_t value);
43 void PutUint16LE(uint16_t value);
44 void PutSint16LE(int16_t value);
45 void PutUint32LE(uint32_t value);
46 void PutSint32LE(int32_t value);
47 void PutUint64LE(uint64_t value);
48 void PutSint64LE(int64_t value);
49
50 void PutChar(char c);
51 void PutUtf8(char32_t c);
52
56 template<class T>
57 void PutIntegerBase(T value, int base)
58 {
59 std::array<char, 32> buf;
60 auto result = std::to_chars(buf.data(), buf.data() + buf.size(), value, base);
61 if (result.ec != std::errc{}) return;
62 size_type len = result.ptr - buf.data();
63 this->PutBuffer(buf.data(), len);
64 }
65};
66
70class StringBuilder final : public BaseStringBuilder
71{
72 std::string *dest;
73public:
78 StringBuilder(std::string &dest) : dest(&dest) {}
79
83 [[nodiscard]] bool AnyBytesWritten() const noexcept { return !this->dest->empty(); }
87 [[nodiscard]] size_type GetBytesWritten() const noexcept { return this->dest->size(); }
91 [[nodiscard]] const std::string &GetWrittenData() const noexcept { return *dest; }
95 [[nodiscard]] std::string &GetString() noexcept { return *dest; }
96
98 void PutBuffer(const char *str, size_type len) override;
99
103 StringBuilder& operator+=(std::string_view str)
104 {
105 this->Put(str);
106 return *this;
107 }
108
109 using back_insert_iterator = std::back_insert_iterator<std::string>;
113 back_insert_iterator back_inserter()
114 {
115 return back_insert_iterator(*this->dest);
116 }
117};
118
119#endif /* STRING_BUILDER_HPP */
Compose data into a string / 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 PutBuffer(std::span< const char > str)
Append span.
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.
virtual void PutBuffer(const char *str, size_type len)=0
Append buffer.
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.
void PutBuffer(const char *str, size_type len) override
Append buffer.
StringBuilder & operator+=(std::string_view str)
Append string.
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.