OpenTTD Source 20250205-master-gfd85ab1e2c
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"
15
18 StringParameterData data;
19 char32_t type;
20};
21
23protected:
25 std::span<StringParameter> parameters = {};
26
27 size_t offset = 0;
28 char32_t next_type = 0;
29
30 StringParameters(std::span<StringParameter> parameters = {}) :
32 {}
33
35
36public:
45
46 void PrepareForNextRun();
47 void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
48
54 size_t GetOffset() { return this->offset; }
55
61 void SetOffset(size_t offset)
62 {
63 /*
64 * The offset must be fewer than the number of parameters when it is
65 * being set. Unless restoring a backup, then the original value is
66 * correct as well as long as the offset was not changed. In other
67 * words, when the offset was already at the end of the parameters and
68 * the string did not consume any parameters.
69 */
70 assert(offset < this->parameters.size() || this->offset == offset);
71 this->offset = offset;
72 }
73
79 void AdvanceOffset(size_t advance)
80 {
81 this->offset += advance;
82 assert(this->offset <= this->parameters.size());
83 }
84
91 template <typename T>
93 {
94 struct visitor {
95 uint64_t operator()(const uint64_t &arg) { return arg; }
96 uint64_t operator()(const std::string &) { throw std::out_of_range("Attempt to read string parameter as integer"); }
97 };
98
99 const auto &param = GetNextParameterReference();
100 return static_cast<T>(std::visit(visitor{}, param.data));
101 }
102
110 {
111 struct visitor {
112 const char *operator()(const uint64_t &) { throw std::out_of_range("Attempt to read integer parameter as string"); }
113 const char *operator()(const std::string &arg) { return arg.c_str(); }
114 };
115
116 const auto &param = GetNextParameterReference();
117 return std::visit(visitor{}, param.data);
118 }
119
129
141 {
142 return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
143 }
144
146 size_t GetDataLeft() const
147 {
148 return this->parameters.size() - this->offset;
149 }
150
152 char32_t GetTypeAtOffset(size_t offset) const
153 {
154 assert(offset < this->parameters.size());
155 return this->parameters[offset].type;
156 }
157
158 void SetParam(size_t n, const StringParameterData &v)
159 {
160 assert(n < this->parameters.size());
161 this->parameters[n].data = v;
162 }
163
164 void SetParam(size_t n, uint64_t v)
165 {
166 assert(n < this->parameters.size());
167 this->parameters[n].data = v;
168 }
169
170 void SetParam(size_t n, ConvertibleThroughBase auto v)
171 {
172 SetParam(n, v.base());
173 }
174
175 void SetParam(size_t n, const char *str)
176 {
177 assert(n < this->parameters.size());
178 this->parameters[n].data = str;
179 }
180
181 void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
182
183 void SetParam(size_t n, std::string &&str)
184 {
185 assert(n < this->parameters.size());
186 this->parameters[n].data = std::move(str);
187 }
188
189 const StringParameterData &GetParam(size_t n) const
190 {
191 assert(n < this->parameters.size());
192 return this->parameters[n].data;
193 }
194};
195
200template <size_t N>
202 std::array<StringParameter, N> params{};
203
204public:
206 {
207 this->parameters = std::span(params.data(), params.size());
208 }
209
211 {
212 *this = std::move(other);
213 }
214
215 ArrayStringParameters& operator=(ArrayStringParameters &&other) noexcept
216 {
217 this->offset = other.offset;
218 this->next_type = other.next_type;
219 this->params = std::move(other.params);
220 this->parameters = std::span(params.data(), params.size());
221 return *this;
222 }
223
224 ArrayStringParameters(const ArrayStringParameters &other) = delete;
225 ArrayStringParameters& operator=(const ArrayStringParameters &other) = delete;
226};
227
234template <typename... Args>
235static auto MakeParameters(const Args&... args)
236{
237 ArrayStringParameters<sizeof...(args)> parameters;
238 size_t index = 0;
239 (parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
240 return parameters;
241}
242
248 std::string *string;
249
250public:
251 /* Required type for this to be an output_iterator; mimics std::back_insert_iterator. */
252 using value_type = void;
253 using difference_type = void;
254 using iterator_category = std::output_iterator_tag;
255 using pointer = void;
256 using reference = void;
257
262 StringBuilder(std::string &string) : string(&string) {}
263
264 /* Required operators for this to be an output_iterator; mimics std::back_insert_iterator, which has no-ops. */
265 StringBuilder &operator++() { return *this; }
266 StringBuilder operator++(int) { return *this; }
267 StringBuilder &operator*() { return *this; }
268
276 StringBuilder &operator=(const char value)
277 {
278 return this->operator+=(value);
279 }
280
286 StringBuilder &operator+=(const char value)
287 {
288 this->string->push_back(value);
289 return *this;
290 }
291
297 StringBuilder &operator+=(std::string_view str)
298 {
299 *this->string += str;
300 return *this;
301 }
302
307 void Utf8Encode(char32_t c)
308 {
309 auto iterator = std::back_inserter(*this->string);
310 ::Utf8Encode(iterator, c);
311 }
312
317 void RemoveElementsFromBack(size_t amount)
318 {
319 this->string->erase(this->string->size() - std::min(amount, this->string->size()));
320 }
321
327 {
328 return this->string->size();
329 }
330
335 char &operator[](size_t index)
336 {
337 return (*this->string)[index];
338 }
339};
340
341void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
342std::string GetStringWithArgs(StringID string, StringParameters &args);
343
344/* Do not leak the StringBuilder to everywhere. */
345void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed);
346void GetTownName(StringBuilder &builder, const struct Town *t);
347void GRFTownNameGenerate(StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed);
348
349char32_t RemapNewGRFStringControlCode(char32_t scc, const char **str, StringParameters &parameters, bool modify_parameters);
350
351#endif /* STRINGS_INTERNAL_H */
Extension of StringParameters with its own statically sized buffer for the parameters.
std::array< StringParameter, N > params
The actual parameters.
Equivalent to the std::back_insert_iterator in function, with some convenience helpers for string con...
char & operator[](size_t index)
Get the reference to the character at the given index.
StringBuilder & operator+=(const char value)
Operator to add a character to the end of the buffer.
void RemoveElementsFromBack(size_t amount)
Remove the given amount of characters from the back of the string.
StringBuilder & operator+=(std::string_view str)
Operator to append the given string to the output buffer.
void Utf8Encode(char32_t c)
Encode the given Utf8 character into the output buffer.
size_t CurrentIndex()
Get the current index in the string.
StringBuilder(std::string &string)
Create the builder of an external buffer.
StringBuilder & operator=(const char value)
Operator to add a character to the end of the 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.
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.
void PrepareForNextRun()
Prepare the string parameters for the next formatting run.
Definition strings.cpp:68
const StringParameter & GetNextParameterReference()
Get the next parameter from our parameters.
Definition strings.cpp:81
void AdvanceOffset(size_t advance)
Advance the offset within the string from where to return the next result of GetInt64 or GetInt32.
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...
const char * GetNextParameterString()
Get the next string parameter from our 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, const char **str, StringParameters &parameters, bool modify_parameters)
FormatString for NewGRF specific "magic" string control codes.
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:256
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.
StringParameterData data
The data of the parameter.
char32_t type
The StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
Town data structure.
Definition town.h:52