Skip to content

Commit

Permalink
Cleanup 'L' handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 18, 2021
1 parent b4b8917 commit 7fd535c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
23 changes: 13 additions & 10 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,6 @@ FMT_CONSTEXPR void handle_int_type_spec(char spec, Handler&& handler) {
#ifdef FMT_DEPRECATED_N_SPECIFIER
case 'n':
#endif
case 'L':
handler.on_num();
break;
case 'c':
Expand Down Expand Up @@ -1463,7 +1462,6 @@ FMT_CONSTEXPR float_specs parse_float_type_spec(
#ifdef FMT_DEPRECATED_N_SPECIFIER
case 'n':
#endif
case 'L':
result.locale = true;
break;
default:
Expand Down Expand Up @@ -1676,6 +1674,14 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {
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>
FMT_CONSTEXPR int_writer(OutputIt output, locale_ref loc, Int value,
const basic_format_specs<Char>& s)
Expand All @@ -1697,11 +1703,7 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {

FMT_CONSTEXPR void on_dec() {
if (specs.localized) return on_num();
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;
});
write_dec();
}

FMT_CONSTEXPR void on_hex() {
Expand Down Expand Up @@ -1746,9 +1748,9 @@ template <typename OutputIt, typename Char, typename UInt> struct int_writer {

void on_num() {
std::string groups = grouping<Char>(locale);
if (groups.empty()) return on_dec();
if (groups.empty()) return write_dec();
auto sep = thousands_sep<Char>(locale);
if (!sep) return on_dec();
if (!sep) return write_dec();
int num_digits = count_digits(abs_value);
int size = num_digits, n = num_digits;
std::string::const_iterator group = groups.cbegin();
Expand Down Expand Up @@ -3179,7 +3181,8 @@ struct format_handler : detail::error_handler {
return parse_context.begin();
}
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++);
} else {
using parse_context_t = basic_format_parse_context<Char>;
Expand Down
5 changes: 3 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1408,15 +1408,16 @@ TEST(FormatterTest, FormatLongDouble) {
}

TEST(FormatterTest, FormatChar) {
const char types[] = "cbBdoxXL";
const char types[] = "cbBdoxX";
check_unknown_types('a', types, "char");
EXPECT_EQ("a", format("{0}", 'a'));
EXPECT_EQ("z", format("{0:c}", 'z'));
EXPECT_EQ(L"a", format(L"{0}", 'a'));
int n = 'x';
for (const char* type = types + 1; *type; ++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'));
}
Expand Down

0 comments on commit 7fd535c

Please sign in to comment.