From 358f5a7e504520f77e8b06e92d95ef147dcee953 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 11 May 2022 06:34:51 -0700 Subject: [PATCH] Make precision computation consistent with width --- include/fmt/format.h | 15 ++++++++++----- test/format-test.cc | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index d35207c8e230..9951d1bb5b0e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -680,8 +680,8 @@ FMT_CONSTEXPR inline size_t compute_width(string_view s) { } inline auto compute_width(basic_string_view s) -> size_t { - return compute_width(basic_string_view( - reinterpret_cast(s.data()), s.size())); + return compute_width( + string_view(reinterpret_cast(s.data()), s.size())); } template @@ -691,9 +691,8 @@ inline auto code_point_index(basic_string_view s, size_t n) -> size_t { } // Calculates the index of the nth code point in a UTF-8 string. -inline auto code_point_index(basic_string_view s, size_t n) - -> size_t { - const char8_type* data = s.data(); +inline auto code_point_index(string_view s, size_t n) -> size_t { + const char* data = s.data(); size_t num_code_points = 0; for (size_t i = 0, size = s.size(); i != size; ++i) { if ((data[i] & 0xc0) != 0x80 && ++num_code_points > n) return i; @@ -701,6 +700,12 @@ inline auto code_point_index(basic_string_view s, size_t n) return s.size(); } +inline auto code_point_index(basic_string_view s, size_t n) + -> size_t { + return code_point_index( + string_view(reinterpret_cast(s.data()), s.size()), n); +} + #ifndef FMT_USE_FLOAT128 # ifdef __SIZEOF_FLOAT128__ # define FMT_USE_FLOAT128 1 diff --git a/test/format-test.cc b/test/format-test.cc index 1dc48ee876bd..0743b60c0727 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1035,6 +1035,7 @@ TEST(format_test, precision) { format_error, "number is too big"); EXPECT_EQ("st", fmt::format("{0:.2}", "str")); + EXPECT_EQ("вожык", fmt::format("{0:.5}", "вожыкі")); } TEST(format_test, runtime_precision) {