


A cross-platform C++20 high-performance string builder with Small Buffer Optimization and efficient memory management
Overview
nfx-stringbuilder is a modern C++20 library providing efficient string building capabilities with zero heap allocations for small strings (≤256 bytes). Designed for applications requiring high-performance string concatenation with minimal allocations, it features Small Buffer Optimization (SBO), comprehensive type support, and C++20 std::format integration.
Key Features
🛠️ Rich String Building Interface
- Fluent API: Method chaining and stream operators (<<) for natural concatenation
- Variadic append(): Batch multiple arguments in a single call for optimal performance
- Type Support: Strings, string_view, C-strings, characters, and numeric types (int8/16/32/64, uint8/16/32/64, float, double)
- C++20 std::format Integration: Template format() method for modern formatting
- std::formatter Specializations: Zero-copy integration with std::format for StringBuilder
- Capacity Hints: Pre-allocate buffers with constructor parameter for optimal performance
- Direct Buffer Access: High-performance operations without wrappers
- Iterator Support: Range-based for loops and STL algorithms
📊 Real-World Applications
- Web Servers & HTTP: Building request/response strings, headers, JSON payloads
- Logging Systems: High-frequency log message formatting with minimal allocations
- SQL Query Generation: Dynamic query construction from parameters and conditions
- Template Rendering: Efficient HTML/XML generation from templates
- Data Serialization: CSV, JSON, XML output generation with streaming performance
- Command-Line Tools: Building formatted output, tables, and progress indicators
⚡ Performance Optimized
- Small Buffer Optimization (SBO): 256-byte stack buffer eliminates heap allocations for most strings
- Zero-Copy Operations: string_view access without allocation
- Batch Operations: Variadic append() reduces function call overhead
- Sub-Microsecond Operations: Competitive with {fmt} and Abseil
- Memory Efficiency: Smart capacity management and growth strategies
🌍 Cross-Platform Support
- Operating Systems: Linux, Windows
- Compilers: GCC 14+, Clang 18+, MSVC 2022+
- Thread-Safe: Each StringBuilder instance is thread-safe when not shared
- Consistent Behavior: Same performance characteristics across platforms
Quick Start
Requirements
- C++20 compatible compiler:
- GCC 14+ (14.2.0 tested)
- Clang 18+ (19.1.7 tested)
- MSVC 2022+ (19.44+ tested)
- CMake 3.20 or higher
CMake Integration
# --- Library build types ---
option(NFX_STRINGBUILDER_BUILD_STATIC "Build static library" ON )
option(NFX_STRINGBUILDER_BUILD_SHARED "Build shared library" OFF)
# --- Build components ---
option(NFX_STRINGBUILDER_BUILD_TESTS "Build tests" OFF)
option(NFX_STRINGBUILDER_BUILD_SAMPLES "Build samples" OFF)
option(NFX_STRINGBUILDER_BUILD_BENCHMARKS "Build benchmarks" OFF)
option(NFX_STRINGBUILDER_BUILD_COMPARATIVE_BENCHMARKS "Build comparative benchmarks" OFF)
option(NFX_STRINGBUILDER_BUILD_DOCUMENTATION "Build Doxygen documentation" OFF)
# --- Performance optimizations ---
option(NFX_STRINGBUILDER_ENABLE_SIMD "Enable SIMD optimizations" ON )
# --- Installation ---
option(NFX_STRINGBUILDER_INSTALL_PROJECT "Install project" OFF)
# --- Packaging ---
option(NFX_STRINGBUILDER_PACKAGE_SOURCE "Enable source package generation" OFF)
Using in Your Project
Option 1: Using FetchContent (Recommended)
include(FetchContent)
FetchContent_Declare(
nfx-stringbuilder
GIT_REPOSITORY https://github.com/nfx-libs/nfx-stringbuilder.git
GIT_TAG main # or use specific version tag like "0.1.0"
)
FetchContent_MakeAvailable(nfx-stringbuilder)
# Link with static library
target_link_libraries(your_target PRIVATE nfx-stringbuilder::static)
Option 2: As a Git Submodule
# Add as submodule
git submodule add https://github.com/nfx-libs/nfx-stringbuilder.git third-party/nfx-stringbuilder
# In your CMakeLists.txt
add_subdirectory(third-party/nfx-stringbuilder)
target_link_libraries(your_target PRIVATE nfx-stringbuilder::static)
Option 3: Using find_package (After Installation)
find_package(nfx-stringbuilder REQUIRED)
target_link_libraries(your_target PRIVATE nfx-stringbuilder::static)
Building
Build Commands:
# Clone the repository
git clone https://github.com/nfx-libs/nfx-stringbuilder.git
cd nfx-stringbuilder
# Create build directory
mkdir build && cd build
# Configure with CMake
cmake .. -DCMAKE_BUILD_TYPE=Release
# Build the library
cmake --build . --config Release --parallel
# Run tests (optional)
ctest -C Release --output-on-failure
# Run benchmarks (optional)
./bin/benchmarks/BM_StringBuilder
Documentation
nfx-stringbuilder includes API documentation generated with Doxygen.
📚 Online Documentation
The complete API documentation is available online at: https://nfx-libs.github.io/nfx-stringbuilder
Building Documentation Locally
# Configure with documentation enabled
cmake .. -DCMAKE_BUILD_TYPE=Release -DNFX_STRINGBUILDER_BUILD_DOCUMENTATION=ON
# Build the documentation
cmake --build . --target nfx-stringbuilder-documentation
Requirements
- Doxygen - Documentation generation tool
- Graphviz Dot (optional) - For generating class diagrams
Accessing Local Documentation
After building, open ./build/doc/html/index.html in your web browser.
Usage Examples
Basic String Building
using namespace nfx::string;
int main()
{
std::string result = builder.
toString();
return 0;
}
High-performance string building with Small Buffer Optimization (SBO).
High-performance string builder with efficient memory management.
std::string toString() const &
Convert buffer content to std::string.
StringBuilder & append(std::string_view str)
Appends string_view contents to the buffer efficiently.
Efficient String Concatenation
#include <vector>
using namespace nfx::string;
std::string buildReport(const std::vector<std::string>& items)
{
builder.
append(
"=== Report ===\n");
for (size_t i = 0; i < items.size(); ++i)
{
.
append(std::to_string(i + 1))
}
builder.
append(
"Total items: ")
.
append(std::to_string(items.size()));
}
void reserve(size_t newCapacity)
Reserve minimum capacity for buffer.
Working with Different String Types
using namespace nfx::string;
void demonstrateStringTypes()
{
std::string stdStr = "from std::string";
std::string_view sv = "from string_view";
builder.
append(
"from C-string")
std::string result = builder.
toString();
}
Numeric Type Support
using namespace nfx::string;
void demonstrateNumericTypes()
{
int32_t i32 = -42;
uint32_t u32 = 42;
int64_t i64 = -9223372036854775807LL;
uint64_t u64 = 18446744073709551615ULL;
float f = 3.14159f;
double d = 2.718281828459045;
std::string result = builder.
toString();
}
void demonstrateStreamOperators()
{
int value = 42;
double price = 19.99;
builder << "Product #" << value << " costs $" << price;
std::string result = builder.
toString();
}
void demonstrateVariadicAppend()
{
int userId = 12345;
const char* userName = "alice";
int64_t timestamp = 1672531200;
builder.
append(
"User ", userId,
" (", userName,
") logged in at ", timestamp);
std::string result = builder.
toString();
}
format() Method Integration
using namespace nfx::string;
void demonstrateFormatMethod()
{
builder.
format(
"{} (ID: {:08})",
"Alice", 123);
builder.
format(
"${:.2f}", 19.99);
builder.
format(
"hex=0x{:X}, bin=0b{:b}", 255, 15);
std::string result = builder.
toString();
}
void demonstrateFormatChaining()
{
builder.
format(
"Name: {}",
"Bob")
.
format(
"Score: {:.1f}", 95.7);
std::string result = builder.
toString();
}
StringBuilder & format(std::format_string< Args... > fmt, Args &&... args)
Format and append text using std::format.
C++20 std::formatter Integration
#include <format>
using namespace nfx::string;
void demonstrateStdFormatter()
{
std::string formatted = std::format("Result: {}", builder);
std::string bufferFormatted = std::format("Buffer content: {}", builder);
}
Capacity Hints for Pre-allocation
using namespace nfx::string;
void demonstrateCapacityHints()
{
for (int i = 0; i < 100; ++i)
{
.
append(
": Some long description text here\n");
}
std::string result = builder.
toString();
}
Direct Buffer Manipulation
using namespace nfx::string;
void directBufferAccess()
{
size_t size = builder.
size();
for (char c : builder)
{
}
builder.clear();
}
size_t capacity() const noexcept
Get current buffer capacity in bytes.
size_t size() const noexcept
Get current buffer size in bytes.
std::string_view toStringView() const noexcept
Get string_view of buffer content.
Multi-threaded Usage
#include <thread>
#include <vector>
using namespace nfx::string;
void threadWorker(int threadId)
{
for (int i = 0; i < 100; ++i)
{
.
append(std::to_string(threadId))
std::string result = builder.
toString();
}
}
void demonstrateMultithreading()
{
std::vector<std::thread> threads;
for (int i = 0; i < 4; ++i)
{
threads.emplace_back(threadWorker, i);
}
for (auto& t : threads)
{
t.join();
}
std::cout << "All threads completed successfully\n";
}
Sample Output:
[INFO] User logged in: username=john.doe, ip=192.168.1.100, timestamp=2025-10-31
[ERROR] Connection failed: timeout after 30s
Project Structure
nfx-stringbuilder/
├── benchmark/ # Performance benchmarks with Google Benchmark
├── cmake/ # CMake modules and configuration
├── include/nfx/ # Public headers
├── samples/ # Example usage and demonstrations
├── src/ # Implementation files
└── test/ # Comprehensive unit tests with GoogleTest
Performance
For detailed performance metrics and benchmarks, see:
Roadmap
See TODO.md for upcoming features and project roadmap.
Changelog
See the CHANGELOG.md for a detailed history of changes, new features, and bug fixes.
License
This project is licensed under the MIT License.
Dependencies
Development Dependencies
- GoogleTest: Testing framework (BSD 3-Clause License) - Development only
- Google Benchmark: Performance benchmarking framework (Apache 2.0 License) - Development only
- {fmt}: Fast formatting library (MIT License) - Benchmarking only
- Abseil: C++ common libraries (Apache 2.0 License) - Benchmarking only
All dependencies are automatically fetched via CMake FetchContent when building the library, tests, or benchmarks.
Updated on February 15, 2026