Commit 22e98a5b authored by Victor Zverovich's avatar Victor Zverovich

Make compile work with user-defined types

parent f18a3f36
...@@ -173,8 +173,6 @@ class compiled_format { ...@@ -173,8 +173,6 @@ class compiled_format {
compiled_format() = delete; compiled_format() = delete;
using context = buffer_context<char_type>;
template <typename Range, typename Context> template <typename Range, typename Context>
auto vformat_to(Range out, basic_format_args<Context> args) const -> auto vformat_to(Range out, basic_format_args<Context> args) const ->
typename Context::iterator { typename Context::iterator {
...@@ -203,8 +201,7 @@ class compiled_format { ...@@ -203,8 +201,7 @@ class compiled_format {
case format_part_t::kind::arg_name: { case format_part_t::kind::arg_name: {
advance_parse_context_to_specification(parse_ctx, part); advance_parse_context_to_specification(parse_ctx, part);
const auto named_arg_id = value.str; format_arg<Range>(parse_ctx, ctx, value.str);
format_arg<Range>(parse_ctx, ctx, named_arg_id);
} break; } break;
case format_part_t::kind::replacement: { case format_part_t::kind::replacement: {
const auto& arg_id_value = value.repl.arg_id.val; const auto& arg_id_value = value.repl.arg_id.val;
...@@ -242,10 +239,8 @@ class compiled_format { ...@@ -242,10 +239,8 @@ class compiled_format {
template <typename Range, typename Context, typename Id> template <typename Range, typename Context, typename Id>
void format_arg(basic_parse_context<char_type>& parse_ctx, Context& ctx, void format_arg(basic_parse_context<char_type>& parse_ctx, Context& ctx,
Id arg_id) const { Id arg_id) const {
parse_ctx.check_arg_id(arg_id); ctx.advance_to(visit_format_arg(arg_formatter<Range>(ctx, &parse_ctx),
const auto stopped_at = ctx.arg(arg_id)));
visit_format_arg(arg_formatter<Range>(ctx), ctx.arg(arg_id));
ctx.advance_to(stopped_at);
} }
template <typename Char> template <typename Char>
...@@ -359,6 +354,7 @@ FMT_CONSTEXPR auto compile(S format_str) ...@@ -359,6 +354,7 @@ FMT_CONSTEXPR auto compile(S format_str)
} }
#endif #endif
// Compiles the format string which must be a string literal.
template <typename... Args, typename Char, size_t N> template <typename... Args, typename Char, size_t N>
auto compile(const Char (&format_str)[N]) -> internal::compiled_format< auto compile(const Char (&format_str)[N]) -> internal::compiled_format<
std::basic_string<Char>, internal::runtime_parts_provider<Char>, Args...> { std::basic_string<Char>, internal::runtime_parts_provider<Char>, Args...> {
......
...@@ -152,3 +152,17 @@ TEST(CompileTest, FormattedSize) { ...@@ -152,3 +152,17 @@ TEST(CompileTest, FormattedSize) {
auto f = fmt::compile<int>("{:10}"); auto f = fmt::compile<int>("{:10}");
EXPECT_EQ(fmt::formatted_size(f, 42), 10); EXPECT_EQ(fmt::formatted_size(f, 42), 10);
} }
struct formattable {};
template <>
struct fmt::formatter<formattable> : formatter<const char*> {
auto format(formattable, format_context& ctx) -> decltype(ctx.out()) {
return formatter<const char*>::format("foo", ctx);
}
};
TEST(CompileTest, FormatUserDefinedType) {
auto f = fmt::compile<formattable>("{}");
EXPECT_EQ(fmt::format(f, formattable()), "foo");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment