mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 12:14:26 +00:00
Make buffer_range public and update custom formatting docs (#1281)
This commit is contained in:
parent
744302add0
commit
3f75e2b69e
12
doc/api.rst
12
doc/api.rst
@ -278,21 +278,21 @@ Custom Formatting of Built-in Types
|
|||||||
It is possible to change the way arguments are formatted by providing a
|
It is possible to change the way arguments are formatted by providing a
|
||||||
custom argument formatter class::
|
custom argument formatter class::
|
||||||
|
|
||||||
using arg_formatter =
|
using arg_formatter = fmt::arg_formatter<fmt::buffer_range<char>>;
|
||||||
fmt::arg_formatter<fmt::back_insert_range<fmt::internal::buffer>>;
|
|
||||||
|
|
||||||
// A custom argument formatter that formats negative integers as unsigned
|
// A custom argument formatter that formats negative integers as unsigned
|
||||||
// with the ``x`` format specifier.
|
// with the ``x`` format specifier.
|
||||||
class custom_arg_formatter : public arg_formatter {
|
class custom_arg_formatter : public arg_formatter {
|
||||||
public:
|
public:
|
||||||
custom_arg_formatter(fmt::format_context &ctx,
|
custom_arg_formatter(fmt::format_context& ctx,
|
||||||
fmt::format_specs *spec = nullptr)
|
fmt::format_parse_context* parse_ctx = nullptr,
|
||||||
: arg_formatter(ctx, spec) {}
|
fmt::format_specs* spec = nullptr)
|
||||||
|
: arg_formatter(ctx, parse_ctx, spec) {}
|
||||||
|
|
||||||
using arg_formatter::operator();
|
using arg_formatter::operator();
|
||||||
|
|
||||||
auto operator()(int value) {
|
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 (*this)(static_cast<unsigned>(value)); // convert to unsigned and format
|
||||||
return arg_formatter::operator()(value);
|
return arg_formatter::operator()(value);
|
||||||
}
|
}
|
||||||
|
@ -426,7 +426,7 @@ template <typename CompiledFormat, typename... Args,
|
|||||||
typename Char = typename CompiledFormat::char_type>
|
typename Char = typename CompiledFormat::char_type>
|
||||||
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
|
||||||
basic_memory_buffer<Char> buffer;
|
basic_memory_buffer<Char> buffer;
|
||||||
using range = internal::buffer_range<Char>;
|
using range = buffer_range<Char>;
|
||||||
using context = buffer_context<Char>;
|
using context = buffer_context<Char>;
|
||||||
cf.template vformat_to<range, context>(range(buffer),
|
cf.template vformat_to<range, context>(range(buffer),
|
||||||
{make_format_args<context>(args...)});
|
{make_format_args<context>(args...)});
|
||||||
|
@ -413,17 +413,6 @@ class output_range {
|
|||||||
sentinel end() const { return {}; } // Sentinel is not used yet.
|
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>
|
template <typename Char>
|
||||||
inline size_t count_code_points(basic_string_view<Char> s) {
|
inline size_t count_code_points(basic_string_view<Char> s) {
|
||||||
return s.size();
|
return s.size();
|
||||||
@ -478,6 +467,17 @@ void buffer<T>::append(const U* begin, const U* end) {
|
|||||||
}
|
}
|
||||||
} // namespace internal
|
} // 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.
|
// A UTF-8 string view.
|
||||||
class u8string_view : public basic_string_view<char8_t> {
|
class u8string_view : public basic_string_view<char8_t> {
|
||||||
public:
|
public:
|
||||||
@ -2598,7 +2598,7 @@ template <typename Range>
|
|||||||
using basic_writer FMT_DEPRECATED_ALIAS = internal::basic_writer<Range>;
|
using basic_writer FMT_DEPRECATED_ALIAS = internal::basic_writer<Range>;
|
||||||
using writer FMT_DEPRECATED_ALIAS = internal::writer;
|
using writer FMT_DEPRECATED_ALIAS = internal::writer;
|
||||||
using wwriter FMT_DEPRECATED_ALIAS =
|
using wwriter FMT_DEPRECATED_ALIAS =
|
||||||
internal::basic_writer<internal::buffer_range<wchar_t>>;
|
internal::basic_writer<buffer_range<wchar_t>>;
|
||||||
|
|
||||||
/** The default argument formatter. */
|
/** The default argument formatter. */
|
||||||
template <typename Range>
|
template <typename Range>
|
||||||
|
@ -368,8 +368,7 @@ template <typename OutputIt, typename Char> class basic_printf_context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Formats stored arguments and writes the output to the range. */
|
/** Formats stored arguments and writes the output to the range. */
|
||||||
template <typename ArgFormatter =
|
template <typename ArgFormatter = printf_arg_formatter<buffer_range<Char>>>
|
||||||
printf_arg_formatter<internal::buffer_range<Char>>>
|
|
||||||
OutputIt format();
|
OutputIt format();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,9 +18,9 @@
|
|||||||
// A custom argument formatter that doesn't print `-` for floating-point values
|
// A custom argument formatter that doesn't print `-` for floating-point values
|
||||||
// rounded to 0.
|
// rounded to 0.
|
||||||
class custom_arg_formatter
|
class custom_arg_formatter
|
||||||
: public fmt::arg_formatter<fmt::internal::buffer_range<char>> {
|
: public fmt::arg_formatter<fmt::buffer_range<char>> {
|
||||||
public:
|
public:
|
||||||
using range = fmt::internal::buffer_range<char>;
|
using range = fmt::buffer_range<char>;
|
||||||
typedef fmt::arg_formatter<range> base;
|
typedef fmt::arg_formatter<range> base;
|
||||||
|
|
||||||
custom_arg_formatter(fmt::format_context& ctx,
|
custom_arg_formatter(fmt::format_context& ctx,
|
||||||
|
@ -718,7 +718,7 @@ template<class... Args>
|
|||||||
string vformat(string_view fmt, format_args args) {
|
string vformat(string_view fmt, format_args args) {
|
||||||
fmt::memory_buffer mbuf;
|
fmt::memory_buffer mbuf;
|
||||||
fmt::internal::buffer<char>& buf = 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>
|
detail::format_handler<detail::arg_formatter<range>, char, format_context>
|
||||||
h(range(std::back_inserter(buf)), fmt, args, {});
|
h(range(std::back_inserter(buf)), fmt, args, {});
|
||||||
fmt::internal::parse_format_string<false>(fmt::to_string_view(fmt), h);
|
fmt::internal::parse_format_string<false>(fmt::to_string_view(fmt), h);
|
||||||
|
@ -99,7 +99,7 @@ void std_format(long double value, std::wstring& result) {
|
|||||||
template <typename Char, typename T>
|
template <typename Char, typename T>
|
||||||
::testing::AssertionResult check_write(const T& value, const char* type) {
|
::testing::AssertionResult check_write(const T& value, const char* type) {
|
||||||
fmt::basic_memory_buffer<Char> buffer;
|
fmt::basic_memory_buffer<Char> buffer;
|
||||||
using range = fmt::internal::buffer_range<Char>;
|
using range = fmt::buffer_range<Char>;
|
||||||
basic_writer<range> writer(buffer);
|
basic_writer<range> writer(buffer);
|
||||||
writer.write(value);
|
writer.write(value);
|
||||||
std::basic_string<Char> actual = to_string(buffer);
|
std::basic_string<Char> actual = to_string(buffer);
|
||||||
@ -1911,7 +1911,7 @@ enum TestFixedEnum : short { B };
|
|||||||
TEST(FormatTest, FixedEnum) { EXPECT_EQ("0", fmt::format("{}", B)); }
|
TEST(FormatTest, FixedEnum) { EXPECT_EQ("0", fmt::format("{}", B)); }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using buffer_range = fmt::internal::buffer_range<char>;
|
using buffer_range = fmt::buffer_range<char>;
|
||||||
|
|
||||||
class mock_arg_formatter
|
class mock_arg_formatter
|
||||||
: public fmt::internal::arg_formatter_base<buffer_range> {
|
: public fmt::internal::arg_formatter_base<buffer_range> {
|
||||||
|
@ -64,7 +64,7 @@ TEST(OStreamTest, Enum) {
|
|||||||
EXPECT_EQ(L"0", fmt::format(L"{}", unstreamable_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> {
|
struct test_arg_formatter : fmt::arg_formatter<range> {
|
||||||
fmt::format_parse_context parse_ctx;
|
fmt::format_parse_context parse_ctx;
|
||||||
|
@ -561,8 +561,7 @@ TEST(PrintfTest, VSPrintfMakeWArgsExample) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef fmt::printf_arg_formatter<fmt::internal::buffer_range<char>>
|
typedef fmt::printf_arg_formatter<fmt::buffer_range<char>> formatter_t;
|
||||||
formatter_t;
|
|
||||||
typedef fmt::basic_printf_context<formatter_t::iterator, char> context_t;
|
typedef fmt::basic_printf_context<formatter_t::iterator, char> context_t;
|
||||||
|
|
||||||
// A custom printf argument formatter that doesn't print `-` for floating-point
|
// A custom printf argument formatter that doesn't print `-` for floating-point
|
||||||
|
Loading…
Reference in New Issue
Block a user