TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24700 15
we can simply add N characters without first creating a string with them

// Instead of creating a string with 10 'a' characters and adding
... + text + std::string{10, 'a'} + ...
// we use a string expression that simply places 10 'a' characters into the result
... + text + expr_pad<char>{10, 'a'} + ...

The [simstr](https://github.com/orefkov/simstr) library already has many such "smart" string expressions out of the box - for example, joining strings from a container, conditional selection from two expressions, replacing substrings. There are string expressions that take a number and place their decimal or hexadecimal representation into a string (for the decimal representation, `operator+` is specially overloaded for numbers and you can simply write `text + number`).

Using [this library](https://github.com/orefkov/simstr), the code for working with strings will be easier to write, and it will work faster.

The acceleration of string operations has been confirmed by [many benchmarks](https://orefkov.github.io/simstr/results.html).

# Usage examples

# Adding strings with numbers

std::string s1 = "start ";
int i;
....
// Was
std::string str = s1 + std::to_string(i) + " end";
// Became
std::string str = +s1 + i + " end";

`+s1` \- converts `std::string` into an object - a string expression, for which there is an efficient concatenation with numbers and string literals.

According to benchmarks, [acceleration is 1.6 - 2 times](https://orefkov.github.io/simstr/results.html#bs70109915512075798510).

# Adding strings with numbers in hex format

....
// Was
std::string str = s1 + std::format("0x{:x}", i) + " end";
// Became
std::string str = +s1 + e_hex<HexFlags::Short>(i) + " end";

Acceleration in [**9 - 14 times!!!**](https://orefkov.github.io/simstr/results.html#bs146911715078927772520)

# Adding multiple literals and searching in std::string_view

// It was like this
size_t find_pos(std::string_view src, std::string_view name) {
// before C++26 we can not concatenate string and string_view...
return src.find("\n- "s + std::string{name} + " -\n");
}
// When using only "strexpr.h" it became like this
size_t find_pos(ssa src, ssa name) {
return src.find(std::string{"\n- " + name + " -\n"});
}

// And when using the full library, you can do this
size_t find_pos(ssa src, ssa name) {
// In this version, if the result of the concatenation fits into 207 characters, it is produced in a buffer on the stack,
// without allocation and deallocation of memory, acceleration is several times. And only if the result is longer than 207 characters -
// there will be only one allocation, and the concatenation will be immediately into the allocated buffer, without copying characters.
return src.find(lstringa<200>{"\n- " + name + " -\n"});
}

`ssa` \- alias for `simple_str<char>` \- analogue of `std::string_view`, allows you to accept any string object as a function parameter with minimal costs, which does not need to be modified or passed to the C-API: `std::string`, `std::string_view`, `"string literal"`, `simple_str_nt`, `sstring`, `lstring`. Also, since it is also a "string expression", it allows you to easily build concatenations with its participation.

According to measurements, [acceleration is 1.5 - 9 times](https://orefkov.github.io/simstr/results.html#bs68116594352702954700).

You can see more examples on [GitHub](https://github.com/orefkov/simstr).

https://redd.it/1qiyxis
@r_cpp
GitHub GitHub - orefkov/simstr: Yet another C++ strings library implementation Yet another C++ strings library implementation. Contribute to orefkov/simstr development by creating an account on GitHub.
More from @r_cpp
  1. Oct 8, 2026How to Fix autoconf-style Configuration Probing https://build2.org/blog/fix-autoconf.xhtml…
  2. Oct 8, 2026owl – a header-only C++23 HTTP framework where route patterns and handler extractors are c…
  3. Oct 8, 2026Removed by Reddit Removed by Reddit on account of violating the [content policy. ] https:/…
  4. Oct 8, 2026Is there a way to search across all P and N papers throughout standardization history? It…
  5. Oct 8, 2026CppCast Episode 412 : Safety and Interoperability with Rust and C++ https://cppcast.com/sa…
  6. Oct 8, 2026VeloCache: A high-performance, Redis-compatible in-memory key-value store built from scrat…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →