From bf790d281927f28c3319b4c89399cbaaa952abca Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 7 Jun 2014 07:31:25 -0700 Subject: [PATCH] Ignore '0' flag for non-numeric types as printf does. --- format.cc | 58 ++++++++++++++++++++++++++++++------------------------- format.h | 4 +++- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/format.cc b/format.cc index ab867259..7e389de1 100644 --- a/format.cc +++ b/format.cc @@ -575,6 +575,33 @@ void fmt::BasicWriter::FormatParser::CheckSign( ++s; } +template +void fmt::BasicWriter::PrintfParser::ParseFlags( + FormatSpec &spec, const Char *&s) { + // TODO: parse optional flags + for (;;) { + switch (*s) { + case '-': + ++s; + spec.align_ = ALIGN_LEFT; + break; + case '+': + // TODO + ++s; + spec.flags_ |= SIGN_FLAG | PLUS_FLAG; + break; + case '0': + spec.fill_ = '0'; + case ' ': + case '#': + ++s; + break; + default: + return; + } + } +} + template void fmt::BasicWriter::PrintfParser::Format( BasicWriter &writer, BasicStringRef format, @@ -655,29 +682,7 @@ void fmt::BasicWriter::PrintfParser::Format( switch (spec.width_) { case UINT_MAX: { spec.width_ = 0; - // TODO: parse optional flags - bool stop = false; - do { - switch (*s) { - case '-': - ++s; - spec.align_ = ALIGN_LEFT; - break; - case '+': - // TODO - ++s; - spec.flags_ |= SIGN_FLAG | PLUS_FLAG; - break; - case '0': - spec.fill_ = '0'; - case ' ': - case '#': - ++s; - break; - default: - stop = true; - } - } while (!stop); + ParseFlags(spec, s); /* // Parse fill and alignment. @@ -744,9 +749,10 @@ void fmt::BasicWriter::PrintfParser::Format( // Fall through. default: if (spec.fill_ == '0') { - spec.align_ = ALIGN_NUMERIC; - if (arg->type > LAST_NUMERIC_TYPE) - throw FormatError("format specifier '0' requires numeric argument"); + if (arg->type <= LAST_NUMERIC_TYPE) + spec.align_ = ALIGN_NUMERIC; + else + spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. } break; } diff --git a/format.h b/format.h index b17683d8..29852ccf 100644 --- a/format.h +++ b/format.h @@ -1033,7 +1033,9 @@ class BasicWriter { const ArgInfo *args_; int next_arg_index_; - public: + void ParseFlags(FormatSpec &spec, const Char *&s); + + public: void Format(BasicWriter &writer, BasicStringRef format, std::size_t num_args, const ArgInfo *args); };