Ignore '0' flag for non-numeric types as printf does.

This commit is contained in:
Victor Zverovich
2014-06-07 07:31:25 -07:00
parent 80c99760fb
commit bf790d2819
2 changed files with 35 additions and 27 deletions

View File

@ -575,6 +575,33 @@ void fmt::BasicWriter<Char>::FormatParser::CheckSign(
++s; ++s;
} }
template <typename Char>
void fmt::BasicWriter<Char>::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 <typename Char> template <typename Char>
void fmt::BasicWriter<Char>::PrintfParser::Format( void fmt::BasicWriter<Char>::PrintfParser::Format(
BasicWriter<Char> &writer, BasicStringRef<Char> format, BasicWriter<Char> &writer, BasicStringRef<Char> format,
@ -655,29 +682,7 @@ void fmt::BasicWriter<Char>::PrintfParser::Format(
switch (spec.width_) { switch (spec.width_) {
case UINT_MAX: { case UINT_MAX: {
spec.width_ = 0; spec.width_ = 0;
// TODO: parse optional flags ParseFlags(spec, s);
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);
/* /*
// Parse fill and alignment. // Parse fill and alignment.
@ -744,9 +749,10 @@ void fmt::BasicWriter<Char>::PrintfParser::Format(
// Fall through. // Fall through.
default: default:
if (spec.fill_ == '0') { if (spec.fill_ == '0') {
spec.align_ = ALIGN_NUMERIC; if (arg->type <= LAST_NUMERIC_TYPE)
if (arg->type > LAST_NUMERIC_TYPE) spec.align_ = ALIGN_NUMERIC;
throw FormatError("format specifier '0' requires numeric argument"); else
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
} }
break; break;
} }

View File

@ -1033,7 +1033,9 @@ class BasicWriter {
const ArgInfo *args_; const ArgInfo *args_;
int next_arg_index_; int next_arg_index_;
public: void ParseFlags(FormatSpec &spec, const Char *&s);
public:
void Format(BasicWriter<Char> &writer, void Format(BasicWriter<Char> &writer,
BasicStringRef<Char> format, std::size_t num_args, const ArgInfo *args); BasicStringRef<Char> format, std::size_t num_args, const ArgInfo *args);
}; };