https://github.com/estnafinema0/cpp-format
C++20 header‑only library providing Python‑style numbered placeholders in format strings with strict syntax and bounds checking.
https://github.com/estnafinema0/cpp-format
cpp20 formatting headeronly stringformat variadic-templates
Last synced: 7 months ago
JSON representation
C++20 header‑only library providing Python‑style numbered placeholders in format strings with strict syntax and bounds checking.
- Host: GitHub
- URL: https://github.com/estnafinema0/cpp-format
- Owner: estnafinema0
- Created: 2025-07-10T14:20:25.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-07-15T11:26:16.000Z (7 months ago)
- Last Synced: 2025-07-16T01:35:18.035Z (7 months ago)
- Topics: cpp20, formatting, headeronly, stringformat, variadic-templates
- Language: C++
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# C++20 Format String Utility
Header-only library for formatting to C++ by letting you embed numbered placeholders such as {0} and {1} directly in a template string and replace them at runtime with any types streamable into std::ostream. It enforces strict syntax validation, throwing a FormatError immediately if braces are misused, non-digit characters appear inside placeholders, or an index goes out of range, while leaving any text outside the placeholders untouched.
## Example
```cpp
#include
#include "format.hpp"
int main() {
try {
auto s = format("[{1}] {0} + {0} = {2}", "sum", 42, 84);
std::cout << s << "\n"; // prints: [42] sum + sum = 84
}
catch (const FormatError& e) {
std::cerr << "Formatting failed: " << e.what() << '\n';
}
}
```
## Error Handling
Any misuse of braces, non‑digit characters inside `{}` or references to non‑existent argument indices will immediately throw `FormatError`.
## Build and Test
A simple `Makefile` compiles everything under C++20 and has Google Test for unit tests.
```bash
make # compiles the library and tests
make test # runs the full test suite
make clean
```