Skip to content

Commit

Permalink
Ignore 0 character with align
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnZhong authored and vitaut committed Dec 24, 2022
1 parent 840ec85 commit a585571
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,9 @@ template <typename Char> class specs_setter {
FMT_CONSTEXPR void on_localized() { specs_.localized = true; }

FMT_CONSTEXPR void on_zero() {
if (specs_.align == align::none) specs_.align = align::numeric;
// If the 0 character and an align option both appear, the 0 character is ignored.
if (specs_.align != align::none) return;
specs_.align = align::numeric;
specs_.fill[0] = Char('0');
}

Expand Down
12 changes: 11 additions & 1 deletion test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,16 @@ TEST(format_test, zero_flag) {
format_error, "format specifier requires numeric argument");
}

TEST(format_test, zero_flag_and_align) {
// If the 0 character and an align option both appear, the 0 character is ignored.
EXPECT_EQ("42 ", fmt::format("{0:<05}", 42));
EXPECT_EQ("-42 ", fmt::format("{0:<05}", -42));
EXPECT_EQ(" 42 ", fmt::format("{0:^05}", 42));
EXPECT_EQ(" -42 ", fmt::format("{0:^05}", -42));
EXPECT_EQ(" 42", fmt::format("{0:>05}", 42));
EXPECT_EQ(" -42", fmt::format("{0:>05}", -42));
}

TEST(format_test, width) {
char format_str[buffer_size];
safe_sprintf(format_str, "{0:%u", UINT_MAX);
Expand Down Expand Up @@ -833,7 +843,7 @@ TEST(format_test, width) {
EXPECT_EQ(fmt::format("{:*^8}", "你好"), "**你好**");
EXPECT_EQ(fmt::format("{:#6}", 42.0), " 42.0");
EXPECT_EQ(fmt::format("{:6c}", static_cast<int>('x')), "x ");
EXPECT_EQ(fmt::format("{:>06.0f}", 0.00884311), "000000");
EXPECT_EQ(fmt::format("{:>06.0f}", 0.00884311), " 0");
}

TEST(format_test, runtime_width) {
Expand Down

0 comments on commit a585571

Please sign in to comment.