繁体   English   中英

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

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

在我的 C++17 项目中,我有一个 std::string 已知包含 UTF-8 编码数据。 有什么方法可以强制 fmt 将其数据视为 UTF-8 以使其按预期工作?

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

{fmt} 中的 UTF-8 处理最近得到了改进,您的示例现在可以与master分支一起使用:

#include <fmt/core.h>

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

印刷

----あいう----

将字段宽度作为下一个参数传递并自己计算:

#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