OpenTTD Source  14.0-beta1
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  uint64_t data;
19  std::unique_ptr<std::string> string;
20  char32_t type;
21 };
22 
24 protected:
25  StringParameters *parent = nullptr;
26  std::span<StringParameter> parameters = {};
27 
28  size_t offset = 0;
29  char32_t next_type = 0;
30 
31  StringParameters(std::span<StringParameter> parameters = {}) :
33  {}
34 
36 
37 public:
43  parent(&parent),
44  parameters(parent.parameters.subspan(parent.offset, size))
45  {}
46 
47  void PrepareForNextRun();
48  void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
49 
55  size_t GetOffset() { return this->offset; }
56 
62  void SetOffset(size_t offset)
63  {
64  /*
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.
70  */
71  assert(offset < this->parameters.size() || this->offset == offset);
72  this->offset = offset;
73  }
74 
80  void AdvanceOffset(size_t advance)
81  {
82  this->offset += advance;
83  assert(this->offset <= this->parameters.size());
84  }
85 
92  template <typename T>
94  {
95  auto ptr = GetNextParameterPointer();
96  return static_cast<T>(ptr->data);
97  }
98 
106  {
107  auto ptr = GetNextParameterPointer();
108  return ptr->string != nullptr ? ptr->string->c_str() : nullptr;
109  }
110 
120 
132  {
133  return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
134  }
135 
137  size_t GetDataLeft() const
138  {
139  return this->parameters.size() - this->offset;
140  }
141 
143  char32_t GetTypeAtOffset(size_t offset) const
144  {
145  assert(offset < this->parameters.size());
146  return this->parameters[offset].type;
147  }
148 
149  void SetParam(size_t n, uint64_t v)
150  {
151  assert(n < this->parameters.size());
152  this->parameters[n].data = v;
153  this->parameters[n].string.reset();
154  }
155 
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)
158  {
159  SetParam(n, v.base());
160  }
161 
162  void SetParam(size_t n, const char *str)
163  {
164  assert(n < this->parameters.size());
165  this->parameters[n].data = 0;
166  this->parameters[n].string = std::make_unique<std::string>(str);
167  }
168 
169  void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
170 
171  void SetParam(size_t n, std::string &&str)
172  {
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));
176  }
177 
178  uint64_t GetParam(size_t n) const
179  {
180  assert(n < this->parameters.size());
181  assert(this->parameters[n].string == nullptr);
182  return this->parameters[n].data;
183  }
184 
190  const char *GetParamStr(size_t n) const
191  {
192  assert(n < this->parameters.size());
193  auto &param = this->parameters[n];
194  return param.string != nullptr ? param.string->c_str() : nullptr;
195  }
196 };
197 
202 template <size_t N>
204  std::array<StringParameter, N> params{};
205 
206 public:
208  {
209  this->parameters = std::span(params.data(), params.size());
210  }
211 
213  {
214  *this = std::move(other);
215  }
216 
217  ArrayStringParameters& operator=(ArrayStringParameters &&other) noexcept
218  {
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());
223  return *this;
224  }
225 
226  ArrayStringParameters(const ArrayStringParameters &other) = delete;
227  ArrayStringParameters& operator=(const ArrayStringParameters &other) = delete;
228 };
229 
236 template <typename... Args>
237 static auto MakeParameters(const Args&... args)
238 {
239  ArrayStringParameters<sizeof...(args)> parameters;
240  size_t index = 0;
241  (parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
242  return parameters;
243 }
244 
250  std::string *string;
251 
252 public:
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;
259 
265  StringBuilder(std::string &string) : string(&string) {}
266 
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; }
271 
279  StringBuilder &operator=(const char value)
280  {
281  return this->operator+=(value);
282  }
283 
289  StringBuilder &operator+=(const char value)
290  {
291  this->string->push_back(value);
292  return *this;
293  }
294 
300  StringBuilder &operator+=(std::string_view str)
301  {
302  *this->string += str;
303  return *this;
304  }
305 
310  void Utf8Encode(char32_t c)
311  {
312  auto iterator = std::back_inserter(*this->string);
313  ::Utf8Encode(iterator, c);
314  }
315 
321  void RemoveElementsFromBack(size_t amount)
322  {
323  this->string->erase(this->string->size() - std::min(amount, this->string->size()));
324  }
325 
330  size_t CurrentIndex()
331  {
332  return this->string->size();
333  }
334 
339  char &operator[](size_t index)
340  {
341  return (*this->string)[index];
342  }
343 };
344 
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);
347 
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);
352 
353 uint RemapNewGRFStringControlCode(uint scc, const char **str, StringParameters &parameters, bool modify_parameters);
354 
355 #endif /* STRINGS_INTERNAL_H */
StringParameters::SetOffset
void SetOffset(size_t offset)
Set the offset within the string from where to return the next result of GetInt64 or GetInt32.
Definition: strings_internal.h:62
StringBuilder
Equivalent to the std::back_insert_iterator in function, with some convenience helpers for string con...
Definition: strings_internal.h:249
StringParameters::GetOffset
size_t GetOffset()
Get the current offset, so it can be backed up for certain processing steps, or be used to offset the...
Definition: strings_internal.h:55
ArrayStringParameters
Extension of StringParameters with its own statically sized buffer for the parameters.
Definition: strings_internal.h:203
StringParameters::PrepareForNextRun
void PrepareForNextRun()
Prepare the string parameters for the next formatting run.
Definition: strings.cpp:68
StringParameter::data
uint64_t data
The data of the parameter.
Definition: strings_internal.h:18
StringParameters::parent
StringParameters * parent
If not nullptr, this instance references data from this parent instance.
Definition: strings_internal.h:25
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
StringParameters::GetNextParameter
T GetNextParameter()
Get the next parameter from our parameters.
Definition: strings_internal.h:93
GenerateTownNameString
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.
Definition: townname.cpp:1013
StringParameters::GetParamStr
const char * GetParamStr(size_t n) const
Get the stored string of the parameter, or nullptr when there is none.
Definition: strings_internal.h:190
StringParameters::GetNextParameterPointer
StringParameter * GetNextParameterPointer()
Get the next parameter from our parameters.
Definition: strings.cpp:81
StringParameter
The data required to format and validate a single parameter of a string.
Definition: strings_internal.h:17
StringParameters::GetNextParameterString
const char * GetNextParameterString()
Get the next string parameter from our parameters.
Definition: strings_internal.h:105
StringBuilder::RemoveElementsFromBack
void RemoveElementsFromBack(size_t amount)
Remove the given amount of characters from the back of the string.
Definition: strings_internal.h:321
GetStringWithArgs
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:261
StringParameters::GetRemainingParameters
StringParameters GetRemainingParameters()
Get a new instance of StringParameters that is a "range" into the remaining existing parameters.
Definition: strings_internal.h:119
RemapNewGRFStringControlCode
uint RemapNewGRFStringControlCode(uint scc, const char **str, StringParameters &parameters, bool modify_parameters)
FormatString for NewGRF specific "magic" string control codes.
Definition: newgrf_text.cpp:828
StringParameters::AdvanceOffset
void AdvanceOffset(size_t advance)
Advance the offset within the string from where to return the next result of GetInt64 or GetInt32.
Definition: strings_internal.h:80
StringParameters::GetTypeAtOffset
char32_t GetTypeAtOffset(size_t offset) const
Get the type of a specific element.
Definition: strings_internal.h:143
StringParameters::GetDataLeft
size_t GetDataLeft() const
Return the amount of elements which can still be read.
Definition: strings_internal.h:137
StringBuilder::operator+=
StringBuilder & operator+=(const char value)
Operator to add a character to the end of the buffer.
Definition: strings_internal.h:289
ArrayStringParameters::params
std::array< StringParameter, N > params
The actual parameters.
Definition: strings_internal.h:204
StringBuilder::operator[]
char & operator[](size_t index)
Get the reference to the character at the given index.
Definition: strings_internal.h:339
StringBuilder::operator+=
StringBuilder & operator+=(std::string_view str)
Operator to append the given string to the output buffer.
Definition: strings_internal.h:300
StringBuilder::StringBuilder
StringBuilder(std::string &string)
Create the builder of an external buffer.
Definition: strings_internal.h:265
string_func.h
strings_func.h
StringParameters
Definition: strings_internal.h:23
StringParameters::StringParameters
StringParameters(StringParameters &parent, size_t size)
Create a new StringParameters instance that can reference part of the data of the given parent instan...
Definition: strings_internal.h:42
StringParameters::offset
size_t offset
Current offset in the parameters span.
Definition: strings_internal.h:28
GetTownName
static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32_t townnameparts)
Fills builder with specified town name.
Definition: townname.cpp:48
StringParameter::string
std::unique_ptr< std::string > string
Copied string value, if it has any.
Definition: strings_internal.h:19
StringBuilder::CurrentIndex
size_t CurrentIndex()
Get the current index in the string.
Definition: strings_internal.h:330
StringParameters::parameters
std::span< StringParameter > parameters
Array with the actual parameters.
Definition: strings_internal.h:26
Town
Town data structure.
Definition: town.h:50
StringParameter::type
char32_t type
The StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
Definition: strings_internal.h:20
StringBuilder::operator=
StringBuilder & operator=(const char value)
Operator to add a character to the end of the buffer.
Definition: strings_internal.h:279
StringParameters::GetRemainingParameters
StringParameters GetRemainingParameters(size_t offset)
Get a new instance of StringParameters that is a "range" into the remaining existing parameters from ...
Definition: strings_internal.h:131
StringBuilder::Utf8Encode
void Utf8Encode(char32_t c)
Encode the given Utf8 character into the output buffer.
Definition: strings_internal.h:310
StringParameters::next_type
char32_t next_type
The type of the next data that is retrieved.
Definition: strings_internal.h:29