Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85629 - trunk/boost/log/utility
From: andrey.semashev_at_[hidden]
Date: 2013-09-09 16:29:23


Author: andysem
Date: 2013-09-09 16:29:22 EDT (Mon, 09 Sep 2013)
New Revision: 85629
URL: http://svn.boost.org/trac/boost/changeset/85629

Log:
Fixed string_literal adjustment in the operator<<. Minor optimization of the formatting_ostream insertion operators.

Text files modified:
   trunk/boost/log/utility/formatting_ostream.hpp | 82 ++++++++++++++++++++++------
   trunk/boost/log/utility/string_literal.hpp | 116 +++++++++++++++++++++++++++------------
   2 files changed, 144 insertions(+), 54 deletions(-)

Modified: trunk/boost/log/utility/formatting_ostream.hpp
==============================================================================
--- trunk/boost/log/utility/formatting_ostream.hpp Mon Sep 9 15:36:55 2013 (r85628)
+++ trunk/boost/log/utility/formatting_ostream.hpp 2013-09-09 16:29:22 EDT (Mon, 09 Sep 2013) (r85629)
@@ -545,6 +545,24 @@
         m_stream.fill(static_cast< char_type >(' '));
     }
 
+ basic_formatting_ostream& formatted_write(const char_type* p, std::streamsize size)
+ {
+ sentry guard(*this);
+ if (guard)
+ {
+ m_stream.flush();
+
+ if (m_stream.width() <= size)
+ m_streambuf.storage()->append(p, static_cast< std::size_t >(size));
+ else
+ this->aligned_write(p, size);
+
+ m_stream.width(0);
+ }
+
+ return *this;
+ }
+
     template< typename OtherCharT >
     basic_formatting_ostream& formatted_write(const OtherCharT* p, std::streamsize size)
     {
@@ -552,26 +570,11 @@
         if (guard)
         {
             m_stream.flush();
- string_type* const storage = m_streambuf.storage();
 
- const std::streamsize w = m_stream.width();
- if (w <= size)
- {
- aux::code_convert(p, static_cast< std::size_t >(size), *storage, m_stream.getloc());
- }
+ if (m_stream.width() <= size)
+ aux::code_convert(p, static_cast< std::size_t >(size), *m_streambuf.storage(), m_stream.getloc());
             else
- {
- const bool align_left = (m_stream.flags() & ostream_type::adjustfield) == ostream_type::left;
- typename string_type::size_type const alignment_size =
- static_cast< typename string_type::size_type >(w - size);
- if (!align_left)
- storage->append(alignment_size, m_stream.fill());
-
- aux::code_convert(p, static_cast< std::size_t >(size), *storage, m_stream.getloc());
-
- if (align_left)
- storage->append(alignment_size, m_stream.fill());
- }
+ this->aligned_write(p, size);
 
             m_stream.width(0);
         }
@@ -579,6 +582,11 @@
         return *this;
     }
 
+ void aligned_write(const char_type* p, std::streamsize size);
+
+ template< typename OtherCharT >
+ void aligned_write(const OtherCharT* p, std::streamsize size);
+
     //! Copy constructor (closed)
     BOOST_DELETED_FUNCTION(basic_formatting_ostream(basic_formatting_ostream const& that))
     //! Assignment (closed)
@@ -656,6 +664,44 @@
 template< typename CharT, typename TraitsT, typename AllocatorT >
 BOOST_CONSTEXPR_OR_CONST typename basic_formatting_ostream< CharT, TraitsT, AllocatorT >::event basic_formatting_ostream< CharT, TraitsT, AllocatorT >::copyfmt_event;
 
+template< typename CharT, typename TraitsT, typename AllocatorT >
+void basic_formatting_ostream< CharT, TraitsT, AllocatorT >::aligned_write(const char_type* p, std::streamsize size)
+{
+ string_type* const storage = m_streambuf.storage();
+ typename string_type::size_type const alignment_size =
+ static_cast< typename string_type::size_type >(m_stream.width() - size);
+ const bool align_left = (m_stream.flags() & ostream_type::adjustfield) == ostream_type::left;
+ if (align_left)
+ {
+ storage->append(p, static_cast< std::size_t >(size));
+ storage->append(alignment_size, m_stream.fill());
+ }
+ else
+ {
+ storage->append(alignment_size, m_stream.fill());
+ storage->append(p, static_cast< std::size_t >(size));
+ }
+}
+
+template< typename CharT, typename TraitsT, typename AllocatorT >
+template< typename OtherCharT >
+void basic_formatting_ostream< CharT, TraitsT, AllocatorT >::aligned_write(const OtherCharT* p, std::streamsize size)
+{
+ string_type* const storage = m_streambuf.storage();
+ typename string_type::size_type const alignment_size =
+ static_cast< typename string_type::size_type >(m_stream.width() - size);
+ const bool align_left = (m_stream.flags() & ostream_type::adjustfield) == ostream_type::left;
+ if (align_left)
+ {
+ aux::code_convert(p, static_cast< std::size_t >(size), *storage, m_stream.getloc());
+ storage->append(alignment_size, m_stream.fill());
+ }
+ else
+ {
+ storage->append(alignment_size, m_stream.fill());
+ aux::code_convert(p, static_cast< std::size_t >(size), *storage, m_stream.getloc());
+ }
+}
 
 template< typename CharT, typename TraitsT, typename AllocatorT, typename T >
 inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >&

