mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 21:16:56 +00:00
Implement println
(#3267)
This commit is contained in:
parent
9409b2e4d8
commit
87c066a35b
@ -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
|
||||
|
@ -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_
|
||||
|
@ -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*.
|
||||
*/
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user