Skip to content

Commit

Permalink
Align hex floats right as default (#2317)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liedtke authored May 28, 2021
1 parent ece4b4b commit 98b9ff4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 8 additions & 7 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1308,14 +1308,14 @@ constexpr OutputIt write_padded(OutputIt out,
return write_padded<align>(out, specs, size, size, f);
}

template <typename Char, typename OutputIt>
template <align::type align = align::left, typename Char, typename OutputIt>
FMT_CONSTEXPR OutputIt write_bytes(OutputIt out, string_view bytes,
const basic_format_specs<Char>& specs) {
return write_padded(out, specs, bytes.size(),
[bytes](reserve_iterator<OutputIt> it) {
const char* data = bytes.data();
return copy_str<Char>(data, data + bytes.size(), it);
});
return write_padded<align>(
out, specs, bytes.size(), [bytes](reserve_iterator<OutputIt> it) {
const char* data = bytes.data();
return copy_str<Char>(data, data + bytes.size(), it);
});
}

template <typename Char, typename OutputIt, typename UIntPtr>
Expand Down Expand Up @@ -1801,7 +1801,8 @@ OutputIt write(OutputIt out, T value, basic_format_specs<Char> specs,
if (fspecs.format == float_format::hex) {
if (fspecs.sign) buffer.push_back(data::signs[fspecs.sign]);
snprintf_float(promote_float(value), specs.precision, fspecs, buffer);
return write_bytes(out, {buffer.data(), buffer.size()}, specs);
return write_bytes<align::right>(out, {buffer.data(), buffer.size()},
specs);
}
int precision = specs.precision >= 0 || !specs.type ? specs.precision : 6;
if (fspecs.format == float_format::exp) {
Expand Down
2 changes: 2 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,8 @@ TEST(format_test, format_double) {
EXPECT_EQ("392.650000", fmt::format("{:f}", 392.65));
EXPECT_EQ("392.650000", fmt::format("{:F}", 392.65));
EXPECT_EQ("42", fmt::format("{:L}", 42.0));
EXPECT_EQ(" 0x1.0cccccccccccdp+2", fmt::format("{:24a}", 4.2));
EXPECT_EQ("0x1.0cccccccccccdp+2 ", fmt::format("{:<24a}", 4.2));
char buffer[buffer_size];
safe_sprintf(buffer, "%e", 392.65);
EXPECT_EQ(buffer, fmt::format("{0:e}", 392.65));
Expand Down

0 comments on commit 98b9ff4

Please sign in to comment.