diff --git a/format.cc b/format.cc index 566a0104..1ccaa74f 100644 --- a/format.cc +++ b/format.cc @@ -1104,15 +1104,6 @@ void fmt::ReportWinError( } #endif -void fmt::ANSITerminalSink::operator()( - const fmt::BasicWriter &w) const { - char escape[] = "\x1b[30m"; - escape[3] = '0' + static_cast(color_); - std::fputs(escape, file_); - std::fwrite(w.data(), 1, w.size(), file_); - std::fputs(RESET_COLOR, file_); -} - void fmt::print(StringRef format, const ArgList &args) { Writer w; w.write(format, args); @@ -1125,6 +1116,14 @@ void fmt::print(std::FILE *f, StringRef format, const ArgList &args) { std::fwrite(w.data(), 1, w.size(), f); } +void fmt::print_colored(Color c, StringRef format, const ArgList &args) { + char escape[] = "\x1b[30m"; + escape[3] = '0' + static_cast(c); + std::fputs(escape, stdout); + print(format, args); + std::fputs(RESET_COLOR, stdout); +} + void fmt::printf(StringRef format, const ArgList &args) { Writer w; printf(w, format, args); diff --git a/format.h b/format.h index c4182588..7704c3c9 100644 --- a/format.h +++ b/format.h @@ -119,16 +119,7 @@ TypeName(const TypeName&); \ void operator=(const TypeName&) -#ifdef FMT_DEPRECATED -// Do nothing. -#elif defined(__GNUC__) -# define FMT_DEPRECATED(func) func __attribute__((deprecated)) -#elif defined(_MSC_VER) -# define FMT_DEPRECATED(func) __declspec(deprecated) func -#else -# define FMT_DEPRECATED(func) func -#endif - +// TODO: remove #if FMT_MSC_VER # pragma warning(push) # pragma warning(disable: 4521) // 'class' : multiple copy constructors specified @@ -175,7 +166,7 @@ struct FormatSpec; different types of strings to a function, for example:: template - Writer format(StringRef format, const Args & ... args); + std::string format(StringRef format, const Args & ... args); format("{}", 42); format(std::string("{}"), 42); @@ -1451,32 +1442,8 @@ class BasicWriter { } void clear() { buffer_.clear(); } - -#if !defined(FMT_NO_DEPRECATED) - FMT_DEPRECATED(BasicFormatter Format(StringRef format)); - -#if FMT_USE_VARIADIC_TEMPLATES - // This function is deprecated. Use Writer::write instead. - template - FMT_DEPRECATED(void Format(BasicStringRef format, const Args & ... args)); -#endif - - // This function is deprecated. Use Writer::write instead. - FMT_DEPRECATED(void Write(const std::basic_string &s, const FormatSpec &spec)); - - // This function is deprecated. Use Writer::clear instead. - FMT_DEPRECATED(void Clear()); -#endif }; -template -inline void BasicWriter::Write(const std::basic_string &s, const FormatSpec &spec) { - write(s, spec); -} - -template -inline void BasicWriter::Clear() { clear(); } - template template typename BasicWriter::CharPtr BasicWriter::write_str( @@ -1645,12 +1612,6 @@ void BasicWriter::FormatInt(T value, const Spec &spec) { } } -template -BasicFormatter BasicWriter::Format(StringRef format) { - BasicFormatter f(*this, format.c_str()); - return f; -} - // The default formatting function. template void format(BasicWriter &w, const FormatSpec &spec, const T &value) { @@ -1762,148 +1723,12 @@ class BasicFormatter { } }; -template -FMT_DEPRECATED(std::basic_string str(const BasicWriter &f)); - -// This function is deprecated. Use BasicWriter::str() instead. -template -inline std::basic_string str(const BasicWriter &f) { - return f.str(); -} - -template -FMT_DEPRECATED(const Char *c_str(const BasicWriter &f)); - -// This function is deprecated. Use BasicWriter::c_str() instead. -template -inline const Char *c_str(const BasicWriter &f) { return f.c_str(); } - -FMT_DEPRECATED(std::string str(StringRef s)); - -/** - Converts a string reference to `std::string`. - */ -// This function is deprecated. Use StringRef::c_str() instead. -inline std::string str(StringRef s) { - return std::string(s.c_str(), s.size()); -} - -FMT_DEPRECATED(const char *c_str(StringRef s)); - -/** - Returns the pointer to a C string. - */ -// This function is deprecated. Use StringRef::c_str() instead. -inline const char *c_str(StringRef s) { - return s.c_str(); -} - -FMT_DEPRECATED(std::wstring str(WStringRef s)); - -// This function is deprecated. Use WStringRef::c_str() instead. -inline std::wstring str(WStringRef s) { - return std::wstring(s.c_str(), s.size()); -} - -FMT_DEPRECATED(const wchar_t *c_str(WStringRef s)); - -// This function is deprecated. Use WStringRef::c_str() instead. -inline const wchar_t *c_str(WStringRef s) { - return s.c_str(); -} - -// This class is deprecated. Use variadic functions instead of sinks. -class NullSink { - public: - template - void operator()(const BasicWriter &) const {} -}; - -// This class is deprecated. Use variadic functions instead. -template -class Formatter : private Sink, public BasicFormatter { - private: - BasicWriter writer_; - bool inactive_; - - FMT_DISALLOW_COPY_AND_ASSIGN(Formatter); - - public: - explicit Formatter(BasicStringRef format, Sink s = Sink()) - : Sink(s), BasicFormatter(writer_, format.c_str()), - inactive_(false) { - } - - Formatter(Formatter &other) - : Sink(other), BasicFormatter(writer_, other.TakeFormatString()), - inactive_(false) { - other.inactive_ = true; - } - - ~Formatter() FMT_NOEXCEPT(false) { - if (!inactive_) { - this->CompleteFormatting(); - (*this)(writer_); - } - } -}; - -#if !defined(FMT_NO_DEPRECATED) -// This function is deprecated. Use fmt::format instead. -FMT_DEPRECATED(Formatter<> Format(StringRef format)); -inline Formatter<> Format(StringRef format) { - Formatter<> f(format); - return f; -} - -// This function is deprecated. Use fmt::format instead. -Formatter FMT_DEPRECATED(Format(WStringRef format)); -inline Formatter Format(WStringRef format) { - Formatter f(format); - return f; -} - -// This class is deprecated. Use variadic functions instead of sinks. -class SystemErrorSink { - private: - int error_code_; - - public: - explicit SystemErrorSink(int error_code) : error_code_(error_code) {} - - void operator()(const Writer &w) const { - throw SystemError(error_code_, "{}", w.c_str()); - } -}; -#endif - -FMT_DEPRECATED(Formatter ThrowSystemError( - int error_code, StringRef format)); - -// This function is deprecated. Use fmt::SystemError instead. -inline Formatter ThrowSystemError( - int error_code, StringRef format) { - Formatter f(format, SystemErrorSink(error_code)); - return f; -} - // Reports a system error without throwing an exception. // Can be used to report errors from destructors. void ReportSystemError(int error_code, StringRef message) FMT_NOEXCEPT(true); #ifdef _WIN32 -// This class is deprecated. Use variadic functions instead of sinks. -class WinErrorSink { - private: - int error_code_; - - public: - explicit WinErrorSink(int error_code) : error_code_(error_code) {} - - void operator()(const Writer &w) const; -}; - /** A Windows error. */ @@ -1927,68 +1752,21 @@ class WindowsError : public SystemError { FMT_VARIADIC_CTOR(WindowsError, init, int, StringRef) }; -FMT_DEPRECATED(Formatter ThrowWinError(int error_code, StringRef format)); - -// This function is deprecated. Use WindowsError instead. -inline Formatter ThrowWinError(int error_code, StringRef format) { - Formatter f(format, WinErrorSink(error_code)); - return f; -} - // Reports a Windows error without throwing an exception. // Can be used to report errors from destructors. void ReportWinError(int error_code, StringRef message) FMT_NOEXCEPT(true); #endif -// This class is deprecated. Use variadic functions instead of sinks. -class FileSink { - private: - std::FILE *file_; - - public: - explicit FileSink(std::FILE *f) : file_(f) {} - - void operator()(const BasicWriter &w) const { - if (std::fwrite(w.data(), w.size(), 1, file_) == 0) - throw SystemError(errno, "cannot write to file"); - } -}; - -inline Formatter Print(StringRef format) { - Formatter f(format, FileSink(stdout)); - return f; -} - -inline Formatter Print(std::FILE *file, StringRef format) { - Formatter f(format, FileSink(file)); - return f; -} - enum Color { BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE }; -// This class is deprecated. Use variadic functions instead of sinks. -class ANSITerminalSink { - private: - std::FILE *file_; - Color color_; - - public: - ANSITerminalSink(std::FILE *f, Color c) : file_(f), color_(c) {} - - void operator()(const BasicWriter &w) const; -}; - /** Formats a string and prints it to stdout using ANSI escape sequences to specify color (experimental). Example: PrintColored(fmt::RED, "Elapsed time: {0:.2f} seconds") << 1.23; */ -inline Formatter PrintColored(Color c, StringRef format) { - Formatter f(format, ANSITerminalSink(stdout, c)); - return f; -} +void print_colored(Color c, StringRef format, const ArgList &args); /** \rst @@ -2051,61 +1829,6 @@ inline std::string sprintf(StringRef format, const ArgList &args) { void printf(StringRef format, const ArgList &args); -#if !defined(FMT_NO_DEPRECATED) && FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES - -template -template -void BasicWriter::Format( - BasicStringRef format, const Args & ... args) { - this->format(format, args...); -} - -// This function is deprecated. Use fmt::format instead. -template -FMT_DEPRECATED(Writer Format(StringRef format, const Args & ... args)); - -template -inline Writer Format(StringRef format, const Args & ... args) { - Writer w; - w.Format(format, args...); - return std::move(w); -} - -// This function is deprecated. Use fmt::format instead. -template -FMT_DEPRECATED(WWriter Format(WStringRef format, const Args & ... args)); - -template -inline WWriter Format(WStringRef format, const Args & ... args) { - WWriter w; - w.Format(format, args...); - return std::move(w); -} - -// This function is deprecated. Use fmt::print instead. -template -FMT_DEPRECATED(void Print(StringRef format, const Args & ... args)); - -template -void Print(StringRef format, const Args & ... args) { - Writer w; - w.write(format, args...); - std::fwrite(w.data(), 1, w.size(), stdout); -} - -// This function is deprecated. Use fmt::print instead. -template -FMT_DEPRECATED(void Print(std::FILE *f, StringRef format, const Args & ... args)); - -template -void Print(std::FILE *f, StringRef format, const Args & ... args) { - Writer w; - w.Format(format, args...); - std::fwrite(w.data(), 1, w.size(), f); -} - -#endif // FMT_USE_VARIADIC_TEMPLATES && FMT_USE_RVALUE_REFERENCES - /** Fast integer formatter. */ @@ -2306,6 +2029,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_colored, Color, StringRef) FMT_VARIADIC(std::string, sprintf, StringRef) FMT_VARIADIC(void, printf, StringRef) } diff --git a/test/format-test.cc b/test/format-test.cc index f8acaa81..b8ec32e8 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1232,7 +1232,7 @@ TEST(FormatterTest, FormatNaN) { if (fmt::internal::SignBitNoInline(-nan)) EXPECT_EQ("-nan", format("{}", -nan)); else - fmt::Print("Warning: compiler doesn't handle negative NaN correctly"); + fmt::print("Warning: compiler doesn't handle negative NaN correctly"); EXPECT_EQ(" nan", format("{: }", nan)); EXPECT_EQ("NAN", format("{:F}", nan)); EXPECT_EQ("nan ", format("{:<7}", nan)); @@ -1386,100 +1386,6 @@ TEST(StringRefTest, ConvertToString) { EXPECT_EQ("abc", s); } -TEST(FormatterTest, Ctor) { - fmt::Formatter<> f1("test"); - fmt::Formatter<> f1copy(f1); - fmt::Formatter<> f2("test", fmt::NullSink()); - fmt::Formatter f3("test"); - fmt::Formatter f4(L"test"); - fmt::Formatter f4copy(f4); - fmt::Formatter f5(L"test", fmt::NullSink()); -} - -// A sink that counts the number of times the output is written to it. -struct CountingSink { - int &num_writes; - - explicit CountingSink(int &num_writes) : num_writes(num_writes) {} - - void operator()(const Writer &) const { - ++num_writes; - } -}; - -TEST(FormatterTest, Sink) { - int num_writes = 0; - { - fmt::Formatter f("test", CountingSink(num_writes)); - EXPECT_EQ(0, num_writes); - } - EXPECT_EQ(1, num_writes); -} - -TEST(FormatterTest, Move) { - // Test if formatting is performed once if we "move" a formatter. - int num_writes = 0; - { - typedef fmt::Formatter TestFormatter; - TestFormatter *f = new TestFormatter("test", CountingSink(num_writes)); - TestFormatter f2(*f); - delete f; - EXPECT_EQ(0, num_writes); - } - EXPECT_EQ(1, num_writes); -} - -TEST(FormatterTest, OutputNotWrittenOnError) { - int num_writes = 0; - { - typedef fmt::Formatter TestFormatter; - EXPECT_THROW(TestFormatter f("{0", CountingSink(num_writes)), FormatError); - } - EXPECT_EQ(0, num_writes); -} - -#if FMT_USE_FILE_DESCRIPTORS - -using fmt::BufferedFile; -using fmt::File; - -TEST(FormatterTest, FileSink) { - File read_end, write_end; - File::pipe(read_end, write_end); - BufferedFile f = write_end.fdopen("w"); - EXPECT_WRITE(f.get(), { - fmt::FileSink fs(f.get()); - fs(Writer() << "test"); - }, "test"); -} - -TEST(FormatterTest, FileSinkWriteError) { - File read_end, write_end; - File::pipe(read_end, write_end); - BufferedFile f = read_end.fdopen("r"); - fmt::FileSink fs(f.get()); - std::size_t result = std::fwrite(" ", 1, 1, f.get()); - int error_code = errno; - EXPECT_EQ(0u, result); - EXPECT_SYSTEM_ERROR( - fs(Writer() << "test"), error_code, "cannot write to file"); -} - -#else -# pragma message "warning: some tests are disabled" -#endif - -struct PrintError { - void operator()(const fmt::Writer &w) const { - std::cerr << "Error: " << w.str() << std::endl; - } -}; - -fmt::Formatter ReportError(const char *format) { - fmt::Formatter f(format); - return f; -} - TEST(FormatterTest, Examples) { EXPECT_EQ("First, thou shalt count to three", format("First, thou shalt count to {0}", "three")); @@ -1517,9 +1423,6 @@ TEST(FormatterTest, Examples) { EXPECT_EQ("int: 42; hex: 0x2a; oct: 052", format("int: {0:d}; hex: {0:#x}; oct: {0:#o}", 42)); - std::string path = "somefile"; - ReportError("File not found: {0}") << path; - EXPECT_EQ("The answer is 42", format("The answer is {}", 42)); EXPECT_THROW_MSG( format("The answer is {:d}", "forty-two"), FormatError, @@ -1584,7 +1487,7 @@ TEST(FormatTest, Print) { } TEST(FormatTest, PrintColored) { - EXPECT_WRITE(stdout, fmt::PrintColored(fmt::RED, "Hello, {}!\n") << "world", + EXPECT_WRITE(stdout, fmt::print_colored(fmt::RED, "Hello, {}!\n", "world"), "\x1b[31mHello, world!\n\x1b[0m"); }