OpenTTD Source 20260311-master-g511d3794ce
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef STRING_BUILDER_HPP
11#define STRING_BUILDER_HPP
12
13#include <charconv>
14
19public:
21 using size_type = std::string_view::size_type;
22
24 virtual ~BaseStringBuilder() = default;
25
30 virtual void PutBuffer(std::span<const char> str) = 0;
31
36 void Put(std::string_view str) { this->PutBuffer(str); }
37
38 void PutUint8(uint8_t value);
39 void PutSint8(int8_t value);
40 void PutUint16LE(uint16_t value);
41 void PutSint16LE(int16_t value);
42 void PutUint32LE(uint32_t value);
43 void PutSint32LE(int32_t value);
44 void PutUint64LE(uint64_t value);
45 void PutSint64LE(int64_t value);
46
47 void PutChar(char c);
48 void PutUtf8(char32_t c);
49
55 template <class T>
56 void PutIntegerBase(T value, int base)
57 {
58 std::array<char, 32> buf;
59 auto result = std::to_chars(buf.data(), buf.data() + buf.size(), value, base);
60 if (result.ec != std::errc{}) return;
61 size_type len = result.ptr - buf.data();
62 this->PutBuffer({buf.data(), len});
63 }
64};
65
69class StringBuilder final : public BaseStringBuilder {
70 std::string *dest;
71public:
77 StringBuilder(std::string &dest) : dest(&dest) {}
78
83 [[nodiscard]] bool AnyBytesWritten() const noexcept { return !this->dest->empty(); }
88 [[nodiscard]] size_type GetBytesWritten() const noexcept { return this->dest->size(); }
93 [[nodiscard]] const std::string &GetWrittenData() const noexcept { return *dest; }
98 [[nodiscard]] std::string &GetString() noexcept { return *dest; }
99
100 void PutBuffer(std::span<const char> str) override;
101
107 StringBuilder& operator+=(std::string_view str)
108 {
109 this->Put(str);
110 return *this;
111 }
112
114 using back_insert_iterator = std::back_insert_iterator<std::string>;
120 {
121 return back_insert_iterator(*this->dest);
122 }
123};
124
125#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.
std::string_view::size_type size_type
The type of the size of our strings.
virtual ~BaseStringBuilder()=default
Ensure the destructor of the sub classes are called as well.
void PutUint8(uint8_t value)
Append binary uint8.
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.
std::string * dest
The string to write to.
StringBuilder(std::string &dest)
Construct StringBuilder into destination string.
bool AnyBytesWritten() const noexcept
Check whether any bytes have been written.
std::back_insert_iterator< std::string > back_insert_iterator
Iterator for inserting at the back of our buffer.
#define T
Climate temperate.
Definition engines.h:91