Skip to content

Commit

Permalink
Make buffer_range public and update custom formatting docs (#1281)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 28, 2019
1 parent 744302a commit 3f75e2b
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 29 deletions.
12 changes: 6 additions & 6 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,21 @@ Custom Formatting of Built-in Types
It is possible to change the way arguments are formatted by providing a
custom argument formatter class::

using arg_formatter =
fmt::arg_formatter<fmt::back_insert_range<fmt::internal::buffer>>;
using arg_formatter = fmt::arg_formatter<fmt::buffer_range<char>>;

// A custom argument formatter that formats negative integers as unsigned
// with the ``x`` format specifier.
class custom_arg_formatter : public arg_formatter {
public:
custom_arg_formatter(fmt::format_context &ctx,
fmt::format_specs *spec = nullptr)
: arg_formatter(ctx, spec) {}
custom_arg_formatter(fmt::format_context& ctx,
fmt::format_parse_context* parse_ctx = nullptr,
fmt::format_specs* spec = nullptr)
: arg_formatter(ctx, parse_ctx, spec) {}

using arg_formatter::operator();

auto operator()(int value) {
if (spec().type() == 'x')
if (specs() && specs()->type == 'x')
return (*this)(static_cast<unsigned>(value)); // convert to unsigned and format
return arg_formatter::operator()(value);
}
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ template <typename CompiledFormat, typename... Args,
typename Char = typename CompiledFormat::char_type>
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
basic_memory_buffer<Char> buffer;
using range = internal::buffer_range<Char>;
using range = buffer_range<Char>;
using context = buffer_context<Char>;
cf.template vformat_to<range, context>(range(buffer),
{make_format_args<context>(args...)});
Expand Down
24 changes: 12 additions & 12 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,6 @@ class output_range {
sentinel end() const { return {}; } // Sentinel is not used yet.
};

// A range with an iterator appending to a buffer.
template <typename T>
class buffer_range
: public output_range<std::back_insert_iterator<buffer<T>>, T> {
public:
using iterator = std::back_insert_iterator<buffer<T>>;
using output_range<iterator, T>::output_range;
buffer_range(buffer<T>& buf)
: output_range<iterator, T>(std::back_inserter(buf)) {}
};

template <typename Char>
inline size_t count_code_points(basic_string_view<Char> s) {
return s.size();
Expand Down Expand Up @@ -478,6 +467,17 @@ void buffer<T>::append(const U* begin, const U* end) {
}
} // namespace internal

// A range with an iterator appending to a buffer.
template <typename T>
class buffer_range : public internal::output_range<
std::back_insert_iterator<internal::buffer<T>>, T> {
public:
using iterator = std::back_insert_iterator<internal::buffer<T>>;
using internal::output_range<iterator, T>::output_range;
buffer_range(internal::buffer<T>& buf)
: internal::output_range<iterator, T>(std::back_inserter(buf)) {}
};

// A UTF-8 string view.
class u8string_view : public basic_string_view<char8_t> {
public:
Expand Down Expand Up @@ -2598,7 +2598,7 @@ template <typename Range>
using basic_writer FMT_DEPRECATED_ALIAS = internal::basic_writer<Range>;
using writer FMT_DEPRECATED_ALIAS = internal::writer;
using wwriter FMT_DEPRECATED_ALIAS =
internal::basic_writer<internal::buffer_range<wchar_t>>;
internal::basic_writer<buffer_range<wchar_t>>;

/** The default argument formatter. */
template <typename Range>
Expand Down
3 changes: 1 addition & 2 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,7 @@ template <typename OutputIt, typename Char> class basic_printf_context {
}

/** Formats stored arguments and writes the output to the range. */
template <typename ArgFormatter =
printf_arg_formatter<internal::buffer_range<Char>>>
template <typename ArgFormatter = printf_arg_formatter<buffer_range<Char>>>
OutputIt format();
};

Expand Down
4 changes: 2 additions & 2 deletions test/custom-formatter-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class custom_arg_formatter
: public fmt::arg_formatter<fmt::internal::buffer_range<char>> {
: public fmt::arg_formatter<fmt::buffer_range<char>> {
public:
using range = fmt::internal::buffer_range<char>;
using range = fmt::buffer_range<char>;
typedef fmt::arg_formatter<range> base;

custom_arg_formatter(fmt::format_context& ctx,
Expand Down
2 changes: 1 addition & 1 deletion test/format
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ template<class... Args>
string vformat(string_view fmt, format_args args) {
fmt::memory_buffer mbuf;
fmt::internal::buffer<char>& buf = mbuf;
using range = fmt::internal::buffer_range<char>;
using range = fmt::buffer_range<char>;
detail::format_handler<detail::arg_formatter<range>, char, format_context>
h(range(std::back_inserter(buf)), fmt, args, {});
fmt::internal::parse_format_string<false>(fmt::to_string_view(fmt), h);
Expand Down
4 changes: 2 additions & 2 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void std_format(long double value, std::wstring& result) {
template <typename Char, typename T>
::testing::AssertionResult check_write(const T& value, const char* type) {
fmt::basic_memory_buffer<Char> buffer;
using range = fmt::internal::buffer_range<Char>;
using range = fmt::buffer_range<Char>;
basic_writer<range> writer(buffer);
writer.write(value);
std::basic_string<Char> actual = to_string(buffer);
Expand Down Expand Up @@ -1911,7 +1911,7 @@ enum TestFixedEnum : short { B };
TEST(FormatTest, FixedEnum) { EXPECT_EQ("0", fmt::format("{}", B)); }
#endif

using buffer_range = fmt::internal::buffer_range<char>;
using buffer_range = fmt::buffer_range<char>;

class mock_arg_formatter
: public fmt::internal::arg_formatter_base<buffer_range> {
Expand Down
2 changes: 1 addition & 1 deletion test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_enum()));
}

using range = fmt::internal::buffer_range<char>;
using range = fmt::buffer_range<char>;

struct test_arg_formatter : fmt::arg_formatter<range> {
fmt::format_parse_context parse_ctx;
Expand Down
3 changes: 1 addition & 2 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,7 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) {
#endif
}

typedef fmt::printf_arg_formatter<fmt::internal::buffer_range<char>>
formatter_t;
typedef fmt::printf_arg_formatter<fmt::buffer_range<char>> formatter_t;
typedef fmt::basic_printf_context<formatter_t::iterator, char> context_t;

// A custom printf argument formatter that doesn't print `-` for floating-point
Expand Down

0 comments on commit 3f75e2b

Please sign in to comment.