2023-05-11 19:30:58 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file strings_interal.h Types and functions related to the internal workings of formatting OpenTTD's strings. */
|
|
|
|
|
|
|
|
#ifndef STRINGS_INTERNAL_H
|
|
|
|
#define STRINGS_INTERNAL_H
|
|
|
|
|
2023-06-15 15:09:34 +00:00
|
|
|
#include "string_func.h"
|
2023-12-19 01:03:18 +00:00
|
|
|
#include "core/strong_typedef_type.hpp"
|
2023-06-17 10:00:00 +00:00
|
|
|
|
2023-11-09 01:35:31 +00:00
|
|
|
#include <array>
|
|
|
|
|
2023-06-17 10:00:00 +00:00
|
|
|
/** The data required to format and validate a single parameter of a string. */
|
|
|
|
struct StringParameter {
|
|
|
|
uint64_t data; ///< The data of the parameter.
|
2023-06-27 14:59:44 +00:00
|
|
|
std::unique_ptr<std::string> string; ///< Copied string value, if it has any.
|
2023-11-09 01:35:31 +00:00
|
|
|
char32_t type; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
|
2023-06-17 10:00:00 +00:00
|
|
|
};
|
2023-06-15 15:09:34 +00:00
|
|
|
|
|
|
|
class StringParameters {
|
2023-06-13 21:46:08 +00:00
|
|
|
protected:
|
2023-06-13 21:46:08 +00:00
|
|
|
StringParameters *parent = nullptr; ///< If not nullptr, this instance references data from this parent instance.
|
2024-01-16 21:46:00 +00:00
|
|
|
std::span<StringParameter> parameters = {}; ///< Array with the actual parameters.
|
2023-06-15 15:09:34 +00:00
|
|
|
|
2023-06-17 10:00:00 +00:00
|
|
|
size_t offset = 0; ///< Current offset in the parameters span.
|
2023-11-09 01:35:31 +00:00
|
|
|
char32_t next_type = 0; ///< The type of the next data that is retrieved.
|
2023-06-18 08:48:54 +00:00
|
|
|
|
2024-01-16 21:46:00 +00:00
|
|
|
StringParameters(std::span<StringParameter> parameters = {}) :
|
2023-06-17 10:00:00 +00:00
|
|
|
parameters(parameters)
|
|
|
|
{}
|
2023-06-15 15:09:34 +00:00
|
|
|
|
2023-06-22 15:05:32 +00:00
|
|
|
StringParameter *GetNextParameterPointer();
|
|
|
|
|
2023-06-13 21:46:08 +00:00
|
|
|
public:
|
2023-06-15 15:09:34 +00:00
|
|
|
/**
|
|
|
|
* Create a new StringParameters instance that can reference part of the data of
|
2024-01-04 00:25:07 +00:00
|
|
|
* the given parent instance.
|
2023-06-15 15:09:34 +00:00
|
|
|
*/
|
2023-06-18 05:35:30 +00:00
|
|
|
StringParameters(StringParameters &parent, size_t size) :
|
2023-06-15 15:09:34 +00:00
|
|
|
parent(&parent),
|
2023-06-17 10:00:00 +00:00
|
|
|
parameters(parent.parameters.subspan(parent.offset, size))
|
|
|
|
{}
|
2023-06-15 15:09:34 +00:00
|
|
|
|
2023-06-18 08:53:08 +00:00
|
|
|
void PrepareForNextRun();
|
2023-11-09 01:35:31 +00:00
|
|
|
void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
|
2023-06-15 15:09:34 +00:00
|
|
|
|
2023-06-18 08:57:22 +00:00
|
|
|
/**
|
|
|
|
* Get the current offset, so it can be backed up for certain processing
|
|
|
|
* steps, or be used to offset the argument index within sub strings.
|
|
|
|
* @return The current offset.
|
|
|
|
*/
|
|
|
|
size_t GetOffset() { return this->offset; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the offset within the string from where to return the next result of
|
|
|
|
* \c GetInt64 or \c GetInt32.
|
|
|
|
* @param offset The offset.
|
|
|
|
*/
|
|
|
|
void SetOffset(size_t offset)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The offset must be fewer than the number of parameters when it is
|
|
|
|
* being set. Unless restoring a backup, then the original value is
|
|
|
|
* correct as well as long as the offset was not changed. In other
|
|
|
|
* words, when the offset was already at the end of the parameters and
|
|
|
|
* the string did not consume any parameters.
|
|
|
|
*/
|
2023-06-17 10:00:00 +00:00
|
|
|
assert(offset < this->parameters.size() || this->offset == offset);
|
2023-06-18 08:57:22 +00:00
|
|
|
this->offset = offset;
|
|
|
|
}
|
|
|
|
|
2024-01-04 00:25:07 +00:00
|
|
|
/**
|
|
|
|
* Advance the offset within the string from where to return the next result of
|
|
|
|
* \c GetInt64 or \c GetInt32.
|
|
|
|
* @param advance The amount to advance the offset by.
|
|
|
|
*/
|
|
|
|
void AdvanceOffset(size_t advance)
|
|
|
|
{
|
|
|
|
this->offset += advance;
|
|
|
|
assert(this->offset <= this->parameters.size());
|
|
|
|
}
|
2023-06-18 08:57:22 +00:00
|
|
|
|
2023-06-22 15:05:32 +00:00
|
|
|
/**
|
|
|
|
* Get the next parameter from our parameters.
|
|
|
|
* This updates the offset, so the next time this is called the next parameter
|
|
|
|
* will be read.
|
|
|
|
* @return The next parameter's value.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T GetNextParameter()
|
|
|
|
{
|
|
|
|
auto ptr = GetNextParameterPointer();
|
2024-01-04 20:50:58 +00:00
|
|
|
return static_cast<T>(ptr->data);
|
2023-06-22 15:05:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 16:47:32 +00:00
|
|
|
/**
|
|
|
|
* Get the next string parameter from our parameters.
|
|
|
|
* This updates the offset, so the next time this is called the next parameter
|
|
|
|
* will be read.
|
|
|
|
* @return The next parameter's value.
|
|
|
|
*/
|
|
|
|
const char *GetNextParameterString()
|
|
|
|
{
|
|
|
|
auto ptr = GetNextParameterPointer();
|
2024-01-29 22:25:11 +00:00
|
|
|
return ptr->string != nullptr ? ptr->string->c_str() : nullptr;
|
2023-06-22 16:47:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 15:09:34 +00:00
|
|
|
/**
|
|
|
|
* Get a new instance of StringParameters that is a "range" into the
|
2023-06-17 21:17:04 +00:00
|
|
|
* remaining existing parameters. Upon destruction the offset in the parent
|
2023-06-15 15:09:34 +00:00
|
|
|
* is not updated. However, calls to SetDParam do update the parameters.
|
|
|
|
*
|
|
|
|
* The returned StringParameters must not outlive this StringParameters.
|
|
|
|
* @return A "range" of the string parameters.
|
|
|
|
*/
|
2023-06-17 21:17:04 +00:00
|
|
|
StringParameters GetRemainingParameters() { return GetRemainingParameters(this->offset); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a new instance of StringParameters that is a "range" into the
|
|
|
|
* remaining existing parameters from the given offset. Upon destruction the
|
|
|
|
* offset in the parent is not updated. However, calls to SetDParam do
|
|
|
|
* update the parameters.
|
|
|
|
*
|
|
|
|
* The returned StringParameters must not outlive this StringParameters.
|
|
|
|
* @param offset The offset to get the remaining parameters for.
|
|
|
|
* @return A "range" of the string parameters.
|
|
|
|
*/
|
|
|
|
StringParameters GetRemainingParameters(size_t offset)
|
2023-06-15 15:09:34 +00:00
|
|
|
{
|
2023-11-30 11:47:51 +00:00
|
|
|
return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
|
2023-06-15 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Return the amount of elements which can still be read. */
|
2023-06-18 05:35:30 +00:00
|
|
|
size_t GetDataLeft() const
|
2023-06-15 15:09:34 +00:00
|
|
|
{
|
2023-06-17 10:00:00 +00:00
|
|
|
return this->parameters.size() - this->offset;
|
2023-06-15 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the type of a specific element. */
|
2023-11-09 01:35:31 +00:00
|
|
|
char32_t GetTypeAtOffset(size_t offset) const
|
2023-06-15 15:09:34 +00:00
|
|
|
{
|
2023-06-17 10:00:00 +00:00
|
|
|
assert(offset < this->parameters.size());
|
|
|
|
return this->parameters[offset].type;
|
2023-06-15 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 01:35:31 +00:00
|
|
|
void SetParam(size_t n, uint64_t v)
|
2023-06-15 15:09:34 +00:00
|
|
|
{
|
2023-06-17 10:00:00 +00:00
|
|
|
assert(n < this->parameters.size());
|
|
|
|
this->parameters[n].data = v;
|
2023-06-27 14:59:44 +00:00
|
|
|
this->parameters[n].string.reset();
|
2023-06-22 16:47:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 01:03:18 +00:00
|
|
|
template <typename T, std::enable_if_t<std::is_base_of<StrongTypedefBase, T>::value, int> = 0>
|
|
|
|
void SetParam(size_t n, T v)
|
|
|
|
{
|
|
|
|
SetParam(n, v.base());
|
|
|
|
}
|
2023-11-09 01:35:31 +00:00
|
|
|
|
2023-06-22 16:47:32 +00:00
|
|
|
void SetParam(size_t n, const char *str)
|
|
|
|
{
|
|
|
|
assert(n < this->parameters.size());
|
|
|
|
this->parameters[n].data = 0;
|
2024-01-29 22:25:11 +00:00
|
|
|
this->parameters[n].string = std::make_unique<std::string>(str);
|
2023-06-15 15:09:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-09 01:35:31 +00:00
|
|
|
void SetParam(size_t n, std::string str)
|
2023-06-27 14:59:44 +00:00
|
|
|
{
|
|
|
|
assert(n < this->parameters.size());
|
|
|
|
this->parameters[n].data = 0;
|
|
|
|
this->parameters[n].string = std::make_unique<std::string>(std::move(str));
|
|
|
|
}
|
2023-06-13 21:46:08 +00:00
|
|
|
|
2023-11-09 01:35:31 +00:00
|
|
|
uint64_t GetParam(size_t n) const
|
2023-06-15 15:09:34 +00:00
|
|
|
{
|
2023-06-17 10:00:00 +00:00
|
|
|
assert(n < this->parameters.size());
|
2024-01-29 22:25:11 +00:00
|
|
|
assert(this->parameters[n].string == nullptr);
|
2023-06-17 10:00:00 +00:00
|
|
|
return this->parameters[n].data;
|
2023-06-15 15:09:34 +00:00
|
|
|
}
|
2023-06-22 16:47:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the stored string of the parameter, or \c nullptr when there is none.
|
|
|
|
* @param n The index into the parameters.
|
|
|
|
* @return The stored string.
|
|
|
|
*/
|
|
|
|
const char *GetParamStr(size_t n) const
|
|
|
|
{
|
|
|
|
assert(n < this->parameters.size());
|
2023-06-27 14:59:44 +00:00
|
|
|
auto ¶m = this->parameters[n];
|
2024-01-29 22:25:11 +00:00
|
|
|
return param.string != nullptr ? param.string->c_str() : nullptr;
|
2023-06-22 16:47:32 +00:00
|
|
|
}
|
2023-06-15 15:09:34 +00:00
|
|
|
};
|
2023-05-18 16:33:18 +00:00
|
|
|
|
2023-11-09 01:35:31 +00:00
|
|
|
/**
|
|
|
|
* Extension of StringParameters with its own statically sized buffer for
|
|
|
|
* the parameters.
|
|
|
|
*/
|
|
|
|
template <size_t N>
|
|
|
|
class ArrayStringParameters : public StringParameters {
|
2023-11-09 17:43:20 +00:00
|
|
|
std::array<StringParameter, N> params{}; ///< The actual parameters
|
2023-11-09 01:35:31 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ArrayStringParameters()
|
|
|
|
{
|
2024-01-16 21:46:00 +00:00
|
|
|
this->parameters = std::span(params.data(), params.size());
|
2023-11-09 01:35:31 +00:00
|
|
|
}
|
2023-11-09 17:43:20 +00:00
|
|
|
|
|
|
|
ArrayStringParameters(ArrayStringParameters&& other) noexcept
|
|
|
|
{
|
|
|
|
*this = std::move(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayStringParameters& operator=(ArrayStringParameters &&other) noexcept
|
|
|
|
{
|
|
|
|
this->offset = other.offset;
|
|
|
|
this->next_type = other.next_type;
|
|
|
|
this->params = std::move(other.params);
|
2024-01-16 21:46:00 +00:00
|
|
|
this->parameters = std::span(params.data(), params.size());
|
2023-11-09 17:43:20 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-01-03 21:33:38 +00:00
|
|
|
ArrayStringParameters(const ArrayStringParameters &other) = delete;
|
2023-11-09 17:43:20 +00:00
|
|
|
ArrayStringParameters& operator=(const ArrayStringParameters &other) = delete;
|
2023-11-09 01:35:31 +00:00
|
|
|
};
|
|
|
|
|
2023-06-13 21:46:08 +00:00
|
|
|
/**
|
|
|
|
* Helper to create the StringParameters with its own buffer with the given
|
|
|
|
* parameter values.
|
|
|
|
* @param args The parameters to set for the to be created StringParameters.
|
|
|
|
* @return The constructed StringParameters.
|
|
|
|
*/
|
|
|
|
template <typename... Args>
|
|
|
|
static auto MakeParameters(const Args&... args)
|
|
|
|
{
|
2023-11-09 01:35:31 +00:00
|
|
|
ArrayStringParameters<sizeof...(args)> parameters;
|
2023-06-13 21:46:08 +00:00
|
|
|
size_t index = 0;
|
|
|
|
(parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
|
|
|
|
return parameters;
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:45:55 +00:00
|
|
|
/**
|
|
|
|
* Equivalent to the std::back_insert_iterator in function, with some
|
|
|
|
* convenience helpers for string concatenation.
|
|
|
|
*/
|
|
|
|
class StringBuilder {
|
|
|
|
std::string *string;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/* Required type for this to be an output_iterator; mimics std::back_insert_iterator. */
|
|
|
|
using value_type = void;
|
|
|
|
using difference_type = void;
|
|
|
|
using iterator_category = std::output_iterator_tag;
|
|
|
|
using pointer = void;
|
|
|
|
using reference = void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the builder of an external buffer.
|
|
|
|
* @param start The start location to write to.
|
|
|
|
* @param last The last location to write to.
|
|
|
|
*/
|
|
|
|
StringBuilder(std::string &string) : string(&string) {}
|
|
|
|
|
|
|
|
/* Required operators for this to be an output_iterator; mimics std::back_insert_iterator, which has no-ops. */
|
|
|
|
StringBuilder &operator++() { return *this; }
|
|
|
|
StringBuilder operator++(int) { return *this; }
|
|
|
|
StringBuilder &operator*() { return *this; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator to add a character to the end of the buffer. Like the back
|
|
|
|
* insert iterators this also increases the position of the end of the
|
|
|
|
* buffer.
|
|
|
|
* @param value The character to add.
|
|
|
|
* @return Reference to this inserter.
|
|
|
|
*/
|
|
|
|
StringBuilder &operator=(const char value)
|
|
|
|
{
|
|
|
|
return this->operator+=(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator to add a character to the end of the buffer.
|
|
|
|
* @param value The character to add.
|
|
|
|
* @return Reference to this inserter.
|
|
|
|
*/
|
|
|
|
StringBuilder &operator+=(const char value)
|
|
|
|
{
|
|
|
|
this->string->push_back(value);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Operator to append the given string to the output buffer.
|
|
|
|
* @param str The string to add.
|
|
|
|
* @return Reference to this inserter.
|
|
|
|
*/
|
|
|
|
StringBuilder &operator+=(std::string_view str)
|
|
|
|
{
|
|
|
|
*this->string += str;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the given Utf8 character into the output buffer.
|
|
|
|
* @param c The character to encode.
|
|
|
|
*/
|
|
|
|
void Utf8Encode(char32_t c)
|
|
|
|
{
|
|
|
|
auto iterator = std::back_inserter(*this->string);
|
|
|
|
::Utf8Encode(iterator, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the given amount of characters from the back of the string.
|
|
|
|
* @param amount The amount of characters to remove.
|
|
|
|
* @return true iff there was enough space and the character got added.
|
|
|
|
*/
|
|
|
|
void RemoveElementsFromBack(size_t amount)
|
|
|
|
{
|
|
|
|
this->string->erase(this->string->size() - std::min(amount, this->string->size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current index in the string.
|
|
|
|
* @return The index.
|
|
|
|
*/
|
|
|
|
size_t CurrentIndex()
|
|
|
|
{
|
|
|
|
return this->string->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the reference to the character at the given index.
|
|
|
|
* @return The reference to the character.
|
|
|
|
*/
|
|
|
|
char &operator[](size_t index)
|
|
|
|
{
|
|
|
|
return (*this->string)[index];
|
|
|
|
}
|
2024-01-05 21:12:54 +00:00
|
|
|
|
|
|
|
std::string *GetTargetString()
|
|
|
|
{
|
|
|
|
return this->string;
|
|
|
|
}
|
2024-01-05 14:45:55 +00:00
|
|
|
};
|
|
|
|
|
2024-01-05 21:12:54 +00:00
|
|
|
void GetStringWithArgs(StringBuilder builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
|
|
|
|
std::string GetStringWithArgs(StringID string, StringParameters &args);
|
|
|
|
|
|
|
|
void GetString(StringBuilder builder, StringID string);
|
|
|
|
|
|
|
|
/* Do not leak the StringBuilder to everywhere. */
|
|
|
|
void GenerateTownNameString(StringBuilder builder, size_t lang, uint32_t seed);
|
|
|
|
void GetTownName(StringBuilder builder, const struct Town *t);
|
|
|
|
void GRFTownNameGenerate(StringBuilder builder, uint32_t grfid, uint16_t gen, uint32_t seed);
|
|
|
|
|
2023-05-11 19:30:58 +00:00
|
|
|
#endif /* STRINGS_INTERNAL_H */
|