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/>.
8 /** @file strings_interal.h Types and functions related to the internal workings of formatting OpenTTD's strings. */
10 #ifndef STRINGS_INTERNAL_H
11 #define STRINGS_INTERNAL_H
13 #include "strings_func.h"
14 #include "string_func.h"
16 /** The data required to format and validate a single parameter of a string. */
17 struct StringParameter
{
18 StringParameterData data
; ///< The data of the parameter.
19 char32_t type
; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
22 class StringParameters
{
24 StringParameters
*parent
= nullptr; ///< If not nullptr, this instance references data from this parent instance.
25 std::span
<StringParameter
> parameters
= {}; ///< Array with the actual parameters.
27 size_t offset
= 0; ///< Current offset in the parameters span.
28 char32_t next_type
= 0; ///< The type of the next data that is retrieved.
30 StringParameters(std::span
<StringParameter
> parameters
= {}) :
31 parameters(parameters
)
34 const StringParameter
&GetNextParameterReference();
38 * Create a new StringParameters instance that can reference part of the data of
39 * the given parent instance.
41 StringParameters(StringParameters
&parent
, size_t size
) :
43 parameters(parent
.parameters
.subspan(parent
.offset
, size
))
46 void PrepareForNextRun();
47 void SetTypeOfNextParameter(char32_t type
) { this->next_type
= type
; }
50 * Get the current offset, so it can be backed up for certain processing
51 * steps, or be used to offset the argument index within sub strings.
52 * @return The current offset.
54 size_t GetOffset() { return this->offset
; }
57 * Set the offset within the string from where to return the next result of
58 * \c GetInt64 or \c GetInt32.
59 * @param offset The offset.
61 void SetOffset(size_t offset
)
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.
70 assert(offset
< this->parameters
.size() || this->offset
== offset
);
71 this->offset
= offset
;
75 * Advance the offset within the string from where to return the next result of
76 * \c GetInt64 or \c GetInt32.
77 * @param advance The amount to advance the offset by.
79 void AdvanceOffset(size_t advance
)
81 this->offset
+= advance
;
82 assert(this->offset
<= this->parameters
.size());
86 * Get the next parameter from our parameters.
87 * This updates the offset, so the next time this is called the next parameter
89 * @return The next parameter's value.
94 const auto ¶m
= GetNextParameterReference();
95 const uint64_t *data
= std::get_if
<uint64_t>(¶m
.data
);
96 if (data
!= nullptr) return static_cast<T
>(*data
);
97 throw std::out_of_range("Attempt to read string parameter as integer");
101 * Get the next string parameter from our parameters.
102 * This updates the offset, so the next time this is called the next parameter
104 * @return The next parameter's value.
106 const char *GetNextParameterString()
108 const auto ¶m
= GetNextParameterReference();
109 const std::string
*data
= std::get_if
<std::string
>(¶m
.data
);
110 if (data
!= nullptr) return data
->c_str();
111 throw std::out_of_range("Attempt to read integer parameter as string");
115 * Get a new instance of StringParameters that is a "range" into the
116 * remaining existing parameters. Upon destruction the offset in the parent
117 * is not updated. However, calls to SetDParam do update the parameters.
119 * The returned StringParameters must not outlive this StringParameters.
120 * @return A "range" of the string parameters.
122 StringParameters
GetRemainingParameters() { return GetRemainingParameters(this->offset
); }
125 * Get a new instance of StringParameters that is a "range" into the
126 * remaining existing parameters from the given offset. Upon destruction the
127 * offset in the parent is not updated. However, calls to SetDParam do
128 * update the parameters.
130 * The returned StringParameters must not outlive this StringParameters.
131 * @param offset The offset to get the remaining parameters for.
132 * @return A "range" of the string parameters.
134 StringParameters
GetRemainingParameters(size_t offset
)
136 return StringParameters(this->parameters
.subspan(offset
, this->parameters
.size() - offset
));
139 /** Return the amount of elements which can still be read. */
140 size_t GetDataLeft() const
142 return this->parameters
.size() - this->offset
;
145 /** Get the type of a specific element. */
146 char32_t
GetTypeAtOffset(size_t offset
) const
148 assert(offset
< this->parameters
.size());
149 return this->parameters
[offset
].type
;
152 void SetParam(size_t n
, const StringParameterData
&v
)
154 assert(n
< this->parameters
.size());
155 this->parameters
[n
].data
= v
;
158 void SetParam(size_t n
, uint64_t v
)
160 assert(n
< this->parameters
.size());
161 this->parameters
[n
].data
= v
;
164 template <typename T
, std::enable_if_t
<std::is_base_of
<StrongTypedefBase
, T
>::value
, int> = 0>
165 void SetParam(size_t n
, T v
)
167 SetParam(n
, v
.base());
170 void SetParam(size_t n
, const char *str
)
172 assert(n
< this->parameters
.size());
173 this->parameters
[n
].data
= str
;
176 void SetParam(size_t n
, const std::string
&str
) { this->SetParam(n
, str
.c_str()); }
178 void SetParam(size_t n
, std::string
&&str
)
180 assert(n
< this->parameters
.size());
181 this->parameters
[n
].data
= std::move(str
);
184 const StringParameterData
&GetParam(size_t n
) const
186 assert(n
< this->parameters
.size());
187 return this->parameters
[n
].data
;
192 * Extension of StringParameters with its own statically sized buffer for
196 class ArrayStringParameters
: public StringParameters
{
197 std::array
<StringParameter
, N
> params
{}; ///< The actual parameters
200 ArrayStringParameters()
202 this->parameters
= std::span(params
.data(), params
.size());
205 ArrayStringParameters(ArrayStringParameters
&& other
) noexcept
207 *this = std::move(other
);
210 ArrayStringParameters
& operator=(ArrayStringParameters
&&other
) noexcept
212 this->offset
= other
.offset
;
213 this->next_type
= other
.next_type
;
214 this->params
= std::move(other
.params
);
215 this->parameters
= std::span(params
.data(), params
.size());
219 ArrayStringParameters(const ArrayStringParameters
&other
) = delete;
220 ArrayStringParameters
& operator=(const ArrayStringParameters
&other
) = delete;
224 * Helper to create the StringParameters with its own buffer with the given
226 * @param args The parameters to set for the to be created StringParameters.
227 * @return The constructed StringParameters.
229 template <typename
... Args
>
230 static auto MakeParameters(const Args
&... args
)
232 ArrayStringParameters
<sizeof...(args
)> parameters
;
234 (parameters
.SetParam(index
++, std::forward
<const Args
&>(args
)), ...);
239 * Equivalent to the std::back_insert_iterator in function, with some
240 * convenience helpers for string concatenation.
242 class StringBuilder
{
246 /* Required type for this to be an output_iterator; mimics std::back_insert_iterator. */
247 using value_type
= void;
248 using difference_type
= void;
249 using iterator_category
= std::output_iterator_tag
;
250 using pointer
= void;
251 using reference
= void;
254 * Create the builder of an external buffer.
255 * @param string The string to write to.
257 StringBuilder(std::string
&string
) : string(&string
) {}
259 /* Required operators for this to be an output_iterator; mimics std::back_insert_iterator, which has no-ops. */
260 StringBuilder
&operator++() { return *this; }
261 StringBuilder
operator++(int) { return *this; }
262 StringBuilder
&operator*() { return *this; }
265 * Operator to add a character to the end of the buffer. Like the back
266 * insert iterators this also increases the position of the end of the
268 * @param value The character to add.
269 * @return Reference to this inserter.
271 StringBuilder
&operator=(const char value
)
273 return this->operator+=(value
);
277 * Operator to add a character to the end of the buffer.
278 * @param value The character to add.
279 * @return Reference to this inserter.
281 StringBuilder
&operator+=(const char value
)
283 this->string
->push_back(value
);
288 * Operator to append the given string to the output buffer.
289 * @param str The string to add.
290 * @return Reference to this inserter.
292 StringBuilder
&operator+=(std::string_view str
)
294 *this->string
+= str
;
299 * Encode the given Utf8 character into the output buffer.
300 * @param c The character to encode.
302 void Utf8Encode(char32_t c
)
304 auto iterator
= std::back_inserter(*this->string
);
305 ::Utf8Encode(iterator
, c
);
309 * Remove the given amount of characters from the back of the string.
310 * @param amount The amount of characters to remove.
312 void RemoveElementsFromBack(size_t amount
)
314 this->string
->erase(this->string
->size() - std::min(amount
, this->string
->size()));
318 * Get the current index in the string.
321 size_t CurrentIndex()
323 return this->string
->size();
327 * Get the reference to the character at the given index.
328 * @return The reference to the character.
330 char &operator[](size_t index
)
332 return (*this->string
)[index
];
336 void GetStringWithArgs(StringBuilder
&builder
, StringID string
, StringParameters
&args
, uint case_index
= 0, bool game_script
= false);
337 std::string
GetStringWithArgs(StringID string
, StringParameters
&args
);
339 /* Do not leak the StringBuilder to everywhere. */
340 void GenerateTownNameString(StringBuilder
&builder
, size_t lang
, uint32_t seed
);
341 void GetTownName(StringBuilder
&builder
, const struct Town
*t
);
342 void GRFTownNameGenerate(StringBuilder
&builder
, uint32_t grfid
, uint16_t gen
, uint32_t seed
);
344 uint
RemapNewGRFStringControlCode(uint scc
, const char **str
, StringParameters
¶meters
, bool modify_parameters
);
346 #endif /* STRINGS_INTERNAL_H */