fixed formatting

This commit is contained in:
wind85 2017-07-03 12:57:02 +02:00
parent 49e6c114ef
commit 8cc8ca1548

View File

@ -49,8 +49,7 @@ class PrecisionHandler : public ArgVisitor<PrecisionHandler, int> {
template <typename T> template <typename T>
int visit_any_int(T value) { int visit_any_int(T value) {
if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int( if (!IntChecker<std::numeric_limits<T>::is_signed>::fits_in_int(value))
value))
FMT_THROW(FormatError("number is too big")); FMT_THROW(FormatError("number is too big"));
return static_cast<int>(value); return static_cast<int>(value);
} }
@ -110,8 +109,7 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> {
FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter); FMT_DISALLOW_COPY_AND_ASSIGN(ArgConverter);
public: public:
ArgConverter(internal::Arg &arg, wchar_t type) ArgConverter(internal::Arg &arg, wchar_t type) : arg_(arg), type_(type) {}
: arg_(arg), type_(type) {}
void visit_bool(bool value) { void visit_bool(bool value) {
if (type_ != 's') visit_any_int(value); if (type_ != 's') visit_any_int(value);
@ -129,20 +127,17 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> {
} }
using internal::Arg; using internal::Arg;
typedef typename internal::Conditional<is_same<T, void>::value, typedef typename internal::Conditional<is_same<T, void>::value, U, T>::type
U, T>::type TargetType; TargetType;
if (sizeof(TargetType) <= sizeof(int)) { if (sizeof(TargetType) <= sizeof(int)) {
// Extra casts are used to silence warnings. // Extra casts are used to silence warnings.
if (is_signed) { if (is_signed) {
arg_.type = Arg::INT; arg_.type = Arg::INT;
arg_.int_value = static_cast<int>( arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
static_cast<TargetType>(value));
} else { } else {
arg_.type = Arg::UINT; arg_.type = Arg::UINT;
typedef typename internal::MakeUnsigned< typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
TargetType>::Type Unsigned; arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
arg_.uint_value = static_cast<unsigned>(
static_cast<Unsigned>(value));
} }
} else { } else {
if (is_signed) { if (is_signed) {
@ -153,13 +148,11 @@ class ArgConverter : public ArgVisitor<ArgConverter<T>, void> {
// "4294967254" // "4294967254"
// but we don't have to do the same because it's // but we don't have to do the same because it's
// a UB. // a UB.
arg_.long_long_value = arg_.long_long_value = static_cast<LongLong>(value);
static_cast<LongLong>(value);
} else { } else {
arg_.type = Arg::ULONG_LONG; arg_.type = Arg::ULONG_LONG;
arg_.ulong_long_value = static_cast< arg_.ulong_long_value =
typename internal::MakeUnsigned<U>::Type>( static_cast<typename internal::MakeUnsigned<U>::Type>(value);
value);
} }
} }
} }
@ -206,8 +199,7 @@ class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
width = 0 - width; width = 0 - width;
} }
unsigned int_max = std::numeric_limits<int>::max(); unsigned int_max = std::numeric_limits<int>::max();
if (width > int_max) if (width > int_max) FMT_THROW(FormatError("number is too big"));
FMT_THROW(FormatError("number is too big"));
return static_cast<unsigned>(width); return static_cast<unsigned>(width);
} }
}; };
@ -265,8 +257,7 @@ class BasicPrintfArgFormatter
void visit_char(int value) { void visit_char(int value) {
const Spec &fmt_spec = this->spec(); const Spec &fmt_spec = this->spec();
BasicWriter<Char> &w = this->writer(); BasicWriter<Char> &w = this->writer();
if (fmt_spec.type_ && fmt_spec.type_ != 'c') if (fmt_spec.type_ && fmt_spec.type_ != 'c') w.write_int(value, fmt_spec);
w.write_int(value, fmt_spec);
typedef typename BasicWriter<Char>::CharPtr CharPtr; typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (fmt_spec.width_ > 1) { if (fmt_spec.width_ > 1) {
@ -318,8 +309,8 @@ class PrintfArgFormatter
public: public:
/** Constructs an argument formatter object. */ /** Constructs an argument formatter object. */
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s) PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec>(
FormatSpec>(w, s) {} w, s) {}
}; };
/** This template formats data and writes the output to a writer. */ /** This template formats data and writes the output to a writer. */
@ -390,8 +381,7 @@ internal::Arg PrintfFormatter<Char, AF>::get_arg(const Char *s,
internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max() internal::Arg arg = arg_index == std::numeric_limits<unsigned>::max()
? next_arg(error) ? next_arg(error)
: FormatterBase::get_arg(arg_index - 1, error); : FormatterBase::get_arg(arg_index - 1, error);
if (error) if (error) FMT_THROW(FormatError(!*s ? "invalid format string" : error));
FMT_THROW(FormatError(!*s ? "invalid format string" : error));
return arg; return arg;
} }
@ -454,13 +444,10 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
if (*s == '.') { if (*s == '.') {
++s; ++s;
if ('0' <= *s && *s <= '9') { if ('0' <= *s && *s <= '9') {
spec.precision_ = static_cast<int>( spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
internal::parse_nonnegative_int(s));
} else if (*s == '*') { } else if (*s == '*') {
++s; ++s;
spec.precision_ = spec.precision_ = internal::PrecisionHandler().visit(get_arg(s));
internal::PrecisionHandler().visit(
get_arg(s));
} else { } else {
spec.precision_ = 0; spec.precision_ = 0;
} }
@ -483,15 +470,13 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
switch (*s++) { switch (*s++) {
case 'h': case 'h':
if (*s == 'h') if (*s == 'h')
ArgConverter<signed char>(arg, *++s) ArgConverter<signed char>(arg, *++s).visit(arg);
.visit(arg);
else else
ArgConverter<short>(arg, *s).visit(arg); ArgConverter<short>(arg, *s).visit(arg);
break; break;
case 'l': case 'l':
if (*s == 'l') if (*s == 'l')
ArgConverter<fmt::LongLong>(arg, *++s) ArgConverter<fmt::LongLong>(arg, *++s).visit(arg);
.visit(arg);
else else
ArgConverter<long>(arg, *s).visit(arg); ArgConverter<long>(arg, *s).visit(arg);
break; break;
@ -502,8 +487,7 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
ArgConverter<std::size_t>(arg, *s).visit(arg); ArgConverter<std::size_t>(arg, *s).visit(arg);
break; break;
case 't': case 't':
ArgConverter<std::ptrdiff_t>(arg, *s).visit( ArgConverter<std::ptrdiff_t>(arg, *s).visit(arg);
arg);
break; break;
case 'L': case 'L':
// printf produces garbage when 'L' is omitted // printf produces garbage when 'L' is omitted