Modified: trunk/boost/log/utility/string_literal.hpp
==============================================================================
--- trunk/boost/log/utility/string_literal.hpp Mon Sep 9 15:36:55 2013 (r85628)
+++ trunk/boost/log/utility/string_literal.hpp 2013-09-09 16:29:22 EDT (Mon, 09 Sep 2013) (r85629)
@@ -92,7 +92,7 @@
      *
      * \post <tt>empty() == true</tt>
      */
- basic_string_literal() { clear(); }
+ basic_string_literal() BOOST_NOEXCEPT { clear(); }
 
     /*!
      * Constructor from a string literal
@@ -105,7 +105,7 @@
         //! \cond
         , typename enable_if< is_same< T, const value_type >, int >::type = 0
         //! \endcond
- )
+ ) BOOST_NOEXCEPT
         : m_pStart(p), m_Len(LenV - 1)
     {
     }
@@ -116,7 +116,7 @@
      * \post <tt>*this == that</tt>
      * \param that Source literal to copy string from
      */
- basic_string_literal(basic_string_literal const& that) : m_pStart(that.m_pStart), m_Len(that.m_Len) {}
+ basic_string_literal(basic_string_literal const& that) BOOST_NOEXCEPT : m_pStart(that.m_pStart), m_Len(that.m_Len) {}
 
     /*!
      * Assignment operator
@@ -124,7 +124,7 @@
      * \post <tt>*this == that</tt>
      * \param that Source literal to copy string from
      */
- this_type& operator= (this_type const& that)
+ this_type& operator= (this_type const& that) BOOST_NOEXCEPT
     {
         return assign(that);
     }
@@ -143,7 +143,7 @@
 #else
     this_type&
 #endif // BOOST_LOG_DOXYGEN_PASS
- operator= (T(&p)[LenV])
+ operator= (T(&p)[LenV]) BOOST_NOEXCEPT
     {
         return assign(p);
     }
@@ -154,7 +154,7 @@
      * \param that Comparand
      * \return \c true if the comparand string equals to this string, \c false otherwise
      */
- bool operator== (this_type const& that) const
+ bool operator== (this_type const& that) const BOOST_NOEXCEPT
     {
         return (compare_internal(m_pStart, m_Len, that.m_pStart, that.m_Len) == 0);
     }
@@ -164,7 +164,7 @@
      * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
      * \return \c true if the comparand string equals to this string, \c false otherwise
      */
- bool operator== (const_pointer str) const
+ bool operator== (const_pointer str) const BOOST_NOEXCEPT
     {
         return (compare_internal(m_pStart, m_Len, str, traits_type::length(str)) == 0);
     }
@@ -185,7 +185,7 @@
      * \param that Comparand
      * \return \c true if this string is less than the comparand, \c false otherwise
      */
- bool operator< (this_type const& that) const
+ bool operator< (this_type const& that) const BOOST_NOEXCEPT
     {
         return (compare_internal(m_pStart, m_Len, that.m_pStart, that.m_Len) < 0);
     }
@@ -195,7 +195,7 @@
      * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
      * \return \c true if this string is less than the comparand, \c false otherwise
      */
- bool operator< (const_pointer str) const
+ bool operator< (const_pointer str) const BOOST_NOEXCEPT
     {
         return (compare_internal(m_pStart, m_Len, str, traits_type::length(str)) < 0);
     }
@@ -216,7 +216,7 @@
      * \param that Comparand
      * \return \c true if this string is greater than the comparand, \c false otherwise
      */
- bool operator> (this_type const& that) const
+ bool operator> (this_type const& that) const BOOST_NOEXCEPT
     {
         return (compare_internal(m_pStart, m_Len, that.m_pStart, that.m_Len) > 0);
     }
