|
nfx-stringbuilder 0.7.0
Cross-platform C++20 library for zero-allocation string building with Small Buffer Optimization
|
High-performance string builder with efficient memory management. More...
#include <nfx/string/StringBuilder.h>
Public Types | |
| using | value_type = char |
| Character type for iterator compatibility. | |
| using | Iterator = char* |
| Mutable iterator type for buffer traversal. | |
| using | ConstIterator = const char* |
| Immutable iterator type for buffer traversal. | |
| using | iterator = Iterator |
| Type alias for iterator. | |
| using | const_iterator = ConstIterator |
| Type alias for const iterator. | |
Public Member Functions | |
| StringBuilder () | |
| Default constructor. | |
| StringBuilder (size_t initialCapacity) | |
| Constructor with specified initial capacity. | |
| StringBuilder (const StringBuilder &other) | |
| Copy constructor. | |
| StringBuilder (StringBuilder &&other) noexcept | |
| Move constructor. | |
| ~StringBuilder ()=default | |
| Destructor. | |
| StringBuilder & | operator= (const StringBuilder &other) |
| Copy assignment operator. | |
| StringBuilder & | operator= (StringBuilder &&other) noexcept |
| Move assignment operator. | |
| size_t | size () const noexcept |
| Get current buffer size in bytes. | |
| size_t | capacity () const noexcept |
| Get current buffer capacity in bytes. | |
| bool | isEmpty () const noexcept |
| Check if buffer is empty. | |
| char * | data () noexcept |
| Get mutable pointer to buffer data. | |
| const char * | data () const noexcept |
| Get immutable pointer to buffer data. | |
| char & | operator[] (size_t index) |
| Access buffer element by index (mutable). | |
| const char & | operator[] (size_t index) const |
| Access buffer element by index (immutable). | |
| void | clear () noexcept |
| Clear buffer content without deallocating memory. | |
| void | reserve (size_t newCapacity) |
| Reserve minimum capacity for buffer. | |
| void | resize (size_t newSize) |
| Resize buffer to specified size. | |
| StringBuilder & | append (std::string_view str) |
| Appends string_view contents to the buffer efficiently. | |
| StringBuilder & | append (const std::string &str) |
| Appends std::string contents to the buffer. | |
| StringBuilder & | append (const char *str) |
| Appends null-terminated C-string to the buffer. | |
| template<size_t N> | |
| NFX_STRINGBUILDER_FORCE_INLINE StringBuilder & | append (const char(&str)[N]) |
| Appends string literal with compile-time length (zero strlen overhead). | |
| StringBuilder & | append (char c) |
| Appends single character to the buffer. | |
| StringBuilder & | append (std::int8_t value) |
| Appends 8-bit signed integer to the buffer. | |
| StringBuilder & | append (std::uint8_t value) |
| Appends 8-bit unsigned integer to the buffer. | |
| StringBuilder & | append (std::int16_t value) |
| Appends 16-bit signed integer to the buffer. | |
| StringBuilder & | append (std::uint16_t value) |
| Appends 16-bit unsigned integer to the buffer. | |
| StringBuilder & | append (std::int32_t value) |
| Appends 32-bit signed integer to the buffer. | |
| StringBuilder & | append (std::uint32_t value) |
| Appends 32-bit unsigned integer to the buffer. | |
| StringBuilder & | append (std::int64_t value) |
| Appends 64-bit signed integer to the buffer. | |
| StringBuilder & | append (std::uint64_t value) |
| Appends 64-bit unsigned integer to the buffer. | |
| StringBuilder & | append (float value) |
| Appends single-precision floating-point to the buffer. | |
| StringBuilder & | append (double value) |
| Appends double-precision floating-point to the buffer. | |
| StringBuilder & | appendLine (std::string_view str="") |
| Appends string_view contents followed by a newline character. | |
| StringBuilder & | appendLine (const std::string &str) |
| Appends std::string contents followed by a newline character. | |
| StringBuilder & | appendLine (const char *str) |
| Appends null-terminated C-string followed by a newline character. | |
| StringBuilder & | prepend (std::string_view str) |
| Prepends string_view contents to the buffer. | |
| StringBuilder & | prepend (const std::string &str) |
| Prepends std::string contents to the buffer. | |
| StringBuilder & | prepend (const char *str) |
| Prepends null-terminated C-string to the buffer. | |
| StringBuilder & | prepend (char c) |
| Prepends single character to the buffer. | |
| StringBuilder & | prepend (std::int8_t value) |
| Prepends 8-bit signed integer to the buffer. | |
| StringBuilder & | prepend (std::uint8_t value) |
| Prepends 8-bit unsigned integer to the buffer. | |
| StringBuilder & | prepend (std::int16_t value) |
| Prepends 16-bit signed integer to the buffer. | |
| StringBuilder & | prepend (std::uint16_t value) |
| Prepends 16-bit unsigned integer to the buffer. | |
| StringBuilder & | prepend (std::int32_t value) |
| Prepends 32-bit signed integer to the buffer. | |
| StringBuilder & | prepend (std::uint32_t value) |
| Prepends 32-bit unsigned integer to the buffer. | |
| StringBuilder & | prepend (std::int64_t value) |
| Prepends 64-bit signed integer to the buffer. | |
| StringBuilder & | prepend (std::uint64_t value) |
| Prepends 64-bit unsigned integer to the buffer. | |
| StringBuilder & | prepend (float value) |
| Prepends single-precision floating-point to the buffer. | |
| StringBuilder & | prepend (double value) |
| Prepends double-precision floating-point to the buffer. | |
| template<typename T, typename... Args, typename = std::enable_if_t<( sizeof...( Args ) > 0 )>> | |
| StringBuilder & | append (T &&first, Args &&... args) |
| Append multiple arguments in one call (strings, characters, numeric types). | |
| template<typename Container> | |
| StringBuilder & | join (const Container &items, std::string_view delimiter) |
| Join container elements with delimiter. | |
| template<typename Container> | |
| StringBuilder & | join (const Container &items, char delimiter) |
| Join container elements with character delimiter. | |
| StringBuilder & | operator<< (std::string_view str) |
| Stream operator for string_view. | |
| StringBuilder & | operator<< (const std::string &str) |
| Stream operator for std::string. | |
| StringBuilder & | operator<< (const char *str) |
| Stream operator for C-string. | |
| template<size_t N> | |
| NFX_STRINGBUILDER_FORCE_INLINE StringBuilder & | operator<< (const char(&str)[N]) |
| Stream operator for string literal (zero strlen overhead). | |
| StringBuilder & | operator<< (char c) |
| Stream operator for single character. | |
| StringBuilder & | operator<< (std::int8_t value) |
| Stream operator for 8-bit signed integer. | |
| StringBuilder & | operator<< (std::uint8_t value) |
| Stream operator for 8-bit unsigned integer. | |
| StringBuilder & | operator<< (std::int16_t value) |
| Stream operator for 16-bit signed integer. | |
| StringBuilder & | operator<< (std::uint16_t value) |
| Stream operator for 16-bit unsigned integer. | |
| StringBuilder & | operator<< (std::int32_t value) |
| Stream operator for 32-bit signed integer. | |
| StringBuilder & | operator<< (std::uint32_t value) |
| Stream operator for 32-bit unsigned integer. | |
| StringBuilder & | operator<< (std::int64_t value) |
| Stream operator for 64-bit signed integer. | |
| StringBuilder & | operator<< (std::uint64_t value) |
| Stream operator for 64-bit unsigned integer. | |
| StringBuilder & | operator<< (float value) |
| Stream operator for single-precision floating-point. | |
| StringBuilder & | operator<< (double value) |
| Stream operator for double-precision floating-point. | |
| template<typename... Args> | |
| StringBuilder & | format (std::format_string< Args... > fmt, Args &&... args) |
| Format and append text using std::format. | |
| std::string | toString () const & |
| Convert buffer content to std::string. | |
| std::string | toString () && |
| Convert buffer to string by moving (rvalue-qualified). | |
| std::string_view | toStringView () const noexcept |
| Get string_view of buffer content. | |
| void | push_back (char c) |
| Appends single character to the buffer (for STL compatibility). | |
| Iterator | begin () noexcept |
| Get mutable iterator to beginning of buffer. | |
| ConstIterator | begin () const noexcept |
| Get immutable iterator to beginning of buffer. | |
| Iterator | end () noexcept |
| Get mutable iterator to end of buffer. | |
| ConstIterator | end () const noexcept |
| Get immutable iterator to end of buffer. | |
High-performance string builder with efficient memory management.
Provides a growable character buffer optimized for string building operations. Features automatic capacity management, Small Buffer Optimization (SBO), iterator support, and zero-copy string_view access.
Definition at line 107 of file StringBuilder.h.
Type alias for const iterator.
Definition at line 748 of file StringBuilder.h.
| using nfx::string::StringBuilder::ConstIterator = const char* |
Immutable iterator type for buffer traversal.
Definition at line 742 of file StringBuilder.h.
| using nfx::string::StringBuilder::Iterator = char* |
Mutable iterator type for buffer traversal.
Definition at line 739 of file StringBuilder.h.
Type alias for iterator.
Definition at line 745 of file StringBuilder.h.
| using nfx::string::StringBuilder::value_type = char |
Character type for iterator compatibility.
Definition at line 736 of file StringBuilder.h.
|
explicit |
Constructor with specified initial capacity.
| initialCapacity | Initial buffer capacity in bytes |
Pre-allocates buffer to avoid reallocations during initial growth
| nfx::string::StringBuilder::StringBuilder | ( | const StringBuilder & | other | ) |
Copy constructor.
| other | The StringBuilder to copy from |
|
noexcept |
Move constructor.
| other | The StringBuilder to move from |
|
inline |
Appends single character to the buffer.
| c | Character to append |
|
inline |
Appends null-terminated C-string to the buffer.
| str | Null-terminated C-string to append (null pointer handled gracefully) |
| NFX_STRINGBUILDER_FORCE_INLINE StringBuilder & nfx::string::StringBuilder::append | ( | const char(&) | str[N] | ) |
Appends string literal with compile-time length (zero strlen overhead).
| N | String literal length (deduced by compiler) |
| str | String literal |
|
inline |
Appends std::string contents to the buffer.
| str | String to append |
|
inline |
Appends double-precision floating-point to the buffer.
| value | Floating-point value to append |
|
inline |
Appends single-precision floating-point to the buffer.
| value | Floating-point value to append |
|
inline |
Appends 16-bit signed integer to the buffer.
| value | Integer value to append |
|
inline |
Appends 32-bit signed integer to the buffer.
| value | Integer value to append |
|
inline |
Appends 64-bit signed integer to the buffer.
| value | Integer value to append |
|
inline |
Appends 8-bit signed integer to the buffer.
| value | Integer value to append |
|
inline |
Appends string_view contents to the buffer efficiently.
| str | String view to append |
|
inline |
Appends 16-bit unsigned integer to the buffer.
| value | Integer value to append |
|
inline |
Appends 32-bit unsigned integer to the buffer.
| value | Integer value to append |
|
inline |
Appends 64-bit unsigned integer to the buffer.
| value | Integer value to append |
|
inline |
Appends 8-bit unsigned integer to the buffer.
| value | Integer value to append |
|
inline |
Append multiple arguments in one call (strings, characters, numeric types).
| T | Type of the first argument (any type supported by append()) |
| Args | Types of the remaining arguments (any type supported by append()) |
| first | First argument to append |
| args | Remaining arguments to append |
Example:
|
inline |
Appends null-terminated C-string followed by a newline character.
| str | Null-terminated C-string to append (null pointer handled gracefully) |
|
inline |
Appends std::string contents followed by a newline character.
| str | String to append |
|
inline |
Appends string_view contents followed by a newline character.
| str | String view to append (default: empty) |
Efficiently appends the string and newline in one operation. Useful for building line-based formats like logs, CSV, or ISO 19848 messages.
Example:
|
inlinenodiscardnoexcept |
Get immutable iterator to beginning of buffer.
Safe read-only iteration over buffer contents
|
inlinenodiscardnoexcept |
Get mutable iterator to beginning of buffer.
Enables range-based for loops and STL algorithms
|
inlinenodiscardnoexcept |
Get current buffer capacity in bytes.
Capacity may be larger than size to avoid frequent reallocations
|
inlinenoexcept |
Clear buffer content without deallocating memory.
Sets size to 0 but preserves allocated capacity for reuse
|
inlinenodiscardnoexcept |
Get immutable pointer to buffer data.
Safe read-only access to buffer contents
|
inlinenodiscardnoexcept |
Get mutable pointer to buffer data.
Provides direct memory access for high-performance operations
|
inlinenodiscardnoexcept |
Get immutable iterator to end of buffer.
Standard STL end iterator semantics
|
inlinenodiscardnoexcept |
Get mutable iterator to end of buffer.
Standard STL end iterator semantics
|
inline |
Format and append text using std::format.
| Args | Types of the formatting arguments |
| fmt | Format string with format specifiers |
| args | Arguments to be formatted |
Provides type-safe formatting directly into the builder without intermediate allocations. Uses std::format_to with back_inserter for optimal performance. Example: builder.format("User {} (ID: {}) logged in at {}", name, userId, timestamp);
|
inlinenodiscardnoexcept |
Check if buffer is empty.
|
inline |
Join container elements with character delimiter.
| Container | Container type (vector, array, list, etc.) with begin()/end() |
| items | Container of items to join |
| delimiter | Character delimiter to insert between elements |
Optimized variant for single-character delimiters (e.g., ',', ';', '|').
Example:
|
inline |
Join container elements with delimiter.
| Container | Container type (vector, array, list, etc.) with begin()/end() |
| items | Container of items to join |
| delimiter | Delimiter to insert between elements |
Appends all container elements separated by delimiter. Works with any container providing iterators and elements convertible to string. Uses append() internally, so supports strings, string_view, and types with operator<<.
Example:
|
inline |
Stream operator for single character.
| c | Character to append |
|
inline |
Stream operator for C-string.
| str | Null-terminated C-string to append |
| NFX_STRINGBUILDER_FORCE_INLINE StringBuilder & nfx::string::StringBuilder::operator<< | ( | const char(&) | str[N] | ) |
Stream operator for string literal (zero strlen overhead).
| N | String literal length (deduced by compiler) |
| str | String literal |
|
inline |
Stream operator for std::string.
| str | String to append |
|
inline |
Stream operator for double-precision floating-point.
| value | Floating-point value to append |
|
inline |
Stream operator for single-precision floating-point.
| value | Floating-point value to append |
|
inline |
Stream operator for 16-bit signed integer.
| value | Integer value to append |
|
inline |
Stream operator for 32-bit signed integer.
| value | Integer value to append |
|
inline |
Stream operator for 64-bit signed integer.
| value | Integer value to append |
|
inline |
Stream operator for 8-bit signed integer.
| value | Integer value to append |
|
inline |
Stream operator for string_view.
| str | String view to append |
|
inline |
Stream operator for 16-bit unsigned integer.
| value | Integer value to append |
|
inline |
Stream operator for 32-bit unsigned integer.
| value | Integer value to append |
|
inline |
Stream operator for 64-bit unsigned integer.
| value | Integer value to append |
|
inline |
Stream operator for 8-bit unsigned integer.
| value | Integer value to append |
| StringBuilder & nfx::string::StringBuilder::operator= | ( | const StringBuilder & | other | ) |
Copy assignment operator.
| other | The StringBuilder to copy from |
|
noexcept |
Move assignment operator.
| other | The StringBuilder to move from |
|
inline |
Access buffer element by index (mutable).
| index | Zero-based index of element to access |
No bounds checking - undefined behavior if index >= size()
|
inline |
Access buffer element by index (immutable).
| index | Zero-based index of element to access |
No bounds checking - undefined behavior if index >= size()
|
inline |
Prepends single character to the buffer.
| c | Character to prepend |
|
inline |
Prepends null-terminated C-string to the buffer.
| str | Null-terminated C-string to prepend (null pointer handled gracefully) |
|
inline |
Prepends std::string contents to the buffer.
| str | String to prepend |
|
inline |
Prepends double-precision floating-point to the buffer.
| value | Floating-point value to prepend |
|
inline |
Prepends single-precision floating-point to the buffer.
| value | Floating-point value to prepend |
|
inline |
Prepends 16-bit signed integer to the buffer.
| value | Integer value to prepend |
|
inline |
Prepends 32-bit signed integer to the buffer.
| value | Integer value to prepend |
|
inline |
Prepends 64-bit signed integer to the buffer.
| value | Integer value to prepend |
|
inline |
Prepends 8-bit signed integer to the buffer.
| value | Integer value to prepend |
| StringBuilder & nfx::string::StringBuilder::prepend | ( | std::string_view | str | ) |
Prepends string_view contents to the buffer.
| str | String view to prepend |
Shifts existing content and inserts at the beginning. Performance: O(n) due to memory shift operation.
|
inline |
Prepends 16-bit unsigned integer to the buffer.
| value | Integer value to prepend |
|
inline |
Prepends 32-bit unsigned integer to the buffer.
| value | Integer value to prepend |
|
inline |
Prepends 64-bit unsigned integer to the buffer.
| value | Integer value to prepend |
|
inline |
Prepends 8-bit unsigned integer to the buffer.
| value | Integer value to prepend |
|
inline |
Appends single character to the buffer (for STL compatibility).
| c | Character to append |
Inline alias for append(char) to support std::back_inserter
|
inline |
Reserve minimum capacity for buffer.
| newCapacity | Minimum desired capacity in bytes |
May allocate more than requested for efficiency
| std::bad_alloc | if memory allocation fails |
|
inline |
Resize buffer to specified size.
| newSize | New buffer size in bytes |
May truncate content or extend with undefined bytes
| std::bad_alloc | if memory allocation fails |
|
inlinenodiscardnoexcept |
Get current buffer size in bytes.
Returns actual content size, not allocated capacity
|
nodiscard |
Convert buffer to string by moving (rvalue-qualified).
For heap buffers, transfers ownership without copying. For stack buffers, performs a copy. Resets builder to empty state after move.
|
inlinenodiscard |
Convert buffer content to std::string.
Creates new string object - consider toStringView() for read-only access
|
inlinenodiscardnoexcept |
Get string_view of buffer content.
Zero-copy access - view becomes invalid if buffer is modified