mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Ignore '0' flag for non-numeric types as printf does.
This commit is contained in:
58
format.cc
58
format.cc
@ -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;
|
||||||
}
|
}
|
||||||
|
4
format.h
4
format.h
@ -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);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user