OpenTTD Source  20241108-master-g80f628063a
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 
23 protected:
24  StringParameters *parent = nullptr;
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 
36 public:
42  parent(&parent),
43  parameters(parent.parameters.subspan(parent.offset, size))
44  {}
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  const auto &param = GetNextParameterReference();
95  const uint64_t *data = std::get_if<uint64_t>(&param.data);
96  if (data != nullptr) return static_cast<T>(*data);
97  throw std::out_of_range("Attempt to read string parameter as integer");
98  }
99 
107  {
108  const auto &param = GetNextParameterReference();
109  const std::string *data = std::get_if<std::string>(&param.data);
110  if (data != nullptr) return data->c_str();
111  throw std::out_of_range("Attempt to read integer parameter as string");
112  }
113 
123 
135  {
136  return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
137  }
138 
140  size_t GetDataLeft() const
141  {
142  return this->parameters.size() - this->offset;
143  }
144 
146  char32_t GetTypeAtOffset(size_t offset) const
147  {
148  assert(offset < this->parameters.size());
149  return this->parameters[offset].type;
150  }
151 
152  void SetParam(size_t n, const StringParameterData &v)
153  {
154  assert(n < this->parameters.size());
155  this->parameters[n].data = v;
156  }
157 
158  void SetParam(size_t n, uint64_t v)
159  {
160  assert(n < this->parameters.size());
161  this->parameters[n].data = v;
162  }
163 
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)
166  {
167  SetParam(n, v.base());
168  }
169 
170  void SetParam(size_t n, const char *str)
171  {
172  assert(n < this->parameters.size());
173  this->parameters[n].data = str;
174  }
175 
176  void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
177 
178  void SetParam(size_t n, std::string &&str)
179  {
180  assert(n < this->parameters.size());
181  this->parameters[n].data = std::move(str);
182  }
183 
184  const StringParameterData &GetParam(size_t n) const
185  {
186  assert(n < this->parameters.size());
187  return this->parameters[n].data;
188  }
189 };
190 
195 template <size_t N>
197  std::array<StringParameter, N> params{};
198 
199 public:
201  {
202  this->parameters = std::span(params.data(), params.size());
203  }
204 
206  {
207  *this = std::move(other);
208  }
209 
210  ArrayStringParameters& operator=(ArrayStringParameters &&other) noexcept
211  {
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());
216  return *this;
217  }
218 
219  ArrayStringParameters(const ArrayStringParameters &other) = delete;
220  ArrayStringParameters& operator=(const ArrayStringParameters &other) = delete;
221 };
222 
229 template <typename... Args>
230 static auto MakeParameters(const Args&... args)
231 {
232  ArrayStringParameters<sizeof...(args)> parameters;
233  size_t index = 0;
234  (parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
235  return parameters;
236 }
237 
243  std::string *string;
244 
245 public:
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;
252 
257  StringBuilder(std::string &string) : string(&string) {}
258 
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; }
263 
271  StringBuilder &operator=(const char value)
272  {
273  return this->operator+=(value);
274  }
275 
281  StringBuilder &operator+=(const char value)
282  {
283  this->string->push_back(value);
284  return *this;
285  }
286 
292  StringBuilder &operator+=(std::string_view str)
293  {
294  *this->string += str;
295  return *this;
296  }
297 
302  void Utf8Encode(char32_t c)
303  {
304  auto iterator = std::back_inserter(*this->string);
305  ::Utf8Encode(iterator, c);
306  }
307 
312  void RemoveElementsFromBack(size_t amount)
313  {
314  this->string->erase(this->string->size() - std::min(amount, this->string->size()));
315  }
316 
321  size_t CurrentIndex()
322  {
323  return this->string->size();
324  }
325 
330  char &operator[](size_t index)
331  {
332  return (*this->string)[index];
333  }
334 };
335 
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);
338 
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);
343 
344 uint RemapNewGRFStringControlCode(uint scc, const char **str, StringParameters &parameters, bool modify_parameters);
345 
346 #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...
void RemoveElementsFromBack(size_t amount)
Remove the given amount of characters from the back of the string.
StringBuilder & operator+=(const char value)
Operator to add a character to the end of the buffer.
StringBuilder & operator=(const char value)
Operator to add a character to the end of the buffer.
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.
char & operator[](size_t index)
Get the reference to the character at the given index.
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.
const char * GetNextParameterString()
Get the next string 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 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 ...
uint RemapNewGRFStringControlCode(uint 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:243
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
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:54
static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32_t townnameparts)
Fills builder with specified town name.
Definition: townname.cpp:48
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.
Definition: townname.cpp:1013