Skip to content

Commit

Permalink
Ignore '0' flag for non-numeric types as printf does.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 7, 2014
1 parent 80c9976 commit bf790d2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
58 changes: 32 additions & 26 deletions format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,33 @@ void fmt::BasicWriter<Char>::FormatParser::CheckSign(
++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>
void fmt::BasicWriter<Char>::PrintfParser::Format(
BasicWriter<Char> &writer, BasicStringRef<Char> format,
Expand Down Expand Up @@ -655,29 +682,7 @@ void fmt::BasicWriter<Char>::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.
Expand Down Expand Up @@ -744,9 +749,10 @@ void fmt::BasicWriter<Char>::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;
}
Expand Down
4 changes: 3 additions & 1 deletion format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Char> &writer,
BasicStringRef<Char> format, std::size_t num_args, const ArgInfo *args);
};
Expand Down

0 comments on commit bf790d2

Please sign in to comment.