Commit 0a96c032 authored by Victor Zverovich's avatar Victor Zverovich

Parameterize v*printf on string type (#920)

parent 61e6d2e3
...@@ -1398,7 +1398,7 @@ typename std::enable_if< ...@@ -1398,7 +1398,7 @@ typename std::enable_if<
const S &format_str, const S &format_str,
basic_format_args<typename buffer_context<FMT_CHAR(S)>::type> args) { basic_format_args<typename buffer_context<FMT_CHAR(S)>::type> args) {
internal::container_buffer<Container> buf(internal::get_container(out)); internal::container_buffer<Container> buf(internal::get_container(out));
vformat_to(buf, to_string_view(format_str), args); internal::vformat_to(buf, to_string_view(format_str), args);
return out; return out;
} }
......
...@@ -943,14 +943,14 @@ FMT_FUNC void report_windows_error( ...@@ -943,14 +943,14 @@ FMT_FUNC void report_windows_error(
FMT_FUNC void vprint(std::FILE *f, string_view format_str, format_args args) { FMT_FUNC void vprint(std::FILE *f, string_view format_str, format_args args) {
memory_buffer buffer; memory_buffer buffer;
vformat_to(buffer, format_str, internal::vformat_to(buffer, format_str,
basic_format_args<buffer_context<char>::type>(args)); basic_format_args<buffer_context<char>::type>(args));
std::fwrite(buffer.data(), 1, buffer.size(), f); std::fwrite(buffer.data(), 1, buffer.size(), f);
} }
FMT_FUNC void vprint(std::FILE *f, wstring_view format_str, wformat_args args) { FMT_FUNC void vprint(std::FILE *f, wstring_view format_str, wformat_args args) {
wmemory_buffer buffer; wmemory_buffer buffer;
vformat_to(buffer, format_str, args); internal::vformat_to(buffer, format_str, args);
std::fwrite(buffer.data(), sizeof(wchar_t), buffer.size(), f); std::fwrite(buffer.data(), sizeof(wchar_t), buffer.size(), f);
} }
......
...@@ -3378,7 +3378,7 @@ template <typename S, typename Char = FMT_CHAR(S)> ...@@ -3378,7 +3378,7 @@ template <typename S, typename Char = FMT_CHAR(S)>
inline typename buffer_context<Char>::type::iterator vformat_to( inline typename buffer_context<Char>::type::iterator vformat_to(
internal::basic_buffer<Char> &buf, const S &format_str, internal::basic_buffer<Char> &buf, const S &format_str,
basic_format_args<typename buffer_context<Char>::type> args) { basic_format_args<typename buffer_context<Char>::type> args) {
return vformat_to(buf, to_string_view(format_str), args); return internal::vformat_to(buf, to_string_view(format_str), args);
} }
template < template <
...@@ -3391,8 +3391,8 @@ inline typename buffer_context<Char>::type::iterator format_to( ...@@ -3391,8 +3391,8 @@ inline typename buffer_context<Char>::type::iterator format_to(
internal::check_format_string<Args...>(format_str); internal::check_format_string<Args...>(format_str);
typedef typename buffer_context<Char>::type context; typedef typename buffer_context<Char>::type context;
format_arg_store<context, Args...> as{args...}; format_arg_store<context, Args...> as{args...};
return vformat_to(buf, to_string_view(format_str), return internal::vformat_to(buf, to_string_view(format_str),
basic_format_args<context>(as)); basic_format_args<context>(as));
} }
template <typename OutputIt, typename Char = char> template <typename OutputIt, typename Char = char>
...@@ -3495,7 +3495,7 @@ inline std::basic_string<Char> internal::vformat( ...@@ -3495,7 +3495,7 @@ inline std::basic_string<Char> internal::vformat(
basic_string_view<Char> format_str, basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args) { basic_format_args<typename buffer_context<Char>::type> args) {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
vformat_to(buffer, format_str, args); internal::vformat_to(buffer, format_str, args);
return fmt::to_string(buffer); return fmt::to_string(buffer);
} }
......
...@@ -129,7 +129,7 @@ inline void vprint(std::basic_ostream<Char> &os, ...@@ -129,7 +129,7 @@ inline void vprint(std::basic_ostream<Char> &os,
basic_string_view<Char> format_str, basic_string_view<Char> format_str,
basic_format_args<typename buffer_context<Char>::type> args) { basic_format_args<typename buffer_context<Char>::type> args) {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
vformat_to(buffer, format_str, args); internal::vformat_to(buffer, format_str, args);
internal::write(os, buffer); internal::write(os, buffer);
} }
/** /**
......
...@@ -580,13 +580,13 @@ struct printf_context { ...@@ -580,13 +580,13 @@ struct printf_context {
typedef basic_format_args<printf_context<internal::buffer>::type> printf_args; typedef basic_format_args<printf_context<internal::buffer>::type> printf_args;
typedef basic_format_args<printf_context<internal::wbuffer>::type> wprintf_args; typedef basic_format_args<printf_context<internal::wbuffer>::type> wprintf_args;
template <typename Char> template <typename S, typename Char = FMT_CHAR(S)>
inline std::basic_string<Char> inline std::basic_string<Char>
vsprintf(basic_string_view<Char> format, vsprintf(const S &format,
basic_format_args<typename printf_context< basic_format_args<typename printf_context<
internal::basic_buffer<Char>>::type> args) { internal::basic_buffer<Char>>::type> args) {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
printf(buffer, format, args); printf(buffer, to_string_view(format), args);
return to_string(buffer); return to_string(buffer);
} }
...@@ -601,21 +601,21 @@ vsprintf(basic_string_view<Char> format, ...@@ -601,21 +601,21 @@ vsprintf(basic_string_view<Char> format,
*/ */
template <typename S, typename... Args> template <typename S, typename... Args>
inline FMT_ENABLE_IF_STRING(S, std::basic_string<FMT_CHAR(S)>) inline FMT_ENABLE_IF_STRING(S, std::basic_string<FMT_CHAR(S)>)
sprintf(const S &format_str, const Args & ... args) { sprintf(const S &format, const Args & ... args) {
internal::check_format_string<Args...>(format_str); internal::check_format_string<Args...>(format);
typedef internal::basic_buffer<FMT_CHAR(S)> buffer; typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
typedef typename printf_context<buffer>::type context; typedef typename printf_context<buffer>::type context;
format_arg_store<context, Args...> as{ args... }; format_arg_store<context, Args...> as{ args... };
return vsprintf(to_string_view(format_str), return vsprintf(to_string_view(format),
basic_format_args<context>(as)); basic_format_args<context>(as));
} }
template <typename Char> template <typename S, typename Char = FMT_CHAR(S)>
inline int vfprintf(std::FILE *f, basic_string_view<Char> format, inline int vfprintf(std::FILE *f, const S &format,
basic_format_args<typename printf_context< basic_format_args<typename printf_context<
internal::basic_buffer<Char>>::type> args) { internal::basic_buffer<Char>>::type> args) {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
printf(buffer, format, args); printf(buffer, to_string_view(format), args);
std::size_t size = buffer.size(); std::size_t size = buffer.size();
return std::fwrite( return std::fwrite(
buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size); buffer.data(), sizeof(Char), size, f) < size ? -1 : static_cast<int>(size);
...@@ -632,20 +632,20 @@ inline int vfprintf(std::FILE *f, basic_string_view<Char> format, ...@@ -632,20 +632,20 @@ inline int vfprintf(std::FILE *f, basic_string_view<Char> format,
*/ */
template <typename S, typename... Args> template <typename S, typename... Args>
inline FMT_ENABLE_IF_STRING(S, int) inline FMT_ENABLE_IF_STRING(S, int)
fprintf(std::FILE *f, const S &format_str, const Args & ... args) { fprintf(std::FILE *f, const S &format, const Args & ... args) {
internal::check_format_string<Args...>(format_str); internal::check_format_string<Args...>(format);
typedef internal::basic_buffer<FMT_CHAR(S)> buffer; typedef internal::basic_buffer<FMT_CHAR(S)> buffer;
typedef typename printf_context<buffer>::type context; typedef typename printf_context<buffer>::type context;
format_arg_store<context, Args...> as{ args... }; format_arg_store<context, Args...> as{ args... };
return vfprintf(f, to_string_view(format_str), return vfprintf(f, to_string_view(format),
basic_format_args<context>(as)); basic_format_args<context>(as));
} }
template <typename Char> template <typename S, typename Char = FMT_CHAR(S)>
inline int vprintf(basic_string_view<Char> format, inline int vprintf(const S &format,
basic_format_args<typename printf_context< basic_format_args<typename printf_context<
internal::basic_buffer<Char>>::type> args) { internal::basic_buffer<Char>>::type> args) {
return vfprintf(stdout, format, args); return vfprintf(stdout, to_string_view(format), args);
} }
/** /**
...@@ -668,13 +668,13 @@ inline FMT_ENABLE_IF_STRING(S, int) ...@@ -668,13 +668,13 @@ inline FMT_ENABLE_IF_STRING(S, int)
basic_format_args<context>(as)); basic_format_args<context>(as));
} }
template <typename Char> template <typename S, typename Char = FMT_CHAR(S)>
inline int vfprintf(std::basic_ostream<Char> &os, inline int vfprintf(std::basic_ostream<Char> &os,
basic_string_view<Char> format_str, const S &format,
basic_format_args<typename printf_context< basic_format_args<typename printf_context<
internal::basic_buffer<Char>>::type> args) { internal::basic_buffer<Char>>::type> args) {
basic_memory_buffer<Char> buffer; basic_memory_buffer<Char> buffer;
printf(buffer, format_str, args); printf(buffer, to_string_view(format), args);
internal::write(os, buffer); internal::write(os, buffer);
return static_cast<int>(buffer.size()); return static_cast<int>(buffer.size());
} }
......
...@@ -499,3 +499,13 @@ TEST(PrintfTest, OStream) { ...@@ -499,3 +499,13 @@ TEST(PrintfTest, OStream) {
EXPECT_EQ("Don't panic!", os.str()); EXPECT_EQ("Don't panic!", os.str());
EXPECT_EQ(12, ret); EXPECT_EQ(12, ret);
} }
TEST(PrintfTest, VPrintf) {
typedef fmt::printf_context<fmt::internal::buffer>::type context;
fmt::format_arg_store<context, int> as{42};
fmt::basic_format_args<context> args(as);
EXPECT_EQ(fmt::vsprintf("%d", args), "42");
EXPECT_WRITE(stdout, fmt::vprintf("%d", args), "42");
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
EXPECT_WRITE(stdout, fmt::vfprintf(std::cout, "%d", args), "42");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment