Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force UTF-8 handling for std::string in fmt

Tags:

c++

fmt

In my C++17 project, I have a std::string which is known to contain UTF-8 encoded data. Is there any way to force fmt to treat its data as UTF-8 such that this works as expected?

fmt::print("{:-^11}", "あいう");
// should print "----あいう----", currently prints "-あいう-"
like image 614
Frawstcrabs Avatar asked Mar 11 '26 11:03

Frawstcrabs


1 Answers

UTF-8 handling in {fmt} was recently improved and your example now works with the master branch:

#include <fmt/core.h>

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

prints

----あいう----
like image 82
vitaut Avatar answered Mar 12 '26 23:03

vitaut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!