From 91ecb38a34e5323193417265c74f0fe8d4f8b4f6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 3 Sep 2022 07:01:11 -0700 Subject: [PATCH] Localize negative integers --- include/fmt/format.h | 23 ++++++----------------- test/format-test.cc | 1 + 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/include/fmt/format.h b/include/fmt/format.h index dc5108f9..32ae14dc 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2041,23 +2041,8 @@ inline auto write_int(OutputIt, basic_format_arg>, template auto write_int(OutputIt& out, UInt value, unsigned prefix, const basic_format_specs& specs, locale_ref loc) -> bool { - auto buf = memory_buffer(); - auto written = write_int(appender(buf), make_arg>(value), - specs, loc); - if (!written) { - auto grouping = digit_grouping(loc); - out = write_int(out, value, prefix, specs, grouping); - return true; - } - size_t size = to_unsigned((prefix != 0 ? 1 : 0) + buf.size()); - out = write_padded( - out, specs, size, size, [&](reserve_iterator it) { - if (prefix != 0) { - char sign = static_cast(prefix); - *it++ = static_cast(sign); - } - return copy_str(buf.data(), buf.data() + buf.size(), it); - }); + auto grouping = digit_grouping(loc); + out = write_int(out, value, prefix, specs, grouping); return true; } @@ -4168,6 +4153,10 @@ void vformat_to(buffer& buf, basic_string_view fmt, begin = parse_format_specs(begin, end, handler); if (begin == end || *begin != '}') on_error("missing '}' in format string"); + if (specs.localized && arg.is_integral() && + write_int(context.out(), arg, specs, context.locale())) { + return begin; + } auto f = arg_formatter{context.out(), specs, context.locale()}; context.advance_to(visit_format_arg(f, arg)); return begin; diff --git a/test/format-test.cc b/test/format-test.cc index 08dadfe3..ee82bd9e 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -2342,6 +2342,7 @@ void format_facet::do_put(fmt::appender out, TEST(format_test, format_facet) { auto loc = std::locale(std::locale(), new format_facet()); EXPECT_EQ(fmt::format(loc, "{:L}", 42), "[42]"); + EXPECT_EQ(fmt::format(loc, "{:L}", -42), "[-42]"); } #endif // FMT_STATIC_THOUSANDS_SEPARATOR