Strf is a C++11 text formatting library that
Attention : Branch master
was renamed to main
at the time of release 0.15.0.
- Overview
- How to extend strf:
- Header references:
<strf.hpp>
is the main header. This document is big and covers many details you will probably never need to know. So it's not the best starting point.<strf/destination.hpp>
is a lightweight and freestanding header that defines thedestination
class template. All other headers depend on this one.<strf/to_string.hpp>
adds support for writting tostd::basic_string
. It includes<strf.hpp>
.<strf/to_cfile.hpp>
adds support for writting toFILE*
. It includes<strf.hpp>
.<strf/to_streambuf.hpp>
adds support for writting tostd::basic_streambuf
. It includes<strf.hpp>
.
- Miscellaneous
Strf has been tested in the following compilers:
- Clang 3.8.1
- GCC 6.3.0
- Visual Studio 2017 15.8
- NVCC 11.0
#include <strf/to_string.hpp>
#include <assert>
constexpr int x = 255;
void samples()
{
// Creating std::string
auto str = strf::to_string(x, " in hexadecimal is ", *strf::hex(x), '.');
assert("255 in hexadecimal is 0xff.");
// Alternative syntax
auto str_tr = strf::to_string.tr("{} in hexadecimal is {}.", x, *strf::hex(x));
assert(str_tr == str);
// Applying a facet
auto to_string_mc = strf::to_string.with(strf::mixedcase);
auto str_mc = to_string_mc(x, " in hexadecimal is ", *strf::hex(x), '.');
assert(str_mc == "255 in hexadecimal is 0xFF.");
// Achieving the same result, but in multiple steps:
strf::string_maker str_maker;
strf::to(str_maker) (x, " in hexadecimal is ");
strf::to(str_maker).with(strf::mixedcase) (*strf::hex(x), '.');
auto str_mc_2 = str_maker.finish();
assert(str_mc_2 == str_mc);
// Writing instead to char*
char buff[200];
strf::to(buff, sizeof(buff)) (x, " in hexadecimal is ", *strf::hex(x), '.');
assert(str == buff);
}