inline void insert_fill_chars(std::basic_ostream<charT, traits>& os, std::size_t n) { enum { chunk_size = 8 }; charT fill_chars[chunk_size]; std::fill_n(fill_chars, static_cast< std::size_t >(chunk_size), os.fill()); for (; n >= chunk_size && os.good(); n -= chunk_size) os.write(fill_chars, static_cast< std::size_t >(chunk_size)); if (n > 0 && os.good()) os.write(fill_chars, n); }
void write(const CharT* s, size_t length) { size_t diff = end_buffer_ - p_; if (diff >= length) { std::memcpy(p_, s, length*sizeof(CharT)); p_ += length; } else { os_->write(begin_buffer_, (p_ - begin_buffer_)); os_->write(s, length); p_ = begin_buffer_; } }
void insert_aligned(std::basic_ostream<charT, traits>& os, const basic_string_ref<charT,traits>& str) { const std::size_t size = str.size(); const std::size_t alignment_size = static_cast< std::size_t >(os.width()) - size; const bool align_left = (os.flags() & std::basic_ostream<charT, traits>::adjustfield) == std::basic_ostream<charT, traits>::left; if (!align_left) { detail::insert_fill_chars(os, alignment_size); if (os.good()) os.write(str.data(), size); } else { os.write(str.data(), size); if (os.good()) detail::insert_fill_chars(os, alignment_size); } }
void put(CharT c) { if (p_ < end_buffer_) { *p_++ = c; } else { os_->write(begin_buffer_, (p_-begin_buffer_)); p_ = begin_buffer_; *p_++ = c; } }
void flush() { os_->write(begin_buffer_, (p_ - begin_buffer_)); p_ = begin_buffer_; os_->flush(); }
~buffered_ostream() { os_->write(begin_buffer_, (p_ - begin_buffer_)); os_->flush(); }