Minimal Unicode‑aware & ANSI‑aware C++ string library (header‑only, class‑less)
struiis a tiny C++17 header‑only library for measuring terminal strings.
Think of it ascut,sed, orwc, but escape‑safe, UTF‑8‑capable (incl. wide & combined emojis).
Ready to drop in one header.
- UTF‑8 aware (powered by
libunistring) - ANSI / escape‑safe cleaning & width detection
- Visual width handling (CJK, wide & combined emojis)
- Line height calculation
- Substring occurrence counting
- Split & repeat helpers
- Zero‑dependency at runtime, header‑only at build time
- Plays nicely with any POSIX shell tool or C++ project
Just include the header and go:
#include <strui.hpp> // path relative to your project
#include <iostream>
int main() {
std::cout << strui::width("\033[31m1🛑4\033[0m") << std::endl; // → 4
}Add the include directory to your compiler flags, e.g.:
g++ -std=c++17 -I/path/to/strui/include example.cpp -lunistringNote:
libunistringis only required at link time when you compile the optional.cpptests.
The header does#ifdefguards so you can keep your project header‑only if you already linklibunistring.
| Function | Description |
|---|---|
strui::width(str) |
Return visual width (columns) of str |
strui::height(str) |
Return number of lines in str |
strui::clean(str) |
Remove ANSI escape sequences |
strui::split(str, sep) |
Vector‑split str on sep |
strui::join(str, sep = "") |
Return merged std::string (optional sep) |
strui::repeat(count, str, sep = "") |
Repeat str count times (optional sep) |
strui::count(str, value) |
Count occurrences of value in str |
Display widthis measured incolumns, ofcharactersorstrings, when output to a device that usesnon-proportional fonts.
Note that for some rarely used characters the actualfontsorterminal emulatorscan use a differentwidth. There is no mechanism for communicating thedisplay widthofcharactersacross a Unix pseudo-terminal (tty).
Also, there are scripts with complex rendering, like theIndicscripts. For these scripts, there is no such concept asnon-proportional fonts. Therefore the results of these functions usually work fine on most scripts and on mostcharactersbut can fail to represent the actualdisplay width.
📚 See libunistring documentation – Display width <uniwidth.h> for details on how display width is computed.
No build is needed to use the library ✌️.
But you can build and run the library test program:
# run unit tests locally + inside an Ubuntu container (requires Docker)
sh tests/run.shlibunistring≥ 1.1 (link‑time only)- Any C++17 compiler (
g++,clang++, …)
strui/
├── LICENCE.txt
├── README.md
├── include/
│ └── strui.hpp
└── tests/
├── run.sh
├── Makefile
├── test.cpp
└── Dockerfile
“Measure what you see — not what you store.” 📏