Files
@ r28773:0150d0acc334
Branch filter:
Location: cpp/openttd-patchpack/source/src/strings_internal.h
r28773:0150d0acc334
10.9 KiB
text/x-c
Change: Remove saving of digit group and decimal separator configurations from the savegame
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | /*
* 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
#include "strings_func.h"
#include "string_func.h"
/** The data required to format and validate a single parameter of a string. */
struct StringParameter {
uint64_t data; ///< The data of the parameter.
std::unique_ptr<std::string> string; ///< Copied string value, if it has any.
char32_t type; ///< The #StringControlCode to interpret this data with when it's the first parameter, otherwise '\0'.
};
class StringParameters {
protected:
StringParameters *parent = nullptr; ///< If not nullptr, this instance references data from this parent instance.
std::span<StringParameter> parameters = {}; ///< Array with the actual parameters.
size_t offset = 0; ///< Current offset in the parameters span.
char32_t next_type = 0; ///< The type of the next data that is retrieved.
StringParameters(std::span<StringParameter> parameters = {}) :
parameters(parameters)
{}
StringParameter *GetNextParameterPointer();
public:
/**
* Create a new StringParameters instance that can reference part of the data of
* the given parent instance.
*/
StringParameters(StringParameters &parent, size_t size) :
parent(&parent),
parameters(parent.parameters.subspan(parent.offset, size))
{}
void PrepareForNextRun();
void SetTypeOfNextParameter(char32_t type) { this->next_type = type; }
/**
* 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.
*/
assert(offset < this->parameters.size() || this->offset == offset);
this->offset = offset;
}
/**
* 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());
}
/**
* 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();
return static_cast<T>(ptr->data);
}
/**
* 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();
return ptr->string != nullptr ? ptr->string->c_str() : nullptr;
}
/**
* Get a new instance of StringParameters that is a "range" into the
* remaining existing parameters. 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.
* @return A "range" of the string parameters.
*/
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)
{
return StringParameters(this->parameters.subspan(offset, this->parameters.size() - offset));
}
/** Return the amount of elements which can still be read. */
size_t GetDataLeft() const
{
return this->parameters.size() - this->offset;
}
/** Get the type of a specific element. */
char32_t GetTypeAtOffset(size_t offset) const
{
assert(offset < this->parameters.size());
return this->parameters[offset].type;
}
void SetParam(size_t n, uint64_t v)
{
assert(n < this->parameters.size());
this->parameters[n].data = v;
this->parameters[n].string.reset();
}
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());
}
void SetParam(size_t n, const char *str)
{
assert(n < this->parameters.size());
this->parameters[n].data = 0;
this->parameters[n].string = std::make_unique<std::string>(str);
}
void SetParam(size_t n, const std::string &str) { this->SetParam(n, str.c_str()); }
void SetParam(size_t n, std::string &&str)
{
assert(n < this->parameters.size());
this->parameters[n].data = 0;
this->parameters[n].string = std::make_unique<std::string>(std::move(str));
}
uint64_t GetParam(size_t n) const
{
assert(n < this->parameters.size());
assert(this->parameters[n].string == nullptr);
return this->parameters[n].data;
}
/**
* 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());
auto ¶m = this->parameters[n];
return param.string != nullptr ? param.string->c_str() : nullptr;
}
};
/**
* Extension of StringParameters with its own statically sized buffer for
* the parameters.
*/
template <size_t N>
class ArrayStringParameters : public StringParameters {
std::array<StringParameter, N> params{}; ///< The actual parameters
public:
ArrayStringParameters()
{
this->parameters = std::span(params.data(), params.size());
}
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);
this->parameters = std::span(params.data(), params.size());
return *this;
}
ArrayStringParameters(const ArrayStringParameters &other) = delete;
ArrayStringParameters& operator=(const ArrayStringParameters &other) = delete;
};
/**
* 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)
{
ArrayStringParameters<sizeof...(args)> parameters;
size_t index = 0;
(parameters.SetParam(index++, std::forward<const Args&>(args)), ...);
return parameters;
}
/**
* 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];
}
};
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters &args, uint case_index = 0, bool game_script = false);
std::string GetStringWithArgs(StringID string, StringParameters &args);
/* 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);
uint RemapNewGRFStringControlCode(uint scc, const char **str, StringParameters ¶meters, bool modify_parameters);
#endif /* STRINGS_INTERNAL_H */
|