Detemplatize printf more

This commit is contained in:
Victor Zverovich 2023-05-17 12:34:12 -07:00
parent 0bf6ed7e1d
commit 821f8cdb45
2 changed files with 12 additions and 18 deletions

View File

@ -573,12 +573,6 @@ inline auto make_printf_args(const T&... args)
return {args...};
}
/**
\rst
Constructs an `~fmt::format_arg_store` object that contains references to
arguments and can be implicitly converted to `~fmt::wprintf_args`.
\endrst
*/
// DEPRECATED!
template <typename... T>
inline auto make_wprintf_args(const T&... args)
@ -586,13 +580,13 @@ inline auto make_wprintf_args(const T&... args)
return {args...};
}
template <typename S, typename Char = char_t<S>>
template <typename Char>
inline auto vsprintf(
const S& fmt,
basic_string_view<Char> fmt,
basic_format_args<basic_printf_context<type_identity_t<Char>>> args)
-> std::basic_string<Char> {
auto buf = basic_memory_buffer<Char>();
detail::vprintf(buf, detail::to_string_view(fmt), args);
detail::vprintf(buf, fmt, args);
return to_string(buf);
}
@ -612,13 +606,13 @@ inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string<Char> {
fmt::make_format_args<basic_printf_context<Char>>(args...));
}
template <typename S, typename Char = char_t<S>>
template <typename Char>
inline auto vfprintf(
std::FILE* f, const S& fmt,
std::FILE* f, basic_string_view<Char> fmt,
basic_format_args<basic_printf_context<type_identity_t<Char>>> args)
-> int {
auto buf = basic_memory_buffer<Char>();
detail::vprintf(buf, detail::to_string_view(fmt), args);
detail::vprintf(buf, fmt, args);
size_t size = buf.size();
return std::fwrite(buf.data(), sizeof(Char), size, f) < size
? -1
@ -636,9 +630,8 @@ inline auto vfprintf(
*/
template <typename S, typename... T, typename Char = char_t<S>>
inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int {
using context = basic_printf_context<Char>;
return vfprintf(f, detail::to_string_view(fmt),
fmt::make_format_args<context>(args...));
fmt::make_format_args<basic_printf_context<Char>>(args...));
}
template <typename Char>

View File

@ -532,8 +532,9 @@ TEST(printf_test, vprintf) {
int n = 42;
auto store = fmt::format_arg_store<fmt::printf_context, int>(n);
auto args = fmt::basic_format_args<fmt::printf_context>(store);
EXPECT_EQ(fmt::vsprintf("%d", args), "42");
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, "%d", args), "42");
EXPECT_EQ(fmt::vsprintf(fmt::string_view("%d"), args), "42");
EXPECT_WRITE(stdout, fmt::vfprintf(stdout, fmt::string_view("%d"), args),
"42");
}
template <typename... Args>
@ -551,9 +552,9 @@ TEST(printf_test, fixed_large_exponent) {
TEST(printf_test, make_printf_args) {
EXPECT_EQ("[42] something happened",
fmt::vsprintf("[%d] %s happened",
fmt::vsprintf(fmt::string_view("[%d] %s happened"),
{fmt::make_printf_args(42, "something")}));
EXPECT_EQ(L"[42] something happened",
fmt::vsprintf(L"[%d] %s happened",
fmt::vsprintf(fmt::basic_string_view<wchar_t>(L"[%d] %s happened"),
{fmt::make_wprintf_args(42, L"something")}));
}