Parse '0' flag.

This commit is contained in:
Victor Zverovich 2014-06-06 07:07:57 -07:00
parent bf8b29fbe7
commit 9aba05b76a
2 changed files with 35 additions and 12 deletions

View File

@ -611,12 +611,16 @@ void fmt::BasicWriter<Char>::PrintfParser::Format(
unsigned arg_index = 0; unsigned arg_index = 0;
bool have_width = false, have_arg_index = false; bool have_width = false, have_arg_index = false;
if (*s >= '0' && *s <= '9') { c = *s;
if (c >= '0' && c <= '9') {
unsigned value = internal::ParseNonnegativeInt(s, error); unsigned value = internal::ParseNonnegativeInt(s, error);
if (*s != '$') { if (*s != '$') {
// TODO: handle '0' if (c == '0')
have_width = true; spec.fill_ = '0';
spec.width_ = value; if (value != 0) {
have_width = true;
spec.width_ = value;
}
} else { } else {
++s; ++s;
if (next_arg_index_ <= 0) { if (next_arg_index_ <= 0) {
@ -659,9 +663,11 @@ void fmt::BasicWriter<Char>::PrintfParser::Format(
spec.align_ = ALIGN_LEFT; spec.align_ = ALIGN_LEFT;
break; break;
case '+': case '+':
// TODO
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
case ' ': case ' ':
case '#': case '#':
//++s; ++s;
break; break;
} }
@ -725,17 +731,18 @@ void fmt::BasicWriter<Char>::PrintfParser::Format(
// Parse width and zero flag. // Parse width and zero flag.
if (*s < '0' || *s > '9') if (*s < '0' || *s > '9')
break; break;
if (*s == '0') { if (*s == '0')
spec.align_ = ALIGN_NUMERIC;
spec.fill_ = '0'; spec.fill_ = '0';
}
// Zero may be parsed again as a part of the width, but it is simpler // Zero may be parsed again as a part of the width, but it is simpler
// and more efficient than checking if the next char is a digit. // and more efficient than checking if the next char is a digit.
spec.width_ = internal::ParseNonnegativeInt(s, error); spec.width_ = internal::ParseNonnegativeInt(s, error);
// Fall through. // Fall through.
default: default:
if (spec.fill_ == '0' && arg->type > LAST_NUMERIC_TYPE) if (spec.fill_ == '0') {
throw FormatError("format specifier '0' requires numeric argument"); spec.align_ = ALIGN_NUMERIC;
if (arg->type > LAST_NUMERIC_TYPE)
throw FormatError("format specifier '0' requires numeric argument");
}
break; break;
} }

View File

@ -124,15 +124,31 @@ TEST(PrintfTest, Width) {
// Width cannot be specified twice. // Width cannot be specified twice.
EXPECT_THROW_MSG(fmt::sprintf("%5-5d", 42), FormatError, EXPECT_THROW_MSG(fmt::sprintf("%5-5d", 42), FormatError,
"unknown format code '-' for integer"); "unknown format code '-' for integer");
}
TEST(PrintfTest, InvalidWidth) {
EXPECT_THROW_MSG(fmt::sprintf(str(Format("%{}d", BIG_NUM)), 42), EXPECT_THROW_MSG(fmt::sprintf(str(Format("%{}d", BIG_NUM)), 42),
FormatError, "number is too big in format"); FormatError, "number is too big in format");
EXPECT_THROW_MSG(fmt::sprintf(str(Format("%1${}d", BIG_NUM)), 42), EXPECT_THROW_MSG(fmt::sprintf(str(Format("%1${}d", BIG_NUM)), 42),
FormatError, "number is too big in format"); FormatError, "number is too big in format");
} }
TEST(PrintfTest, ZeroFlag) {
EXPECT_EQ("00042", str(fmt::sprintf("%05d", 42)));
EXPECT_EQ("00042", str(fmt::sprintf("%1$05d", 42)));
EXPECT_EQ("-0042", str(fmt::sprintf("%05d", -42)));
EXPECT_EQ("-0042", str(fmt::sprintf("%1$05d", -42)));
EXPECT_EQ("00042", str(fmt::sprintf("%05d", 42)));
EXPECT_EQ("00042", str(fmt::sprintf("%1$05d", 42)));
EXPECT_EQ("-0042", str(fmt::sprintf("%05d", -42)));
EXPECT_EQ("-0042", str(fmt::sprintf("%1$05d", -42)));
EXPECT_EQ("-004.2", str(fmt::sprintf("%06g", -4.2)));
EXPECT_EQ("-004.2", str(fmt::sprintf("%1$06g", -4.2)));
EXPECT_EQ("+00042", str(fmt::sprintf("%00+6d", 42)));
// TODO: test for error if argument is non-numeric
}
// TODO // TODO
TEST(PrintfTest, Align) { TEST(PrintfTest, Align) {