diff --git a/doc/api.rst b/doc/api.rst index c48dfb0c..ba847ae3 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -81,8 +81,6 @@ formatting of user-defined types that have overloaded ``operator<<``:: .. doxygenfunction:: print(std::ostream&, CStringRef, ArgList) -.. doxygenfunction:: fprintf(std::ostream&, CStringRef, ArgList) - Argument formatters ------------------- @@ -140,6 +138,8 @@ argument type doesn't match its format specification. .. doxygenfunction:: fprintf(std::FILE *, CStringRef, ArgList) +.. doxygenfunction:: fprintf(std::ostream&, CStringRef, ArgList) + .. doxygenfunction:: sprintf(CStringRef, ArgList) .. doxygenclass:: fmt::PrintfFormatter diff --git a/fmt/ostream.cc b/fmt/ostream.cc index e1d9acce..2890b4a8 100644 --- a/fmt/ostream.cc +++ b/fmt/ostream.cc @@ -8,13 +8,11 @@ */ #include "fmt/ostream.h" -#include "fmt/printf.h" namespace fmt { -namespace { -// Write the content of w to os. -void write(std::ostream &os, Writer &w) { +namespace internal { +FMT_FUNC void write(std::ostream &os, Writer &w) { const char *data = w.data(); typedef internal::MakeUnsigned::Type UnsignedStreamSize; UnsignedStreamSize size = w.size(); @@ -32,13 +30,6 @@ void write(std::ostream &os, Writer &w) { FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) { MemoryWriter w; w.write(format_str, args); - write(os, w); -} - -FMT_FUNC int fprintf(std::ostream &os, CStringRef format, ArgList args) { - MemoryWriter w; - printf(w, format, args); - write(os, w); - return static_cast(w.size()); + internal::write(os, w); } } // namespace fmt diff --git a/fmt/ostream.h b/fmt/ostream.h index 599d8d72..00b7d717 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -66,6 +66,9 @@ struct ConvertToIntImpl { value = sizeof(convert(get() << get())) == sizeof(No) }; }; + +// Write the content of w to os. +void write(std::ostream &os, Writer &w); } // namespace internal // Formats a value. @@ -94,18 +97,6 @@ void format(BasicFormatter &f, */ FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args); FMT_VARIADIC(void, print, std::ostream &, CStringRef) - -/** - \rst - Prints formatted data to the stream *os*. - - **Example**:: - - fprintf(cerr, "Don't %s!", "panic"); - \endrst - */ -FMT_API int fprintf(std::ostream &os, CStringRef format_str, ArgList args); -FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef) } // namespace fmt #ifdef FMT_HEADER_ONLY diff --git a/fmt/printf.h b/fmt/printf.h index 080b0023..f0c0b9a2 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -13,7 +13,7 @@ #include // std::fill_n #include // std::numeric_limits -#include "fmt/format.h" +#include "fmt/ostream.h" namespace fmt { namespace internal { @@ -536,6 +536,23 @@ inline int printf(CStringRef format, ArgList args) { return fprintf(stdout, format, args); } FMT_VARIADIC(int, printf, CStringRef) + +/** + \rst + Prints formatted data to the stream *os*. + + **Example**:: + + fprintf(cerr, "Don't %s!", "panic"); + \endrst + */ +inline int fprintf(std::ostream &os, CStringRef format_str, ArgList args) { + MemoryWriter w; + printf(w, format_str, args); + internal::write(os, w); + return static_cast(w.size()); +} +FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef) } // namespace fmt #endif // FMT_PRINTF_H_ diff --git a/test/ostream-test.cc b/test/ostream-test.cc index bbcce95e..4081b43f 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -25,7 +25,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "fmt/ostream.cc" +#include "fmt/ostream.h" #include #include "gmock/gmock.h" @@ -35,13 +35,6 @@ using fmt::format; using fmt::FormatError; -template -std::basic_ostream &operator<<( - std::basic_ostream &os, const BasicTestString &s) { - os << s.value(); - return os; -} - std::ostream &operator<<(std::ostream &os, const Date &d) { os << d.year() << '-' << d.month() << '-' << d.day(); return os; @@ -128,22 +121,11 @@ TEST(OStreamTest, Print) { EXPECT_EQ("Don't panic!", os.str()); } -TEST(OStreamTest, PrintfCustom) { - EXPECT_EQ("abc", fmt::sprintf("%s", TestString("abc"))); -} - -TEST(OStreamTest, FPrintf) { - std::ostringstream os; - int ret = fmt::fprintf(os, "Don't %s!", "panic"); - EXPECT_EQ("Don't panic!", os.str()); - EXPECT_EQ(12, ret); -} - TEST(OStreamTest, WriteToOStream) { std::ostringstream os; fmt::MemoryWriter w; w << "foo"; - fmt::write(os, w); + fmt::internal::write(os, w); EXPECT_EQ("foo", os.str()); } @@ -188,5 +170,5 @@ TEST(OStreamTest, WriteToOStreamMaxSize) { data += n; size -= static_cast(n); } while (size != 0); - fmt::write(os, w); + fmt::internal::write(os, w); } diff --git a/test/printf-test.cc b/test/printf-test.cc index 2e3f19c7..6bba02e1 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -479,3 +479,14 @@ TEST(PrintfTest, PrintfError) { TEST(PrintfTest, WideString) { EXPECT_EQ(L"abc", fmt::sprintf(L"%s", L"abc")); } + +TEST(PrintfTest, PrintfCustom) { + EXPECT_EQ("abc", fmt::sprintf("%s", TestString("abc"))); +} + +TEST(PrintfTest, OStream) { + std::ostringstream os; + int ret = fmt::fprintf(os, "Don't %s!", "panic"); + EXPECT_EQ("Don't panic!", os.str()); + EXPECT_EQ(12, ret); +} diff --git a/test/util.h b/test/util.h index 21d76b2d..b7faf62a 100644 --- a/test/util.h +++ b/test/util.h @@ -87,6 +87,13 @@ const Char BasicTestString::EMPTY[] = {0}; typedef BasicTestString TestString; typedef BasicTestString TestWString; +template +std::basic_ostream &operator<<( + std::basic_ostream &os, const BasicTestString &s) { + os << s.value(); + return os; +} + class Date { int year_, month_, day_; public: