From 46c90cac1c34f8f449038965ed2e71db1a203ab4 Mon Sep 17 00:00:00 2001 From: Jin S Date: Mon, 11 Dec 2023 15:47:07 -0500 Subject: [PATCH] fix build --- include/fmt/format.h | 8 ++++---- test/format-test.cc | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index c3f74949..c15605df 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2130,7 +2130,7 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, switch (specs.type) { case presentation_type::none: case presentation_type::dec: { - num_digits = count_digits(value); + num_digits = std::min(count_digits(value), 40); format_decimal(digits, value, num_digits); break; } @@ -2139,7 +2139,7 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, bool upper = specs.type == presentation_type::hex_upper; if (specs.alt) prefix_append(prefix, unsigned(upper ? 'X' : 'x') << 8 | '0'); - num_digits = count_digits<4>(value); + num_digits = std::min(count_digits<4>(value), 40); format_uint<4>(digits, value, num_digits, upper); break; } @@ -2148,12 +2148,12 @@ auto write_int(OutputIt out, UInt value, unsigned prefix, bool upper = specs.type == presentation_type::bin_upper; if (specs.alt) prefix_append(prefix, unsigned(upper ? 'B' : 'b') << 8 | '0'); - num_digits = count_digits<1>(value); + num_digits = std::min(count_digits<1>(value), 40); format_uint<1>(digits, value, num_digits); break; } case presentation_type::oct: { - num_digits = count_digits<3>(value); + num_digits = std::min(count_digits<3>(value), 40); // Octal prefix '0' is counted as a digit, so only add it if precision // is not greater than the number of digits. if (specs.alt && specs.precision <= num_digits && value != 0) diff --git a/test/format-test.cc b/test/format-test.cc index ee1ff999..e967ed32 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1335,9 +1335,6 @@ TEST(format_test, format_oct) { TEST(format_test, format_int_locale) { EXPECT_EQ("1234", fmt::format("{:L}", 1234)); - EXPECT_EQ("7,5bc,d15", fmt::format(std::locale("en_US.UTF-8"), "{:Lx}", 123456789)); - EXPECT_EQ("-0b111,010,110,111,100,110,100,010,101", fmt::format(std::locale("en_US.UTF-8"), "{:#Lb}", -123456789)); - EXPECT_EQ(" 30,071", fmt::format(std::locale("en_US.UTF-8"), "{:10Lo}", 12345)); } TEST(format_test, format_float) { @@ -2300,6 +2297,14 @@ TEST(format_test, format_named_arg_with_locale) { "42"); } +TEST(format_test, format_locale) { + auto loc = + std::locale({}, new fmt::format_facet(",")); + EXPECT_EQ("7,5bc,d15", fmt::format(loc, "{:Lx}", 123456789)); + EXPECT_EQ("-0b111,010,110,111,100,110,100,010,101", fmt::format(loc, "{:#Lb}", -123456789)); + EXPECT_EQ(" 30,071", fmt::format(loc, "{:10Lo}", 12345)); +} + #endif // FMT_STATIC_THOUSANDS_SEPARATOR struct convertible_to_nonconst_cstring {