🎉 format_to array test suite in base-test!
This commit is contained in:
parent
c2488d1304
commit
45595fd148
@ -2822,30 +2822,6 @@ auto vformat_to(OutputIt&& out, string_view fmt, format_args args)
|
||||
return detail::get_iterator(buf, out);
|
||||
}
|
||||
|
||||
template <typename OutputIt, typename OutputSen = OutputIt>
|
||||
struct format_to_result {
|
||||
/** Iterator past the end of the last write. */
|
||||
OutputIt out;
|
||||
/** Iterator to the end of the output range. */
|
||||
OutputSen out_last;
|
||||
|
||||
FMT_CONSTEXPR operator OutputIt&() & noexcept { return out; }
|
||||
FMT_CONSTEXPR operator const OutputIt&() const& noexcept { return out; }
|
||||
FMT_CONSTEXPR operator OutputIt&&() && noexcept {
|
||||
return static_cast<OutputIt&&>(out);
|
||||
}
|
||||
};
|
||||
|
||||
/** Formats a string and writes the output to ``out``. */
|
||||
template <size_t Size>
|
||||
auto vformat_to(char (&out)[Size], string_view fmt, format_args args)
|
||||
-> format_to_result<char*> {
|
||||
using traits = detail::fixed_buffer_traits;
|
||||
auto buf = detail::iterator_buffer<char*, char, traits>(out, Size);
|
||||
detail::vformat_to(buf, fmt, args, {});
|
||||
return {out + buf.count(), out + Size};
|
||||
}
|
||||
|
||||
/**
|
||||
\rst
|
||||
Formats ``args`` according to specifications in ``fmt``, writes the result to
|
||||
@ -2866,24 +2842,6 @@ FMT_INLINE auto format_to(OutputIt&& out, format_string<T...> fmt, T&&... args)
|
||||
return vformat_to(FMT_FWD(out), fmt, fmt::make_format_args(args...));
|
||||
}
|
||||
|
||||
/**
|
||||
\rst
|
||||
Formats ``args`` according to specifications in ``fmt``, writes the result to
|
||||
the output iterator ``out`` and returns the iterator past the end of the output
|
||||
range. `format_to` does not append a terminating null character.
|
||||
|
||||
**Example**::
|
||||
|
||||
auto out = std::vector<char>();
|
||||
fmt::format_to(std::back_inserter(out), "{}", 42);
|
||||
\endrst
|
||||
*/
|
||||
template <size_t Size, typename... T>
|
||||
FMT_INLINE auto format_to(char (&out)[Size], format_string<T...> fmt,
|
||||
T&&... args) -> format_to_result<char*> {
|
||||
return vformat_to(out, fmt, fmt::make_format_args(args...));
|
||||
}
|
||||
|
||||
template <typename OutputIt> struct format_to_n_result {
|
||||
/** Iterator past the end of the output range. */
|
||||
OutputIt out;
|
||||
@ -2916,6 +2874,33 @@ FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string<T...> fmt,
|
||||
return vformat_to_n(out, n, fmt, fmt::make_format_args(args...));
|
||||
}
|
||||
|
||||
template <typename OutputIt, typename OutputSen = OutputIt>
|
||||
struct format_to_result {
|
||||
/** Iterator past the end of the last write. */
|
||||
OutputIt out;
|
||||
/** Sentinel indicating the end of the output range. */
|
||||
OutputSen out_last;
|
||||
|
||||
FMT_CONSTEXPR operator OutputIt&() & noexcept { return out; }
|
||||
FMT_CONSTEXPR operator const OutputIt&() const& noexcept { return out; }
|
||||
FMT_CONSTEXPR operator OutputIt&&() && noexcept {
|
||||
return static_cast<OutputIt&&>(out);
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t Size>
|
||||
auto vformat_to(char (&out)[Size], string_view fmt, format_args args)
|
||||
-> format_to_result<char*> {
|
||||
format_to_n_result<char*> result = vformat_to_n(out, Size, fmt, args);
|
||||
return {result.out, out + Size};
|
||||
}
|
||||
|
||||
template <size_t Size, typename... T>
|
||||
FMT_INLINE auto format_to(char (&out)[Size], format_string<T...> fmt,
|
||||
T&&... args) -> format_to_result<char*> {
|
||||
return vformat_to(out, fmt, fmt::make_format_args(args...));
|
||||
}
|
||||
|
||||
/** Returns the number of chars in the output of ``format(fmt, args...)``. */
|
||||
template <typename... T>
|
||||
FMT_NODISCARD FMT_INLINE auto formatted_size(format_string<T...> fmt,
|
||||
|
@ -12,7 +12,7 @@
|
||||
#include <climits> // INT_MAX
|
||||
#include <cstring> // std::strlen
|
||||
#include <functional> // std::equal_to
|
||||
#include <iterator> // std::back_insert_iterator
|
||||
#include <iterator> // std::back_insert_iterator, std::distance
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <string> // std::string
|
||||
#include <type_traits> // std::is_same
|
||||
@ -692,6 +692,47 @@ TEST(core_test, format_to) {
|
||||
EXPECT_EQ(s, "42");
|
||||
}
|
||||
|
||||
TEST(core_test, format_to_c_array) {
|
||||
char buffer[4];
|
||||
auto result = fmt::format_to(buffer, "{}", 12345);
|
||||
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
||||
EXPECT_EQ(0, std::distance(result.out, result.out_last));
|
||||
EXPECT_EQ(buffer + 4, result.out);
|
||||
EXPECT_EQ("1234", fmt::string_view(buffer, 4));
|
||||
|
||||
result = fmt::format_to(buffer, "{:s}", "foobar");
|
||||
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
||||
EXPECT_EQ(0, std::distance(result.out, result.out_last));
|
||||
EXPECT_EQ(buffer + 4, result.out);
|
||||
EXPECT_EQ("foob", fmt::string_view(buffer, 4));
|
||||
|
||||
buffer[0] = 'x';
|
||||
buffer[1] = 'x';
|
||||
buffer[2] = 'x';
|
||||
buffer[3] = 'x';
|
||||
result = fmt::format_to(buffer, "{}", 'A');
|
||||
EXPECT_EQ(1, std::distance(&buffer[0], result.out));
|
||||
EXPECT_EQ(3, std::distance(result.out, result.out_last));
|
||||
EXPECT_EQ(buffer + 1, result.out);
|
||||
EXPECT_EQ("Axxx", fmt::string_view(buffer, 4));
|
||||
|
||||
result = fmt::format_to(buffer, "{}{} ", 'B', 'C');
|
||||
EXPECT_EQ(3, std::distance(&buffer[0], result.out));
|
||||
EXPECT_EQ(1, std::distance(result.out, result.out_last));
|
||||
EXPECT_EQ(buffer + 3, result.out);
|
||||
EXPECT_EQ("BC x", fmt::string_view(buffer, 4));
|
||||
|
||||
result = fmt::format_to(buffer, "{}", "ABCDE");
|
||||
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
||||
EXPECT_EQ(0, std::distance(result.out, result.out_last));
|
||||
EXPECT_EQ("ABCD", fmt::string_view(buffer, 4));
|
||||
|
||||
result = fmt::format_to(buffer, "{}", std::string(1000, '*'));
|
||||
EXPECT_EQ(4, std::distance(&buffer[0], result.out));
|
||||
EXPECT_EQ(0, std::distance(result.out, result.out_last));
|
||||
EXPECT_EQ("****", fmt::string_view(buffer, 4));
|
||||
}
|
||||
|
||||
#ifdef __cpp_lib_byte
|
||||
TEST(core_test, format_byte) {
|
||||
auto s = std::string();
|
||||
|
Loading…
Reference in New Issue
Block a user