Add wchar_t overload of format_to_n (#764)

This commit is contained in:
Victor Zverovich 2018-06-13 08:24:32 +02:00
parent c2fbadb9cf
commit 0508bbc7ae
2 changed files with 25 additions and 1 deletions

View File

@ -3526,6 +3526,13 @@ inline OutputIt vformat_to(OutputIt out, string_view format_str,
typedef output_range<OutputIt, char> range; typedef output_range<OutputIt, char> range;
return vformat_to<arg_formatter<range>>(range(out), format_str, args); return vformat_to<arg_formatter<range>>(range(out), format_str, args);
} }
template <typename OutputIt, typename... Args>
inline OutputIt vformat_to(
OutputIt out, wstring_view format_str,
typename format_args_t<OutputIt, wchar_t>::type args) {
typedef output_range<OutputIt, wchar_t> range;
return vformat_to<arg_formatter<range>>(range(out), format_str, args);
}
/** /**
\rst \rst
@ -3578,12 +3585,20 @@ struct format_to_n_result {
*/ */
template <typename OutputIt, typename... Args> template <typename OutputIt, typename... Args>
inline format_to_n_result<OutputIt> format_to_n( inline format_to_n_result<OutputIt> format_to_n(
OutputIt out, std::size_t n, string_view format_str, const Args & ... args) { OutputIt out, std::size_t n, string_view format_str, const Args &... args) {
typedef internal::truncating_iterator<OutputIt> It; typedef internal::truncating_iterator<OutputIt> It;
auto it = vformat_to(It(out, n), format_str, auto it = vformat_to(It(out, n), format_str,
make_format_args<typename format_context_t<It>::type>(args...)); make_format_args<typename format_context_t<It>::type>(args...));
return {it.base(), it.count()}; return {it.base(), it.count()};
} }
template <typename OutputIt, typename... Args>
inline format_to_n_result<OutputIt> format_to_n(
OutputIt out, std::size_t n, wstring_view format_str, const Args &... args) {
typedef internal::truncating_iterator<OutputIt> It;
auto it = vformat_to(It(out, n), format_str,
make_format_args<typename format_context_t<It, wchar_t>::type>(args...));
return {it.base(), it.count()};
}
inline std::string vformat(string_view format_str, format_args args) { inline std::string vformat(string_view format_str, format_args args) {
memory_buffer buffer; memory_buffer buffer;

View File

@ -1525,6 +1525,15 @@ TEST(FormatTest, FormatToN) {
EXPECT_EQ("foox", fmt::string_view(buffer, 4)); EXPECT_EQ("foox", fmt::string_view(buffer, 4));
} }
TEST(FormatTest, WideFormatToN) {
wchar_t buffer[4];
buffer[3] = L'x';
auto result = fmt::format_to_n(buffer, 3, L"{}", 12345);
EXPECT_EQ(5u, result.size);
EXPECT_EQ(buffer + 3, result.out);
EXPECT_EQ(L"123x", fmt::wstring_view(buffer, 4));
}
#if FMT_USE_CONSTEXPR #if FMT_USE_CONSTEXPR
struct test_arg_id_handler { struct test_arg_id_handler {
enum result { NONE, EMPTY, INDEX, NAME, ERROR }; enum result { NONE, EMPTY, INDEX, NAME, ERROR };