OpenTTD Source 20260218-master-g2123fca5ea
string_builder.cpp
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#include "../stdafx.h"
11#include "string_builder.hpp"
12#include "utf8.hpp"
13#include "../safeguards.h"
14
19void BaseStringBuilder::PutUint8(uint8_t value)
20{
21 std::array<char, 1> buf{
22 static_cast<char>(value)
23 };
24 this->PutBuffer(buf);
25}
26
32{
33 this->PutUint8(static_cast<uint8_t>(value));
34}
35
41{
42 std::array<char, 2> buf{
43 static_cast<char>(static_cast<uint8_t>(value)),
44 static_cast<char>(static_cast<uint8_t>(value >> 8))
45 };
46 this->PutBuffer(buf);
47}
48
54{
55 this->PutUint16LE(static_cast<uint16_t>(value));
56}
57
63{
64 std::array<char, 4> buf{
65 static_cast<char>(static_cast<uint8_t>(value)),
66 static_cast<char>(static_cast<uint8_t>(value >> 8)),
67 static_cast<char>(static_cast<uint8_t>(value >> 16)),
68 static_cast<char>(static_cast<uint8_t>(value >> 24))
69 };
70 this->PutBuffer(buf);
71}
72
78{
79 this->PutUint32LE(static_cast<uint32_t>(value));
80}
81
87{
88 std::array<char, 8> buf{
89 static_cast<char>(static_cast<uint8_t>(value)),
90 static_cast<char>(static_cast<uint8_t>(value >> 8)),
91 static_cast<char>(static_cast<uint8_t>(value >> 16)),
92 static_cast<char>(static_cast<uint8_t>(value >> 24)),
93 static_cast<char>(static_cast<uint8_t>(value >> 32)),
94 static_cast<char>(static_cast<uint8_t>(value >> 40)),
95 static_cast<char>(static_cast<uint8_t>(value >> 48)),
96 static_cast<char>(static_cast<uint8_t>(value >> 56))
97 };
98 this->PutBuffer(buf);
99}
100
106{
107 this->PutUint64LE(static_cast<uint64_t>(value));
108}
109
115{
116 this->PutUint8(static_cast<uint8_t>(c));
117}
118
124{
125 auto [buf, len] = EncodeUtf8(c);
126 this->PutBuffer({buf, len});
127}
128
133void StringBuilder::PutBuffer(std::span<const char> str)
134{
135 this->dest->append(str.data(), str.size());
136}
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 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.
void PutBuffer(std::span< const char > str) override
Append buffer.
std::string * dest
The string to write to.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Compose strings from textual and binary data.
std::pair< char[4], size_t > EncodeUtf8(char32_t c)
Encode a character to UTF-8.
Definition utf8.cpp:19
Handling of UTF-8 encoded data.