Fix precision with non-ascii characters

This commit is contained in:
Shawn Zhong 2023-02-07 04:29:55 -06:00
parent 05e3a9233a
commit f5cd979adf
2 changed files with 4 additions and 1 deletions

View File

@ -773,7 +773,8 @@ inline auto code_point_index(string_view s, size_t n) -> size_t {
for (size_t i = 0, size = s.size(); i != size; ++i) {
if ((data[i] & 0xc0) != 0x80 && ++num_code_points > n) return i;
}
return s.size();
size_t size = s.size();
return n < size ? n : size;
}
inline auto code_point_index(basic_string_view<char8_type> s, size_t n)

View File

@ -155,6 +155,8 @@ TEST(xchar_test, format_utf8_precision) {
EXPECT_EQ(fmt::detail::compute_width(result), 4);
EXPECT_EQ(result.size(), 5);
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5)));
EXPECT_EQ(fmt::format("{:.0}", "\xad").size(), 0);
}
TEST(xchar_test, format_to) {