Cleanup 'L' handling

This commit is contained in:
Victor Zverovich 2021-01-18 07:57:38 -08:00
parent b4b8917caf
commit 7fd535c6ae
2 changed files with 16 additions and 12 deletions

View File

@ -1413,7 +1413,6 @@ FMT_CONSTEXPR void handle_int_type_spec(char spec, Handler&& handler) {
#ifdef FMT_DEPRECATED_N_SPECIFIER #ifdef FMT_DEPRECATED_N_SPECIFIER
case 'n': case 'n':
#endif #endif
case 'L':
handler.on_num(); handler.on_num();
break; break;
case 'c': case 'c':
@ -1463,7 +1462,6 @@ FMT_CONSTEXPR float_specs parse_float_type_spec(
#ifdef FMT_DEPRECATED_N_SPECIFIER #ifdef FMT_DEPRECATED_N_SPECIFIER
case 'n': case 'n':
#endif #endif
case 'L':
result.locale = true; result.locale = true;
break; break;
default: default:
@ -1676,6 +1674,14 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {
return string_view(prefix, prefix_size); return string_view(prefix, prefix_size);
} }
void write_dec() {
auto num_digits = count_digits(abs_value);
out = write_int(
out, num_digits, get_prefix(), specs, [this, num_digits](iterator it) {
return format_decimal<Char>(it, abs_value, num_digits).end;
});
}
template <typename Int> template <typename Int>
FMT_CONSTEXPR int_writer(OutputIt output, locale_ref loc, Int value, FMT_CONSTEXPR int_writer(OutputIt output, locale_ref loc, Int value,
const basic_format_specs<Char>& s) const basic_format_specs<Char>& s)
@ -1697,11 +1703,7 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {
FMT_CONSTEXPR void on_dec() { FMT_CONSTEXPR void on_dec() {
if (specs.localized) return on_num(); if (specs.localized) return on_num();
auto num_digits = count_digits(abs_value); write_dec();
out = write_int(
out, num_digits, get_prefix(), specs, [this, num_digits](iterator it) {
return format_decimal<Char>(it, abs_value, num_digits).end;
});
} }
FMT_CONSTEXPR void on_hex() { FMT_CONSTEXPR void on_hex() {
@ -1746,9 +1748,9 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {
void on_num() { void on_num() {
std::string groups = grouping<Char>(locale); std::string groups = grouping<Char>(locale);
if (groups.empty()) return on_dec(); if (groups.empty()) return write_dec();
auto sep = thousands_sep<Char>(locale); auto sep = thousands_sep<Char>(locale);
if (!sep) return on_dec(); if (!sep) return write_dec();
int num_digits = count_digits(abs_value); int num_digits = count_digits(abs_value);
int size = num_digits, n = num_digits; int size = num_digits, n = num_digits;
std::string::const_iterator group = groups.cbegin(); std::string::const_iterator group = groups.cbegin();
@ -3179,7 +3181,8 @@ struct format_handler : detail::error_handler {
return parse_context.begin(); return parse_context.begin();
} }
auto specs = basic_format_specs<Char>(); auto specs = basic_format_specs<Char>();
if (begin + 1 < end && begin[1] == '}' && is_ascii_letter(*begin)) { if (begin + 1 < end && begin[1] == '}' && is_ascii_letter(*begin) &&
*begin != 'L') {
specs.type = static_cast<char>(*begin++); specs.type = static_cast<char>(*begin++);
} else { } else {
using parse_context_t = basic_format_parse_context<Char>; using parse_context_t = basic_format_parse_context<Char>;

View File

@ -1408,7 +1408,7 @@ TEST(FormatterTest, FormatLongDouble) {
} }
TEST(FormatterTest, FormatChar) { TEST(FormatterTest, FormatChar) {
const char types[] = "cbBdoxXL"; const char types[] = "cbBdoxX";
check_unknown_types('a', types, "char"); check_unknown_types('a', types, "char");
EXPECT_EQ("a", format("{0}", 'a')); EXPECT_EQ("a", format("{0}", 'a'));
EXPECT_EQ("z", format("{0:c}", 'z')); EXPECT_EQ("z", format("{0:c}", 'z'));
@ -1416,7 +1416,8 @@ TEST(FormatterTest, FormatChar) {
int n = 'x'; int n = 'x';
for (const char* type = types + 1; *type; ++type) { for (const char* type = types + 1; *type; ++type) {
std::string format_str = fmt::format("{{:{}}}", *type); std::string format_str = fmt::format("{{:{}}}", *type);
EXPECT_EQ(fmt::format(format_str, n), fmt::format(format_str, 'x')); EXPECT_EQ(fmt::format(format_str, n), fmt::format(format_str, 'x'))
<< format_str;
} }
EXPECT_EQ(fmt::format("{:02X}", n), fmt::format("{:02X}", 'x')); EXPECT_EQ(fmt::format("{:02X}", n), fmt::format("{:02X}", 'x'));
} }