10 #ifndef STRINGS_INTERNAL_H
11 #define STRINGS_INTERNAL_H
47 void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
81 this->offset += advance;
82 assert(this->offset <= this->
parameters.size());
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"); }
100 return static_cast<T
>(std::visit(visitor{}, param.data));
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(); }
117 return std::visit(visitor{}, param.data);
148 return this->parameters.size() - this->
offset;
155 return this->parameters[
offset].type;
158 void SetParam(
size_t n,
const StringParameterData &v)
161 this->parameters[n].data = v;
164 void SetParam(
size_t n, uint64_t v)
167 this->parameters[n].data = v;
170 template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value,
int> = 0>
171 void SetParam(
size_t n, T v)
173 SetParam(n, v.base());
176 void SetParam(
size_t n,
const char *str)
179 this->parameters[n].data = str;
182 void SetParam(
size_t n,
const std::string &str) { this->SetParam(n, str.c_str()); }
184 void SetParam(
size_t n, std::string &&str)
187 this->parameters[n].data = std::move(str);
190 const StringParameterData &GetParam(
size_t n)
const
193 return this->parameters[n].data;
208 this->parameters = std::span(
params.data(),
params.size());
213 *
this = std::move(other);
218 this->offset = other.
offset;
219 this->next_type = other.next_type;
220 this->
params = std::move(other.params);
221 this->parameters = std::span(
params.data(),
params.size());
235 template <
typename... Args>
236 static auto MakeParameters(
const Args&... args)
240 (parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
253 using value_type = void;
254 using difference_type = void;
255 using iterator_category = std::output_iterator_tag;
256 using pointer = void;
257 using reference = void;
289 this->
string->push_back(value);
300 *this->
string += str;
310 auto iterator = std::back_inserter(*this->
string);
320 this->
string->erase(this->string->size() - std::min(amount, this->string->size()));
329 return this->
string->size();
338 return (*this->
string)[index];
348 void GRFTownNameGenerate(
StringBuilder &builder, uint32_t grfid, uint16_t gen, uint32_t seed);
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.
const StringParameter & GetNextParameterReference()
Get the next parameter from our parameters.
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 ¶meters, 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.
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'.
static void GetTownName(StringBuilder &builder, const TownNameParams *par, uint32_t townnameparts)
Fills builder with specified town name.
void GenerateTownNameString(StringBuilder &builder, size_t lang, uint32_t seed)
Generates town name from given seed.