OpenTTD Source 20250529-master-g10c159a79f
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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "string_builder.hpp"
12#include "utf8.hpp"
13#include "../safeguards.h"
14
18void BaseStringBuilder::PutUint8(uint8_t value)
19{
20 std::array<char, 1> buf{
21 static_cast<char>(value)
22 };
23 this->PutBuffer(buf);
24}
25
30{
31 this->PutUint8(static_cast<uint8_t>(value));
32}
33
38{
39 std::array<char, 2> buf{
40 static_cast<char>(static_cast<uint8_t>(value)),
41 static_cast<char>(static_cast<uint8_t>(value >> 8))
42 };
43 this->PutBuffer(buf);
44}
45
50{
51 this->PutUint16LE(static_cast<uint16_t>(value));
52}
53
58{
59 std::array<char, 4> buf{
60 static_cast<char>(static_cast<uint8_t>(value)),
61 static_cast<char>(static_cast<uint8_t>(value >> 8)),
62 static_cast<char>(static_cast<uint8_t>(value >> 16)),
63 static_cast<char>(static_cast<uint8_t>(value >> 24))
64 };
65 this->PutBuffer(buf);
66}
67
72{
73 this->PutUint32LE(static_cast<uint32_t>(value));
74}
75
80{
81 std::array<char, 8> buf{
82 static_cast<char>(static_cast<uint8_t>(value)),
83 static_cast<char>(static_cast<uint8_t>(value >> 8)),
84 static_cast<char>(static_cast<uint8_t>(value >> 16)),
85 static_cast<char>(static_cast<uint8_t>(value >> 24)),
86 static_cast<char>(static_cast<uint8_t>(value >> 32)),
87 static_cast<char>(static_cast<uint8_t>(value >> 40)),
88 static_cast<char>(static_cast<uint8_t>(value >> 48)),
89 static_cast<char>(static_cast<uint8_t>(value >> 56))
90 };
91 this->PutBuffer(buf);
92}
93
98{
99 this->PutUint64LE(static_cast<uint64_t>(value));
100}
101
106{
107 this->PutUint8(static_cast<uint8_t>(c));
108}
109
114{
115 auto [buf, len] = EncodeUtf8(c);
116 this->PutBuffer({buf, len});
117}
118
122void StringBuilder::PutBuffer(std::span<const char> str)
123{
124 this->dest->append(str.data(), str.size());
125}
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::pair< char[4], size_t > EncodeUtf8(char32_t c)
Encode a character to UTF-8.
Definition utf8.cpp:21
Compose strings from textual and binary data.
Handling of UTF-8 encoded data.