Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement println #3267

Merged
merged 1 commit into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3001,6 +3001,36 @@ FMT_INLINE void print(std::FILE* f, format_string<T...> fmt, T&&... args) {
: detail::vprint_mojibake(f, fmt, vargs);
}

/**
\rst
Formats ``args`` according to specifications in ``fmt`` and writes the
output to the file ``f`` followed by a newline.

**Example**::

fmt::println(stderr, "Don't {}!", "panic");
\endrst
*/
template <typename... T>
FMT_INLINE void println(std::FILE* f, format_string<T...> fmt, T&&... args) {
return fmt::print(f, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
}

/**
\rst
Formats ``args`` according to specifications in ``fmt`` and writes the output
to ``stdout`` followed by a newline.

**Example**::

fmt::println("Elapsed time: {0:.2f} seconds", 1.23);
\endrst
*/
template <typename... T>
FMT_INLINE void println(format_string<T...> fmt, T&&... args) {
return fmt::println(stdout, fmt, std::forward<T>(args)...);
}

FMT_MODULE_EXPORT_END
FMT_GCC_PRAGMA("GCC pop_options")
FMT_END_NAMESPACE
Expand Down
13 changes: 13 additions & 0 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ void print(std::wostream& os,
vprint(os, fmt, fmt::make_format_args<buffer_context<wchar_t>>(args...));
}

FMT_MODULE_EXPORT template <typename... T>
void println(std::ostream& os, format_string<T...> fmt, T&&... args) {
print(os, "{}\n", fmt::format(fmt, std::forward<T>(args)...));
}

FMT_MODULE_EXPORT
template <typename... Args>
void println(std::wostream& os,
basic_format_string<wchar_t, type_identity_t<Args>...> fmt,
Args&&... args) {
print(os, L"{}\n", fmt::format(fmt, std::forward<Args>(args)...));
}

FMT_END_NAMESPACE

#endif // FMT_OSTREAM_H_
9 changes: 9 additions & 0 deletions include/fmt/xchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ template <typename... T> void print(wformat_string<T...> fmt, T&&... args) {
return vprint(wstring_view(fmt), fmt::make_wformat_args(args...));
}

template <typename... T>
void println(std::FILE* f, wformat_string<T...> fmt, T&&... args) {
return print(f, L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
}

template <typename... T> void println(wformat_string<T...> fmt, T&&... args) {
return print(L"{}\n", fmt::format(fmt, std::forward<T>(args)...));
}

/**
Converts *value* to ``std::wstring`` using the default format for type *T*.
*/
Expand Down
3 changes: 3 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,9 @@ TEST(format_test, print) {
EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!");
EXPECT_WRITE(stderr, fmt::print(stderr, "Don't {}!", "panic"),
"Don't panic!");
EXPECT_WRITE(stdout, fmt::println("Don't {}!", "panic"), "Don't panic!\n");
EXPECT_WRITE(stderr, fmt::println(stderr, "Don't {}!", "panic"),
"Don't panic!\n");
}

TEST(format_test, variadic) {
Expand Down
14 changes: 11 additions & 3 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,17 @@ TEST(ostream_test, empty_custom_output) {
}

TEST(ostream_test, print) {
std::ostringstream os;
fmt::print(os, "Don't {}!", "panic");
EXPECT_EQ("Don't panic!", os.str());
{
std::ostringstream os;
fmt::print(os, "Don't {}!", "panic");
EXPECT_EQ("Don't panic!", os.str());
}

{
std::ostringstream os;
fmt::println(os, "Don't {}!", "panic");
EXPECT_EQ("Don't panic!\n", os.str());
}
}

TEST(ostream_test, write_to_ostream) {
Expand Down
19 changes: 15 additions & 4 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ TEST(xchar_test, named_arg_udl) {

TEST(xchar_test, print) {
// Check that the wide print overload compiles.
if (fmt::detail::const_check(false)) fmt::print(L"test");
if (fmt::detail::const_check(false)) {
fmt::print(L"test");
fmt::println(L"test");
}
}

TEST(xchar_test, join) {
Expand Down Expand Up @@ -382,9 +385,17 @@ TEST(xchar_test, color) {

TEST(xchar_test, ostream) {
#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 409
std::wostringstream wos;
fmt::print(wos, L"Don't {}!", L"panic");
EXPECT_EQ(wos.str(), L"Don't panic!");
{
std::wostringstream wos;
fmt::print(wos, L"Don't {}!", L"panic");
EXPECT_EQ(wos.str(), L"Don't panic!");
}

{
std::wostringstream wos;
fmt::println(wos, L"Don't {}!", L"panic");
EXPECT_EQ(wos.str(), L"Don't panic!\n");
}
#endif
}

Expand Down