Codechange: Generalize StringBuilder.

This commit is contained in:
frosch
2025-03-27 18:15:56 +01:00
committed by frosch
parent f306f9726c
commit ad8e9634ec
10 changed files with 358 additions and 103 deletions

View File

@@ -3,6 +3,7 @@ add_files(
backup_type.hpp
base_bitset_type.hpp
bitmath_func.hpp
container_func.hpp
convertible_through_base.hpp
endian_func.hpp
enum_type.hpp
@@ -22,7 +23,8 @@ add_files(
random_func.cpp
random_func.hpp
smallstack_type.hpp
container_func.hpp
string_builder.cpp
string_builder.hpp
strong_typedef_type.hpp
utf8.cpp
utf8.hpp

125
src/core/string_builder.cpp Normal file
View File

@@ -0,0 +1,125 @@
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file string_builder.cpp Implementation of string composing. */
#include "../stdafx.h"
#include "string_builder.hpp"
#include "utf8.hpp"
#include "../safeguards.h"
/**
* Append binary uint8.
*/
void BaseStringBuilder::PutUint8(uint8_t value)
{
std::array<char, 1> buf{
static_cast<char>(value)
};
this->PutBuffer(buf);
}
/**
* Append binary int8.
*/
void BaseStringBuilder::PutSint8(int8_t value)
{
this->PutUint8(static_cast<uint8_t>(value));
}
/**
* Append binary uint16 using little endian.
*/
void BaseStringBuilder::PutUint16LE(uint16_t value)
{
std::array<char, 2> buf{
static_cast<char>(static_cast<uint8_t>(value)),
static_cast<char>(static_cast<uint8_t>(value >> 8))
};
this->PutBuffer(buf);
}
/**
* Append binary int16 using little endian.
*/
void BaseStringBuilder::PutSint16LE(int16_t value)
{
this->PutUint16LE(static_cast<uint16_t>(value));
}
/**
* Append binary uint32 using little endian.
*/
void BaseStringBuilder::PutUint32LE(uint32_t value)
{
std::array<char, 4> buf{
static_cast<char>(static_cast<uint8_t>(value)),
static_cast<char>(static_cast<uint8_t>(value >> 8)),
static_cast<char>(static_cast<uint8_t>(value >> 16)),
static_cast<char>(static_cast<uint8_t>(value >> 24))
};
this->PutBuffer(buf);
}
/**
* Append binary int32 using little endian.
*/
void BaseStringBuilder::PutSint32LE(int32_t value)
{
this->PutUint32LE(static_cast<uint32_t>(value));
}
/**
* Append binary uint64 using little endian.
*/
void BaseStringBuilder::PutUint64LE(uint64_t value)
{
std::array<char, 8> buf{
static_cast<char>(static_cast<uint8_t>(value)),
static_cast<char>(static_cast<uint8_t>(value >> 8)),
static_cast<char>(static_cast<uint8_t>(value >> 16)),
static_cast<char>(static_cast<uint8_t>(value >> 24)),
static_cast<char>(static_cast<uint8_t>(value >> 32)),
static_cast<char>(static_cast<uint8_t>(value >> 40)),
static_cast<char>(static_cast<uint8_t>(value >> 48)),
static_cast<char>(static_cast<uint8_t>(value >> 56))
};
this->PutBuffer(buf);
}
/**
* Append binary int64 using little endian.
*/
void BaseStringBuilder::PutSint64LE(int64_t value)
{
this->PutUint64LE(static_cast<uint64_t>(value));
}
/**
* Append 8-bit char.
*/
void BaseStringBuilder::PutChar(char c)
{
this->PutUint8(static_cast<uint8_t>(c));
}
/**
* Append UTF.8 char.
*/
void BaseStringBuilder::PutUtf8(char32_t c)
{
auto [buf, len] = EncodeUtf8(c);
this->PutBuffer(buf, len);
}
/**
* Append buffer.
*/
void StringBuilder::PutBuffer(const char *str, size_type len)
{
this->dest->append(str, len);
}

119
src/core/string_builder.hpp Normal file
View File

@@ -0,0 +1,119 @@
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/**
* @file string_builder.hpp Compose strings from textual and binary data.
*/
#ifndef STRING_BUILDER_HPP
#define STRING_BUILDER_HPP
#include <charconv>
/**
* Compose data into a string / buffer.
*/
class BaseStringBuilder {
public:
using size_type = std::string_view::size_type;
virtual ~BaseStringBuilder() = default;
/**
* Append buffer.
*/
virtual void PutBuffer(const char *str, size_type len) = 0;
/**
* Append span.
*/
void PutBuffer(std::span<const char> str) { this->PutBuffer(str.data(), str.size()); }
/**
* Append string.
*/
void Put(std::string_view str) { this->PutBuffer(str.data(), str.size()); }
void PutUint8(uint8_t value);
void PutSint8(int8_t value);
void PutUint16LE(uint16_t value);
void PutSint16LE(int16_t value);
void PutUint32LE(uint32_t value);
void PutSint32LE(int32_t value);
void PutUint64LE(uint64_t value);
void PutSint64LE(int64_t value);
void PutChar(char c);
void PutUtf8(char32_t c);
/**
* Append integer 'value' in given number 'base'.
*/
template<class T>
void PutIntegerBase(T value, int base)
{
std::array<char, 32> buf;
auto result = std::to_chars(buf.data(), buf.data() + buf.size(), value, base);
if (result.ec != std::errc{}) return;
size_type len = result.ptr - buf.data();
this->PutBuffer(buf.data(), len);
}
};
/**
* Compose data into a growing std::string.
*/
class StringBuilder final : public BaseStringBuilder
{
std::string *dest;
public:
/**
* Construct StringBuilder into destination string.
* @note The lifetime of the string must exceed the lifetime of the StringBuilder.
*/
StringBuilder(std::string &dest) : dest(&dest) {}
/**
* Check whether any bytes have been written.
*/
[[nodiscard]] bool AnyBytesWritten() const noexcept { return !this->dest->empty(); }
/**
* Get number of already written bytes.
*/
[[nodiscard]] size_type GetBytesWritten() const noexcept { return this->dest->size(); }
/**
* Get already written data.
*/
[[nodiscard]] const std::string &GetWrittenData() const noexcept { return *dest; }
/**
* Get mutable already written data.
*/
[[nodiscard]] std::string &GetString() noexcept { return *dest; }
using BaseStringBuilder::PutBuffer;
void PutBuffer(const char *str, size_type len) override;
/**
* Append string.
*/
StringBuilder& operator+=(std::string_view str)
{
this->Put(str);
return *this;
}
using back_insert_iterator = std::back_insert_iterator<std::string>;
/**
* Create a back-insert-iterator.
*/
back_insert_iterator back_inserter()
{
return back_insert_iterator(*this->dest);
}
};
#endif /* STRING_BUILDER_HPP */