Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::core::StringBuilder Class Reference

String builder. More...

#include <string_builder.h>

Inheritance diagram for roc::core::StringBuilder:
Collaboration diagram for roc::core::StringBuilder:

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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ StringBuilder() [1/2]

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).

◆ StringBuilder() [2/2]

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.

Member Function Documentation

◆ actual_size()

size_t roc::core::StringBuilder::actual_size ( ) const

Get number of bytes actually written to the output string. Includes terminating zero byte.

◆ append_char()

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.

◆ append_str() [1/2]

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.

◆ append_str() [2/2]

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.

◆ append_uint()

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.

◆ assign_str() [1/2]

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.

◆ assign_str() [2/2]

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.

◆ is_ok()

bool roc::core::StringBuilder::is_ok ( ) const

Check for errors.

Remarks
Error flag is raised if any of the methods fail, and is cleared if an assign* method succeeds.

◆ needed_size()

size_t roc::core::StringBuilder::needed_size ( ) const

Get number of bytes required to store the output string. Includes terminating zero byte.

Remarks
If there is non-NULL output buffer, and no error occurred, this size is equal to actual_size(). Otherwise it may be larger.

The documentation for this class was generated from the following file: