This is a simple library for text formatting in C++. It is inspired by (but not compatible to) std::format and {fmt}. Please make sure that your compiler supports at least C++20. That's GCC 10 or Clang 13.
Just copy the safmat.hpp
file into your projects include directory.
Alternatively you can install it into /usr/local/include
.
Then you should be able to #include "safmat.hpp"
the file and use it.
Just #define
the following to 0 or 1, before #include
ing the file.
SAFMAT_OUT_OSTREAM
(std::ostream&
OutputIterator)SAFMAT_OUT_FILE
(FILE*
OutputIterator)
#include "safmat.hpp"
int main() {
safmat::println("Hello {}!", "World");
}
<--snip-->
struct Person {
std::string name;
unsigned int age;
};
template<>
struct safmat::Formatter<Person> {
char rep{'s'}; // s=simple x=extended
void parse(safmat::InputIterator &in) {
switch (*in) {
case 's':
case 'x':
rep = *in++;
break;
case '}':
return;
default:
throw safmat::format_error{"Invalid format specifier."};
}
}
void format_to(safmat::FormatContext &ctx, const Person &p) {
auto &out = ctx.out;
switch (rep) {
case 's':
safmat::format_to(out, "{}", p.name);
break;
case 'x':
safmat::format_to(out, "Person{{ name=\"{}\", age={} }}", p.name, p.age);
break;
default:
throw safmat::format_error{"Unimplemented format specifier."};
}
}
};
int main() {
Person p{"Max Mustermann", 42};
safmat::println("p = {}", p);
}
<--snip-->
template<>
struct safmat::io::OutputAdapter<std::vector<char>> {
inline static void write(std::vector<char> *vec, std::string_view s) {
vec->insert(end(*vec), begin(s), end(s));
}
};
int main() {
std::vector<char> chars{};
safmt::print(chars, "Hello World {}", 42);
safmat::println("{}", chars);
}
For more examples look into test.cpp.
- Implement more Formatter<> specializations.
- std::floating_point
- std::chrono::*
- std::pair
- std::tuple (maybe?)
- T*
- Implement more standard format specifiers and do it properly
- Implement a way to do nested arguments (eg.
"{:0{}x}"
). - Replace OutputIterator with something different.
- Somehow make the library char-independent without templating vformat_to().
- vformat()
- vprint(), vprintln()
- (maybe) implement locale-specific stuff (probably not)
- Make the library constexpr (requires C++23's constexpr unique_ptr)