Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
|
String builder. More...
#include <string_builder.h>
Public Member Functions | |
StringBuilder (char *buf, size_t bufsz) | |
Construct string builder on top of fixed-size buffer. More... | |
StringBuilder (StringBuffer &buf) | |
Construct string builder on top of dynamic buffer. More... | |
size_t | needed_size () const |
Get number of bytes required to store the output string. Includes terminating zero byte. More... | |
size_t | actual_size () const |
Get number of bytes actually written to the output string. Includes terminating zero byte. More... | |
bool | is_ok () const |
Check for errors. More... | |
bool | assign_str (const char *str) |
Overwrite result with given string. If there is not enough space, truncates the string and returns false. More... | |
bool | assign_str (const char *str_begin, const char *str_end) |
Overwrite result with given range. If there is not enough space, truncates the string and returns false. More... | |
bool | append_str (const char *str) |
Append to result given string. If there is not enough space, truncates the string and returns false. More... | |
bool | append_str (const char *str_begin, const char *str_end) |
Append to result given range. If there is not enough space, truncates the string and returns false. More... | |
bool | append_char (char ch) |
Append to result given character. If there is not enough space, truncates the string and returns false. More... | |
bool | append_uint (uint64_t number, unsigned int base) |
Format and append to result given number. If there is not enough space, truncates the string and returns false. More... | |
String builder.
Allows to incrementally build a string. Doesn't own the string itself, but instead holds a reference to external fixed-size or dynamic buffer. Supports "dry run" mode when no actual writing happens. This can be used to calculate the required buffer size before writing.
When used with fixed-sized buffer, all methods are signal-safe and hence can be used from a signal handler.
Definition at line 34 of file string_builder.h.
roc::core::StringBuilder::StringBuilder | ( | char * | buf, |
size_t | bufsz | ||
) |
Construct string builder on top of fixed-size buffer.
The builder will write output string into the given buffer. If the output buffer is too small, the output string is truncated and error flag is set. If the output buffer has at least one byte, it will be always zero-terminated, even if truncation occurred.
buf
may be NULL. In this case, nothing will be written, but needed_size() will be still calculated.
If buf
is non-NULL, bufsz
should be non-zero so that buffer could hold at least zero terminator. Otherwise, error flag is raised immediately in constructor.
If buf
is NULL, bufsz
may be both zero and non-zero. Use non-zero to get an error when buffer size is exceeded (like if it was a real buffer); use zero to disable buffer size checking (there is no buffer anyway).
roc::core::StringBuilder::StringBuilder | ( | StringBuffer & | buf | ) |
Construct string builder on top of dynamic buffer.
The builder will write output string into the given buffer. The buffer will be resized accordingly to the output string size plus terminating zero byte. The buffer will be always zero-terminated.
size_t roc::core::StringBuilder::actual_size | ( | ) | const |
Get number of bytes actually written to the output string. Includes terminating zero byte.
bool roc::core::StringBuilder::append_char | ( | char | ch | ) |
Append to result given character. If there is not enough space, truncates the string and returns false.
bool roc::core::StringBuilder::append_str | ( | const char * | str | ) |
Append to result given string. If there is not enough space, truncates the string and returns false.
bool roc::core::StringBuilder::append_str | ( | const char * | str_begin, |
const char * | str_end | ||
) |
Append to result given range. If there is not enough space, truncates the string and returns false.
bool roc::core::StringBuilder::append_uint | ( | uint64_t | number, |
unsigned int | base | ||
) |
Format and append to result given number. If there is not enough space, truncates the string and returns false.
bool roc::core::StringBuilder::assign_str | ( | const char * | str | ) |
Overwrite result with given string. If there is not enough space, truncates the string and returns false.
bool roc::core::StringBuilder::assign_str | ( | const char * | str_begin, |
const char * | str_end | ||
) |
Overwrite result with given range. If there is not enough space, truncates the string and returns false.
bool roc::core::StringBuilder::is_ok | ( | ) | const |
Check for errors.
size_t roc::core::StringBuilder::needed_size | ( | ) | const |
Get number of bytes required to store the output string. Includes terminating zero byte.