【3rd_Party】format() 处理一些常见的格式化解决方案

fmt_crop

fmt_spec_chrono_crop

C++20 引入了新的 format() 函数,该函数以字符串形式返回参数的格式化表示。format() 使用

python 风格的格式化字符串,具有简洁的语法、类型安全,以及出色的性能。

format() 函数接受一个格式字符串和一个模板形参包作为参数:

template< class ... Args>
string format(const string_view fmt,  Args &&... args);

格式化字符串使用大括号 {} 作为格式化参数的占位符:

const int a{47};
// format("a is {}\n", a);
fmt::print("a is {}\n", a);

// 输出为:
// a is 47
// 还使用大括号作为格式说明符,例如:
format("Hex: {:x} Octal: {:o} Decimal {:d} \n", a, a, a);
fmt::print("Hex: {:x} Octal: {:o} Decimal: {:d} \n", a,a,a);

本示例展示了如何将 fmt::print() 函数,用于一些常见的字符串格式化解决方案。

本章是使用 Windows 10/11 上的 Microsoft Visual C++ 编译器预览版开发的。撰写本文时,这是

唯一完全支持 C++20 库的编译器。最终的实现可能在某些细节上有所不同。

*How to do it

使用 format() 函数来考虑一些常见的格式化解决方案:

  • 先从一些变量开始格式化:
const int inta {47};
const char * human {"Tian_"};
const std::string_view alien {"vulcans_"};
const double df_pi {std::numbers::pi}; // pi 常数在 <numbers> 头文件和 std::numbers 命名空间中。
  • 可以使用 cout 显示变量:
cout << "inta is " << inta << '\n'
<< "hello, " << human << '\n'
<< "All " << alien << " are welcome here\n"
<< "π is " << df_pi << '\n';

得到这样的输出

a is 47
hello, Tian_
All vulcans_ are welcome here
π is 3.14159

现在,来看看 format() 如何对它们进行处理:

cout << format("Hello {}\n", human);

这是 format() 函数的最简单形式,格式字符串有一个占位符 {} 和一个对应的变量 human。输

出结果为:

Hello earthlings
  • format() 函数返回一个字符串,我们使用 cout<< 来显示该字符串。

    format() 库的建议包括一个 print() 函数,使用与 format() 相同的参数,这就可以打印格式化的

    字符串:

print("Hello {}\n", cstr);

但 print() 没有进入 C++20,但有望加入 C++23。

我们可以用一个简单的函数,使用 vformat() 提供相同的功能:

template<typename... Args>
constexpr void print(const std::string_view str_fmt,
                     Args && ... args) {
    fputs(std::vformat(str_fmt,
                       std::make_format_args(args...)).c_str(),
          stdout);
}

推荐一个更简单的,就是使用 vcpkg 引入 fmt 第三方库,可以保证不出问题

也正是博主所使用。

  • 这个简单的单行函数提供了一个有用的 print() 函数,可以用它来代替 cout << format() 组合
// using namespace fmt;
print("Hello {}\n", human);

格式字符串还提供了位置选项:

print("Hello {} we are {}\n", human, alien);
// 可以在格式字符串中使用位置选项来改变参数的顺序:
print("Hello {1} we are {0}\n", human, alien);

注意,参数保持不变。只有大括号中的位置值发生了变化。位置索引是从零开始的,就像 [] 操作符一样

这个特性对于国际化 (或本地化) 非常有用,因为不同的语言在句子中,可以使用不同的顺序。

  • 数字有很多格式化选项:
print("π is {}\n", df_pi);

// e.g: 指定精度的位数
print("π is {:.5}\n", df_pi);

// 冒号字符“:”用于分隔位置索引和格式化参数:
print("inta is {1:}, π is {0:.5}\n", df_pi, inta);

// 若想要一个值占用一定的空间,可以这样指定字符的数量:
print("inta is [{:10}]\n", inta);
// 输出: inta is [        47]

// 可以向左或向右对齐: 默认右对齐
print("inta is [{:<10}]\n", inta); // 左对齐
print("inta is [{:>10}]\n", inta); // 右对齐

// 默认情况下,用空格字符填充,但可以进行修改:
print("inta is [{:*<10}]\n", inta); // 使用 * 填充
print("inta is [{:0>10}]\n", inta); // 使用 0 填充

// 还可以将值居中:
print("inta is [{:^10}]\n", inta);
print("inta is [{:_^10}]\n", inta);
// 输出
// inta is [    47    ]
// inta is [____47____]

// 可以将整数格式化为十六进制、八进制或默认的十进制表示形式:
print("{:>8}: [{:04x}]\n", "Hex", inta);
print("{:>8}: [{:4o}]\n", "Octal", inta);
print("{:>8}: [{:4d}]\n", "Decimal", inta);

/* 输出, 注意,这里使用右对齐来排列标签
     Hex: [002f]
   Octal: [  57]
 Decimal: [  47]
*/

// 大写十六进制使用大写 X:
print("{:>8}: [{:04X}]\n", "Hex", inta);
/*
     Hex: [002F]
*/

默认情况下,Windows 使用不常见的字符编码。最新版本可能默认为 UTF-16 或 UTF-8 BOM。旧版本可能默认为“代码页”1252,这是 ISO 8859-1 ASCII 标准的超集。Windows 系统默认为更常见的 UTF-8 (No BOM)。

默认情况下,Windows 不会显示标准 UTF-8 π 字符。要使 Windows 兼容 UTF-8 编码 (以及其他编码),请在测试时使用编译器开关/utf-8 并在命令行上发出命令 chcp 65001。现在,你也可以得到 π 并使用它。

posted @ 2023-09-20 14:58  Koshkaaa  阅读(34)  评论(0编辑  收藏  举报