12#ifndef ROC_CORE_STRING_BUILDER_H_
13#define ROC_CORE_STRING_BUILDER_H_
55 writer_.
reset(
new (writer_) StaticBufferWriter(buf, bufsz));
94 bool assign_str(
const char* str_begin,
const char* str_end);
113 class IBufferWriter {
115 virtual ~IBufferWriter();
117 virtual bool is_noop() = 0;
118 virtual bool reset() = 0;
119 virtual bool grow_by(
size_t n_chars) = 0;
120 virtual ssize_t extend_by(
size_t n_chars) = 0;
121 virtual char* write_ptr() = 0;
124 class StaticBufferWriter :
public IBufferWriter {
126 StaticBufferWriter(
char* buf,
size_t buf_size);
128 virtual bool is_noop();
129 virtual bool reset();
130 virtual bool grow_by(
size_t n_chars);
131 virtual ssize_t extend_by(
size_t n_chars);
132 virtual char* write_ptr();
136 const size_t buf_max_size_;
137 size_t buf_cur_size_;
141 template <
class Buffer = StringBuffer<> >
142 class DynamicBufferWriter :
public IBufferWriter {
144 DynamicBufferWriter(Buffer& buf)
146 , buf_wr_ptr_(NULL) {
149 virtual bool is_noop() {
153 virtual bool reset() {
159 virtual bool grow_by(
size_t n_chars) {
160 return buf_.grow_exp(buf_.len() + n_chars);
163 virtual ssize_t extend_by(
size_t n_chars) {
164 buf_wr_ptr_ = buf_.extend(n_chars);
165 return buf_wr_ptr_ ? (ssize_t)n_chars : -1;
168 virtual char* write_ptr() {
179 bool append_(
const char* str,
size_t str_size,
bool grow);
181 Optional<IBufferWriter,
182 ROC_MAX(
sizeof(StaticBufferWriter),
sizeof(DynamicBufferWriter<>))>
188 bool truncation_error_;
Base class for non-copyable objects.
void reset(T *ptr=NULL)
Set pointer to the newly created object, destroy old pointed object if set.
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.
bool ok() const
Check for errors.
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...
StringBuilder(StringBuffer< N > &buf)
Construct string builder on top of dynamic buffer.
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.