Localize negative integers

This commit is contained in:
Victor Zverovich 2022-09-03 07:01:11 -07:00
parent aec3bb5d0a
commit 91ecb38a34
2 changed files with 7 additions and 17 deletions

View File

@ -2041,23 +2041,8 @@ inline auto write_int(OutputIt, basic_format_arg<buffer_context<Char>>,
template <typename OutputIt, typename UInt, typename Char>
auto write_int(OutputIt& out, UInt value, unsigned prefix,
const basic_format_specs<Char>& specs, locale_ref loc) -> bool {
auto buf = memory_buffer();
auto written = write_int(appender(buf), make_arg<buffer_context<Char>>(value),
specs, loc);
if (!written) {
auto grouping = digit_grouping<Char>(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<align::right>(
out, specs, size, size, [&](reserve_iterator<OutputIt> it) {
if (prefix != 0) {
char sign = static_cast<char>(prefix);
*it++ = static_cast<Char>(sign);
}
return copy_str<Char>(buf.data(), buf.data() + buf.size(), it);
});
auto grouping = digit_grouping<Char>(loc);
out = write_int(out, value, prefix, specs, grouping);
return true;
}
@ -4168,6 +4153,10 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> 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<Char>{context.out(), specs, context.locale()};
context.advance_to(visit_format_arg(f, arg));
return begin;

View File

@ -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