Skip to content

Commit a585571

Browse files
ShawnZhongvitaut
authored andcommitted
Ignore 0 character with align
1 parent 840ec85 commit a585571

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

include/fmt/core.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -2237,7 +2237,9 @@ template <typename Char> class specs_setter {
22372237
FMT_CONSTEXPR void on_localized() { specs_.localized = true; }
22382238

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

test/format-test.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,16 @@ TEST(format_test, zero_flag) {
799799
format_error, "format specifier requires numeric argument");
800800
}
801801

802+
TEST(format_test, zero_flag_and_align) {
803+
// If the 0 character and an align option both appear, the 0 character is ignored.
804+
EXPECT_EQ("42 ", fmt::format("{0:<05}", 42));
805+
EXPECT_EQ("-42 ", fmt::format("{0:<05}", -42));
806+
EXPECT_EQ(" 42 ", fmt::format("{0:^05}", 42));
807+
EXPECT_EQ(" -42 ", fmt::format("{0:^05}", -42));
808+
EXPECT_EQ(" 42", fmt::format("{0:>05}", 42));
809+
EXPECT_EQ(" -42", fmt::format("{0:>05}", -42));
810+
}
811+
802812
TEST(format_test, width) {
803813
char format_str[buffer_size];
804814
safe_sprintf(format_str, "{0:%u", UINT_MAX);
@@ -833,7 +843,7 @@ TEST(format_test, width) {
833843
EXPECT_EQ(fmt::format("{:*^8}", "你好"), "**你好**");
834844
EXPECT_EQ(fmt::format("{:#6}", 42.0), " 42.0");
835845
EXPECT_EQ(fmt::format("{:6c}", static_cast<int>('x')), "x ");
836-
EXPECT_EQ(fmt::format("{:>06.0f}", 0.00884311), "000000");
846+
EXPECT_EQ(fmt::format("{:>06.0f}", 0.00884311), " 0");
837847
}
838848

839849
TEST(format_test, runtime_width) {

0 commit comments

Comments
 (0)