Skip to content

Commit

Permalink
Use a pointer instead of a reference for specs in arg_formatter
Browse files Browse the repository at this point in the history
Per PR suggestion.
  • Loading branch information
kennyweiss committed Jan 9, 2023
1 parent 5f5b955 commit cb7ff15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3580,19 +3580,19 @@ template <typename Char> struct arg_formatter {
using context = buffer_context<Char>;

iterator out;
const format_specs<Char>& specs;
const format_specs<Char>* specs;
locale_ref locale;

arg_formatter(buffer_appender<Char> it, const format_specs<Char>& s)
arg_formatter(buffer_appender<Char> it, const format_specs<Char>* s)
: out(it), specs(s) {}

arg_formatter(buffer_appender<Char> it, const format_specs<Char>& s,
arg_formatter(buffer_appender<Char> it, const format_specs<Char>* s,
locale_ref l)
: out(it), specs(s), locale(l) {}

template <typename T>
FMT_CONSTEXPR FMT_INLINE auto operator()(T value) -> iterator {
return detail::write(out, value, specs, locale);
return detail::write(out, value, *specs, locale);
}
auto operator()(typename basic_format_arg<context>::handle) -> iterator {
// User-defined types are handled separately because they require access
Expand Down Expand Up @@ -4220,7 +4220,7 @@ void vformat_to(buffer<Char>& buf, basic_string_view<Char> fmt,
specs.precision, specs.precision_ref, context);
if (begin == end || *begin != '}')
on_error("missing '}' in format string");
auto f = arg_formatter<Char>{context.out(), specs, context.locale()};
auto f = arg_formatter<Char>{context.out(), &specs, context.locale()};
context.advance_to(visit_format_arg(f, arg));
return begin;
}
Expand Down
12 changes: 6 additions & 6 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,13 @@ class printf_arg_formatter : public arg_formatter<Char> {
context_type& context_;

OutputIt write_null_pointer(bool is_string = false) {
auto s = this->specs;
auto s = *this->specs;
s.type = presentation_type::none;
return write_bytes(this->out, is_string ? "(null)" : "(nil)", s);
}

public:
printf_arg_formatter(OutputIt iter, format_specs<Char>& s, context_type& ctx)
printf_arg_formatter(OutputIt iter, format_specs<Char>* s, context_type& ctx)
: base{iter, s}, context_(ctx) {}

OutputIt operator()(monostate value) { return base::operator()(value); }
Expand All @@ -244,7 +244,7 @@ class printf_arg_formatter : public arg_formatter<Char> {
// MSVC2013 fails to compile separate overloads for bool and Char so use
// std::is_same instead.
if (std::is_same<T, Char>::value) {
format_specs<Char> fmt_specs = this->specs;
format_specs<Char> fmt_specs = *this->specs;
if (fmt_specs.type != presentation_type::none &&
fmt_specs.type != presentation_type::chr) {
return (*this)(static_cast<int>(value));
Expand All @@ -269,13 +269,13 @@ class printf_arg_formatter : public arg_formatter<Char> {
/** Formats a null-terminated C string. */
OutputIt operator()(const char* value) {
if (value) return base::operator()(value);
return write_null_pointer(this->specs.type != presentation_type::pointer);
return write_null_pointer(this->specs->type != presentation_type::pointer);
}

/** Formats a null-terminated wide C string. */
OutputIt operator()(const wchar_t* value) {
if (value) return base::operator()(value);
return write_null_pointer(this->specs.type != presentation_type::pointer);
return write_null_pointer(this->specs->type != presentation_type::pointer);
}

OutputIt operator()(basic_string_view<Char> value) {
Expand Down Expand Up @@ -545,7 +545,7 @@ void vprintf(buffer<Char>& buf, basic_string_view<Char> format,

// Format argument.
out = visit_format_arg(
printf_arg_formatter<iterator, Char>(out, specs, context), arg);
printf_arg_formatter<iterator, Char>(out, &specs, context), arg);
}
write(out, basic_string_view<Char>(start, to_unsigned(it - start)));
}
Expand Down

0 comments on commit cb7ff15

Please sign in to comment.