Write directly to a stream buffer

This commit is contained in:
Victor Zverovich 2024-01-18 07:27:59 -08:00
parent b2cde48de5
commit 6b68dff901
4 changed files with 172 additions and 85 deletions

View File

@ -1432,6 +1432,158 @@ FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string {
} }
namespace detail { namespace detail {
template <typename T> 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 <typename F> 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 <typename F> class glibc_file : public file_base<F> {
public:
using file_base<F>::file_base;
// Returns the file's read buffer as a string_view.
auto get_read_buffer() const -> span<const char> {
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 <typename F> class apple_file : public file_base<F> {
private:
auto offset() const -> ptrdiff_t {
return this->file_->_p - this->file_->_bf._base;
}
public:
using file_base<F>::file_base;
auto is_buffered() const -> bool {
return (this->file_->_flags & 2) == 0; // 2 is __SNBF.
}
auto get_read_buffer() const -> span<const char> {
return {reinterpret_cast<char*>(this->file_->_p),
to_unsigned(this->file_->_r)};
}
auto get_write_buffer() const -> span<char> {
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<char*>(this->file_->_p),
static_cast<size_t>(size - offset())};
}
void advance_write_buffer(size_t size) {
this->file_->_p += size;
this->file_->_w -= size;
}
};
// A fallback FILE wrapper.
template <typename F> class fallback_file : public file_base<F> {
private:
char next_; // The next unconsumed character in the buffer.
bool has_next_ = false;
public:
using file_base<F>::file_base;
auto is_buffered() const -> bool { return false; }
auto get_read_buffer() const -> span<const char> {
return {&next_, has_next_ ? 1u : 0u};
}
auto get_write_buffer() const -> span<char> { return {nullptr, 0}; }
auto get() -> int {
has_next_ = false;
return file_base<F>::get();
}
void unget(char c) {
file_base<F>::unget(c);
next_ = c;
has_next_ = true;
}
void advance_write_buffer(size_t) {}
};
template <typename F, FMT_ENABLE_IF(sizeof(F::_p) != 0)>
auto get_file(F* f, int) -> apple_file<F> {
return f;
}
inline auto get_file(FILE* f, ...) -> fallback_file<FILE> { return f; }
using file_ref = decltype(get_file(static_cast<FILE*>(nullptr), 0));
class file_print_buffer : public buffer<char> {
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<char>& buf, size_t) {
auto& self = static_cast<file_print_buffer&>(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) #if !defined(_WIN32) || defined(FMT_WINDOWS_NO_WCHAR)
FMT_FUNC auto write_console(int, string_view) -> bool { return false; } FMT_FUNC auto write_console(int, string_view) -> bool { return false; }
#else #else
@ -1470,6 +1622,10 @@ FMT_FUNC void print(std::FILE* f, string_view text) {
} // namespace detail } // namespace detail
FMT_FUNC void vprint(std::FILE* f, string_view fmt, format_args args) { 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(); auto buffer = memory_buffer();
detail::vformat_to(buffer, fmt, args); detail::vformat_to(buffer, fmt, args);
detail::print(f, {buffer.data(), buffer.size()}); detail::print(f, {buffer.data(), buffer.size()});

View File

@ -82,7 +82,7 @@ endif()
add_fmt_test(printf-test) add_fmt_test(printf-test)
add_fmt_test(ranges-test ranges-odr-test.cc) 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) check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
if (HAVE_STRPTIME) if (HAVE_STRPTIME)
target_compile_definitions(scan-test PRIVATE FMT_HAVE_STRPTIME) target_compile_definitions(scan-test PRIVATE FMT_HAVE_STRPTIME)

View File

@ -1741,6 +1741,14 @@ TEST(format_test, print) {
"Don't panic!\n"); "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) { TEST(format_test, variadic) {
EXPECT_EQ(fmt::format("{}c{}", "ab", 1), "abc1"); EXPECT_EQ(fmt::format("{}c{}", "ab", 1), "abc1");
} }

View File

@ -42,9 +42,9 @@ class scan_buffer {
: ptr_(ptr), end_(end), contiguous_(contiguous) {} : ptr_(ptr), end_(end), contiguous_(contiguous) {}
~scan_buffer() = default; ~scan_buffer() = default;
void set(string_view buf) { void set(span<const char> buf) {
ptr_ = buf.begin(); ptr_ = buf.data;
end_ = buf.end(); end_ = buf.data + buf.size;
} }
auto ptr() const -> const char* { return ptr_; } 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) {} : 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 <typename F> 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 <typename F> class glibc_file : public file_base<F> {
public:
using file_base<F>::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 <typename F> class apple_file : public file_base<F> {
public:
using file_base<F>::file_base;
auto buffer() const -> string_view {
return {reinterpret_cast<char*>(this->file_->_p),
to_unsigned(this->file_->_r)};
}
};
// A fallback FILE wrapper.
template <typename F> class fallback_file : public file_base<F> {
private:
char next_; // The next unconsumed character in the buffer.
bool has_next_ = false;
public:
using file_base<F>::file_base;
auto buffer() const -> string_view { return {&next_, has_next_ ? 1u : 0u}; }
auto get() -> int {
has_next_ = false;
return file_base<F>::get();
}
void unget(char c) {
file_base<F>::unget(c);
next_ = c;
has_next_ = true;
}
};
class file_scan_buffer : public scan_buffer { class file_scan_buffer : public scan_buffer {
private: private:
template <typename F, FMT_ENABLE_IF(sizeof(F::_IO_read_ptr) != 0)> template <typename F, FMT_ENABLE_IF(sizeof(F::_IO_read_ptr) != 0)>
@ -243,19 +166,19 @@ class file_scan_buffer : public scan_buffer {
// Fills the buffer if it is empty. // Fills the buffer if it is empty.
void fill() { void fill() {
string_view buf = file_.buffer(); span<const char> buf = file_.get_read_buffer();
if (buf.size() == 0) { if (buf.size == 0) {
int c = file_.get(); int c = file_.get();
// Put the character back since we are only filling the buffer. // Put the character back since we are only filling the buffer.
if (c != EOF) file_.unget(static_cast<char>(c)); if (c != EOF) file_.unget(static_cast<char>(c));
buf = file_.buffer(); buf = file_.get_read_buffer();
} }
set(buf); set(buf);
} }
void consume() override { void consume() override {
// Consume the current buffer content. // 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(); for (size_t i = 0; i != n; ++i) file_.get();
fill(); fill();
} }