Add an overload of print that accepts std::ostream.

This commit is contained in:
Victor Zverovich
2014-07-09 06:56:36 -07:00
parent ed421848b3
commit e3a44c11f6
3 changed files with 28 additions and 7 deletions

View File

@ -1,7 +1,7 @@
/* /*
Formatting library for C++ Formatting library for C++
Copyright (c) 2012, Victor Zverovich Copyright (c) 2012 - 2014, Victor Zverovich
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -1122,6 +1122,12 @@ void fmt::print(std::FILE *f, StringRef format, const ArgList &args) {
std::fwrite(w.data(), 1, w.size(), f); std::fwrite(w.data(), 1, w.size(), f);
} }
void fmt::print(std::ostream &os, StringRef format, const ArgList &args) {
Writer w;
w.write(format, args);
os.write(w.data(), w.size());
}
void fmt::print_colored(Color c, StringRef format, const ArgList &args) { void fmt::print_colored(Color c, StringRef format, const ArgList &args) {
char escape[] = "\x1b[30m"; char escape[] = "\x1b[30m";
escape[3] = '0' + static_cast<char>(c); escape[3] = '0' + static_cast<char>(c);

View File

@ -1,7 +1,7 @@
/* /*
Formatting library for C++ Formatting library for C++
Copyright (c) 2012, Victor Zverovich Copyright (c) 2012 - 2014, Victor Zverovich
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -128,8 +128,6 @@ class BasicWriter;
typedef BasicWriter<char> Writer; typedef BasicWriter<char> Writer;
typedef BasicWriter<wchar_t> WWriter; typedef BasicWriter<wchar_t> WWriter;
struct FormatSpec;
template <typename Char> template <typename Char>
class BasicFormatter; class BasicFormatter;
@ -751,6 +749,8 @@ class ArgList {
} }
}; };
struct FormatSpec;
// A formatter. // A formatter.
template <typename Char> template <typename Char>
class BasicFormatter { class BasicFormatter {
@ -1691,6 +1691,17 @@ void print(StringRef format, const ArgList &args);
*/ */
void print(std::FILE *f, StringRef format, const ArgList &args); void print(std::FILE *f, StringRef format, const ArgList &args);
/**
\rst
Prints formatted data to a stream.
**Example**::
print(cerr, "Don't {}!", "panic");
\endrst
*/
void print(std::ostream &os, StringRef format, const ArgList &args);
template <typename Char> template <typename Char>
void printf(BasicWriter<Char> &w, void printf(BasicWriter<Char> &w,
BasicStringRef<Char> format, const ArgList &args) { BasicStringRef<Char> format, const ArgList &args) {
@ -1901,6 +1912,7 @@ FMT_VARIADIC(std::string, format, StringRef)
FMT_VARIADIC_W(std::wstring, format, WStringRef) FMT_VARIADIC_W(std::wstring, format, WStringRef)
FMT_VARIADIC(void, print, StringRef) FMT_VARIADIC(void, print, StringRef)
FMT_VARIADIC(void, print, std::FILE *, StringRef) FMT_VARIADIC(void, print, std::FILE *, StringRef)
FMT_VARIADIC(void, print, std::ostream &, StringRef)
FMT_VARIADIC(void, print_colored, Color, StringRef) FMT_VARIADIC(void, print_colored, Color, StringRef)
FMT_VARIADIC(std::string, sprintf, StringRef) FMT_VARIADIC(std::string, sprintf, StringRef)
FMT_VARIADIC(void, printf, StringRef) FMT_VARIADIC(void, printf, StringRef)

View File

@ -1486,19 +1486,22 @@ TEST(FormatIntTest, FormatDec) {
EXPECT_EQ("42", FormatDec(42ull)); EXPECT_EQ("42", FormatDec(42ull));
} }
#if FMT_USE_FILE_DESCRIPTORS
TEST(FormatTest, Print) { TEST(FormatTest, Print) {
#if FMT_USE_FILE_DESCRIPTORS
EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!"); EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!");
EXPECT_WRITE(stderr, EXPECT_WRITE(stderr,
fmt::print(stderr, "Don't {}!", "panic"), "Don't panic!"); fmt::print(stderr, "Don't {}!", "panic"), "Don't panic!");
#endif
std::ostringstream os;
fmt::print(os, "Don't {}!", "panic");
EXPECT_EQ("Don't panic!", os.str());
} }
#if FMT_USE_FILE_DESCRIPTORS
TEST(FormatTest, PrintColored) { TEST(FormatTest, PrintColored) {
EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"), EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"),
"\x1b[31mHello, world!\n\x1b[0m"); "\x1b[31mHello, world!\n\x1b[0m");
} }
#endif #endif
TEST(FormatTest, Variadic) { TEST(FormatTest, Variadic) {