Optimize common case

This commit is contained in:
Victor Zverovich 2020-06-15 18:13:40 -07:00
parent 7431165f38
commit 397ad1bec3
3 changed files with 21 additions and 3 deletions

View File

@ -1365,7 +1365,26 @@ FMT_FUNC void report_system_error(int error_code,
report_error(format_system_error, error_code, message); report_error(format_system_error, error_code, message);
} }
struct stringifier {
template <typename T> FMT_INLINE std::string operator()(T value) const {
return to_string(value);
}
std::string operator()(basic_format_arg<format_context>::handle h) const {
memory_buffer buf;
internal::buffer<char>& base = buf;
format_parse_context parse_ctx({});
format_context format_ctx(std::back_inserter(base), {}, {});
h.format(parse_ctx, format_ctx);
return to_string(buf);
}
};
FMT_FUNC std::string detail::vformat(string_view format_str, format_args args) { FMT_FUNC std::string detail::vformat(string_view format_str, format_args args) {
if (format_str.size() == 2 && equal2(format_str.data(), "{}")) {
auto arg = args.get(0);
if (!arg) error_handler().on_error("argument not found");
return visit_format_arg(stringifier(), arg);
}
memory_buffer buffer; memory_buffer buffer;
detail::vformat_to(buffer, format_str, args); detail::vformat_to(buffer, format_str, args);
return to_string(buffer); return to_string(buffer);

View File

@ -1766,8 +1766,7 @@ template <typename OutputIt, typename Char> struct default_arg_formatter {
} }
OutputIt operator()(typename basic_format_arg<context>::handle handle) { OutputIt operator()(typename basic_format_arg<context>::handle handle) {
auto s = Char('}'); basic_format_parse_context<Char> parse_ctx({});
basic_format_parse_context<Char> parse_ctx(basic_string_view<Char>(&s, 1));
basic_format_context<OutputIt, Char> format_ctx(out, args, loc); basic_format_context<OutputIt, Char> format_ctx(out, args, loc);
handle.format(parse_ctx, format_ctx); handle.format(parse_ctx, format_ctx);
return format_ctx.out(); return format_ctx.out();

View File

@ -1475,7 +1475,7 @@ template <> struct formatter<Date> {
template <typename ParseContext> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin(); auto it = ctx.begin();
if (*it == 'd') ++it; if (it != ctx.end() && *it == 'd') ++it;
return it; return it;
} }