Skip to content

Commit

Permalink
Parameterize core functions on the type of the format string.
Browse files Browse the repository at this point in the history
Take #2 of n

Signed-off-by: Daniela Engert <dani@ngrt.de>
  • Loading branch information
DanielaE committed Oct 2, 2018
1 parent 49b4c1e commit ee2e8db
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
17 changes: 4 additions & 13 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1340,23 +1340,14 @@ template <typename Char>
struct is_contiguous<internal::basic_buffer<Char> >: std::true_type {};

/** Formats a string and writes the output to ``out``. */
template <typename Container>
typename std::enable_if<
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
vformat_to(std::back_insert_iterator<Container> out,
string_view format_str, format_args args) {
internal::container_buffer<Container> buf(internal::get_container(out));
vformat_to(buf, format_str, args);
return out;
}

template <typename Container>
template <typename Container, typename S>
typename std::enable_if<
is_contiguous<Container>::value, std::back_insert_iterator<Container>>::type
vformat_to(std::back_insert_iterator<Container> out,
wstring_view format_str, wformat_args args) {
const S &format_str,
basic_format_args<typename buffer_context<FMT_CHAR(S)>::type> args) {
internal::container_buffer<Container> buf(internal::get_container(out));
vformat_to(buf, format_str, args);
vformat_to(buf, internal::to_string_view(format_str), args);
return out;
}

Expand Down
40 changes: 23 additions & 17 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3465,12 +3465,6 @@ inline typename buffer_context<FMT_CHAR(String)>::type::iterator vformat_to(
buf, basic_string_view<Char>(format_str), args);
}

inline wformat_context::iterator vformat_to(
internal::wbuffer &buf, wstring_view format_str, wformat_args args) {
typedef back_insert_range<internal::wbuffer> range;
return vformat_to<arg_formatter<range>>(buf, format_str, args);
}

template <
typename String, typename... Args,
std::size_t SIZE = inline_buffer_size,
Expand Down Expand Up @@ -3563,19 +3557,31 @@ inline format_to_n_result<OutputIt> vformat_to_n(
end of the output range.
\endrst
*/
template <typename OutputIt, typename... Args>
inline format_to_n_result<OutputIt> format_to_n(
OutputIt out, std::size_t n, string_view format_str, const Args &... args) {
return vformat_to_n<OutputIt>(
out, n, format_str, make_format_to_n_args<OutputIt>(args...));
template <typename OutputIt, typename String, typename... Args>
inline typename std::enable_if<
internal::is_format_string<String>::value &&
std::is_same<FMT_CHAR(String), char>::value,
format_to_n_result<OutputIt>>::type format_to_n(
OutputIt out, std::size_t n, const String &format_str, const Args &... args) {
internal::check_format_string<Args...>(format_str);
typedef FMT_CHAR(String) char_t;
typedef format_to_n_context<OutputIt> context_t;
format_arg_store<context_t, Args...> as{ args... };
return vformat_to_n<OutputIt>(out, n, basic_string_view<char_t>(format_str), as);
}
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) {

template <typename OutputIt, typename String, typename... Args>
inline typename std::enable_if<
internal::is_format_string<String>::value &&
!std::is_same<FMT_CHAR(String), char>::value,
format_to_n_result<OutputIt>>::type format_to_n(
OutputIt out, std::size_t n, const String &format_str, const Args &... args) {
internal::check_format_string<Args...>(format_str);
typedef FMT_CHAR(String) char_t;
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...));
typedef typename format_context_t<It, char_t>::type context_t;
format_arg_store<context_t, Args...> as{ args... };
auto it = vformat_to(It(out, n), basic_string_view<char_t>(format_str), as);
return {it.base(), it.count()};
}

Expand Down
20 changes: 20 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2401,6 +2401,26 @@ TEST(FormatTest, FormatStringErrors) {
"cannot switch from automatic to manual argument indexing",
int, int);
}

TEST(FormatTest, VFormatTo) {
typedef fmt::format_context context;
fmt::basic_format_arg<context> arg = fmt::internal::make_arg<context>(42);
fmt::basic_format_args<context> args(&arg, 1);
std::string s;
fmt::vformat_to(std::back_inserter(s), "{}", args);
EXPECT_EQ("42", s);
s.clear();
fmt::vformat_to(std::back_inserter(s), FMT_STRING("{}"), args);
EXPECT_EQ("42", s);

typedef fmt::wformat_context wcontext;
fmt::basic_format_arg<wcontext> warg = fmt::internal::make_arg<wcontext>(42);
fmt::basic_format_args<wcontext> wargs(&warg, 1);
std::wstring w;
fmt::vformat_to(std::back_inserter(w), L"{}", wargs);
EXPECT_EQ(L"42", w);
}

#endif // FMT_USE_CONSTEXPR

TEST(FormatTest, ConstructU8StringViewFromCString) {
Expand Down

0 comments on commit ee2e8db

Please sign in to comment.