Implement println (#3267)

This commit is contained in:
Shawn Zhong 2023-01-24 14:30:00 -06:00 committed by GitHub
parent 9409b2e4d8
commit 87c066a35b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 7 deletions

View File

@ -3009,6 +3009,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

View File

@ -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_

View File

@ -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*.
*/

View File

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

View File

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

View File

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