OpenTTD Source 20250524-master-gc366e6a48e
string_inplace.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
12#include "../stdafx.h"
13#include "string_inplace.hpp"
14#include "../safeguards.h"
15
20{
21 return this->consumer.GetBytesRead() > this->position;
22}
23
28{
29 return this->consumer.GetBytesRead() - this->position;
30}
31
35void InPlaceBuilder::PutBuffer(std::span<const char> str)
36{
37 auto unused = this->GetBytesUnused();
38 if (str.size() > unused) NOT_REACHED();
39 std::ranges::copy(str, this->dest.data() + this->position);
40 this->position += str.size();
41}
42
49 : consumer(buffer), builder(buffer, consumer)
50{
51}
52
54 : consumer(src.consumer), builder(src.builder, consumer)
55{
56}
57
58InPlaceReplacement& InPlaceReplacement::operator=(const InPlaceReplacement &src)
59{
60 this->consumer = src.consumer;
61 this->builder.AssignBuffer(src.builder);
62 return *this;
63}
Implementation of std::back_insert_iterator for non-growing destination buffer.
bool AnyBytesUnused() const noexcept
Check whether any unused bytes are left between the Builder and Consumer position.
size_type GetBytesUnused() const noexcept
Get number of unused bytes left between the Builder and Consumer position.
void PutBuffer(std::span< const char > str) override
Append buffer.
Compose data into a fixed size buffer, which is consumed at the same time.
InPlaceReplacement(std::span< char > buffer)
Create coupled Consumer+Builder pair.
InPlaceBuilder builder
Builder into shared buffer.
StringConsumer consumer
Consumer from shared buffer.
Inplace-replacement of textual and binary data.