Skip to content

Commit

Permalink
Add an overload of print that accepts std::ostream.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 9, 2014
1 parent ed42184 commit e3a44c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 7 additions & 1 deletion format.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Formatting library for C++
Copyright (c) 2012, Victor Zverovich
Copyright (c) 2012 - 2014, Victor Zverovich
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -1122,6 +1122,12 @@ void fmt::print(std::FILE *f, StringRef format, const ArgList &args) {
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) {
char escape[] = "\x1b[30m";
escape[3] = '0' + static_cast<char>(c);
Expand Down
18 changes: 15 additions & 3 deletions format.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
Formatting library for C++
Copyright (c) 2012, Victor Zverovich
Copyright (c) 2012 - 2014, Victor Zverovich
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -128,8 +128,6 @@ class BasicWriter;
typedef BasicWriter<char> Writer;
typedef BasicWriter<wchar_t> WWriter;

struct FormatSpec;

template <typename Char>
class BasicFormatter;

Expand Down Expand Up @@ -751,6 +749,8 @@ class ArgList {
}
};

struct FormatSpec;

// A formatter.
template <typename Char>
class BasicFormatter {
Expand Down Expand Up @@ -1691,6 +1691,17 @@ void print(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>
void printf(BasicWriter<Char> &w,
BasicStringRef<Char> format, const ArgList &args) {
Expand Down Expand Up @@ -1901,6 +1912,7 @@ FMT_VARIADIC(std::string, format, StringRef)
FMT_VARIADIC_W(std::wstring, format, WStringRef)
FMT_VARIADIC(void, print, StringRef)
FMT_VARIADIC(void, print, std::FILE *, StringRef)
FMT_VARIADIC(void, print, std::ostream &, StringRef)
FMT_VARIADIC(void, print_colored, Color, StringRef)
FMT_VARIADIC(std::string, sprintf, StringRef)
FMT_VARIADIC(void, printf, StringRef)
Expand Down
9 changes: 6 additions & 3 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1486,19 +1486,22 @@ TEST(FormatIntTest, FormatDec) {
EXPECT_EQ("42", FormatDec(42ull));
}

#if FMT_USE_FILE_DESCRIPTORS

TEST(FormatTest, Print) {
#if FMT_USE_FILE_DESCRIPTORS
EXPECT_WRITE(stdout, fmt::print("Don't {}!", "panic"), "Don't panic!");
EXPECT_WRITE(stderr,
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) {
EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"),
"\x1b[31mHello, world!\n\x1b[0m");
}

#endif

TEST(FormatTest, Variadic) {
Expand Down

0 comments on commit e3a44c1

Please sign in to comment.