Add StringBuilder class from upstream

pull/621/head
Jonathan G Rennison 5 months ago
parent 7f6b1d6b94
commit 8832ab8e5d

@ -248,4 +248,104 @@ static auto MakeParameters(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];
}
};
#endif /* STRINGS_INTERNAL_H */

Loading…
Cancel
Save