Files
@ r27509:bafaa3ffa550
Branch filter:
Location: cpp/openttd-patchpack/source/src/strings_internal.h
r27509:bafaa3ffa550
4.5 KiB
text/x-c
Cleanup: strecat has no uses anymore
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 | /*
* 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"
/**
* Equivalent to the std::back_insert_iterator in function, with some
* convenience helpers for string concatenation.
*
* The formatter is currently backed by an external char buffer, and has some
* extra functions to ease the migration from char buffers to std::string.
*/
class StringBuilder {
char *current; ///< The current location to add strings
const char *last; ///< The last element of the buffer.
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(char *start, const char *last) : current(start), last(last) {}
/* 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)
{
if (this->current != this->last) *this->current++ = 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+=(const char *str)
{
this->current = strecpy(this->current, str, this->last);
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+=(const std::string &str)
{
return this->operator+=(str.c_str());
}
/**
* Encode the given Utf8 character into the output buffer.
* @param c The character to encode.
* @return true iff there was enough space and the character got added.
*/
bool Utf8Encode(WChar c)
{
if (this->Remaining() < Utf8CharLen(c)) return false;
this->current += ::Utf8Encode(this->current, c);
return true;
}
/**
* Get the pointer to the this->last written element in the buffer.
* This call does '\0' terminate the string, whereas other calls do not
* (necessarily) do this.
* @return The this->current end of the string.
*/
char *GetEnd()
{
*this->current = '\0';
return this->current;
}
/**
* Get the remaining number of characters that can be placed.
* @return The number of characters.
*/
ptrdiff_t Remaining()
{
return (ptrdiff_t)(this->last - this->current);
}
/**
* Add a string using the strecpy-esque calling signature.
* @param function The function to pass the current and last location to,
* that will then return the new current location.
*/
void AddViaStreCallback(std::function<char*(char*, const char*)> function)
{
this->current = function(this->current, this->last);
}
};
void GetStringWithArgs(StringBuilder &builder, StringID string, StringParameters *args, uint case_index = 0, bool game_script = false);
/* Do not leak the StringBuilder to everywhere. */
void GetTownName(StringBuilder &builder, const struct Town *t);
void GRFTownNameGenerate(StringBuilder &builder, uint32 grfid, uint16 gen, uint32 seed);
#endif /* STRINGS_INTERNAL_H */
|