mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-14 23:11:05 +00:00
Use '0' fill with numeric align for consistency with std::format
This commit is contained in:
parent
60d85d598c
commit
a71bc9c825
@ -1409,13 +1409,11 @@ inline OutputIt write_padded(OutputIt out,
|
|||||||
template <typename Char> struct write_int_data {
|
template <typename Char> struct write_int_data {
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
std::size_t padding;
|
std::size_t padding;
|
||||||
Char fill;
|
|
||||||
|
|
||||||
write_int_data(int num_digits, string_view prefix,
|
write_int_data(int num_digits, string_view prefix,
|
||||||
basic_format_specs<Char>& specs)
|
basic_format_specs<Char>& specs)
|
||||||
: size(prefix.size() + to_unsigned(num_digits)),
|
: size(prefix.size() + to_unsigned(num_digits)),
|
||||||
padding(0),
|
padding(0) {
|
||||||
fill(specs.fill[0]) {
|
|
||||||
if (specs.align == align::numeric) {
|
if (specs.align == align::numeric) {
|
||||||
auto width = to_unsigned(specs.width);
|
auto width = to_unsigned(specs.width);
|
||||||
if (width > size) {
|
if (width > size) {
|
||||||
@ -1425,7 +1423,6 @@ template <typename Char> struct write_int_data {
|
|||||||
} else if (specs.precision > num_digits) {
|
} else if (specs.precision > num_digits) {
|
||||||
size = prefix.size() + to_unsigned(specs.precision);
|
size = prefix.size() + to_unsigned(specs.precision);
|
||||||
padding = to_unsigned(specs.precision - num_digits);
|
padding = to_unsigned(specs.precision - num_digits);
|
||||||
fill = static_cast<Char>('0');
|
|
||||||
}
|
}
|
||||||
if (specs.align == align::none) specs.align = align::right;
|
if (specs.align == align::none) specs.align = align::right;
|
||||||
}
|
}
|
||||||
@ -1442,7 +1439,7 @@ OutputIt write_int(OutputIt out, int num_digits, string_view prefix,
|
|||||||
return write_padded(out, specs, data.size, [=](iterator it) {
|
return write_padded(out, specs, data.size, [=](iterator it) {
|
||||||
if (prefix.size() != 0)
|
if (prefix.size() != 0)
|
||||||
it = copy_str<Char>(prefix.begin(), prefix.end(), it);
|
it = copy_str<Char>(prefix.begin(), prefix.end(), it);
|
||||||
it = std::fill_n(it, data.padding, data.fill);
|
it = std::fill_n(it, data.padding, static_cast<Char>('0'));
|
||||||
return f(it);
|
return f(it);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -748,35 +748,7 @@ TEST(FormatterTest, RightAlign) {
|
|||||||
|
|
||||||
#if FMT_NUMERIC_ALIGN
|
#if FMT_NUMERIC_ALIGN
|
||||||
TEST(FormatterTest, NumericAlign) {
|
TEST(FormatterTest, NumericAlign) {
|
||||||
EXPECT_EQ(" 42", format("{0:=4}", 42));
|
EXPECT_EQ("0042", format("{0:=4}", 42));
|
||||||
EXPECT_EQ("+ 42", format("{0:=+4}", 42));
|
|
||||||
EXPECT_EQ(" 42", format("{0:=4o}", 042));
|
|
||||||
EXPECT_EQ("+ 42", format("{0:=+4o}", 042));
|
|
||||||
EXPECT_EQ(" 42", format("{0:=4x}", 0x42));
|
|
||||||
EXPECT_EQ("+ 42", format("{0:=+4x}", 0x42));
|
|
||||||
EXPECT_EQ("- 42", format("{0:=5}", -42));
|
|
||||||
EXPECT_EQ(" 42", format("{0:=5}", 42u));
|
|
||||||
EXPECT_EQ("- 42", format("{0:=5}", -42l));
|
|
||||||
EXPECT_EQ(" 42", format("{0:=5}", 42ul));
|
|
||||||
EXPECT_EQ("- 42", format("{0:=5}", -42ll));
|
|
||||||
EXPECT_EQ(" 42", format("{0:=5}", 42ull));
|
|
||||||
EXPECT_EQ("- 42.0", format("{0:=7}", -42.0));
|
|
||||||
EXPECT_EQ("- 42.0", format("{0:=7}", -42.0l));
|
|
||||||
EXPECT_THROW_MSG(format("{0:=5", 'c'), format_error,
|
|
||||||
"missing '}' in format string");
|
|
||||||
EXPECT_THROW_MSG(format("{0:=5}", 'c'), format_error,
|
|
||||||
"invalid format specifier for char");
|
|
||||||
EXPECT_THROW_MSG(format("{0:=5}", "abc"), format_error,
|
|
||||||
"format specifier requires numeric argument");
|
|
||||||
EXPECT_THROW_MSG(format("{0:=8}", reinterpret_cast<void*>(0xface)),
|
|
||||||
format_error, "format specifier requires numeric argument");
|
|
||||||
EXPECT_EQ(" 1.0", fmt::format("{:= }", 1.0));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(FormatToTest, FormatToNonbackInsertIteratorWithSignAndNumericAlignment) {
|
|
||||||
char buffer[16] = {};
|
|
||||||
fmt::format_to(fmt::internal::make_checked(buffer, 16), "{: =+}", 42.0);
|
|
||||||
EXPECT_STREQ("+42.0", buffer);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user