fmtlib/fmt
GitHub: fmtlib/fmt
{fmt} 是一个开源的 C++ 格式化库,提供了比标准库更快、更安全且可扩展的文本处理方案。
Stars: 23301 | Forks: 2834
[](https://github.com/fmtlib/fmt/actions?query=workflow%3Alinux)
[](https://github.com/fmtlib/fmt/actions?query=workflow%3Amacos)
[](https://github.com/fmtlib/fmt/actions?query=workflow%3Awindows)
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?\%0Acolspec=ID%20Type%20Component%20Status%20Proj%20Reported%20Owner%20\%0ASummary&q=proj%3Dfmt&can=1)
[](https://www.bestpractices.dev/projects/8880)
[](https://securityscorecards.dev/viewer/?uri=github.com/fmtlib/fmt)
[](https://stackoverflow.com/questions/tagged/fmt)
**{fmt}** 是一个开源格式化库,提供了快速且安全的 C stdio 和 C++ iostreams 替代方案。
[文档](https://fmt.dev)
[速查表](https://hackingcpp.com/cpp/libs/fmt.html)
问答:在 [StackOverflow 上使用 fmt 标签](https://stackoverflow.com/questions/tagged/fmt) 提问。
在 [Compiler Explorer](https://godbolt.org/z/8Mx1EW73v) 中试用 {fmt}。
# 功能特性
- 简单的 [格式化 API](https://fmt.dev/latest/api/),支持用于本地化的位置参数
- [C++20 std::format](https://en.cppreference.com/w/cpp/utility/format) 和 [C++23 std::print](https://en.cppreference.com/w/cpp/io/print) 的实现
- 类似于 Python [format](https://docs.python.org/3/library/stdtypes.html#str.format) 的 [格式字符串语法](https://fmt.dev/latest/syntax/)
- 快速的 IEEE 754 浮点数格式化器,使用 [Dragonbox](https://github.com/jk-jeon/dragonbox) 算法保证正确舍入、最短长度和往返一致性
- 可移植的 Unicode 支持
- 安全的 [printf 实现](https://fmt.dev/latest/api/#printf-formatting),包括用于位置参数的 POSIX 扩展
- 可扩展性:[支持用户定义类型](https://fmt.dev/latest/api/#formatting-user-defined-types)
- 高性能:比常见的标准库 `(s)printf`、iostreams、`to_string` 和 `to_chars` 实现更快,参见 [速度测试](#speed-tests) 和 [每秒转换一亿个整数为字符串](http://www.zverovich.net/2020/06/13/fast-int-to-string-revisited.html)
- 较小的代码体积:最小配置的源代码仅包含三个文件 `base.h`、`format.h` 和 `format-inl.h`,且编译后的代码也很小;参见 [编译时间和代码膨胀](#compile-time-and-code-bloat)
- 可靠性:该库拥有广泛的 [测试](https://github.com/fmtlib/fmt/tree/master/test) 并 [持续进行模糊测试](https://bugs.chromium.org/p/oss-fuzz/issues/list?colspec=ID%20Type%20Component%20Status%20Proj%20Reported%20Owner%20Summary&q=proj%3Dfmt&can=1)
- 安全性:该库是完全类型安全的,格式字符串中的错误可以在编译时报告,自动内存管理防止缓冲区溢出错误
- 易用性:小巧独立的代码库,无外部依赖,宽松的 MIT [许可证](https://github.com/fmtlib/fmt/blob/master/LICENSE)
- [可移植性](https://fmt.dev/latest/#portability),跨平台输出一致,并支持较旧的编译器
- 整洁且无警告的代码库,即使在 `-Wall -Wextra -pedantic` 等高警告级别下
- 默认情况下区域设置独立
- 可选的 header-only 配置,通过 `FMT_HEADER_ONLY` 宏启用
有关更多详细信息,请参阅 [文档](https://fmt.dev)。
# 示例
**打印到 stdout** ([运行](https://godbolt.org/z/Tevcjh))
```
#include 标签:Bash脚本, C++, C++20, fmt, iostream替代, std::format, 嵌入式开发, 开源库, 搜索引擎爬虫, 数据擦除, 文本处理, 格式化库, 类型安全, 系统编程, 编码规范