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

OpenTTD Source 15.0-RC2

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
10#include "../stdafx.h"
11#include "string_builder.hpp"
12#include "utf8.hpp"
13#include "../safeguards.h"
14
18 void BaseStringBuilder::PutUint8(uint8_t value)
19{
20 std::array<char, 1> buf{
21 static_cast<char>(value)
22 };
23 this->PutBuffer(buf);
24}
25
29 void BaseStringBuilder::PutSint8(int8_t value)
30{
31 this->PutUint8(static_cast<uint8_t>(value));
32}
33
37 void BaseStringBuilder::PutUint16LE(uint16_t value)
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
49 void BaseStringBuilder::PutSint16LE(int16_t value)
50{
51 this->PutUint16LE(static_cast<uint16_t>(value));
52}
53
57 void BaseStringBuilder::PutUint32LE(uint32_t value)
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
71 void BaseStringBuilder::PutSint32LE(int32_t value)
72{
73 this->PutUint32LE(static_cast<uint32_t>(value));
74}
75
79 void BaseStringBuilder::PutUint64LE(uint64_t value)
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
97 void BaseStringBuilder::PutSint64LE(int64_t value)
98{
99 this->PutUint64LE(static_cast<uint64_t>(value));
100}
101
105 void BaseStringBuilder::PutChar(char c)
106{
107 this->PutUint8(static_cast<uint8_t>(c));
108}
109
113 void BaseStringBuilder::PutUtf8(char32_t c)
114{
115 auto [buf, len] = EncodeUtf8(c);
116 this->PutBuffer({buf, len});
117}
118
122 void StringBuilder::PutBuffer(std::span<const char> str)
123{
124 this->dest->append(str.data(), str.size());
125}
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::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::PutBuffer
void PutBuffer(std::span< const char > str) override
Append buffer.
Definition string_builder.cpp:122
EncodeUtf8
std::pair< char[4], size_t > EncodeUtf8(char32_t c)
Encode a character to UTF-8.
Definition utf8.cpp:21
string_builder.hpp
Compose strings from textual and binary data.
utf8.hpp
Handling of UTF-8 encoded data.