Return iterator from the format method

This commit is contained in:
Victor Zverovich
2018-01-14 11:00:27 -08:00
parent 67928eae28
commit 91ee9c9acd
7 changed files with 66 additions and 38 deletions

View File

@@ -1223,8 +1223,9 @@ struct formatter<Date> {
return it;
}
void format(const Date &d, context &ctx) {
auto format(const Date &d, context &ctx) {
format_range(ctx.range(), "{}-{}-{}", d.year(), d.month(), d.day());
return ctx.begin();
}
};
}
@@ -1240,8 +1241,8 @@ class Answer {};
namespace fmt {
template <>
struct formatter<Answer> : formatter<int> {
void format(Answer, fmt::context &ctx) {
formatter<int>::format(42, ctx);
auto format(Answer, fmt::context &ctx) {
return formatter<int>::format(42, ctx);
}
};
}
@@ -1534,11 +1535,10 @@ struct variant {
namespace fmt {
template <>
struct formatter<variant> : dynamic_formatter<> {
void format(variant value, context& ctx) {
auto format(variant value, context& ctx) {
if (value.type == variant::INT)
dynamic_formatter::format(42, ctx);
else
dynamic_formatter::format("foo", ctx);
return dynamic_formatter::format(42, ctx);
return dynamic_formatter::format("foo", ctx);
}
};
}

View File

@@ -83,9 +83,9 @@ struct formatter<Test, Char> {
using range = fmt::internal::dynamic_range<basic_buffer<Char>>;
void format(Test, basic_context<range> &ctx) {
auto format(Test, basic_context<range> &ctx) -> decltype(ctx.begin()) {
const Char *test = "test";
ctx.range().container().append(test, test + std::strlen(test));
return std::copy_n(test, std::strlen(test), ctx.begin());
}
};
}
@@ -434,7 +434,7 @@ TEST(UtilTest, FormatArgs) {
EXPECT_FALSE(args[1]);
}
struct CustomContext {
struct custom_context {
using char_type = char;
template <typename T>
@@ -444,20 +444,22 @@ struct CustomContext {
return ctx.begin();
}
void format(const T &, CustomContext& ctx) {
const char *format(const T &, custom_context& ctx) {
ctx.called = true;
return 0;
}
};
bool called;
fmt::parse_context parse_context() { return fmt::parse_context(""); }
void advance_to(const char *) {}
};
TEST(UtilTest, MakeValueWithCustomFormatter) {
::Test t;
fmt::internal::value<CustomContext> arg(t);
CustomContext ctx = {false};
fmt::internal::value<custom_context> arg(t);
custom_context ctx = {false};
arg.custom.format(&t, ctx);
EXPECT_TRUE(ctx.called);
}