Skip to content

Commit

Permalink
Detemplatize printf more
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 17, 2023
1 parent 0bf6ed7 commit 821f8cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
21 changes: 7 additions & 14 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,26 +573,20 @@ 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)
-> format_arg_store<wprintf_context, T...> {
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);
}

Expand All @@ -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
Expand All @@ -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>
Expand Down
9 changes: 5 additions & 4 deletions test/printf-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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")}));
}

0 comments on commit 821f8cd

Please sign in to comment.