@@ -226,7 +226,7 @@
      * \param str Comparand. Must point to a zero-terminated sequence of characters, must not be NULL.
      * \return \c true if this string is greater than the comparand, \c false otherwise
      */
- bool operator> (const_pointer str) const
+ bool operator> (const_pointer str) const BOOST_NOEXCEPT
     {
         return (compare_internal(m_pStart, m_Len, str, traits_type::length(str)) > 0);
     }
@@ -248,7 +248,7 @@
      * \param i Requested character index
      * \return Constant reference to the requested character
      */
- const_reference operator[] (size_type i) const
+ const_reference operator[] (size_type i) const BOOST_NOEXCEPT
     {
         return m_pStart[i];
     }
@@ -270,24 +270,24 @@
     /*!
      * \return Pointer to the beginning of the literal
      */
- const_pointer c_str() const { return m_pStart; }
+ const_pointer c_str() const BOOST_NOEXCEPT { return m_pStart; }
     /*!
      * \return Pointer to the beginning of the literal
      */
- const_pointer data() const { return m_pStart; }
+ const_pointer data() const BOOST_NOEXCEPT { return m_pStart; }
     /*!
      * \return Length of the literal
      */
- size_type size() const { return m_Len; }
+ size_type size() const BOOST_NOEXCEPT { return m_Len; }
     /*!
      * \return Length of the literal
      */
- size_type length() const { return m_Len; }
+ size_type length() const BOOST_NOEXCEPT { return m_Len; }
 
     /*!
      * \return \c true if the literal is an empty string, \c false otherwise
      */
- bool empty() const
+ bool empty() const BOOST_NOEXCEPT
     {
         return (m_Len == 0);
     }
@@ -295,19 +295,19 @@
     /*!
      * \return Iterator that points to the first character of the literal
      */
- const_iterator begin() const { return m_pStart; }
+ const_iterator begin() const BOOST_NOEXCEPT { return m_pStart; }
     /*!
      * \return Iterator that points after the last character of the literal
      */
- const_iterator end() const { return m_pStart + m_Len; }
+ const_iterator end() const BOOST_NOEXCEPT { return m_pStart + m_Len; }
     /*!
      * \return Reverse iterator that points to the last character of the literal
      */
- const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
+ const_reverse_iterator rbegin() const BOOST_NOEXCEPT { return const_reverse_iterator(end()); }
     /*!
      * \return Reverse iterator that points before the first character of the literal
      */
- const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
+ const_reverse_iterator rend() const BOOST_NOEXCEPT { return const_reverse_iterator(begin()); }
 
     /*!
      * \return STL string constructed from the literal
@@ -322,7 +322,7 @@
      *
      * \post <tt>empty() == true</tt>
      */
