xref: /aosp_15_r20/external/fmtlib/doc/index.md (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1---
2hide:
3  - navigation
4  - toc
5---
6
7# A modern formatting library
8
9<div class="features-container">
10
11<div class="feature">
12<h2>Safety</h2>
13<p>
14  Inspired by Python's formatting facility, {fmt} provides a safe replacement
15  for the <code>printf</code> family of functions. Errors in format strings,
16  which are a common source of vulnerabilities in C, are <b>reported at
17  compile time</b>. For example:
18
19  <pre><code class="language-cpp"
20  >fmt::format("{:d}", "I am not a number");</code></pre>
21
22  will give a compile-time error because <code>d</code> is not a valid
23  format specifier for strings. APIs like <a href="api/#format">
24  <code>fmt::format</code></a> <b>prevent buffer overflow errors</b> via
25  automatic memory management.
26</p>
27<a href="api#compile-time-checks">→ Learn more</a>
28</div>
29
30<div class="feature">
31<h2>Extensibility</h2>
32<p>
33  Formatting of most <b>standard types</b>, including all containers, dates,
34  and times is <b>supported out-of-the-box</b>. For example:
35
36  <pre><code class="language-cpp"
37  >fmt::print("{}", std::vector{1, 2, 3});</code></pre>
38
39  prints the vector in a JSON-like format:
40
41  <pre><code>[1, 2, 3]</code></pre>
42
43  You can <b>make your own types formattable</b> and even make compile-time
44  checks work for them.
45</p>
46<a href="api#udt">→ Learn more</a>
47</div>
48
49<div class="feature">
50<h2>Performance</h2>
51<p>
52  {fmt} can be anywhere from <b>tens of percent to 20-30 times faster</b> than
53  iostreams and <code>sprintf</code>, especially for numeric formatting.
54
55<a href="https://github.com/fmtlib/fmt?tab=readme-ov-file#benchmarks">
56<img src="perf.svg">
57</a>
58
59  The library <b>minimizes dynamic memory allocations</b> and can optionally
60  <a href="api#compile-api">compile format strings</a> to optimal code.
61</p>
62</div>
63
64<div class="feature">
65<h2>Unicode support</h2>
66<p>
67  {fmt} provides <b>portable Unicode support</b> on major operating systems
68  with UTF-8 and <code>char</code> strings. For example:
69
70  <pre><code class="language-cpp"
71  >fmt::print("Слава Україні!");</code></pre>
72
73  will be printed correctly on Linux, macOS, and even Windows console,
74  irrespective of the codepages.
75</p>
76<p>
77  The default is <b>locale-independent</b>, but you can opt into localized
78  formatting and {fmt} makes it work with Unicode, addressing issues in the
79  standard libary.
80</p>
81</div>
82
83<div class="feature">
84<h2>Fast compilation</h2>
85<p>
86  The library makes extensive use of <b>type erasure</b> to achieve fast
87  compilation. <code>fmt/base.h</code> provides a subset of the API with
88  <b>minimal include dependencies</b> and enough functionality to replace
89  all uses of <code>*printf</code>.
90</p>
91<p>
92  Code using {fmt} is usually several times faster to compile than the
93  equivalent iostreams code, and while <code>printf</code> compiles faster
94  still, the gap is narrowing.
95</p>
96<a href=
97"https://github.com/fmtlib/fmt?tab=readme-ov-file#compile-time-and-code-bloat">
98→ Learn more</a>
99</div>
100
101<div class="feature">
102<h2>Small binary footprint</h2>
103<p>
104  Type erasure is also used to prevent template bloat, resulting in <b>compact
105  per-call binary code</b>. For example, a call to <code>fmt::print</code> with
106  a single argument is just <a href="https://godbolt.org/g/TZU4KF">a few
107  instructions</a>, comparable to <code>printf</code> despite adding
108  runtime safety, and much smaller than the equivalent iostreams code.
109</p>
110<p>
111  The library itself has small binary footprint and some components such as
112  floating-point formatting can be disabled to make it even smaller for
113  resource-constrained devices.
114</p>
115</div>
116
117<div class="feature">
118<h2>Portability</h2>
119<p>
120  {fmt} has a <b>small self-contained codebase</b> with the core consisting of
121  just three headers and no external dependencies.
122</p>
123<p>
124  The library is highly portable and requires only a minimal <b>subset of
125  C++11</b> features which are available in GCC 4.9, Clang 3.4, MSVC 19.0
126  (2015) and later. Newer compiler and standard library features are used
127  if available, and enable additional functionality.
128</p>
129<p>
130  Where possible, the output of formatting functions is <b>consistent across
131  platforms</b>.
132</p>
133</p>
134</div>
135
136<div class="feature">
137<h2>Open source</h2>
138<p>
139  {fmt} is in the top hundred open-source C++ libraries on GitHub and has
140  <a href="https://github.com/fmtlib/fmt/graphs/contributors">hundreds of
141  all-time contributors</a>.
142</p>
143<p>
144  The library is distributed under a permissive MIT
145  <a href="https://github.com/fmtlib/fmt#license">license</a> and is
146  <b>relied upon by many open-source projects</b>, including Blender, PyTorch,
147  Apple's FoundationDB, Windows Terminal, MongoDB, and others.
148</p>
149</div>
150
151</div>
152