Skip to content

Commit

Permalink
Parameterize format_to on string type (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Sep 25, 2018
1 parent d66fa22 commit e4ca37c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3501,11 +3501,14 @@ inline OutputIt vformat_to(
fmt::format_to(std::back_inserter(out), "{}", 42);
\endrst
*/
template <typename OutputIt, typename... Args>
inline OutputIt format_to(OutputIt out, string_view format_str,
template <typename OutputIt, typename String, typename... Args>
inline OutputIt format_to(OutputIt out, const String &format_str,
const Args &... args) {
return vformat_to(out, format_str,
make_format_args<typename format_context_t<OutputIt>::type>(args...));
internal::check_format_string<Args...>(format_str);
typedef typename format_context_t<OutputIt, FMT_CHAR(String) >::type context_t;
format_arg_store<context_t, Args...> as{args...};
return vformat_to(out, basic_string_view< FMT_CHAR(String) >(format_str),
basic_format_args<context_t>(as));
}

template <typename OutputIt>
Expand Down
6 changes: 6 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,12 @@ TEST(FormatToTest, Format) {
EXPECT_EQ("part1part2", s);
}

TEST(FormatToTest, WideString) {
std::vector<wchar_t> buf;
fmt::format_to(std::back_inserter(buf), L"{}{}", 42, L'\0');
EXPECT_STREQ(buf.data(), L"42");
}

TEST(FormatToTest, FormatToNonbackInsertIteratorWithSignAndNumericAlignment) {
char buffer[16] = {};
fmt::format_to(buffer, "{: =+}", 42.0);
Expand Down

0 comments on commit e4ca37c

Please sign in to comment.