From f3bb2a493cdd73dd9ce777280d980d509205372a Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Sun, 2 Jun 2013 18:15:53 +0000 Subject: [PATCH] Modifications according to the review. [SVN r84609] --- include/boost/utility/string_ref.hpp | 53 ++++++++++++++++++---------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index 6611dac..febdabe 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -402,6 +402,37 @@ namespace boost { return basic_string_ref(x) >= y; } + namespace detail { + + template + inline void insert_fill_chars(std::basic_ostream& os, std::size_t n) { + charT fill_chars[8]; + std::fill_n(fill_chars, 8, os.fill()); + for (std::size_t m = n / 8u; m > 0 && os.good(); --m) + os.write(fill_chars, 8); + n &= 7u; + if (n > 0 && os.good()) + os.write(fill_chars, static_cast< std::streamsize >(n)); + } + + template + void insert_aligned(std::basic_ostream& os, const basic_string_ref& 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::ios_base::adjustfield) == std::ios_base::left; + if (!align_left) { + detail::insert_fill_chars(os, alignment_size); + if (os.good()) + os.write(str.data(), static_cast< std::streamsize >(size)); + } + else { + os.write(str.data(), static_cast< std::streamsize >(size)); + detail::insert_fill_chars(os, alignment_size); + } + } + + } // namespace detail + // Inserter template inline std::basic_ostream& @@ -409,25 +440,11 @@ namespace boost { if (os.good()) { const std::size_t size = str.size(); const std::size_t w = static_cast< std::size_t >(os.width()); - os.width(0); if (w <= size) - os.write(str.data(), size); - else { - const bool align_left = (os.flags() & std::basic_ostream::adjustfield) == std::basic_ostream::left; - const std::size_t alignment_size = w - size; - if (!align_left) { - const charT fill_char = os.fill(); - for (std::size_t i = 0; i < alignment_size && os.good(); ++i) - os.put(fill_char); - } - if (os.good()) - os.write(str.data(), size); - if (align_left && os.good()) { - const charT fill_char = os.fill(); - for (std::size_t i = 0; i < alignment_size && os.good(); ++i) - os.put(fill_char); - } - } + os.write(str.data(), static_cast< std::streamsize >(size)); + else + detail::insert_aligned(os, str); + os.width(0); } return os; }