fmt README

The project's README from github.com/fmtlib/fmt. As one markdown file: README.md.

{fmt}

image image image fmt is continuously fuzzed at oss-fuzz OpenSSF Best Practices image Ask questions at StackOverflow with the tag fmt Support Ukraine

{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

See the documentation for more details.

Examples

Print to stdout (run)

#include <fmt/core.h>

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

Format a string (run)

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

Format a string using positional arguments (run)

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)

#include <fmt/chrono.h>

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)

#include <vector>
#include <fmt/ranges.h>

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

Output:

[1, 2, 3]

Check a format string at compile time

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

#include <fmt/os.h>

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

This can be up to 9 times faster than fprintf.

Print with colors and text styles

#include <fmt/color.h>

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 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

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 from format-benchmark 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.

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.

fmt (fmtlib/fmt)

Searching Database…

25.9K ·GitHub
这是什么?{fmt} 是一个开源的 C++ 格式化库——快速又安全,可以替代 C 的 stdio 和 C++ 的 iostream,格式字符串语法和 Python 的很相似。
它是一个独立工具,还是要放进我代码里的东西?是一个库:你需要编译并链接到 C++ 程序中。最简单的配置只要三个文件(core.h、format.h、format-inl.h),或者定义 FMT_HEADER_ONLY 以纯头文件方式使用。
不用搭项目,我能先体验一下吗?README 里链接了一个 "Try {fmt} in Compiler Explorer" 在线页面,示例可以直接在浏览器里运行;文档网站则介绍了如何集成到真实的构建中。
引入它会不会拖一堆依赖,或者要求特别新的编译器?不会——它的代码小巧自包含,没有任何外部依赖,而且注重可移植性,支持较老的编译器。
格式化输出会不会因为区域设置或操作系统不同而不一样?不会——默认就与区域设置无关,而且这个库力求跨平台输出一致,同样的格式字符串在任何地方都会生成相同的文本。
它是经过实战检验的,还是个人玩具项目?经过实战检验:Apple 的 FoundationDB、Blizzard Battle.net、Ceph、ClickHouse、MariaDB、MongoDB、PyTorch、spdlog 和 Windows Terminal 都在使用它,而且它带有完善的测试套件和持续的模糊测试。
我能在商业项目里用吗?可以——它采用 MIT 许可证,README 自己也说这个许可证很宽松。
格式字符串写错了真的会让编译失败吗?会——在 C++20 模式下,格式说明符会在编译期对照参数类型进行检查;比如用 'd' 去格式化一个字符串,会直接报编译错误,而不是留到运行时才出问题。
它的浮点数格式化有什么特别之处?它基于 Dragonbox 算法,对 IEEE 754 浮点数保证正确舍入、输出最短以及可往返——打印出来的数字再转换回去,得到的还是同一个值。
如果要往文件里写大量数据,它合适吗?README 展示了单线程写文件的例子,并指出速度最高可以比 fprintf 快 9 倍。
Creator
fmtlib
Size
17.3 MB
Stars
25.9K
Age
13y
Commits · 90 Days