Part of Kronuz C++ Libraries, a collection of small, reusable C++ libraries. Read its story in The Bag of Holding, a Familiars post.
String helpers for C++20: forgiving format, natural joins, ASCII case mapping, and human-readable sizes and durations.
A header of inline templates covers formatting, joining, splitting, padding,
indenting, trimming, case mapping and replacing; one compiled file turns byte
counts and durations into 2KiB and 1.5s. It fits the text chores of servers
and tools: log lines, reports, and CLI output.
- Formatting that falls back.
strings::formatwrapsstd::vformatand returns the format text unchanged when the format string and its arguments do not match. - Natural joins.
join(words, ", ", " and ")gives"a, b and c"; an overload drops the elements a predicate selects, and integers are written withstd::to_chars. - Python's text rules.
indentfollowstextwrap.indent, andreplacewith an empty search inserts the replacement at every position, asstr.replacedoes. - Trims return views.
trim,ltrimandrtrimreturn astd::string_viewinto the input. - Vectorized ASCII case mapping.
loweranduppermapA-Zanda-zwith arithmetic the compiler vectorizes, and leave every other byte as it is. - Humanizers.
from_bytes(2048)is"2KiB";from_time,from_small_timeandfrom_deltarender durations, optionally in ANSI color.
#include "strings.hh"
#include <cstdio>
#include <string>
#include <vector>
int main() {
std::vector<std::string> w{"a", "b", "c"};
std::puts(strings::join(w, ", ", " and ").c_str()); // a, b and c
std::puts(strings::format("{} + {} = {}", 2, 3, 5).c_str()); // 2 + 3 = 5
std::puts(strings::from_bytes(2048).c_str()); // 2KiB
std::puts(strings::from_delta(1'500'000'000LL).c_str()); // 1.5s
std::puts(std::string(strings::trim(" hi ")).c_str()); // hi
}examples/usage.cc runs every helper.
Requires a C++20 compiler and CMake 3.14 or newer; it fetches five sibling libraries (char-classify, split, static-string, repr, term-color) and compiles one source file. CI builds it on Linux (x86_64, arm64), macOS (arm64, x86_64), and FreeBSD, plus ASan, UBSan, and TSan builds.
include(FetchContent)
FetchContent_Declare(strings
GIT_REPOSITORY https://github.com/Kronuz/strings.git
GIT_TAG 78b3c93d8d5306c7d1e94a51ee58c7e2bae3f342) # or main for the tip
FetchContent_MakeAvailable(strings)
target_link_libraries(my_app PRIVATE strings::strings)#include "strings.hh"Use it for the small text chores of servers and tools, where a missing helper would otherwise be a hand-written loop.
Not for:
- Unicode text. Case mapping, padding and trimming work byte by byte; only ASCII letters change case, and widths count bytes.
- Format strings checked at compile time.
formattakes the format at run time, and a mismatch returns the format text without an error. - Headers that must stay out of
std.strings.hhaddsstd::to_stringoverloads, and a globaloperator<<, for strings,static_string, and any type with ato_string()member.
Time per call, in ns (µs where marked), from bench/bench.cc: the best of seven samples of at least
8 ms. strings is this library; abseil is the call a program would make without it, from abseil
20260817.0: absl::AsciiStrToLower for
lower, absl::StrJoin(v, ", ") for join(v, ", "), and absl::StrReplaceAll with one search and
replacement pair for replace. Before it times a row, the bench checks that both return the same
string for that input, and it exits 1 when they differ. lower runs over random printable ASCII;
join over strings like item417 or random integers below 100,000; replace over a bytes with
_ at every 4th or 16th byte, replacing _ by -=- (64 matches is every 4th byte of 256 B). The
ratio is strings' time over abseil's, so below 1 is faster. Apple clang 17, -O3, on an Apple M4
Pro (macOS 26) and an Intel Core i9-9980HK (macOS 15).
| Operation | Apple M4 Pro: strings | abseil | ratio | Intel Core i9-9980HK: strings | abseil | ratio |
|---|---|---|---|---|---|---|
lower, 8 B |
3.3 ns | 4.9 ns | 0.67 | 10.8 ns | 13.7 ns | 0.79 |
lower, 16 B |
4.6 ns | 7.8 ns | 0.59 | 11.7 ns | 18.0 ns | 0.65 |
lower, 4 KB |
128 ns | 1.35 µs | 0.10 | 264 ns | 2.78 µs | 0.09 |
join of 10 strings |
60.8 ns | 48.2 ns | 1.26 | 130 ns | 98.7 ns | 1.31 |
join of 1000 strings |
4.93 µs | 3.75 µs | 1.31 | 9.17 µs | 6.57 µs | 1.39 |
join of 1000 ints |
8.34 µs | 8.97 µs | 0.93 | 19.0 µs | 18.5 µs | 1.03 |
replace 1 byte by 3, 256 B, no match |
28.4 ns | 51.3 ns | 0.55 | 113 ns | 190 ns | 0.59 |
replace 1 byte by 3, 256 B, 64 matches |
600 ns | 788 ns | 0.76 | 1.28 µs | 1.95 µs | 0.66 |
replace 1 byte by 3, 4 KB, 1024 matches |
7.93 µs | 11.6 µs | 0.69 | 14.6 µs | 24.3 µs | 0.60 |
cmake -B build -DCMAKE_BUILD_TYPE=Release -DSTRINGS_BUILD_BENCH=ON
cmake --build build --target strings_bench
./build/strings_benchThe bench uses abseil when CMake finds it. docs/perf/README.md has every row.
- The Bag of Holding:
the Familiars post that covers
stringsamong the small helpers. - docs/API.md: every helper, with examples.
- ARCHITECTURE.md: the header and compiled split, the dependencies, and how the humanizers pick units.
- CHANGELOG.md: what changed, with the measurements.
- AGENTS.md: repository map and traps.
- Kronuz C++ Libraries: the catalog.
cmake -B build
cmake --build build
ctest --test-dir buildThe tests check every helper, the case mapping and replace against reference
implementations, and join, lower, upper and replace against the Xapiand
original. Add
-DSTRINGS_BUILD_BENCH=ON to build strings_bench.
Issues and pull requests are welcome. Read AGENTS.md and the catalog's rules of engagement first; CI must pass on every platform.
MIT, Copyright (c) 2026 Germán Méndez Bravo and Copyright (c) 2015-2020 Dubalu LLC. See LICENSE. Extracted from Xapiand.