Ask associated streambuf for character when needed instead of
accumulate it in istreambuf_iterator object.

Benefits from this:
  - minus one class member in istreambuf_iterator
  - trivial synchronization of states of istreambuf_iterator
    and associated streambuf
---
 libstdc++-v3/include/bits/streambuf_iterator.h | 34 ++++++++++++--------------
 1 file changed, 15 insertions(+), 19 deletions(-)

diff --git a/libstdc++-v3/include/bits/streambuf_iterator.h 
b/libstdc++-v3/include/bits/streambuf_iterator.h
index 08fb13b..203da9d 100644
--- a/libstdc++-v3/include/bits/streambuf_iterator.h
+++ b/libstdc++-v3/include/bits/streambuf_iterator.h
@@ -95,19 +95,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       // NB: This implementation assumes the "end of stream" value
       // is EOF, or -1.
       mutable streambuf_type*  _M_sbuf;
-      mutable int_type         _M_c;
 
     public:
       class proxy
       {
           friend class istreambuf_iterator;
       private:
-          proxy(int_type c, streambuf_type*    sbuf_) :
+          proxy(int_type c, streambuf_type* sbuf_) :
               _M_c(c),
               _M_sbuf(sbuf_)
               { }
           int_type _M_c;
-          streambuf_type*      _M_sbuf;
+          streambuf_type* _M_sbuf;
 
       public:
           char_type
@@ -118,7 +117,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     public:
       ///  Construct end of input stream iterator.
       _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
-      : _M_sbuf(0), _M_c(traits_type::eof()) { }
+      : _M_sbuf(0) { }
 
 #if __cplusplus >= 201103L
       istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
@@ -128,15 +127,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
       ///  Construct start of input stream iterator.
       istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
-      : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
+      : _M_sbuf(__s.rdbuf()) { }
 
       ///  Construct start of streambuf iterator.
       istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
-      : _M_sbuf(__s), _M_c(traits_type::eof()) { }
+      : _M_sbuf(__s) { }
 
       ///  Construct start of istreambuf iterator.
       istreambuf_iterator(const proxy& __p) _GLIBCXX_USE_NOEXCEPT
-      : _M_sbuf(__p._M_sbuf), _M_c(traits_type::eof()) { }
+      : _M_sbuf(__p._M_sbuf) { }
 
       ///  Return the current character pointed to by iterator.  This returns
       ///  streambuf.sgetc().  It cannot be assigned.  NB: The result of
@@ -147,11 +146,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #ifdef _GLIBCXX_DEBUG_PEDANTIC
        // Dereferencing a past-the-end istreambuf_iterator is a
        // libstdc++ extension
-       __glibcxx_requires_cond(!_M_at_eof(),
+       int_type __tmp = _M_get();
+       
__glibcxx_requires_cond(!traits_type::eq_int_type(__tmp,traits_type::eof()),
                                _M_message(__gnu_debug::__msg_deref_istreambuf)
                                ._M_iterator(*this));
-#endif
+       return traits_type::to_char_type(__tmp);
+#else
        return traits_type::to_char_type(_M_get());
+#endif
       }
 
       /// Advance the iterator.  Calls streambuf.sbumpc().
@@ -172,7 +174,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                                    
_M_message(__gnu_debug::__msg_inc_istreambuf)
                                    ._M_iterator(*this));
 #endif
-           _M_c = traits_type::eof();
          }
        return *this;
       }
@@ -181,17 +182,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       proxy
       operator++(int)
       {
-        _M_get();
-       __glibcxx_requires_cond(_M_sbuf
-                               && 
!traits_type::eq_int_type(_M_c,traits_type::eof()),
+        int_type c = _M_get();
+       __glibcxx_requires_cond(!traits_type::eq_int_type(c,traits_type::eof()),
                                _M_message(__gnu_debug::__msg_inc_istreambuf)
                                ._M_iterator(*this));
 
-       proxy __old(_M_c, _M_sbuf);
+       proxy __old(c, _M_sbuf);
        if (_M_sbuf)
          {
            _M_sbuf->sbumpc();
-           _M_c = traits_type::eof();
          }
        return __old;
       }
@@ -209,9 +208,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       _M_get() const
       {
        const int_type __eof = traits_type::eof();
-       if (_M_sbuf && traits_type::eq_int_type(_M_c, __eof))
-          _M_c = _M_sbuf->sgetc();
-       return _M_c;
+       return _M_sbuf ? _M_sbuf->sgetc() : __eof;
       }
 
       bool
@@ -418,7 +415,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
              else
                __c = __sb->snextc();
            }
-         __first._M_c = __c;
        }
       return __first;
     }
-- 
2.10.1

Reply via email to