-
Notifications
You must be signed in to change notification settings - Fork 11
ST::string_stream
#include <string_theory/string_stream>| Name | Summary |
|---|---|
| (constructor) | ST::string_stream constructors |
| (destructor) | ST::string_stream destructor |
| operator= | Move assignment operator |
| append | Directly append some text to the stream |
| append_char | Append one or more copies of a character to the stream |
| operator<< | Stream operator for various formattable types |
| raw_buffer | Retrieve the raw (unterminated) string data pointer |
| size | Return the current size of the stream |
| to_string | Convert the stream data to an ST::string
|
| truncate | Reset the stream's size and position to 0
|
| erase | Erase characters from the back of the stream |
ST::string_stream provides an efficient way to append text and numbers (with
limited formatting options) to a memory buffer. The buffer will grow
automatically as more text is added. If the data stored in the stream is short
enough, it will remain completely on the stack.
The string_stream is particularly useful for building strings incrementally
through various pieces that have some sort of logic or looping involved, as it
is much more efficient than adding strings with operator+ or operator+=.
Example:
// Slow way -- allocates several "fit length" strings:
ST::string s1 = "There are " + ST::string::from_int(count) + " items in boxes (";
for (auto box : boxes)
s1 += box + ", ";
s1 = s1.left(s1.size() - 2); // Erase last comma
s1 += ")";
// Fast way, construct only one string.
ST::string_stream ss;
ss << "There are " << count << " items in boxes (";
for (auto box : boxes)
ss << box << ", ";
s1.erase(2); // Erase last comma
ss << ")";
ST::string s2 = ss.to_string();Note that for more control over formatting, you should use ST::format instead. However, because ST::string_stream can take ST::string objects, it is possible to combine ST::format with ST::string_stream:
ST::string_stream ss;
ss << "Some fancy formatting " << ST::format("{<40x}", number);| Signature | |
|---|---|
| string_stream() noexcept | (1) |
| string_stream(string_stream &&move) noexcept | (2) |
- Creates an empty stream (size() ==
0). - Moves the stream content from
moveinto this string_stream.
Note that string_streams are not copyable.
See also operator=()
| Signature |
|---|
| ~string_stream() noexcept |
Destroys the string_stream, freeing any allocated memory.
| Signature |
|---|
| string_stream &append(const char *data, size_t size = ST_AUTO_SIZE) |
Append the first size bytes of the string data from data to the end of
this stream. If size is ST_AUTO_SIZE, then the length of data is
determined with strlen.
| Signature |
|---|
| string_stream &append(char ch, size_t count = 1) |
Append count copies of the character ch to the end of this stream.
| Signature |
|---|
| void erase(size_t count) noexcept |
Remove count bytes from the end of the stream. If count is greater than
the current stream size, this will truncate the stream to 0 bytes. This
will not cause a buffer reallocation, so it is essentially free to use.
| Signature |
|---|
| string_stream &operator=(string_stream &&move) noexcept |
Move operator. Moves the string content from move into this string_stream.
See also (constructor)
| Signature | |
|---|---|
| string_stream &operator<<(const char *text) | (1) |
| string_stream &operator<<(int num) | (2) |
| string_stream &operator<<(unsigned int num) | (3) |
| string_stream &operator<<(long num) | (4) |
| string_stream &operator<<(unsigned long num) | (5) |
| string_stream &operator<<(int64_t num) | (6) |
| string_stream &operator<<(uint64_t num) | (7) |
| string_stream &operator<<(float num) | (8) |
| string_stream &operator<<(double num) | (9) |
| string_stream &operator<<(char num) | (10) |
| string_stream &operator<<(const ST::string &text) | (11) |
- Append the C-string
textto the end of this stream. The length oftextis determined withstrlen, so it must be nul-terminated. - Append a string representation of
numin signed decimal notation to the end of this stream. - Append a string representation of
numin unsigned decimal notation to the end of this stream. - Append a string representation of
numin signed decimal notation to the end of this stream. - Append a string representation of
numin unsigned decimal notation to the end of this stream. - Append a string representation of
numin signed decimal notation to the end of this stream. - Append a string representation of
numin unsigned decimal notation to the end of this stream. - Append a string representation of
numin floating-point decimal notation (using the"%g"printf-style formatting rules) to the end of this stream. - Append a string representation of
numin floating-point decimal notation (using the"%g"printf-style formatting rules) to the end of this stream. - Append the 7-bit ASCII character
chto the end of this stream. - Append the contents of
textto the end of this stream.
| Signature |
|---|
| const char *raw_buffer() const noexcept |
Return a pointer to the beginning of the stream data. Note that this buffer is NOT nul-terminated, so it should not be used directly in functions that expect C-style strings.
| Signature |
|---|
| size_t size() const noexcept |
Returns the size of the data currently stored in the stream, in bytes.
See also raw_buffer()
| Signature |
|---|
| ST::string to_string(bool utf8_encoded = true, ST::utf_validation_t validation = ST::assert_validity) const |
Construct an ST::string object from the string data. This will return a copy of the data in the string (properly nul-terminated), so you can continue to manipulate the string_stream after creating a string.
If utf8_encoded is true, the data in the stream is converted with
ST::string::from_utf8 with the specified
validation. If false, the data is converted as Latin-1 with
ST::string::from_latin_1, and validation is
ignored.
See also raw_buffer()
| Signature |
|---|
| void truncate(size_t size = 0) noexcept |
This will reset the stream's size to size. If size is greater than the
current stream size, this method does nothing. It will not reallocate its
internal buffer, so this may be useful when building multiple strings using
the same string_stream object to construct multiple strings.