Parameterize core functions on the type of the format string.

Take #2 of n

Signed-off-by: Daniela Engert <dani@ngrt.de>
This commit is contained in:
Daniela Engert 2018-09-30 08:58:59 +02:00 committed by Victor Zverovich
parent 0f98de3011
commit deb901b9e4
3 changed files with 46 additions and 45 deletions

View File

@ -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>
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,
string_view format_str, format_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);
return 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,
wstring_view format_str, wformat_args 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;
}
@ -1366,8 +1357,8 @@ inline typename std::enable_if<
std::back_insert_iterator<Container>>::type
format_to(std::back_insert_iterator<Container> out, const S &format_str,
const Args &... args) {
return vformat_to(out, internal::to_string_view(format_str),
internal::checked_args<S, Args...>(format_str, args...));
internal::checked_args<S, Args...> ca(format_str, args...);
return vformat_to(out, internal::to_string_view(format_str), *ca);
}
template <typename S, typename Char = FMT_CHAR(S)>

View File

@ -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,
@ -3534,23 +3528,23 @@ struct format_to_n_result {
std::size_t size;
};
template <typename OutputIt>
template <typename OutputIt, typename Char = typename OutputIt::value_type>
using format_to_n_context = typename fmt::format_context_t<
fmt::internal::truncating_iterator<OutputIt>>::type;
fmt::internal::truncating_iterator<OutputIt>, Char>::type;
template <typename OutputIt>
using format_to_n_args = fmt::basic_format_args<format_to_n_context<OutputIt>>;
template <typename OutputIt, typename Char = typename OutputIt::value_type>
using format_to_n_args = fmt::basic_format_args<format_to_n_context<OutputIt, Char>>;
template <typename OutputIt, typename ...Args>
inline format_arg_store<format_to_n_context<OutputIt>, Args...>
template <typename OutputIt, typename Char, typename ...Args>
inline format_arg_store<format_to_n_context<OutputIt, Char>, Args...>
make_format_to_n_args(const Args &... args) {
return format_arg_store<format_to_n_context<OutputIt>, Args...>(args...);
return format_arg_store<format_to_n_context<OutputIt, Char>, Args...>(args...);
}
template <typename OutputIt, typename... Args>
template <typename OutputIt, typename Char, typename... Args>
inline format_to_n_result<OutputIt> vformat_to_n(
OutputIt out, std::size_t n, string_view format_str,
format_to_n_args<OutputIt> args) {
OutputIt out, std::size_t n, basic_string_view<Char> format_str,
format_to_n_args<OutputIt, Char> args) {
typedef internal::truncating_iterator<OutputIt> It;
auto it = vformat_to(It(out, n), format_str, args);
return {it.base(), it.count()};
@ -3563,20 +3557,16 @@ 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... 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()};
template <typename OutputIt, typename String, typename... Args>
inline typename std::enable_if<
internal::is_format_string<String>::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;
format_arg_store<format_to_n_context<OutputIt, Char>, Args...> as{ args... };
return vformat_to_n(out, n, internal::to_string_view(format_str),
format_to_n_args<OutputIt, Char>(as));
}
template <typename Char>

View File

@ -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) {