Fast, safe string handling for C and C++ — without repeated scanning.
FastSafeStrings improves performance by eliminating repeated string scans
(strlen, strcat, delimiter searches) using length-aware strings.
In real workloads, this can be several times faster than typical C string handling.
#include "faststr.h"
// Declare fixed-capacity strings (stack allocated)
DCL(a,40);
DCL(b,40);
SET(a,"Hello ");
SET(b,"World");
CAT(a,b);
printf("%s\n", a);#include <iostream>
#include "fast_string.hpp"
int main()
{
fast_string<256> name;
fast_string<256> surname("Smith");
name = "John";
name += " ";
name += surname;
std::cout << name.c_str() << "\n";
std::cout << "Length: " << name.length() << "\n";
return 0;
}Features:
- O(1) length access
- Safe bounded operations
- Efficient append without scanning
Processing 5,000,000 records (~35 bytes each):
- Standard C (fgets + strcat + strlen): 1.506s
- FastSafeStrings (length-aware + VB): 0.205s
👉 ~7× faster in this workload by eliminating repeated scans
Benchmarks compare typical idiomatic C usage, not hand-optimised memcpy-based code.
| Operation | C | FastSafeStrings | Speedup |
|---|---|---|---|
| strlen | 0.176 | 0.007 | ~25× |
| strcpy | 0.137 | 0.044 | ~3× |
| strcat | 0.216 | 0.043 | ~5× |
| strcmp | 0.049 | 0.049 | same |
| literal copy | 0.040 | 0.041 | same |
Traditional C string handling repeatedly scans memory:
read → scan → scan → scan
FastSafeStrings avoids this:
read → process (no scans)
Key idea: 👉 eliminate unnecessary passes over the same data
FastSafeStrings enforces:
- All operations are bounds-checked (length ≤ capacity)
- Strings always track their current length
- Writes are safely truncated if needed (no buffer overflow)
- Descriptor-based strings (stored length + capacity)
- O(1) length access (no
strlen) - Fast append (no destination scanning)
- Zero-copy substring views
- Incremental adoption alongside existing C code
FastSafeStrings is designed for:
- Existing C codebases where changing language is not practical
- Low-level or embedded environments
- Workloads where repeated string scanning is a bottleneck
It focuses on improving C, not replacing it.
FastSafeStrings includes support for variable-length records:
[length][data]
[length][data]
This allows:
- Reading records without delimiter scanning
- Efficient processing of structured text data
Standard C text processing:
fgets → scan for newline
strlen → scan again
strcat → scan destination
FastSafeStrings + VB:
read length → copy → append
👉 No scanning anywhere
DCL(name,size)
SET(dst,"text")
CPY(dst,src)
CAT(dst,src)
CMP(a,b)
LEN(x)
SUBSTR(dst,src,...)
vb_handle_t *VB_OpenRead(path);
VB_Get(handle, buf, max_len, &len);
VB_Put(handle, data, len);
VB_Close(handle);
FastSafeStrings is useful when:
- Processing large volumes of text or records
- Performance matters
- Safety is important
- Rewriting in another language is not practical
- Requires
DCLfor managed strings - Raw
char *remains unsafe outside the API - VB format requires preprocessing or conversion
MIT License
FastSafeStrings combines:
- descriptor-based strings
- length-aware processing
to eliminate repeated scanning in C programs.
👉 Fewer passes over data = faster and safer code