fmtlib/fmt

▲ 2,087 stars today★ 25,778⑂ 3,066

A modern formatting library

25,778Star
3,066Fork
0Watch
0Issue
C++Language
-License
Created · last push · repository size 0 KB · default branch -

README

https://github.com/fmtlib/fmt/blob/HEAD/{fmt}

[image]( https://github.com/fmtlib/fmt/actions?query=workflow%3Alinux) [image]( https://github.com/fmtlib/fmt/actions?query=workflow%3Amacos) [image]( https://github.com/fmtlib/fmt/actions?query=workflow%3Awindows) [fmt is continuously fuzzed at oss-fuzz]( https://issues.oss-fuzz.com/issues?q=title:fmt%20cc:[email protected]) [OpenSSF Best Practices]( https://www.bestpractices.dev/projects/8880) [image]( https://securityscorecards.dev/viewer/?uri=github.com/fmtlib/fmt) [![Ask questions at StackOverflow with the tag fmt]( https://img.shields.io/badge/stackoverflow-fmt-blue.svg)](https://stackoverflow.com/questions/tagged/fmt) [![Support Ukraine]( https://img.shields.io/badge/Support-Ukraine-005BBB?labelColor=FFD500)](https://novaukraine.org/)

{fmt} is an open-source formatting library providing a fast and safe alternative to C stdio and C++ iostreams.

Documentation

Cheat Sheets

Q&A: ask questions on StackOverflow with the tag fmt.

Try {fmt} in Compiler Explorer.

Live demo by Demoshell

Features

arguments for localization std::format and C++23 std::print to Python\'s format shortness and round-trip guarantees using the Dragonbox algorithm implementation including the POSIX extension for positional arguments types implementations of (s)printf, iostreams, to_string and to_chars, see Speed tests and Converting a hundred million integers to strings per second configuration consisting of just three files, core.h, format.h and format-inl.h, and compiled code; see Compile time and code bloat tests and is continuously fuzzed be reported at compile time, automatic memory management prevents buffer overflow errors dependencies, permissive MIT license consistent output across platforms and support for older compilers -Wall -Wextra -pedantic FMT_HEADER_ONLY macro

See the documentation for more details.

Examples

Print to stdout (run)

``` c++

include

int main() { fmt::print("Hello, world!\n"); }


Format a string (run)

c++ std::string s = fmt::format("The answer is {}.", 42); // s == "The answer is 42."

Format a string using positional arguments
(run)

c++ std::string s = fmt::format("I'd rather be {1} than {0}.", "right", "happy"); // s == "I'd rather be happy than right."

Print dates and times (run)

c++

include

int main() { auto now = std::chrono::system_clock::now(); fmt::print("Date and time: {}\n", now); fmt::print("Time: {:%H:%M}\n", now); }


Output:

Date and time: 2023-12-26 19:10:31.557195597 Time: 19:10

Print a container (run)

c++

include

include

int main() { std::vector v = {1, 2, 3}; fmt::print("{}\n", v); }


Output:

[1, 2, 3]

Check a format string at compile time

c++ std::string s = fmt::format("{:d}", "I am not a number");

This gives a compile-time error in C++20 because d is an invalid
format specifier for a string.

Write a file from a single thread

c++

include

int main() { auto out = fmt::output_file("guide.txt"); out.print("Don't {}", "Panic"); }


This can be [up to 9 times faster than fprintf](
https://vitaut.net/posts/2020/optimal-file-buffer-size/).

Print with colors and text styles

c++

include

int main() { fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "Hello, {}!\n", "world"); fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) | fmt::emphasis::underline, "Olá, {}!\n", "Mundo"); fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic, "你好{}!\n", "世界"); } ```

Output on a modern terminal with Unicode support:

image

Performance

{fmt} can be tens of percent to 20–30 times faster than sprintf and iostreams, especially for numeric formatting. It minimizes dynamic memory allocations and can optionally [compile format strings]( https://fmt.dev/latest/api/#compile-api) into efficient formatting code.

See format-benchmark and dtoa-benchmark for benchmarks and methodology.

Time per double (smaller is better):

[![Time per double for floating-point formatting methods]( https://github.com/user-attachments/assets/3678bc4a-9405-489e-8ce1-ca702829cdaa)]( https://github.com/fmtlib/dtoa-benchmark)

ostringstream and sprintf are omitted because they are an order of magnitude slower than the other methods.

Compile time and code bloat

The script [bloat-test.py][test] from [format-benchmark][bench] measures the compile-time and code-size overhead each formatting method adds to application code. It generates 100 translation units and uses printf or its alternative five times in each to simulate a medium-sized project. Library and module build costs are excluded. Results on an Apple M5 Max running macOS 26.6.2 with Apple Clang 21.0.0 (clang-2100.1.1.101), taking the best of three runs, are shown in the following tables.

[test]: https://github.com/fmtlib/format-benchmark/blob/master/bloat-test.py [bench]: https://github.com/fmtlib/format-benchmark

Optimized build (-O3)

| Method | Compile time, s | Binary size, KiB | Stripped size, KiB | |--------------------|----------------:|-----------------:|-------------------:| | printf | 1.6 | 54 | 50 | | IOStreams | 25.5 | 98 | 84 | | fmt 12.2 (headers) | 5.1 | 54 | 50 | | fmt 12.2 (module) | 3.7 | 59 | 50 | | Boost Format 1.92 | 49.1 | 517 | 317 |

Using modular {fmt} reduces optimized application-code compile time by 27% without changing the reported stripped binary size.

Non-optimized build

| Method | Compile time, s | Binary size, KiB | Stripped size, KiB | |--------------------|----------------:|-----------------:|-------------------:| | printf | 1.6 | 54 | 50 | | IOStreams | 26.0 | 88 | 68 | | fmt 12.2 (headers) | 4.9 | 87 | 84 | | fmt 12.2 (module) | 3.2 | 77 | 68 | | Boost Format 1.92 | 35.7 | 741 | 431 |

libc, libc++, libfmt, and libfmt-module were linked as shared libraries to compare formatting function overhead only. Boost Format is header-only.

Projects using {fmt}

Notable users include:

[Find more projects using {fmt} on GitHub]( https://github.com/search?q=fmtlib&type=Code).

More Today's Trending projects

1

debpalash / VoiceStudio

Python★ 29,840⑂ 3,606▲ 2,776 stars
2

JustVugg / colibri

C★ 32,609⑂ 3,430▲ 2,173 stars
3

bilawalsidhu / gods-eye-view

JavaScript★ 33,945⑂ 6,772▲ 1,831 stars
4

alibaba / open-code-review

Go★ 26,516⑂ 1,906▲ 1,571 stars
5

ever-co / ever-gauzy

TypeScript★ 6,164⑂ 994▲ 1,130 stars
6

pacifio / atlas

Rust★ 4,440⑂ 274▲ 1,091 stars