- void clear()
+ void clear() BOOST_NOEXCEPT
     {
         m_pStart = g_EmptyString;
         m_Len = 0;
@@ -330,7 +330,7 @@
     /*!
      * The method swaps two literals
      */
- void swap(this_type& that)
+ void swap(this_type& that) BOOST_NOEXCEPT
     {
         register const_pointer p = m_pStart;
         m_pStart = that.m_pStart;
@@ -347,7 +347,7 @@
      * \post <tt>*this == that</tt>
      * \param that Source literal to copy string from
      */
- this_type& assign(this_type const& that)
+ this_type& assign(this_type const& that) BOOST_NOEXCEPT
     {
         m_pStart = that.m_pStart;
         m_Len = that.m_Len;
@@ -368,7 +368,7 @@
 #else
     this_type&
 #endif // BOOST_LOG_DOXYGEN_PASS
- assign(T(&p)[LenV])
+ assign(T(&p)[LenV]) BOOST_NOEXCEPT
     {
         m_pStart = p;
         m_Len = LenV - 1;
@@ -436,7 +436,7 @@
      *
      * \b Throws: An <tt>std::exception</tt>-based exception if \a pos is out of range.
      */
- int compare(size_type pos, size_type n, const_pointer str) const
+ int compare(size_type pos, size_type n, const_pointer str) const BOOST_NOEXCEPT
     {
         return compare(pos, n, str, traits_type::length(str));
     }
@@ -452,7 +452,7 @@
      *
      * \b Throws: An <tt>std::exception</tt>-based exception if \a pos is out of range.
      */
- int compare(size_type pos, size_type n, this_type const& that) const
+ int compare(size_type pos, size_type n, this_type const& that) const BOOST_NOEXCEPT
     {
         return compare(pos, n, that.c_str(), that.size());
     }
@@ -464,7 +464,7 @@
      * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
      * a positive value if this string is greater than the comparand.
      */
- int compare(const_pointer str, size_type len) const
+ int compare(const_pointer str, size_type len) const BOOST_NOEXCEPT
     {
         return compare(0, m_Len, str, len);
     }
@@ -475,7 +475,7 @@
      * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
      * a positive value if this string is greater than the comparand.
      */
- int compare(const_pointer str) const
+ int compare(const_pointer str) const BOOST_NOEXCEPT
     {
         return compare(0, m_Len, str, traits_type::length(str));
     }
@@ -486,7 +486,7 @@
      * \return Zero if the comparand equals this string, a negative value if this string is less than the comparand,
      * a positive value if this string is greater than the comparand.
      */
- int compare(this_type const& that) const
+ int compare(this_type const& that) const BOOST_NOEXCEPT
     {
         return compare(0, m_Len, that.c_str(), that.size());
     }
@@ -494,7 +494,7 @@
 private:
 #ifndef BOOST_LOG_DOXYGEN_PASS
     //! Internal comparison implementation
- static int compare_internal(const_pointer pLeft, size_type LeftLen, const_pointer pRight, size_type RightLen)
+ static int compare_internal(const_pointer pLeft, size_type LeftLen, const_pointer pRight, size_type RightLen) BOOST_NOEXCEPT
     {
         if (pLeft != pRight)
         {
@@ -503,7 +503,7 @@
             if (result != 0)
                 return result;
         }
- return static_cast< int >(LeftLen - RightLen);
+ return LeftLen < RightLen ? -1 : (LeftLen > RightLen ? 1 : 0);
     }
 #endif // BOOST_LOG_DOXYGEN_PASS
 };
@@ -512,20 +512,64 @@
 typename basic_string_literal< CharT, TraitsT >::value_type const
 basic_string_literal< CharT, TraitsT >::g_EmptyString[1] = { 0 };
 
+namespace aux {
+
+template< typename CharT, typename TraitsT >
+inline void insert_fill_chars(std::basic_ostream< CharT, TraitsT >& strm, std::size_t n)
+{
+ enum { chunk_size = 8 };
+ CharT fill_chars[chunk_size];
+ const CharT filler = strm.fill();
+ for (unsigned int i = 0; i < chunk_size; ++i)
+ fill_chars[i] = filler;
+ for (; n >= chunk_size && strm.good(); n -= chunk_size)
+ strm.write(fill_chars, static_cast< std::size_t >(chunk_size));
+ if (n > 0 && strm.good())
+ strm.write(fill_chars, n);
+}
+
+template< typename CharT, typename TraitsT >
+void insert_aligned(std::basic_ostream< CharT, TraitsT >& strm, const CharT* p, std::size_t size)
+{
+ const std::size_t alignment_size = static_cast< std::size_t >(strm.width()) - size;
+ const bool align_left = (strm.flags() & std::basic_ostream< CharT, TraitsT >::adjustfield) == std::basic_ostream< CharT, TraitsT >::left;
+ if (align_left)
+ {
+ strm.write(p, size);
+ if (strm.good())
+ aux::insert_fill_chars(strm, alignment_size);
+ }
+ else
+ {
+ aux::insert_fill_chars(strm, alignment_size);
+ if (strm.good())
+ strm.write(p, size);
+ }
+}
+
+} // namespace aux
+
 //! Output operator
 template< typename CharT, typename StrmTraitsT, typename LitTraitsT >
 inline std::basic_ostream< CharT, StrmTraitsT >& operator<< (
     std::basic_ostream< CharT, StrmTraitsT >& strm, basic_string_literal< CharT, LitTraitsT > const& lit)
 {
- strm.write(lit.c_str(), static_cast< std::streamsize >(lit.size()));
+ if (strm.good())
+ {
+ const std::size_t size = lit.size();
+ const std::size_t w = static_cast< std::size_t >(strm.width());
+ if (w <= size)
+ strm.write(lit.c_str(), static_cast< std::streamsize >(size));
+ else
+ aux::insert_aligned(strm, lit.c_str(), lit.size());
+ strm.width(0);
+ }
     return strm;
 }
 
 //! External swap
 template< typename CharT, typename TraitsT >
-inline void swap(
- basic_string_literal< CharT, TraitsT >& left,
- basic_string_literal< CharT, TraitsT >& right)
+inline void swap(basic_string_literal< CharT, TraitsT >& left, basic_string_literal< CharT, TraitsT >& right) BOOST_NOEXCEPT
 {
     left.swap(right);
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk