OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "string_inplace.hpp"
12#include "../safeguards.h"
13
18[[nodiscard]] bool InPlaceBuilder::AnyBytesUnused() const noexcept
19{
20 return this->consumer.GetBytesRead() > this->position;
21}
22
28{
29 return this->consumer.GetBytesRead() - this->position;
30}
31
36void InPlaceBuilder::PutBuffer(std::span<const char> str)
37{
38 auto unused = this->GetBytesUnused();
39 if (str.size() > unused) NOT_REACHED();
40 std::ranges::copy(str, this->dest.data() + this->position);
41 this->position += str.size();
42}
43
50 : consumer(buffer), builder(buffer, consumer)
51{
52}
53
63
64InPlaceReplacement& InPlaceReplacement::operator=(const InPlaceReplacement &src)
65{
66 this->consumer = src.consumer;
67 this->builder.AssignBuffer(src.builder);
68 return *this;
69}
std::string_view::size_type size_type
The type of the size of our strings.
void AssignBuffer(const InPlaceBuilder &src)
Copy assignment of the buffer and its position.
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.
const StringConsumer & consumer
The string consumer to read with.
std::span< char > dest
The buffer to process.
size_type position
The location to write to.
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.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Inplace-replacement of textual and binary data.