mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
back_insert_range<internal::buffer> -> buffer_range
This commit is contained in:
@ -191,10 +191,6 @@ FMT_END_NAMESPACE
|
|||||||
FMT_BEGIN_NAMESPACE
|
FMT_BEGIN_NAMESPACE
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
#ifndef FMT_USE_GRISU
|
|
||||||
# define FMT_USE_GRISU 1
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// A fallback implementation of uintptr_t for systems that lack it.
|
// A fallback implementation of uintptr_t for systems that lack it.
|
||||||
struct fallback_uintptr {
|
struct fallback_uintptr {
|
||||||
unsigned char value[sizeof(void*)];
|
unsigned char value[sizeof(void*)];
|
||||||
@ -205,11 +201,6 @@ using uintptr_t = ::uintptr_t;
|
|||||||
using uintptr_t = fallback_uintptr;
|
using uintptr_t = fallback_uintptr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename T> inline bool use_grisu() {
|
|
||||||
return FMT_USE_GRISU && std::numeric_limits<double>::is_iec559 &&
|
|
||||||
sizeof(T) <= sizeof(double);
|
|
||||||
}
|
|
||||||
|
|
||||||
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't produce
|
// An equivalent of `*reinterpret_cast<Dest*>(&source)` that doesn't produce
|
||||||
// undefined behavior (e.g. due to type aliasing).
|
// undefined behavior (e.g. due to type aliasing).
|
||||||
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
|
// Example: uint64_t d = bit_cast<uint64_t>(2.718);
|
||||||
@ -224,6 +215,30 @@ inline Dest bit_cast(const Source& source) {
|
|||||||
// An implementation of iterator_t for pre-C++20 systems.
|
// An implementation of iterator_t for pre-C++20 systems.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
using iterator_t = decltype(std::begin(std::declval<T&>()));
|
using iterator_t = decltype(std::begin(std::declval<T&>()));
|
||||||
|
|
||||||
|
#ifndef FMT_USE_GRISU
|
||||||
|
# define FMT_USE_GRISU 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <typename T> inline bool use_grisu() {
|
||||||
|
return FMT_USE_GRISU && std::numeric_limits<double>::is_iec559 &&
|
||||||
|
sizeof(T) <= sizeof(double);
|
||||||
|
}
|
||||||
|
|
||||||
|
// A range whose output iterator appends to a buffer.
|
||||||
|
template <typename T> class buffer_range {
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
using iterator = std::back_insert_iterator<internal::buffer<T>>;
|
||||||
|
|
||||||
|
private:
|
||||||
|
iterator begin_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
buffer_range(internal::buffer<T>& buf) : begin_(std::back_inserter(buf)) {}
|
||||||
|
explicit buffer_range(iterator it) : begin_(it) {}
|
||||||
|
iterator begin() const { return begin_; }
|
||||||
|
};
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
|
||||||
template <typename OutputIt, typename T = typename OutputIt::value_type>
|
template <typename OutputIt, typename T = typename OutputIt::value_type>
|
||||||
@ -243,22 +258,9 @@ class output_range {
|
|||||||
OutputIt begin() const { return it_; }
|
OutputIt begin() const { return it_; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// A range where begin() returns back_insert_iterator.
|
|
||||||
template <typename Container>
|
|
||||||
class back_insert_range
|
|
||||||
: public output_range<std::back_insert_iterator<Container>> {
|
|
||||||
using base = output_range<std::back_insert_iterator<Container>>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
using value_type = typename Container::value_type;
|
|
||||||
|
|
||||||
back_insert_range(Container& c) : base(std::back_inserter(c)) {}
|
|
||||||
back_insert_range(typename base::iterator it) : base(it) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename Range> class basic_writer;
|
template <typename Range> class basic_writer;
|
||||||
using writer = basic_writer<back_insert_range<internal::buffer<char>>>;
|
using writer = basic_writer<internal::buffer_range<char>>;
|
||||||
using wwriter = basic_writer<back_insert_range<internal::buffer<wchar_t>>>;
|
using wwriter = basic_writer<internal::buffer_range<wchar_t>>;
|
||||||
|
|
||||||
/** A formatting error such as invalid format string. */
|
/** A formatting error such as invalid format string. */
|
||||||
class format_error : public std::runtime_error {
|
class format_error : public std::runtime_error {
|
||||||
@ -3284,7 +3286,7 @@ template <typename Char>
|
|||||||
typename buffer_context<Char>::iterator internal::vformat_to(
|
typename buffer_context<Char>::iterator internal::vformat_to(
|
||||||
internal::buffer<Char>& buf, basic_string_view<Char> format_str,
|
internal::buffer<Char>& buf, basic_string_view<Char> format_str,
|
||||||
basic_format_args<buffer_context<Char>> args) {
|
basic_format_args<buffer_context<Char>> args) {
|
||||||
typedef back_insert_range<internal::buffer<Char>> range;
|
using range = buffer_range<Char>;
|
||||||
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str),
|
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str),
|
||||||
args);
|
args);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ typename buffer_context<Char>::iterator vformat_to(
|
|||||||
const std::locale& loc, buffer<Char>& buf,
|
const std::locale& loc, buffer<Char>& buf,
|
||||||
basic_string_view<Char> format_str,
|
basic_string_view<Char> format_str,
|
||||||
basic_format_args<buffer_context<Char>> args) {
|
basic_format_args<buffer_context<Char>> args) {
|
||||||
typedef back_insert_range<buffer<Char>> range;
|
using range = buffer_range<Char>;
|
||||||
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str), args,
|
return vformat_to<arg_formatter<range>>(buf, to_string_view(format_str), args,
|
||||||
internal::locale_ref(loc));
|
internal::locale_ref(loc));
|
||||||
}
|
}
|
||||||
|
@ -214,7 +214,7 @@ class prepared_format {
|
|||||||
|
|
||||||
std::basic_string<char_type> format(const Args&... args) const {
|
std::basic_string<char_type> format(const Args&... args) const {
|
||||||
basic_memory_buffer<char_type> buffer;
|
basic_memory_buffer<char_type> buffer;
|
||||||
typedef back_insert_range<internal::buffer<char_type>> range;
|
using range = buffer_range<char_type>;
|
||||||
this->vformat_to(range(buffer), basic_format_args<context>{
|
this->vformat_to(range(buffer), basic_format_args<context>{
|
||||||
make_args_checked(format_, args...)});
|
make_args_checked(format_, args...)});
|
||||||
return to_string(buffer);
|
return to_string(buffer);
|
||||||
@ -224,7 +224,7 @@ class prepared_format {
|
|||||||
inline std::back_insert_iterator<Container> format_to(
|
inline std::back_insert_iterator<Container> format_to(
|
||||||
std::back_insert_iterator<Container> out, const Args&... args) const {
|
std::back_insert_iterator<Container> out, const Args&... args) const {
|
||||||
internal::container_buffer<Container> buffer(internal::get_container(out));
|
internal::container_buffer<Container> buffer(internal::get_container(out));
|
||||||
typedef back_insert_range<internal::buffer<char_type>> range;
|
using range = buffer_range<char_type>;
|
||||||
this->vformat_to(range(buffer), basic_format_args<context>{
|
this->vformat_to(range(buffer), basic_format_args<context>{
|
||||||
make_args_checked(format_, args...)});
|
make_args_checked(format_, args...)});
|
||||||
return out;
|
return out;
|
||||||
@ -241,7 +241,7 @@ class prepared_format {
|
|||||||
template <std::size_t SIZE = inline_buffer_size>
|
template <std::size_t SIZE = inline_buffer_size>
|
||||||
inline typename buffer_context<char_type>::iterator format_to(
|
inline typename buffer_context<char_type>::iterator format_to(
|
||||||
basic_memory_buffer<char_type, SIZE>& buf, const Args&... args) const {
|
basic_memory_buffer<char_type, SIZE>& buf, const Args&... args) const {
|
||||||
typedef back_insert_range<internal::buffer<char_type>> range;
|
using range = buffer_range<char_type>;
|
||||||
return this->vformat_to(
|
return this->vformat_to(
|
||||||
range(buf),
|
range(buf),
|
||||||
basic_format_args<context>{make_args_checked(format_, args...)});
|
basic_format_args<context>{make_args_checked(format_, args...)});
|
||||||
|
@ -368,7 +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<back_insert_range<internal::buffer<Char>>>>
|
printf_arg_formatter<internal::buffer_range<Char>>>
|
||||||
OutputIt format();
|
OutputIt format();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,10 +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<
|
: public fmt::arg_formatter<fmt::internal::buffer_range<char>> {
|
||||||
fmt::back_insert_range<fmt::internal::buffer<char>>> {
|
|
||||||
public:
|
public:
|
||||||
typedef fmt::back_insert_range<fmt::internal::buffer<char>> range;
|
using range = fmt::internal::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,
|
||||||
|
@ -719,7 +719,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;
|
||||||
typedef fmt::back_insert_range<fmt::internal::buffer<char>> range;
|
using range = fmt::internal::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;
|
||||||
typedef fmt::back_insert_range<fmt::internal::buffer<Char>> range;
|
using range = fmt::internal::buffer_range<Char>;
|
||||||
fmt::basic_writer<range> writer(buffer);
|
fmt::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);
|
||||||
@ -1894,7 +1894,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
|
||||||
|
|
||||||
typedef fmt::back_insert_range<fmt::internal::buffer<char>> buffer_range;
|
using buffer_range = fmt::internal::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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef fmt::back_insert_range<fmt::internal::buffer<char>> range;
|
using range = fmt::internal::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<
|
typedef fmt::printf_arg_formatter<fmt::internal::buffer_range<char>>
|
||||||
fmt::back_insert_range<fmt::internal::buffer<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;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user