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