OpenTTD Source: src/core/string_builder.hpp Source File

OpenTTD Source 15.3

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
12#ifndef STRING_BUILDER_HPP
13#define STRING_BUILDER_HPP
14
15#include <charconv>
16
20 class BaseStringBuilder {
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
65 class 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 */
BaseStringBuilder
Compose data into a string / buffer.
Definition string_builder.hpp:20
BaseStringBuilder::PutBuffer
virtual void PutBuffer(std::span< const char > str)=0
Append buffer.
BaseStringBuilder::PutUint32LE
void PutUint32LE(uint32_t value)
Append binary uint32 using little endian.
Definition string_builder.cpp:57
BaseStringBuilder::PutSint8
void PutSint8(int8_t value)
Append binary int8.
Definition string_builder.cpp:29
BaseStringBuilder::PutUint64LE
void PutUint64LE(uint64_t value)
Append binary uint64 using little endian.
Definition string_builder.cpp:79
BaseStringBuilder::PutSint64LE
void PutSint64LE(int64_t value)
Append binary int64 using little endian.
Definition string_builder.cpp:97
BaseStringBuilder::PutUtf8
void PutUtf8(char32_t c)
Append UTF.8 char.
Definition string_builder.cpp:113
BaseStringBuilder::PutUint16LE
void PutUint16LE(uint16_t value)
Append binary uint16 using little endian.
Definition string_builder.cpp:37
BaseStringBuilder::PutSint32LE
void PutSint32LE(int32_t value)
Append binary int32 using little endian.
Definition string_builder.cpp:71
BaseStringBuilder::Put
void Put(std::string_view str)
Append string.
Definition string_builder.hpp:34
BaseStringBuilder::PutIntegerBase
void PutIntegerBase(T value, int base)
Append integer 'value' in given number 'base'.
Definition string_builder.hpp:52
BaseStringBuilder::PutChar
void PutChar(char c)
Append 8-bit char.
Definition string_builder.cpp:105
BaseStringBuilder::PutSint16LE
void PutSint16LE(int16_t value)
Append binary int16 using little endian.
Definition string_builder.cpp:49
BaseStringBuilder::PutUint8
void PutUint8(uint8_t value)
Append binary uint8.
Definition string_builder.cpp:18
StringBuilder
Compose data into a growing std::string.
Definition string_builder.hpp:65
StringBuilder::GetWrittenData
const std::string & GetWrittenData() const noexcept
Get already written data.
Definition string_builder.hpp:85
StringBuilder::GetString
std::string & GetString() noexcept
Get mutable already written data.
Definition string_builder.hpp:89
StringBuilder::back_inserter
back_insert_iterator back_inserter()
Create a back-insert-iterator.
Definition string_builder.hpp:106
StringBuilder::operator+=
StringBuilder & operator+=(std::string_view str)
Append string.
Definition string_builder.hpp:96
StringBuilder::PutBuffer
void PutBuffer(std::span< const char > str) override
Append buffer.
Definition string_builder.cpp:122
StringBuilder::GetBytesWritten
size_type GetBytesWritten() const noexcept
Get number of already written bytes.
Definition string_builder.hpp:81
StringBuilder::StringBuilder
StringBuilder(std::string &dest)
Construct StringBuilder into destination string.
Definition string_builder.hpp:72
StringBuilder::AnyBytesWritten
bool AnyBytesWritten() const noexcept
Check whether any bytes have been written.
Definition string_builder.hpp:77