diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index e50ab4a2..1bd0ca51 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -1432,6 +1432,158 @@ FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string { } namespace detail { + +template struct span { + T* data; + size_t size; +}; + +#ifdef _WIN32 +inline void flockfile(FILE* f) { _lock_file(f); } +inline void funlockfile(FILE* f) { _unlock_file(f); } +inline int getc_unlocked(FILE* f) { return _fgetc_nolock(f); } +#endif + +// A FILE wrapper. F is FILE defined as a template parameter to make system API +// detection work. +template class file_base { + public: + F* file_; + + public: + file_base(F* file) : file_(file) {} + operator F*() const { return file_; } + + // Reads a code unit from the stream. + auto get() -> int { + int result = getc_unlocked(file_); + if (result == EOF && ferror(file_) != 0) + FMT_THROW(system_error(errno, FMT_STRING("getc failed"))); + return result; + } + + // Puts the code unit back into the stream buffer. + void unget(char c) { + if (ungetc(c, file_) == EOF) + FMT_THROW(system_error(errno, FMT_STRING("ungetc failed"))); + } +}; + +// A FILE wrapper for glibc. +template class glibc_file : public file_base { + public: + using file_base::file_base; + + // Returns the file's read buffer as a string_view. + auto get_read_buffer() const -> span { + return {this->file_->_IO_read_ptr, + to_unsigned(this->file_->_IO_read_end - this->file_->_IO_read_ptr)}; + } +}; + +// A FILE wrapper for Apple's libc. +template class apple_file : public file_base { + private: + auto offset() const -> ptrdiff_t { + return this->file_->_p - this->file_->_bf._base; + } + + public: + using file_base::file_base; + + auto is_buffered() const -> bool { + return (this->file_->_flags & 2) == 0; // 2 is __SNBF. + } + + auto get_read_buffer() const -> span { + return {reinterpret_cast(this->file_->_p), + to_unsigned(this->file_->_r)}; + } + + auto get_write_buffer() const -> span { + if (!this->file_->_p || offset() == this->file_->_bf._size) { + // Force buffer initialization by placing and removing a char in a buffer. + fputc(0, this->file_); + --this->file_->_p; + ++this->file_->_w; + } + auto size = this->file_->_bf._size; + FMT_ASSERT(offset() < size, ""); + return {reinterpret_cast(this->file_->_p), + static_cast(size - offset())}; + } + + void advance_write_buffer(size_t size) { + this->file_->_p += size; + this->file_->_w -= size; + } +}; + +// A fallback FILE wrapper. +template class fallback_file : public file_base { + private: + char next_; // The next unconsumed character in the buffer. + bool has_next_ = false; + + public: + using file_base::file_base; + + auto is_buffered() const -> bool { return false; } + + auto get_read_buffer() const -> span { + return {&next_, has_next_ ? 1u : 0u}; + } + + auto get_write_buffer() const -> span { return {nullptr, 0}; } + + auto get() -> int { + has_next_ = false; + return file_base::get(); + } + + void unget(char c) { + file_base::unget(c); + next_ = c; + has_next_ = true; + } + + void advance_write_buffer(size_t) {} +}; + +template +auto get_file(F* f, int) -> apple_file { + return f; +} +inline auto get_file(FILE* f, ...) -> fallback_file { return f; } + +using file_ref = decltype(get_file(static_cast(nullptr), 0)); + +class file_print_buffer : public buffer { + private: + file_ref file_; + + void set_buffer() { + file_.advance_write_buffer(size()); + auto buf = file_.get_write_buffer(); + this->set(buf.data, buf.size); + } + + static void grow(buffer& buf, size_t) { + auto& self = static_cast(buf); + self.set_buffer(); + } + + public: + explicit file_print_buffer(FILE* f) : buffer(grow, size_t()), file_(f) { + flockfile(f); + set_buffer(); + } + ~file_print_buffer() { + file_.advance_write_buffer(size()); + funlockfile(file_); + } +}; + #if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR) FMT_FUNC auto write_console(int, string_view) -> bool { return false; } #else @@ -1470,6 +1622,10 @@ FMT_FUNC void print(std::FILE* f, string_view text) { } // namespace detail FMT_FUNC void vprint(std::FILE* f, string_view fmt, format_args args) { + if (detail::file_ref(f).is_buffered()) { + auto&& buffer = detail::file_print_buffer(f); + return detail::vformat_to(buffer, fmt, args); + } auto buffer = memory_buffer(); detail::vformat_to(buffer, fmt, args); detail::print(f, {buffer.data(), buffer.size()}); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index aaca574f..0e903dd9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -82,7 +82,7 @@ endif() add_fmt_test(printf-test) add_fmt_test(ranges-test ranges-odr-test.cc) -add_fmt_test(scan-test) +add_fmt_test(scan-test HEADER_ONLY) check_symbol_exists(strptime "time.h" HAVE_STRPTIME) if (HAVE_STRPTIME) target_compile_definitions(scan-test PRIVATE FMT_HAVE_STRPTIME) diff --git a/test/format-test.cc b/test/format-test.cc index 14121af4..db68ca08 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1741,6 +1741,14 @@ TEST(format_test, print) { "Don't panic!\n"); } +TEST(format_test, big_print) { + enum {count = 5000}; + auto big_print = []() { + for (int i = 0; i < count / 5; ++i) fmt::print("xxxxx"); + }; + EXPECT_WRITE(stdout, big_print(), std::string(count, 'x')); +} + TEST(format_test, variadic) { EXPECT_EQ(fmt::format("{}c{}", "ab", 1), "abc1"); } diff --git a/test/scan.h b/test/scan.h index 43e6a13c..19e9572d 100644 --- a/test/scan.h +++ b/test/scan.h @@ -42,9 +42,9 @@ class scan_buffer { : ptr_(ptr), end_(end), contiguous_(contiguous) {} ~scan_buffer() = default; - void set(string_view buf) { - ptr_ = buf.begin(); - end_ = buf.end(); + void set(span buf) { + ptr_ = buf.data; + end_ = buf.data + buf.size; } auto ptr() const -> const char* { return ptr_; } @@ -150,83 +150,6 @@ class string_scan_buffer : public scan_buffer { : scan_buffer(s.begin(), s.end(), true) {} }; -#ifdef _WIN32 -void flockfile(FILE* f) { _lock_file(f); } -void funlockfile(FILE* f) { _unlock_file(f); } -int getc_unlocked(FILE* f) { return _fgetc_nolock(f); } -#endif - -// A FILE wrapper. F is FILE defined as a template parameter to make -// system-specific API detection work. -template class file_base { - protected: - F* file_; - - public: - file_base(F* file) : file_(file) {} - operator F*() const { return file_; } - - // Reads a code unit from the stream. - auto get() -> int { - int result = getc_unlocked(file_); - if (result == EOF && ferror(file_) != 0) - FMT_THROW(system_error(errno, FMT_STRING("getc failed"))); - return result; - } - - // Puts the code unit back into the stream buffer. - void unget(char c) { - if (ungetc(c, file_) == EOF) - FMT_THROW(system_error(errno, FMT_STRING("ungetc failed"))); - } -}; - -// A FILE wrapper for glibc. -template class glibc_file : public file_base { - public: - using file_base::file_base; - - // Returns the file's read buffer as a string_view. - auto buffer() const -> string_view { - return {this->file_->_IO_read_ptr, - to_unsigned(this->file_->_IO_read_end - this->file_->_IO_read_ptr)}; - } -}; - -// A FILE wrapper for Apple's libc. -template class apple_file : public file_base { - public: - using file_base::file_base; - - auto buffer() const -> string_view { - return {reinterpret_cast(this->file_->_p), - to_unsigned(this->file_->_r)}; - } -}; - -// A fallback FILE wrapper. -template class fallback_file : public file_base { - private: - char next_; // The next unconsumed character in the buffer. - bool has_next_ = false; - - public: - using file_base::file_base; - - auto buffer() const -> string_view { return {&next_, has_next_ ? 1u : 0u}; } - - auto get() -> int { - has_next_ = false; - return file_base::get(); - } - - void unget(char c) { - file_base::unget(c); - next_ = c; - has_next_ = true; - } -}; - class file_scan_buffer : public scan_buffer { private: template @@ -243,19 +166,19 @@ class file_scan_buffer : public scan_buffer { // Fills the buffer if it is empty. void fill() { - string_view buf = file_.buffer(); - if (buf.size() == 0) { + span buf = file_.get_read_buffer(); + if (buf.size == 0) { int c = file_.get(); // Put the character back since we are only filling the buffer. if (c != EOF) file_.unget(static_cast(c)); - buf = file_.buffer(); + buf = file_.get_read_buffer(); } set(buf); } void consume() override { // Consume the current buffer content. - size_t n = to_unsigned(ptr() - file_.buffer().begin()); + size_t n = to_unsigned(ptr() - file_.get_read_buffer().data); for (size_t i = 0; i != n; ++i) file_.get(); fill(); }