简体   繁体   English

强制 UTF-8 处理 fmt 中的 std::string

[英]Force UTF-8 handling for std::string in fmt

In my C++17 project, I have a std::string which is known to contain UTF-8 encoded data.在我的 C++17 项目中,我有一个 std::string 已知包含 UTF-8 编码数据。 Is there any way to force fmt to treat its data as UTF-8 such that this works as expected?有什么方法可以强制 fmt 将其数据视为 UTF-8 以使其按预期工作?

fmt::print("{:-^11}", "あいう");
// should print "----あいう----", currently prints "-あいう-"

UTF-8 handling in {fmt} was recently improved and your example now works with the master branch: {fmt} 中的 UTF-8 处理最近得到了改进,您的示例现在可以与master分支一起使用:

#include <fmt/core.h>

int main() {
  fmt::print("{:-^11}", "あいう");
}

prints印刷

----あいう----

Pass field width as the next argument and calculate it yourself:将字段宽度作为下一个参数传递并自己计算:

#include <fmt/format.h>
#include <cstring>
int main() {
    fmt::print("{:-^{}}", "あいう", 8 + std::strlen("あいう"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM