Fix formatting of more than 15 named arguments (#754)

This commit is contained in:
Victor Zverovich 2018-06-05 08:32:28 +02:00
parent 47d147b65d
commit d5c462596b
3 changed files with 15 additions and 14 deletions

View File

@ -428,7 +428,7 @@ template <typename T, typename Char>
struct is_named_arg<named_arg<T, Char>> : std::true_type {};
enum type {
none_type, name_arg_type,
none_type, named_arg_type,
// Integer types should go first,
int_type, uint_type, long_long_type, ulong_long_type, bool_type, char_type,
last_integer_type = char_type,
@ -438,12 +438,12 @@ enum type {
};
FMT_CONSTEXPR bool is_integral(type t) {
FMT_ASSERT(t != internal::name_arg_type, "invalid argument type");
FMT_ASSERT(t != internal::named_arg_type, "invalid argument type");
return t > internal::none_type && t <= internal::last_integer_type;
}
FMT_CONSTEXPR bool is_arithmetic(type t) {
FMT_ASSERT(t != internal::name_arg_type, "invalid argument type");
FMT_ASSERT(t != internal::named_arg_type, "invalid argument type");
return t > internal::none_type && t <= internal::last_numeric_type;
}
@ -637,7 +637,7 @@ inline typename std::enable_if<
make_value(const T &val) { return val; }
template <typename C, typename T>
typed_value<C, name_arg_type>
typed_value<C, named_arg_type>
make_value(const named_arg<T, typename C::char_type> &val) {
basic_format_arg<C> arg = make_arg<C>(val.value);
std::memcpy(val.data, &arg, sizeof(arg));
@ -1088,7 +1088,7 @@ class basic_format_args {
/** Returns the argument at specified index. */
format_arg get(size_type index) const {
format_arg arg = do_get(index);
return arg.type_ == internal::name_arg_type ?
return arg.type_ == internal::named_arg_type ?
arg.value_.as_named_arg().template deserialize<Context>() : arg;
}

View File

@ -1199,7 +1199,7 @@ FMT_CONSTEXPR typename internal::result_of<Visitor(int)>::type
switch (arg.type_) {
case internal::none_type:
break;
case internal::name_arg_type:
case internal::named_arg_type:
FMT_ASSERT(false, "invalid argument type");
break;
case internal::int_type:
@ -1493,7 +1493,7 @@ void arg_map<Context>::init(const basic_format_args<Context> &args) {
switch (arg_type) {
case internal::none_type:
return;
case internal::name_arg_type:
case internal::named_arg_type:
push_back(args.values_[i]);
break;
default:
@ -1502,15 +1502,11 @@ void arg_map<Context>::init(const basic_format_args<Context> &args) {
}
return;
}
for (unsigned i = 0; i != max_packed_args; ++i) {
if (args.type(i) == internal::name_arg_type)
push_back(args.args_[i].value_);
}
for (unsigned i = max_packed_args; ; ++i) {
for (unsigned i = 0; ; ++i) {
switch (args.args_[i].type_) {
case internal::none_type:
return;
case internal::name_arg_type:
case internal::named_arg_type:
push_back(args.args_[i].value_);
break;
default:
@ -3172,7 +3168,7 @@ struct formatter<
auto eh = ctx.error_handler();
switch (type) {
case internal::none_type:
case internal::name_arg_type:
case internal::named_arg_type:
FMT_ASSERT(false, "invalid argument type");
break;
case internal::int_type:

View File

@ -321,6 +321,11 @@ TEST(FormatterTest, NamedArg) {
EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4)));
EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2)));
EXPECT_EQ("1 2", format("{} {two}", 1, fmt::arg("two", 2)));
EXPECT_EQ("42", format("{c}",
fmt::arg("a", 0), fmt::arg("b", 0), fmt::arg("c", 42), fmt::arg("d", 0),
fmt::arg("e", 0), fmt::arg("f", 0), fmt::arg("g", 0), fmt::arg("h", 0),
fmt::arg("i", 0), fmt::arg("j", 0), fmt::arg("k", 0), fmt::arg("l", 0),
fmt::arg("m", 0), fmt::arg("n", 0), fmt::arg("o", 0), fmt::arg("p", 0)));
}
TEST(FormatterTest, AutoArgIndex) {