OpenTTD Source 20250522-master-g467f832c2f
strings_internal.h
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#ifndef STRINGS_INTERNAL_H
11#define STRINGS_INTERNAL_H
12
13#include "strings_func.h"
14#include "string_func.h"
17
19protected:
21 std::span<StringParameter> parameters = {};
22
23 size_t offset = 0;
24 char32_t next_type = 0;
25
27
28public:
37
38 StringParameters(std::span<StringParameter> parameters = {}) : parameters(parameters) {}
39
40 void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
41
47 size_t GetOffset() { return this->offset; }
48
54 void SetOffset(size_t offset)
55 {
56 /*
57 * The offset must be fewer than the number of parameters when it is
58 * being set. Unless restoring a backup, then the original value is
59 * correct as well as long as the offset was not changed. In other
60 * words, when the offset was already at the end of the parameters and
61 * the string did not consume any parameters.
62 */
63 assert(offset < this->parameters.size() || this->offset == offset);
64 this->offset = offset;
65 }
66
72 void AdvanceOffset(size_t advance)
73 {
74 this->offset += advance;
75 assert(this->offset <= this->parameters.size());
76 }
77
85 {
86 struct visitor {
87 uint64_t operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as integer"); }
88 uint64_t operator()(const uint64_t &arg) { return arg; }
89 uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
90 };
91
92 const auto &param = this->GetNextParameterReference();
93 return std::visit(visitor{}, param.data);
94 }
95
103 template <typename T>
105 {
106 return static_cast<T>(this->GetNextParameter());
107 }
108
115 std::string_view GetNextParameterString()
116 {
117 struct visitor {
118 std::string_view operator()(const std::monostate &) { throw std::out_of_range("Attempt to read uninitialised parameter as string"); }
119 std::string_view operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
120 std::string_view operator()(const std::string &arg) { return arg; }
121 };
122
123 const auto &param = this->GetNextParameterReference();
124 return std::visit(visitor{}, param.data);
125 }
126
136
148 {
149 return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
150 }
151
153 size_t GetDataLeft() const
154 {
155 return this->parameters.size() - this->offset;
156 }
157
159 size_t GetNumParameters() const
160 {
161 return this->parameters.size();
162 }
163
165 char32_t GetTypeAtOffset(size_t offset) const
166 {
167 assert(offset < this->parameters.size());
168 return this->parameters[offset].type;
169 }
170
171 void SetParam(size_t n, const StringParameterData &v)
172 {
173 assert(n < this->parameters.size());
174 this->parameters[n].data = v;
175 }
176
177 void SetParam(size_t n, uint64_t v)
178 {
179 assert(n < this->parameters.size());
180 this->parameters[n].data = v;
181 }
182
183 void SetParam(size_t n, ConvertibleThroughBase auto v)
184 {
185 SetParam(n, v.base());
186 }
187
188 void SetParam(size_t n, const std::string &str)
189 {
190 assert(n < this->parameters.size());
191 this->parameters[n].data = str;
192 }
193
194 void SetParam(size_t n, std::string &&str)
195 {
196 assert(n < this->parameters.size());
197 this->parameters[n].data = std::move(str);
198 }
199
200 const StringParameterData &GetParam(size_t n) const
201 {
202 assert(n < this->parameters.size());
203 return this->parameters[n].data;
204 }
205};
206
207void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
208void GetStringWithArgs(StringBuilder &builder, StringID string, std::span<StringParameter> params, uint case_index = 0, bool game_script = false);
209std::string GetStringWithArgs(StringID string, StringParameters &args);
210
211/* Do not leak the StringBuilder to everywhere. */
212void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
213void GetTownName(StringBuilder &builder, const struct Town *t);
214void GRFTownNameGenerate(StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed);
215
216char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer);
217
218#endif /* STRINGS_INTERNAL_H */
Compose data into a growing std::string.
Parse data from a string / buffer.
StringParameters * parent
If not nullptr, this instance references data from this parent instance.
void SetOffset(size_t offset)
Set the offset within the string from where to return the next result of GetInt64 or GetInt32.
std::string_view GetNextParameterString()
Get the next string parameter from our parameters.
StringParameters GetRemainingParameters()
Get a new instance of StringParameters that is a "range" into the remaining existing parameters.
std::span< StringParameter > parameters
Array with the actual parameters.
const StringParameter & GetNextParameterReference()
Get the next parameter from our parameters.
Definition strings.cpp:69
void AdvanceOffset(size_t advance)
Advance the offset within the string from where to return the next result of GetInt64 or GetInt32.
uint64_t GetNextParameter()
Get the next parameter from our parameters.
size_t offset
Current offset in the parameters span.
StringParameters(StringParameters &parent, size_t size)
Create a new StringParameters instance that can reference part of the data of the given parent instan...
T GetNextParameter()
Get the next parameter from our parameters.
char32_t next_type
The type of the next data that is retrieved.
size_t GetOffset()
Get the current offset, so it can be backed up for certain processing steps, or be used to offset the...
size_t GetNumParameters() const
Return the number of parameters.
size_t GetDataLeft() const
Return the amount of elements which can still be read.
char32_t GetTypeAtOffset(size_t offset) const
Get the type of a specific element.
StringParameters GetRemainingParameters(size_t offset)
Get a new instance of StringParameters that is a "range" into the remaining existing parameters from ...
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
char32_t RemapNewGRFStringControlCode(char32_t scc, StringConsumer &consumer)
Emit OpenTTD's internal string code for the different NewGRF string codes.
Compose strings from textual and binary data.
Parse strings.
Functions related to low-level strings.
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index, bool game_script)
Get a parsed string with most special stringcodes replaced by the string parameters.
Definition strings.cpp:327
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
The data required to format and validate a single parameter of a string.
Town data structure.
Definition town.h:52