1*5c90c05cSAndroid Build Coastguard Worker<img src="https://user-images.githubusercontent.com/576385/156254208-f5b743a9-88cf-439d-b0c0-923d53e8d551.png" alt="{fmt}" width="25%"/> 2*5c90c05cSAndroid Build Coastguard Worker 3*5c90c05cSAndroid Build Coastguard Worker[](https://github.com/fmtlib/fmt/actions?query=workflow%3Alinux) 4*5c90c05cSAndroid Build Coastguard Worker[](https://github.com/fmtlib/fmt/actions?query=workflow%3Amacos) 5*5c90c05cSAndroid Build Coastguard Worker[](https://github.com/fmtlib/fmt/actions?query=workflow%3Awindows) 6*5c90c05cSAndroid Build Coastguard Worker[](https://bugs.chromium.org/p/oss-fuzz/issues/list?\%0Acolspec=ID%20Type%20Component%20Status%20Proj%20Reported%20Owner%20\%0ASummary&q=proj%3Dfmt&can=1) 7*5c90c05cSAndroid Build Coastguard Worker[](https://stackoverflow.com/questions/tagged/fmt) 8*5c90c05cSAndroid Build Coastguard Worker[](https://securityscorecards.dev/viewer/?uri=github.com/fmtlib/fmt) 9*5c90c05cSAndroid Build Coastguard Worker 10*5c90c05cSAndroid Build Coastguard Worker**{fmt}** is an open-source formatting library providing a fast and safe 11*5c90c05cSAndroid Build Coastguard Workeralternative to C stdio and C++ iostreams. 12*5c90c05cSAndroid Build Coastguard Worker 13*5c90c05cSAndroid Build Coastguard WorkerIf you like this project, please consider donating to one of the funds 14*5c90c05cSAndroid Build Coastguard Workerthat help victims of the war in Ukraine: <https://www.stopputin.net/>. 15*5c90c05cSAndroid Build Coastguard Worker 16*5c90c05cSAndroid Build Coastguard Worker[Documentation](https://fmt.dev) 17*5c90c05cSAndroid Build Coastguard Worker 18*5c90c05cSAndroid Build Coastguard Worker[Cheat Sheets](https://hackingcpp.com/cpp/libs/fmt.html) 19*5c90c05cSAndroid Build Coastguard Worker 20*5c90c05cSAndroid Build Coastguard WorkerQ&A: ask questions on [StackOverflow with the tag 21*5c90c05cSAndroid Build Coastguard Workerfmt](https://stackoverflow.com/questions/tagged/fmt). 22*5c90c05cSAndroid Build Coastguard Worker 23*5c90c05cSAndroid Build Coastguard WorkerTry {fmt} in [Compiler Explorer](https://godbolt.org/z/8Mx1EW73v). 24*5c90c05cSAndroid Build Coastguard Worker 25*5c90c05cSAndroid Build Coastguard Worker# Features 26*5c90c05cSAndroid Build Coastguard Worker 27*5c90c05cSAndroid Build Coastguard Worker- Simple [format API](https://fmt.dev/latest/api/) with positional 28*5c90c05cSAndroid Build Coastguard Worker arguments for localization 29*5c90c05cSAndroid Build Coastguard Worker- Implementation of [C++20 30*5c90c05cSAndroid Build Coastguard Worker std::format](https://en.cppreference.com/w/cpp/utility/format) and 31*5c90c05cSAndroid Build Coastguard Worker [C++23 std::print](https://en.cppreference.com/w/cpp/io/print) 32*5c90c05cSAndroid Build Coastguard Worker- [Format string syntax](https://fmt.dev/latest/syntax/) similar 33*5c90c05cSAndroid Build Coastguard Worker to Python\'s 34*5c90c05cSAndroid Build Coastguard Worker [format](https://docs.python.org/3/library/stdtypes.html#str.format) 35*5c90c05cSAndroid Build Coastguard Worker- Fast IEEE 754 floating-point formatter with correct rounding, 36*5c90c05cSAndroid Build Coastguard Worker shortness and round-trip guarantees using the 37*5c90c05cSAndroid Build Coastguard Worker [Dragonbox](https://github.com/jk-jeon/dragonbox) algorithm 38*5c90c05cSAndroid Build Coastguard Worker- Portable Unicode support 39*5c90c05cSAndroid Build Coastguard Worker- Safe [printf 40*5c90c05cSAndroid Build Coastguard Worker implementation](https://fmt.dev/latest/api/#printf-formatting) 41*5c90c05cSAndroid Build Coastguard Worker including the POSIX extension for positional arguments 42*5c90c05cSAndroid Build Coastguard Worker- Extensibility: [support for user-defined 43*5c90c05cSAndroid Build Coastguard Worker types](https://fmt.dev/latest/api/#formatting-user-defined-types) 44*5c90c05cSAndroid Build Coastguard Worker- High performance: faster than common standard library 45*5c90c05cSAndroid Build Coastguard Worker implementations of `(s)printf`, iostreams, `to_string` and 46*5c90c05cSAndroid Build Coastguard Worker `to_chars`, see [Speed tests](#speed-tests) and [Converting a 47*5c90c05cSAndroid Build Coastguard Worker hundred million integers to strings per 48*5c90c05cSAndroid Build Coastguard Worker second](http://www.zverovich.net/2020/06/13/fast-int-to-string-revisited.html) 49*5c90c05cSAndroid Build Coastguard Worker- Small code size both in terms of source code with the minimum 50*5c90c05cSAndroid Build Coastguard Worker configuration consisting of just three files, `core.h`, `format.h` 51*5c90c05cSAndroid Build Coastguard Worker and `format-inl.h`, and compiled code; see [Compile time and code 52*5c90c05cSAndroid Build Coastguard Worker bloat](#compile-time-and-code-bloat) 53*5c90c05cSAndroid Build Coastguard Worker- Reliability: the library has an extensive set of 54*5c90c05cSAndroid Build Coastguard Worker [tests](https://github.com/fmtlib/fmt/tree/master/test) and is 55*5c90c05cSAndroid Build Coastguard Worker [continuously fuzzed](https://bugs.chromium.org/p/oss-fuzz/issues/list?colspec=ID%20Type%20Component%20Status%20Proj%20Reported%20Owner%20Summary&q=proj%3Dfmt&can=1) 56*5c90c05cSAndroid Build Coastguard Worker- Safety: the library is fully type-safe, errors in format strings can 57*5c90c05cSAndroid Build Coastguard Worker be reported at compile time, automatic memory management prevents 58*5c90c05cSAndroid Build Coastguard Worker buffer overflow errors 59*5c90c05cSAndroid Build Coastguard Worker- Ease of use: small self-contained code base, no external 60*5c90c05cSAndroid Build Coastguard Worker dependencies, permissive MIT 61*5c90c05cSAndroid Build Coastguard Worker [license](https://github.com/fmtlib/fmt/blob/master/LICENSE) 62*5c90c05cSAndroid Build Coastguard Worker- [Portability](https://fmt.dev/latest/#portability) with 63*5c90c05cSAndroid Build Coastguard Worker consistent output across platforms and support for older compilers 64*5c90c05cSAndroid Build Coastguard Worker- Clean warning-free codebase even on high warning levels such as 65*5c90c05cSAndroid Build Coastguard Worker `-Wall -Wextra -pedantic` 66*5c90c05cSAndroid Build Coastguard Worker- Locale independence by default 67*5c90c05cSAndroid Build Coastguard Worker- Optional header-only configuration enabled with the 68*5c90c05cSAndroid Build Coastguard Worker `FMT_HEADER_ONLY` macro 69*5c90c05cSAndroid Build Coastguard Worker 70*5c90c05cSAndroid Build Coastguard WorkerSee the [documentation](https://fmt.dev) for more details. 71*5c90c05cSAndroid Build Coastguard Worker 72*5c90c05cSAndroid Build Coastguard Worker# Examples 73*5c90c05cSAndroid Build Coastguard Worker 74*5c90c05cSAndroid Build Coastguard Worker**Print to stdout** ([run](https://godbolt.org/z/Tevcjh)) 75*5c90c05cSAndroid Build Coastguard Worker 76*5c90c05cSAndroid Build Coastguard Worker``` c++ 77*5c90c05cSAndroid Build Coastguard Worker#include <fmt/core.h> 78*5c90c05cSAndroid Build Coastguard Worker 79*5c90c05cSAndroid Build Coastguard Workerint main() { 80*5c90c05cSAndroid Build Coastguard Worker fmt::print("Hello, world!\n"); 81*5c90c05cSAndroid Build Coastguard Worker} 82*5c90c05cSAndroid Build Coastguard Worker``` 83*5c90c05cSAndroid Build Coastguard Worker 84*5c90c05cSAndroid Build Coastguard Worker**Format a string** ([run](https://godbolt.org/z/oK8h33)) 85*5c90c05cSAndroid Build Coastguard Worker 86*5c90c05cSAndroid Build Coastguard Worker``` c++ 87*5c90c05cSAndroid Build Coastguard Workerstd::string s = fmt::format("The answer is {}.", 42); 88*5c90c05cSAndroid Build Coastguard Worker// s == "The answer is 42." 89*5c90c05cSAndroid Build Coastguard Worker``` 90*5c90c05cSAndroid Build Coastguard Worker 91*5c90c05cSAndroid Build Coastguard Worker**Format a string using positional arguments** 92*5c90c05cSAndroid Build Coastguard Worker([run](https://godbolt.org/z/Yn7Txe)) 93*5c90c05cSAndroid Build Coastguard Worker 94*5c90c05cSAndroid Build Coastguard Worker``` c++ 95*5c90c05cSAndroid Build Coastguard Workerstd::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy"); 96*5c90c05cSAndroid Build Coastguard Worker// s == "I'd rather be happy than right." 97*5c90c05cSAndroid Build Coastguard Worker``` 98*5c90c05cSAndroid Build Coastguard Worker 99*5c90c05cSAndroid Build Coastguard Worker**Print dates and times** ([run](https://godbolt.org/z/c31ExdY3W)) 100*5c90c05cSAndroid Build Coastguard Worker 101*5c90c05cSAndroid Build Coastguard Worker``` c++ 102*5c90c05cSAndroid Build Coastguard Worker#include <fmt/chrono.h> 103*5c90c05cSAndroid Build Coastguard Worker 104*5c90c05cSAndroid Build Coastguard Workerint main() { 105*5c90c05cSAndroid Build Coastguard Worker auto now = std::chrono::system_clock::now(); 106*5c90c05cSAndroid Build Coastguard Worker fmt::print("Date and time: {}\n", now); 107*5c90c05cSAndroid Build Coastguard Worker fmt::print("Time: {:%H:%M}\n", now); 108*5c90c05cSAndroid Build Coastguard Worker} 109*5c90c05cSAndroid Build Coastguard Worker``` 110*5c90c05cSAndroid Build Coastguard Worker 111*5c90c05cSAndroid Build Coastguard WorkerOutput: 112*5c90c05cSAndroid Build Coastguard Worker 113*5c90c05cSAndroid Build Coastguard Worker Date and time: 2023-12-26 19:10:31.557195597 114*5c90c05cSAndroid Build Coastguard Worker Time: 19:10 115*5c90c05cSAndroid Build Coastguard Worker 116*5c90c05cSAndroid Build Coastguard Worker**Print a container** ([run](https://godbolt.org/z/MxM1YqjE7)) 117*5c90c05cSAndroid Build Coastguard Worker 118*5c90c05cSAndroid Build Coastguard Worker``` c++ 119*5c90c05cSAndroid Build Coastguard Worker#include <vector> 120*5c90c05cSAndroid Build Coastguard Worker#include <fmt/ranges.h> 121*5c90c05cSAndroid Build Coastguard Worker 122*5c90c05cSAndroid Build Coastguard Workerint main() { 123*5c90c05cSAndroid Build Coastguard Worker std::vector<int> v = {1, 2, 3}; 124*5c90c05cSAndroid Build Coastguard Worker fmt::print("{}\n", v); 125*5c90c05cSAndroid Build Coastguard Worker} 126*5c90c05cSAndroid Build Coastguard Worker``` 127*5c90c05cSAndroid Build Coastguard Worker 128*5c90c05cSAndroid Build Coastguard WorkerOutput: 129*5c90c05cSAndroid Build Coastguard Worker 130*5c90c05cSAndroid Build Coastguard Worker [1, 2, 3] 131*5c90c05cSAndroid Build Coastguard Worker 132*5c90c05cSAndroid Build Coastguard Worker**Check a format string at compile time** 133*5c90c05cSAndroid Build Coastguard Worker 134*5c90c05cSAndroid Build Coastguard Worker``` c++ 135*5c90c05cSAndroid Build Coastguard Workerstd::string s = fmt::format("{:d}", "I am not a number"); 136*5c90c05cSAndroid Build Coastguard Worker``` 137*5c90c05cSAndroid Build Coastguard Worker 138*5c90c05cSAndroid Build Coastguard WorkerThis gives a compile-time error in C++20 because `d` is an invalid 139*5c90c05cSAndroid Build Coastguard Workerformat specifier for a string. 140*5c90c05cSAndroid Build Coastguard Worker 141*5c90c05cSAndroid Build Coastguard Worker**Write a file from a single thread** 142*5c90c05cSAndroid Build Coastguard Worker 143*5c90c05cSAndroid Build Coastguard Worker``` c++ 144*5c90c05cSAndroid Build Coastguard Worker#include <fmt/os.h> 145*5c90c05cSAndroid Build Coastguard Worker 146*5c90c05cSAndroid Build Coastguard Workerint main() { 147*5c90c05cSAndroid Build Coastguard Worker auto out = fmt::output_file("guide.txt"); 148*5c90c05cSAndroid Build Coastguard Worker out.print("Don't {}", "Panic"); 149*5c90c05cSAndroid Build Coastguard Worker} 150*5c90c05cSAndroid Build Coastguard Worker``` 151*5c90c05cSAndroid Build Coastguard Worker 152*5c90c05cSAndroid Build Coastguard WorkerThis can be [5 to 9 times faster than 153*5c90c05cSAndroid Build Coastguard Workerfprintf](http://www.zverovich.net/2020/08/04/optimal-file-buffer-size.html). 154*5c90c05cSAndroid Build Coastguard Worker 155*5c90c05cSAndroid Build Coastguard Worker**Print with colors and text styles** 156*5c90c05cSAndroid Build Coastguard Worker 157*5c90c05cSAndroid Build Coastguard Worker``` c++ 158*5c90c05cSAndroid Build Coastguard Worker#include <fmt/color.h> 159*5c90c05cSAndroid Build Coastguard Worker 160*5c90c05cSAndroid Build Coastguard Workerint main() { 161*5c90c05cSAndroid Build Coastguard Worker fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, 162*5c90c05cSAndroid Build Coastguard Worker "Hello, {}!\n", "world"); 163*5c90c05cSAndroid Build Coastguard Worker fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) | 164*5c90c05cSAndroid Build Coastguard Worker fmt::emphasis::underline, "Olá, {}!\n", "Mundo"); 165*5c90c05cSAndroid Build Coastguard Worker fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic, 166*5c90c05cSAndroid Build Coastguard Worker "你好{}!\n", "世界"); 167*5c90c05cSAndroid Build Coastguard Worker} 168*5c90c05cSAndroid Build Coastguard Worker``` 169*5c90c05cSAndroid Build Coastguard Worker 170*5c90c05cSAndroid Build Coastguard WorkerOutput on a modern terminal with Unicode support: 171*5c90c05cSAndroid Build Coastguard Worker 172*5c90c05cSAndroid Build Coastguard Worker 173*5c90c05cSAndroid Build Coastguard Worker 174*5c90c05cSAndroid Build Coastguard Worker# Benchmarks 175*5c90c05cSAndroid Build Coastguard Worker 176*5c90c05cSAndroid Build Coastguard Worker## Speed tests 177*5c90c05cSAndroid Build Coastguard Worker 178*5c90c05cSAndroid Build Coastguard Worker| Library | Method | Run Time, s | 179*5c90c05cSAndroid Build Coastguard Worker|-------------------|---------------|-------------| 180*5c90c05cSAndroid Build Coastguard Worker| libc | printf | 0.91 | 181*5c90c05cSAndroid Build Coastguard Worker| libc++ | std::ostream | 2.49 | 182*5c90c05cSAndroid Build Coastguard Worker| {fmt} 9.1 | fmt::print | 0.74 | 183*5c90c05cSAndroid Build Coastguard Worker| Boost Format 1.80 | boost::format | 6.26 | 184*5c90c05cSAndroid Build Coastguard Worker| Folly Format | folly::format | 1.87 | 185*5c90c05cSAndroid Build Coastguard Worker 186*5c90c05cSAndroid Build Coastguard Worker{fmt} is the fastest of the benchmarked methods, \~20% faster than 187*5c90c05cSAndroid Build Coastguard Worker`printf`. 188*5c90c05cSAndroid Build Coastguard Worker 189*5c90c05cSAndroid Build Coastguard WorkerThe above results were generated by building `tinyformat_test.cpp` on 190*5c90c05cSAndroid Build Coastguard WorkermacOS 12.6.1 with `clang++ -O3 -DNDEBUG -DSPEED_TEST -DHAVE_FORMAT`, and 191*5c90c05cSAndroid Build Coastguard Workertaking the best of three runs. In the test, the format string 192*5c90c05cSAndroid Build Coastguard Worker`"%0.10f:%04d:%+g:%s:%p:%c:%%\n"` or equivalent is filled 2,000,000 193*5c90c05cSAndroid Build Coastguard Workertimes with output sent to `/dev/null`; for further details refer to the 194*5c90c05cSAndroid Build Coastguard Worker[source](https://github.com/fmtlib/format-benchmark/blob/master/src/tinyformat-test.cc). 195*5c90c05cSAndroid Build Coastguard Worker 196*5c90c05cSAndroid Build Coastguard Worker{fmt} is up to 20-30x faster than `std::ostringstream` and `sprintf` on 197*5c90c05cSAndroid Build Coastguard WorkerIEEE754 `float` and `double` formatting 198*5c90c05cSAndroid Build Coastguard Worker([dtoa-benchmark](https://github.com/fmtlib/dtoa-benchmark)) and faster 199*5c90c05cSAndroid Build Coastguard Workerthan [double-conversion](https://github.com/google/double-conversion) 200*5c90c05cSAndroid Build Coastguard Workerand [ryu](https://github.com/ulfjack/ryu): 201*5c90c05cSAndroid Build Coastguard Worker 202*5c90c05cSAndroid Build Coastguard Worker[](https://fmt.dev/unknown_mac64_clang12.0.html) 203*5c90c05cSAndroid Build Coastguard Worker 204*5c90c05cSAndroid Build Coastguard Worker## Compile time and code bloat 205*5c90c05cSAndroid Build Coastguard Worker 206*5c90c05cSAndroid Build Coastguard WorkerThe script [bloat-test.py][test] from [format-benchmark][bench] tests compile 207*5c90c05cSAndroid Build Coastguard Workertime and code bloat for nontrivial projects. It generates 100 translation units 208*5c90c05cSAndroid Build Coastguard Workerand uses `printf()` or its alternative five times in each to simulate a 209*5c90c05cSAndroid Build Coastguard Workermedium-sized project. The resulting executable size and compile time (Apple 210*5c90c05cSAndroid Build Coastguard Workerclang version 15.0.0 (clang-1500.1.0.2.5), macOS Sonoma, best of three) is shown 211*5c90c05cSAndroid Build Coastguard Workerin the following tables. 212*5c90c05cSAndroid Build Coastguard Worker 213*5c90c05cSAndroid Build Coastguard Worker[test]: https://github.com/fmtlib/format-benchmark/blob/master/bloat-test.py 214*5c90c05cSAndroid Build Coastguard Worker[bench]: https://github.com/fmtlib/format-benchmark 215*5c90c05cSAndroid Build Coastguard Worker 216*5c90c05cSAndroid Build Coastguard Worker**Optimized build (-O3)** 217*5c90c05cSAndroid Build Coastguard Worker 218*5c90c05cSAndroid Build Coastguard Worker| Method | Compile Time, s | Executable size, KiB | Stripped size, KiB | 219*5c90c05cSAndroid Build Coastguard Worker|---------------|-----------------|----------------------|--------------------| 220*5c90c05cSAndroid Build Coastguard Worker| printf | 1.6 | 54 | 50 | 221*5c90c05cSAndroid Build Coastguard Worker| IOStreams | 25.9 | 98 | 84 | 222*5c90c05cSAndroid Build Coastguard Worker| fmt 83652df | 4.8 | 54 | 50 | 223*5c90c05cSAndroid Build Coastguard Worker| tinyformat | 29.1 | 161 | 136 | 224*5c90c05cSAndroid Build Coastguard Worker| Boost Format | 55.0 | 530 | 317 | 225*5c90c05cSAndroid Build Coastguard Worker 226*5c90c05cSAndroid Build Coastguard Worker{fmt} is fast to compile and is comparable to `printf` in terms of per-call 227*5c90c05cSAndroid Build Coastguard Workerbinary size (within a rounding error on this system). 228*5c90c05cSAndroid Build Coastguard Worker 229*5c90c05cSAndroid Build Coastguard Worker**Non-optimized build** 230*5c90c05cSAndroid Build Coastguard Worker 231*5c90c05cSAndroid Build Coastguard Worker| Method | Compile Time, s | Executable size, KiB | Stripped size, KiB | 232*5c90c05cSAndroid Build Coastguard Worker|---------------|-----------------|----------------------|--------------------| 233*5c90c05cSAndroid Build Coastguard Worker| printf | 1.4 | 54 | 50 | 234*5c90c05cSAndroid Build Coastguard Worker| IOStreams | 23.4 | 92 | 68 | 235*5c90c05cSAndroid Build Coastguard Worker| {fmt} 83652df | 4.4 | 89 | 85 | 236*5c90c05cSAndroid Build Coastguard Worker| tinyformat | 24.5 | 204 | 161 | 237*5c90c05cSAndroid Build Coastguard Worker| Boost Format | 36.4 | 831 | 462 | 238*5c90c05cSAndroid Build Coastguard Worker 239*5c90c05cSAndroid Build Coastguard Worker`libc`, `lib(std)c++`, and `libfmt` are all linked as shared libraries 240*5c90c05cSAndroid Build Coastguard Workerto compare formatting function overhead only. Boost Format is a 241*5c90c05cSAndroid Build Coastguard Workerheader-only library so it doesn\'t provide any linkage options. 242*5c90c05cSAndroid Build Coastguard Worker 243*5c90c05cSAndroid Build Coastguard Worker## Running the tests 244*5c90c05cSAndroid Build Coastguard Worker 245*5c90c05cSAndroid Build Coastguard WorkerPlease refer to [Building the 246*5c90c05cSAndroid Build Coastguard Workerlibrary](https://fmt.dev/latest/get-started/#building-from-source) for 247*5c90c05cSAndroid Build Coastguard Workerinstructions on how to build the library and run the unit tests. 248*5c90c05cSAndroid Build Coastguard Worker 249*5c90c05cSAndroid Build Coastguard WorkerBenchmarks reside in a separate repository, 250*5c90c05cSAndroid Build Coastguard Worker[format-benchmarks](https://github.com/fmtlib/format-benchmark), so to 251*5c90c05cSAndroid Build Coastguard Workerrun the benchmarks you first need to clone this repository and generate 252*5c90c05cSAndroid Build Coastguard WorkerMakefiles with CMake: 253*5c90c05cSAndroid Build Coastguard Worker 254*5c90c05cSAndroid Build Coastguard Worker $ git clone --recursive https://github.com/fmtlib/format-benchmark.git 255*5c90c05cSAndroid Build Coastguard Worker $ cd format-benchmark 256*5c90c05cSAndroid Build Coastguard Worker $ cmake . 257*5c90c05cSAndroid Build Coastguard Worker 258*5c90c05cSAndroid Build Coastguard WorkerThen you can run the speed test: 259*5c90c05cSAndroid Build Coastguard Worker 260*5c90c05cSAndroid Build Coastguard Worker $ make speed-test 261*5c90c05cSAndroid Build Coastguard Worker 262*5c90c05cSAndroid Build Coastguard Workeror the bloat test: 263*5c90c05cSAndroid Build Coastguard Worker 264*5c90c05cSAndroid Build Coastguard Worker $ make bloat-test 265*5c90c05cSAndroid Build Coastguard Worker 266*5c90c05cSAndroid Build Coastguard Worker# Migrating code 267*5c90c05cSAndroid Build Coastguard Worker 268*5c90c05cSAndroid Build Coastguard Worker[clang-tidy](https://clang.llvm.org/extra/clang-tidy/) v18 provides the 269*5c90c05cSAndroid Build Coastguard Worker[modernize-use-std-print](https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-std-print.html) 270*5c90c05cSAndroid Build Coastguard Workercheck that is capable of converting occurrences of `printf` and 271*5c90c05cSAndroid Build Coastguard Worker`fprintf` to `fmt::print` if configured to do so. (By default it 272*5c90c05cSAndroid Build Coastguard Workerconverts to `std::print`.) 273*5c90c05cSAndroid Build Coastguard Worker 274*5c90c05cSAndroid Build Coastguard Worker# Notable projects using this library 275*5c90c05cSAndroid Build Coastguard Worker 276*5c90c05cSAndroid Build Coastguard Worker- [0 A.D.](https://play0ad.com/): a free, open-source, cross-platform 277*5c90c05cSAndroid Build Coastguard Worker real-time strategy game 278*5c90c05cSAndroid Build Coastguard Worker- [AMPL/MP](https://github.com/ampl/mp): an open-source library for 279*5c90c05cSAndroid Build Coastguard Worker mathematical programming 280*5c90c05cSAndroid Build Coastguard Worker- [Apple's FoundationDB](https://github.com/apple/foundationdb): an open-source, 281*5c90c05cSAndroid Build Coastguard Worker distributed, transactional key-value store 282*5c90c05cSAndroid Build Coastguard Worker- [Aseprite](https://github.com/aseprite/aseprite): animated sprite 283*5c90c05cSAndroid Build Coastguard Worker editor & pixel art tool 284*5c90c05cSAndroid Build Coastguard Worker- [AvioBook](https://www.aviobook.aero/en): a comprehensive aircraft 285*5c90c05cSAndroid Build Coastguard Worker operations suite 286*5c90c05cSAndroid Build Coastguard Worker- [Blizzard Battle.net](https://battle.net/): an online gaming 287*5c90c05cSAndroid Build Coastguard Worker platform 288*5c90c05cSAndroid Build Coastguard Worker- [Celestia](https://celestia.space/): real-time 3D visualization of 289*5c90c05cSAndroid Build Coastguard Worker space 290*5c90c05cSAndroid Build Coastguard Worker- [Ceph](https://ceph.com/): a scalable distributed storage system 291*5c90c05cSAndroid Build Coastguard Worker- [ccache](https://ccache.dev/): a compiler cache 292*5c90c05cSAndroid Build Coastguard Worker- [ClickHouse](https://github.com/ClickHouse/ClickHouse): an 293*5c90c05cSAndroid Build Coastguard Worker analytical database management system 294*5c90c05cSAndroid Build Coastguard Worker- [ContextVision](https://www.contextvision.com/): medical imaging software 295*5c90c05cSAndroid Build Coastguard Worker- [Contour](https://github.com/contour-terminal/contour/): a modern 296*5c90c05cSAndroid Build Coastguard Worker terminal emulator 297*5c90c05cSAndroid Build Coastguard Worker- [CUAUV](https://cuauv.org/): Cornell University\'s autonomous 298*5c90c05cSAndroid Build Coastguard Worker underwater vehicle 299*5c90c05cSAndroid Build Coastguard Worker- [Drake](https://drake.mit.edu/): a planning, control, and analysis 300*5c90c05cSAndroid Build Coastguard Worker toolbox for nonlinear dynamical systems (MIT) 301*5c90c05cSAndroid Build Coastguard Worker- [Envoy](https://github.com/envoyproxy/envoy): C++ L7 proxy and 302*5c90c05cSAndroid Build Coastguard Worker communication bus (Lyft) 303*5c90c05cSAndroid Build Coastguard Worker- [FiveM](https://fivem.net/): a modification framework for GTA V 304*5c90c05cSAndroid Build Coastguard Worker- [fmtlog](https://github.com/MengRao/fmtlog): a performant 305*5c90c05cSAndroid Build Coastguard Worker fmtlib-style logging library with latency in nanoseconds 306*5c90c05cSAndroid Build Coastguard Worker- [Folly](https://github.com/facebook/folly): Facebook open-source 307*5c90c05cSAndroid Build Coastguard Worker library 308*5c90c05cSAndroid Build Coastguard Worker- [GemRB](https://gemrb.org/): a portable open-source implementation 309*5c90c05cSAndroid Build Coastguard Worker of Bioware's Infinity Engine 310*5c90c05cSAndroid Build Coastguard Worker- [Grand Mountain 311*5c90c05cSAndroid Build Coastguard Worker Adventure](https://store.steampowered.com/app/1247360/Grand_Mountain_Adventure/): 312*5c90c05cSAndroid Build Coastguard Worker a beautiful open-world ski & snowboarding game 313*5c90c05cSAndroid Build Coastguard Worker- [HarpyWar/pvpgn](https://github.com/pvpgn/pvpgn-server): Player vs 314*5c90c05cSAndroid Build Coastguard Worker Player Gaming Network with tweaks 315*5c90c05cSAndroid Build Coastguard Worker- [KBEngine](https://github.com/kbengine/kbengine): an open-source 316*5c90c05cSAndroid Build Coastguard Worker MMOG server engine 317*5c90c05cSAndroid Build Coastguard Worker- [Keypirinha](https://keypirinha.com/): a semantic launcher for 318*5c90c05cSAndroid Build Coastguard Worker Windows 319*5c90c05cSAndroid Build Coastguard Worker- [Kodi](https://kodi.tv/) (formerly xbmc): home theater software 320*5c90c05cSAndroid Build Coastguard Worker- [Knuth](https://kth.cash/): high-performance Bitcoin full-node 321*5c90c05cSAndroid Build Coastguard Worker- [libunicode](https://github.com/contour-terminal/libunicode/): a 322*5c90c05cSAndroid Build Coastguard Worker modern C++17 Unicode library 323*5c90c05cSAndroid Build Coastguard Worker- [MariaDB](https://mariadb.org/): relational database management 324*5c90c05cSAndroid Build Coastguard Worker system 325*5c90c05cSAndroid Build Coastguard Worker- [Microsoft Verona](https://github.com/microsoft/verona): research 326*5c90c05cSAndroid Build Coastguard Worker programming language for concurrent ownership 327*5c90c05cSAndroid Build Coastguard Worker- [MongoDB](https://mongodb.com/): distributed document database 328*5c90c05cSAndroid Build Coastguard Worker- [MongoDB Smasher](https://github.com/duckie/mongo_smasher): a small 329*5c90c05cSAndroid Build Coastguard Worker tool to generate randomized datasets 330*5c90c05cSAndroid Build Coastguard Worker- [OpenSpace](https://openspaceproject.com/): an open-source 331*5c90c05cSAndroid Build Coastguard Worker astrovisualization framework 332*5c90c05cSAndroid Build Coastguard Worker- [PenUltima Online (POL)](https://www.polserver.com/): an MMO server, 333*5c90c05cSAndroid Build Coastguard Worker compatible with most Ultima Online clients 334*5c90c05cSAndroid Build Coastguard Worker- [PyTorch](https://github.com/pytorch/pytorch): an open-source 335*5c90c05cSAndroid Build Coastguard Worker machine learning library 336*5c90c05cSAndroid Build Coastguard Worker- [quasardb](https://www.quasardb.net/): a distributed, 337*5c90c05cSAndroid Build Coastguard Worker high-performance, associative database 338*5c90c05cSAndroid Build Coastguard Worker- [Quill](https://github.com/odygrd/quill): asynchronous low-latency 339*5c90c05cSAndroid Build Coastguard Worker logging library 340*5c90c05cSAndroid Build Coastguard Worker- [QKW](https://github.com/ravijanjam/qkw): generalizing aliasing to 341*5c90c05cSAndroid Build Coastguard Worker simplify navigation, and execute complex multi-line terminal 342*5c90c05cSAndroid Build Coastguard Worker command sequences 343*5c90c05cSAndroid Build Coastguard Worker- [redis-cerberus](https://github.com/HunanTV/redis-cerberus): a Redis 344*5c90c05cSAndroid Build Coastguard Worker cluster proxy 345*5c90c05cSAndroid Build Coastguard Worker- [redpanda](https://vectorized.io/redpanda): a 10x faster Kafka® 346*5c90c05cSAndroid Build Coastguard Worker replacement for mission-critical systems written in C++ 347*5c90c05cSAndroid Build Coastguard Worker- [rpclib](http://rpclib.net/): a modern C++ msgpack-RPC server and 348*5c90c05cSAndroid Build Coastguard Worker client library 349*5c90c05cSAndroid Build Coastguard Worker- [Salesforce Analytics 350*5c90c05cSAndroid Build Coastguard Worker Cloud](https://www.salesforce.com/analytics-cloud/overview/): 351*5c90c05cSAndroid Build Coastguard Worker business intelligence software 352*5c90c05cSAndroid Build Coastguard Worker- [Scylla](https://www.scylladb.com/): a Cassandra-compatible NoSQL 353*5c90c05cSAndroid Build Coastguard Worker data store that can handle 1 million transactions per second on a 354*5c90c05cSAndroid Build Coastguard Worker single server 355*5c90c05cSAndroid Build Coastguard Worker- [Seastar](http://www.seastar-project.org/): an advanced, open-source 356*5c90c05cSAndroid Build Coastguard Worker C++ framework for high-performance server applications on modern 357*5c90c05cSAndroid Build Coastguard Worker hardware 358*5c90c05cSAndroid Build Coastguard Worker- [spdlog](https://github.com/gabime/spdlog): super fast C++ logging 359*5c90c05cSAndroid Build Coastguard Worker library 360*5c90c05cSAndroid Build Coastguard Worker- [Stellar](https://www.stellar.org/): financial platform 361*5c90c05cSAndroid Build Coastguard Worker- [Touch Surgery](https://www.touchsurgery.com/): surgery simulator 362*5c90c05cSAndroid Build Coastguard Worker- [TrinityCore](https://github.com/TrinityCore/TrinityCore): 363*5c90c05cSAndroid Build Coastguard Worker open-source MMORPG framework 364*5c90c05cSAndroid Build Coastguard Worker- [ userver framework](https://userver.tech/): open-source 365*5c90c05cSAndroid Build Coastguard Worker asynchronous framework with a rich set of abstractions and database 366*5c90c05cSAndroid Build Coastguard Worker drivers 367*5c90c05cSAndroid Build Coastguard Worker- [Windows Terminal](https://github.com/microsoft/terminal): the new 368*5c90c05cSAndroid Build Coastguard Worker Windows terminal 369*5c90c05cSAndroid Build Coastguard Worker 370*5c90c05cSAndroid Build Coastguard Worker[More\...](https://github.com/search?q=fmtlib&type=Code) 371*5c90c05cSAndroid Build Coastguard Worker 372*5c90c05cSAndroid Build Coastguard WorkerIf you are aware of other projects using this library, please let me 373*5c90c05cSAndroid Build Coastguard Workerknow by [email](mailto:[email protected]) or by submitting an 374*5c90c05cSAndroid Build Coastguard Worker[issue](https://github.com/fmtlib/fmt/issues). 375*5c90c05cSAndroid Build Coastguard Worker 376*5c90c05cSAndroid Build Coastguard Worker# Motivation 377*5c90c05cSAndroid Build Coastguard Worker 378*5c90c05cSAndroid Build Coastguard WorkerSo why yet another formatting library? 379*5c90c05cSAndroid Build Coastguard Worker 380*5c90c05cSAndroid Build Coastguard WorkerThere are plenty of methods for doing this task, from standard ones like 381*5c90c05cSAndroid Build Coastguard Workerthe printf family of function and iostreams to Boost Format and 382*5c90c05cSAndroid Build Coastguard WorkerFastFormat libraries. The reason for creating a new library is that 383*5c90c05cSAndroid Build Coastguard Workerevery existing solution that I found either had serious issues or 384*5c90c05cSAndroid Build Coastguard Workerdidn\'t provide all the features I needed. 385*5c90c05cSAndroid Build Coastguard Worker 386*5c90c05cSAndroid Build Coastguard Worker## printf 387*5c90c05cSAndroid Build Coastguard Worker 388*5c90c05cSAndroid Build Coastguard WorkerThe good thing about `printf` is that it is pretty fast and readily 389*5c90c05cSAndroid Build Coastguard Workeravailable being a part of the C standard library. The main drawback is 390*5c90c05cSAndroid Build Coastguard Workerthat it doesn\'t support user-defined types. `printf` also has safety 391*5c90c05cSAndroid Build Coastguard Workerissues although they are somewhat mitigated with [\_\_attribute\_\_ 392*5c90c05cSAndroid Build Coastguard Worker((format (printf, 393*5c90c05cSAndroid Build Coastguard Worker\...))](https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html) in 394*5c90c05cSAndroid Build Coastguard WorkerGCC. There is a POSIX extension that adds positional arguments required 395*5c90c05cSAndroid Build Coastguard Workerfor 396*5c90c05cSAndroid Build Coastguard Worker[i18n](https://en.wikipedia.org/wiki/Internationalization_and_localization) 397*5c90c05cSAndroid Build Coastguard Workerto `printf` but it is not a part of C99 and may not be available on some 398*5c90c05cSAndroid Build Coastguard Workerplatforms. 399*5c90c05cSAndroid Build Coastguard Worker 400*5c90c05cSAndroid Build Coastguard Worker## iostreams 401*5c90c05cSAndroid Build Coastguard Worker 402*5c90c05cSAndroid Build Coastguard WorkerThe main issue with iostreams is best illustrated with an example: 403*5c90c05cSAndroid Build Coastguard Worker 404*5c90c05cSAndroid Build Coastguard Worker``` c++ 405*5c90c05cSAndroid Build Coastguard Workerstd::cout << std::setprecision(2) << std::fixed << 1.23456 << "\n"; 406*5c90c05cSAndroid Build Coastguard Worker``` 407*5c90c05cSAndroid Build Coastguard Worker 408*5c90c05cSAndroid Build Coastguard Workerwhich is a lot of typing compared to printf: 409*5c90c05cSAndroid Build Coastguard Worker 410*5c90c05cSAndroid Build Coastguard Worker``` c++ 411*5c90c05cSAndroid Build Coastguard Workerprintf("%.2f\n", 1.23456); 412*5c90c05cSAndroid Build Coastguard Worker``` 413*5c90c05cSAndroid Build Coastguard Worker 414*5c90c05cSAndroid Build Coastguard WorkerMatthew Wilson, the author of FastFormat, called this \"chevron hell\". 415*5c90c05cSAndroid Build Coastguard Workeriostreams don\'t support positional arguments by design. 416*5c90c05cSAndroid Build Coastguard Worker 417*5c90c05cSAndroid Build Coastguard WorkerThe good part is that iostreams support user-defined types and are safe 418*5c90c05cSAndroid Build Coastguard Workeralthough error handling is awkward. 419*5c90c05cSAndroid Build Coastguard Worker 420*5c90c05cSAndroid Build Coastguard Worker## Boost Format 421*5c90c05cSAndroid Build Coastguard Worker 422*5c90c05cSAndroid Build Coastguard WorkerThis is a very powerful library that supports both `printf`-like format 423*5c90c05cSAndroid Build Coastguard Workerstrings and positional arguments. Its main drawback is performance. 424*5c90c05cSAndroid Build Coastguard WorkerAccording to various benchmarks, it is much slower than other methods 425*5c90c05cSAndroid Build Coastguard Workerconsidered here. Boost Format also has excessive build times and severe 426*5c90c05cSAndroid Build Coastguard Workercode bloat issues (see [Benchmarks](#benchmarks)). 427*5c90c05cSAndroid Build Coastguard Worker 428*5c90c05cSAndroid Build Coastguard Worker## FastFormat 429*5c90c05cSAndroid Build Coastguard Worker 430*5c90c05cSAndroid Build Coastguard WorkerThis is an interesting library that is fast, safe and has positional 431*5c90c05cSAndroid Build Coastguard Workerarguments. However, it has significant limitations, citing its author: 432*5c90c05cSAndroid Build Coastguard Worker 433*5c90c05cSAndroid Build Coastguard Worker> Three features that have no hope of being accommodated within the 434*5c90c05cSAndroid Build Coastguard Worker> current design are: 435*5c90c05cSAndroid Build Coastguard Worker> 436*5c90c05cSAndroid Build Coastguard Worker> - Leading zeros (or any other non-space padding) 437*5c90c05cSAndroid Build Coastguard Worker> - Octal/hexadecimal encoding 438*5c90c05cSAndroid Build Coastguard Worker> - Runtime width/alignment specification 439*5c90c05cSAndroid Build Coastguard Worker 440*5c90c05cSAndroid Build Coastguard WorkerIt is also quite big and has a heavy dependency, on STLSoft, which might be 441*5c90c05cSAndroid Build Coastguard Workertoo restrictive for use in some projects. 442*5c90c05cSAndroid Build Coastguard Worker 443*5c90c05cSAndroid Build Coastguard Worker## Boost Spirit.Karma 444*5c90c05cSAndroid Build Coastguard Worker 445*5c90c05cSAndroid Build Coastguard WorkerThis is not a formatting library but I decided to include it here for 446*5c90c05cSAndroid Build Coastguard Workercompleteness. As iostreams, it suffers from the problem of mixing 447*5c90c05cSAndroid Build Coastguard Workerverbatim text with arguments. The library is pretty fast, but slower on 448*5c90c05cSAndroid Build Coastguard Workerinteger formatting than `fmt::format_to` with format string compilation 449*5c90c05cSAndroid Build Coastguard Workeron Karma\'s own benchmark, see [Converting a hundred million integers to 450*5c90c05cSAndroid Build Coastguard Workerstrings per 451*5c90c05cSAndroid Build Coastguard Workersecond](http://www.zverovich.net/2020/06/13/fast-int-to-string-revisited.html). 452*5c90c05cSAndroid Build Coastguard Worker 453*5c90c05cSAndroid Build Coastguard Worker# License 454*5c90c05cSAndroid Build Coastguard Worker 455*5c90c05cSAndroid Build Coastguard Worker{fmt} is distributed under the MIT 456*5c90c05cSAndroid Build Coastguard Worker[license](https://github.com/fmtlib/fmt/blob/master/LICENSE). 457*5c90c05cSAndroid Build Coastguard Worker 458*5c90c05cSAndroid Build Coastguard Worker# Documentation License 459*5c90c05cSAndroid Build Coastguard Worker 460*5c90c05cSAndroid Build Coastguard WorkerThe [Format String Syntax](https://fmt.dev/latest/syntax/) section 461*5c90c05cSAndroid Build Coastguard Workerin the documentation is based on the one from Python [string module 462*5c90c05cSAndroid Build Coastguard Workerdocumentation](https://docs.python.org/3/library/string.html#module-string). 463*5c90c05cSAndroid Build Coastguard WorkerFor this reason, the documentation is distributed under the Python 464*5c90c05cSAndroid Build Coastguard WorkerSoftware Foundation license available in 465*5c90c05cSAndroid Build Coastguard Worker[doc/python-license.txt](https://raw.github.com/fmtlib/fmt/master/doc/python-license.txt). 466*5c90c05cSAndroid Build Coastguard WorkerIt only applies if you distribute the documentation of {fmt}. 467*5c90c05cSAndroid Build Coastguard Worker 468*5c90c05cSAndroid Build Coastguard Worker# Maintainers 469*5c90c05cSAndroid Build Coastguard Worker 470*5c90c05cSAndroid Build Coastguard WorkerThe {fmt} library is maintained by Victor Zverovich 471*5c90c05cSAndroid Build Coastguard Worker([vitaut](https://github.com/vitaut)) with contributions from many other 472*5c90c05cSAndroid Build Coastguard Workerpeople. See 473*5c90c05cSAndroid Build Coastguard Worker[Contributors](https://github.com/fmtlib/fmt/graphs/contributors) and 474*5c90c05cSAndroid Build Coastguard Worker[Releases](https://github.com/fmtlib/fmt/releases) for some of the 475*5c90c05cSAndroid Build Coastguard Workernames. Let us know if your contribution is not listed or mentioned 476*5c90c05cSAndroid Build Coastguard Workerincorrectly and we\'ll make it right. 477*5c90c05cSAndroid Build Coastguard Worker 478*5c90c05cSAndroid Build Coastguard Worker# Security Policy 479*5c90c05cSAndroid Build Coastguard Worker 480*5c90c05cSAndroid Build Coastguard WorkerTo report a security issue, please disclose it at [security 481*5c90c05cSAndroid Build Coastguard Workeradvisory](https://github.com/fmtlib/fmt/security/advisories/new). 482*5c90c05cSAndroid Build Coastguard Worker 483*5c90c05cSAndroid Build Coastguard WorkerThis project is maintained by a team of volunteers on a 484*5c90c05cSAndroid Build Coastguard Workerreasonable-effort basis. As such, please give us at least *90* days to 485*5c90c05cSAndroid Build Coastguard Workerwork on a fix before public exposure. 486