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 uint64_t data
; ///< The data of the parameter.
19 std::unique_ptr
<std::string
> string
; ///< Copied string value, if it has any.
20 char32_t type
; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
23 class StringParameters
{
25 StringParameters
*parent
= nullptr; ///< If not nullptr, this instance references data from this parent instance.
26 std::span
<StringParameter
> parameters
= {}; ///< Array with the actual parameters.
28 size_t offset
= 0; ///< Current offset in the parameters span.
29 char32_t next_type
= 0; ///< The type of the next data that is retrieved.
31 StringParameters(std::span
<StringParameter
> parameters
= {}) :
32 parameters(parameters
)
35 StringParameter
*GetNextParameterPointer();
39 * Create a new StringParameters instance that can reference part of the data of
40 * the given parent instance.
42 StringParameters(StringParameters
&parent
, size_t size
) :
44 parameters(parent
.parameters
.subspan(parent
.offset
, size
))
47 void PrepareForNextRun();
48 void SetTypeOfNextParameter(char32_t type
) { this->next_type
= type
; }
51 * Get the current offset, so it can be backed up for certain processing
52 * steps, or be used to offset the argument index within sub strings.
53 * @return The current offset.
55 size_t GetOffset() { return this->offset
; }
58 * Set the offset within the string from where to return the next result of
59 * \c GetInt64 or \c GetInt32.
60 * @param offset The offset.
62 void SetOffset(size_t offset
)
65 * The offset must be fewer than the number of parameters when it is
66 * being set. Unless restoring a backup, then the original value is
67 * correct as well as long as the offset was not changed. In other
68 * words, when the offset was already at the end of the parameters and
69 * the string did not consume any parameters.
71 assert(offset
< this->parameters
.size() || this->offset
== offset
);
72 this->offset
= offset
;
76 * Advance the offset within the string from where to return the next result of
77 * \c GetInt64 or \c GetInt32.
78 * @param advance The amount to advance the offset by.
80 void AdvanceOffset(size_t advance
)
82 this->offset
+= advance
;
83 assert(this->offset
<= this->parameters
.size());
87 * Get the next parameter from our parameters.
88 * This updates the offset, so the next time this is called the next parameter
90 * @return The next parameter's value.
95 auto ptr
= GetNextParameterPointer();
96 return static_cast<T
>(ptr
->data
);
100 * Get the next string parameter from our parameters.
101 * This updates the offset, so the next time this is called the next parameter
103 * @return The next parameter's value.
105 const char *GetNextParameterString()
107 auto ptr
= GetNextParameterPointer();
108 return ptr
->string
!= nullptr ? ptr
->string
->c_str() : nullptr;
112 * Get a new instance of StringParameters that is a "range" into the
113 * remaining existing parameters. Upon destruction the offset in the parent
114 * is not updated. However, calls to SetDParam do update the parameters.
116 * The returned StringParameters must not outlive this StringParameters.
117 * @return A "range" of the string parameters.
119 StringParameters
GetRemainingParameters() { return GetRemainingParameters(this->offset
); }
122 * Get a new instance of StringParameters that is a "range" into the
123 * remaining existing parameters from the given offset. Upon destruction the
124 * offset in the parent is not updated. However, calls to SetDParam do
125 * update the parameters.
127 * The returned StringParameters must not outlive this StringParameters.
128 * @param offset The offset to get the remaining parameters for.
129 * @return A "range" of the string parameters.
131 StringParameters
GetRemainingParameters(size_t offset
)
133 return StringParameters(this->parameters
.subspan(offset
, this->parameters
.size() - offset
));
136 /** Return the amount of elements which can still be read. */
137 size_t GetDataLeft() const
139 return this->parameters
.size() - this->offset
;
142 /** Get the type of a specific element. */
143 char32_t
GetTypeAtOffset(size_t offset
) const
145 assert(offset
< this->parameters
.size());
146 return this->parameters
[offset
].type
;
149 void SetParam(size_t n
, uint64_t v
)
151 assert(n
< this->parameters
.size());
152 this->parameters
[n
].data
= v
;
153 this->parameters
[n
].string
.reset();
156 template <typename T
, std::enable_if_t
<std::is_base_of
<StrongTypedefBase
, T
>::value
, int> = 0>
157 void SetParam(size_t n
, T v
)
159 SetParam(n
, v
.base());
162 void SetParam(size_t n
, const char *str
)
164 assert(n
< this->parameters
.size());
165 this->parameters
[n
].data
= 0;
166 this->parameters
[n
].string
= std::make_unique
<std::string
>(str
);
169 void SetParam(size_t n
, const std::string
&str
) { this->SetParam(n
, str
.c_str()); }
171 void SetParam(size_t n
, std::string
&&str
)
173 assert(n
< this->parameters
.size());
174 this->parameters
[n
].data
= 0;
175 this->parameters
[n
].string
= std::make_unique
<std::string
>(std::move(str
));
178 uint64_t GetParam(size_t n
) const
180 assert(n
< this->parameters
.size());
181 assert(this->parameters
[n
].string
== nullptr);
182 return this->parameters
[n
].data
;
186 * Get the stored string of the parameter, or \c nullptr when there is none.
187 * @param n The index into the parameters.
188 * @return The stored string.
190 const char *GetParamStr(size_t n
) const
192 assert(n
< this->parameters
.size());
193 auto ¶m
= this->parameters
[n
];
194 return param
.string
!= nullptr ? param
.string
->c_str() : nullptr;
199 * Extension of StringParameters with its own statically sized buffer for
203 class ArrayStringParameters
: public StringParameters
{
204 std::array
<StringParameter
, N
> params
{}; ///< The actual parameters
207 ArrayStringParameters()
209 this->parameters
= std::span(params
.data(), params
.size());
212 ArrayStringParameters(ArrayStringParameters
&& other
) noexcept
214 *this = std::move(other
);
217 ArrayStringParameters
& operator=(ArrayStringParameters
&&other
) noexcept
219 this->offset
= other
.offset
;
220 this->next_type
= other
.next_type
;
221 this->params
= std::move(other
.params
);
222 this->parameters
= std::span(params
.data(), params
.size());
226 ArrayStringParameters(const ArrayStringParameters
&other
) = delete;
227 ArrayStringParameters
& operator=(const ArrayStringParameters
&other
) = delete;
231 * Helper to create the StringParameters with its own buffer with the given
233 * @param args The parameters to set for the to be created StringParameters.
234 * @return The constructed StringParameters.
236 template <typename
... Args
>
237 static auto MakeParameters(const Args
&... args
)
239 ArrayStringParameters
<sizeof...(args
)> parameters
;
241 (parameters
.SetParam(index
++, std::forward
<const Args
&>(args
)), ...);
246 * Equivalent to the std::back_insert_iterator in function, with some
247 * convenience helpers for string concatenation.
249 class StringBuilder
{
253 /* Required type for this to be an output_iterator; mimics std::back_insert_iterator. */
254 using value_type
= void;
255 using difference_type
= void;
256 using iterator_category
= std::output_iterator_tag
;
257 using pointer
= void;
258 using reference
= void;
261 * Create the builder of an external buffer.
262 * @param start The start location to write to.
263 * @param last The last location to write to.
265 StringBuilder(std::string
&string
) : string(&string
) {}
267 /* Required operators for this to be an output_iterator; mimics std::back_insert_iterator, which has no-ops. */
268 StringBuilder
&operator++() { return *this; }
269 StringBuilder
operator++(int) { return *this; }
270 StringBuilder
&operator*() { return *this; }
273 * Operator to add a character to the end of the buffer. Like the back
274 * insert iterators this also increases the position of the end of the
276 * @param value The character to add.
277 * @return Reference to this inserter.
279 StringBuilder
&operator=(const char value
)
281 return this->operator+=(value
);
285 * Operator to add a character to the end of the buffer.
286 * @param value The character to add.
287 * @return Reference to this inserter.
289 StringBuilder
&operator+=(const char value
)
291 this->string
->push_back(value
);
296 * Operator to append the given string to the output buffer.
297 * @param str The string to add.
298 * @return Reference to this inserter.
300 StringBuilder
&operator+=(std::string_view str
)
302 *this->string
+= str
;
307 * Encode the given Utf8 character into the output buffer.
308 * @param c The character to encode.
310 void Utf8Encode(char32_t c
)
312 auto iterator
= std::back_inserter(*this->string
);
313 ::Utf8Encode(iterator
, c
);
317 * Remove the given amount of characters from the back of the string.
318 * @param amount The amount of characters to remove.
319 * @return true iff there was enough space and the character got added.
321 void RemoveElementsFromBack(size_t amount
)
323 this->string
->erase(this->string
->size() - std::min(amount
, this->string
->size()));
327 * Get the current index in the string.
330 size_t CurrentIndex()
332 return this->string
->size();
336 * Get the reference to the character at the given index.
337 * @return The reference to the character.
339 char &operator[](size_t index
)
341 return (*this->string
)[index
];
345 void GetStringWithArgs(StringBuilder
&builder
, StringID string
, StringParameters
&args
, uint case_index
= 0, bool game_script
= false);
346 std::string
GetStringWithArgs(StringID string
, StringParameters
&args
);
348 /* Do not leak the StringBuilder to everywhere. */
349 void GenerateTownNameString(StringBuilder
&builder
, size_t lang
, uint32_t seed
);
350 void GetTownName(StringBuilder
&builder
, const struct Town
*t
);
351 void GRFTownNameGenerate(StringBuilder
&builder
, uint32_t grfid
, uint16_t gen
, uint32_t seed
);
353 uint
RemapNewGRFStringControlCode(uint scc
, const char **str
, StringParameters
¶meters
, bool modify_parameters
);
355 #endif /* STRINGS_INTERNAL_H */