fmt/doc/index.md

152 lines
4.6 KiB
Markdown
Raw Normal View History

2024-06-03 03:46:16 +00:00
---
hide:
- navigation
- toc
---
# A modern formatting library
<div class="features-container">
<div class="feature">
<h2>Safety</h2>
<p>
2024-06-04 22:26:38 +00:00
Inspired by Python's formatting facility, {fmt} provides a safe replacement
for the <code>printf</code> family of functions. Errors in format strings,
which are a common source of vulnerabilities in C, are <b>reported at
compile time</b>. For example:
2024-06-03 03:46:16 +00:00
<pre><code class="language-cpp"
2024-06-03 13:25:14 +00:00
>fmt::format("{:d}", "I am not a number");</code></pre>
2024-06-03 03:46:16 +00:00
will give a compile-time error because <code>d</code> is not a valid
format specifier for strings. APIs like <a href="api/#format">
<code>fmt::format</code></a> <b>prevent buffer overflow errors</b> via
automatic memory management.
</p>
2024-06-15 16:07:04 +00:00
<a href="api#compile-time-checks">→ Learn more</a>
2024-06-03 03:46:16 +00:00
</div>
<div class="feature">
<h2>Extensibility</h2>
<p>
2024-06-04 22:26:38 +00:00
Formatting of most <b>standard types</b>, including all containers, dates,
and times is <b>supported out-of-the-box</b>. For example:
2024-06-03 03:46:16 +00:00
<pre><code class="language-cpp"
>fmt::print("{}", std::vector{1, 2, 3});</code></pre>
prints the vector in a JSON-like format:
<pre><code>[1, 2, 3]</code></pre>
You can <b>make your own types formattable</b> and even make compile-time
checks work for them.
</p>
<a href="api#udt">→ Learn more</a>
</div>
<div class="feature">
<h2>Performance</h2>
<p>
{fmt} can be anywhere from <b>tens of percent to 20-30 times faster</b> than
2024-06-04 22:26:38 +00:00
iostreams and <code>sprintf</code>, especially for numeric formatting.
2024-06-03 03:46:16 +00:00
2024-06-05 00:01:04 +00:00
<a href="https://github.com/fmtlib/fmt?tab=readme-ov-file#benchmarks">
2024-06-04 22:26:38 +00:00
<img src="perf.svg">
2024-06-05 00:01:04 +00:00
</a>
2024-06-03 03:46:16 +00:00
2024-06-04 22:26:38 +00:00
The library <b>minimizes dynamic memory allocations</b> and can optionally
<a href="api#compile-api">compile format strings</a> to optimal code.
2024-06-03 03:46:16 +00:00
</p>
</div>
<div class="feature">
<h2>Unicode support</h2>
<p>
{fmt} provides <b>portable Unicode support</b> on major operating systems
2024-06-03 13:25:14 +00:00
with UTF-8 and <code>char</code> strings. For example:
2024-06-03 03:46:16 +00:00
<pre><code class="language-cpp"
>fmt::print("Слава Україні!");</code></pre>
2024-06-04 22:26:38 +00:00
will be printed correctly on Linux, macOS, and even Windows console,
2024-06-05 00:01:04 +00:00
irrespective of the codepages.
2024-06-03 03:46:16 +00:00
</p>
<p>
2024-06-04 22:26:38 +00:00
The default is <b>locale-independent</b>, but you can opt into localized
2024-06-05 00:01:04 +00:00
formatting and {fmt} makes it work with Unicode, addressing issues in the
standard libary.
2024-06-03 03:46:16 +00:00
</p>
</div>
<div class="feature">
<h2>Fast compilation</h2>
<p>
The library makes extensive use of <b>type erasure</b> to achieve fast
2024-06-09 18:22:57 +00:00
compilation. <code>fmt/base.h</code> provides a subset of the API with
<b>minimal include dependencies</b> and enough functionality to replace
all uses of <code>*printf</code>.
2024-06-03 03:46:16 +00:00
</p>
<p>
Code using {fmt} is usually several times faster to compile than the
2024-06-04 22:26:38 +00:00
equivalent iostreams code, and while <code>printf</code> compiles faster
still, the gap is narrowing.
2024-06-03 03:46:16 +00:00
</p>
2024-06-05 00:01:04 +00:00
<a href=
"https://github.com/fmtlib/fmt?tab=readme-ov-file#compile-time-and-code-bloat">
2024-06-03 03:46:16 +00:00
→ Learn more</a>
</div>
<div class="feature">
<h2>Small binary footprint</h2>
<p>
2024-06-04 22:26:38 +00:00
Type erasure is also used to prevent template bloat, resulting in <b>compact
2024-06-03 03:46:16 +00:00
per-call binary code</b>. For example, a call to <code>fmt::print</code> with
2024-06-15 16:07:04 +00:00
a single argument is just <a href="https://godbolt.org/g/TZU4KF">a few
instructions</a>, comparable to <code>printf</code> despite adding
2024-06-05 02:46:45 +00:00
runtime safety, and much smaller than the equivalent iostreams code.
2024-06-03 03:46:16 +00:00
</p>
<p>
The library itself has small binary footprint and some components such as
floating-point formatting can be disabled to make it even smaller for
2024-06-04 22:26:38 +00:00
resource-constrained devices.
2024-06-03 03:46:16 +00:00
</p>
</div>
<div class="feature">
<h2>Portability</h2>
<p>
2024-06-04 22:26:38 +00:00
{fmt} has a <b>small self-contained codebase</b> with the core consisting of
2024-06-05 00:01:04 +00:00
just three headers and no external dependencies.
2024-06-03 03:46:16 +00:00
</p>
<p>
2024-06-04 22:26:38 +00:00
The library is highly portable and requires only a minimal <b>subset of
C++11</b> features which are available in GCC 4.8, Clang 3.4, MSVC 19.0
(2015) and later. Newer compiler and standard library features are used
if available, and enable additional functionality.
2024-06-03 03:46:16 +00:00
</p>
<p>
2024-06-04 22:26:38 +00:00
Where possible, the output of formatting functions is <b>consistent across
platforms</b>.
2024-06-03 03:46:16 +00:00
</p>
</p>
</div>
<div class="feature">
<h2>Open source</h2>
<p>
2024-06-04 22:26:38 +00:00
{fmt} is in the top hundred open-source C++ libraries on GitHub and has
<a href="https://github.com/fmtlib/fmt/graphs/contributors">hundreds of
all-time contributors</a>.
2024-06-03 03:46:16 +00:00
</p>
<p>
2024-06-05 00:01:04 +00:00
The library is distributed under a permissive MIT
<a href="https://github.com/fmtlib/fmt#license">license</a> and is
2024-06-05 13:19:40 +00:00
<b>relied upon by many open-source projects</b>, including Blender, PyTorch,
Apple's FoundationDB, Windows Terminal, MongoDB, and others.
2024-06-03 03:46:16 +00:00
</p>
</div>
</div>