12 #ifndef ROC_CORE_STRING_BUILDER_H_
13 #define ROC_CORE_STRING_BUILDER_H_
87 bool assign_str(
const char* str_begin,
const char* str_end);
95 bool append_str(
const char* str_begin,
const char* str_end);
106 class IBufferWriter {
108 virtual ~IBufferWriter();
110 virtual bool is_noop() = 0;
111 virtual bool reset() = 0;
112 virtual bool grow_by(
size_t n_chars) = 0;
113 virtual ssize_t extend_by(
size_t n_chars) = 0;
114 virtual char* write_ptr() = 0;
117 class StaticBufferWriter :
public IBufferWriter {
119 StaticBufferWriter(
char* buf,
size_t buf_size);
121 virtual bool is_noop();
122 virtual bool reset();
123 virtual bool grow_by(
size_t n_chars);
124 virtual ssize_t extend_by(
size_t n_chars);
125 virtual char* write_ptr();
129 const size_t buf_max_size_;
130 size_t buf_cur_size_;
134 class DynamicBufferWriter :
public IBufferWriter {
136 DynamicBufferWriter(StringBuffer& buf);
138 virtual bool is_noop();
139 virtual bool reset();
140 virtual bool grow_by(
size_t n_chars);
141 virtual ssize_t extend_by(
size_t n_chars);
142 virtual char* write_ptr();
151 bool append_(
const char* str,
size_t str_size,
bool grow);
153 Optional<IBufferWriter,
154 ROC_MAX(
sizeof(StaticBufferWriter),
sizeof(DynamicBufferWriter))>
160 bool truncation_error_;
Base class for non-copyable objects.
size_t needed_size() const
Get number of bytes required to store the output string. Includes terminating zero byte.
bool assign_str(const char *str)
Overwrite result with given string. If there is not enough space, truncates the string and returns fa...
bool append_str(const char *str)
Append to result given string. If there is not enough space, truncates the string and returns false.
StringBuilder(StringBuffer &buf)
Construct string builder on top of dynamic buffer.
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 retu...
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 fal...
bool is_ok() const
Check for errors.
StringBuilder(char *buf, size_t bufsz)
Construct string builder on top of fixed-size buffer.
size_t actual_size() const
Get number of bytes actually written to the output string. Includes terminating zero byte.
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.
bool append_char(char ch)
Append to result given character. If there is not enough space, truncates the string and returns fals...
#define ROC_MAX(a, b)
Select minum value.
Optionally constructed object.
Commonly used types and functions.