Have basic_istream seekg, putback and unget first clear eofbit. Fixes http://llvm...
[lldb.git] / libcxx / include / istream
index 02c54ff..b48f856 100644 (file)
@@ -1,10 +1,10 @@
 // -*- C++ -*-
 //===--------------------------- istream ----------------------------------===//
 //
-// ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure
+//                     The LLVM Compiler Infrastructure
 //
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 
@@ -155,12 +155,16 @@ template <class charT, class traits, class T>
 #include <__config>
 #include <ostream>
 
+#include <__undef_min_max>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #pragma GCC system_header
+#endif
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _CharT, class _Traits>
-class basic_istream
+class _LIBCPP_VISIBLE basic_istream
     : virtual public basic_ios<_CharT, _Traits>
 {
     streamsize __gc_;
@@ -176,12 +180,14 @@ public:
     explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb);
     virtual ~basic_istream();
 protected:
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+    _LIBCPP_INLINE_VISIBILITY
     basic_istream(basic_istream&& __rhs);
 #endif
 
     // 27.7.1.1.2 Assign/swap:
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+    _LIBCPP_INLINE_VISIBILITY
     basic_istream& operator=(basic_istream&& __rhs);
 #endif
     void swap(basic_istream& __rhs);
@@ -211,6 +217,7 @@ public:
     basic_istream& operator>>(void*& __p);
 
     // 27.7.1.3 Unformatted input:
+    _LIBCPP_INLINE_VISIBILITY
     streamsize gcount() const {return __gc_;}
     int_type get();
     basic_istream& get(char_type& __c);
@@ -237,7 +244,7 @@ public:
 };
 
 template <class _CharT, class _Traits>
-class basic_istream<_CharT, _Traits>::sentry
+class _LIBCPP_VISIBLE basic_istream<_CharT, _Traits>::sentry
 {
     bool __ok_;
 
@@ -248,7 +255,8 @@ public:
     explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
 //    ~sentry() = default;
 
-    // explicit
+    _LIBCPP_INLINE_VISIBILITY
+        _LIBCPP_EXPLICIT
         operator bool() const {return __ok_;}
 };
 
@@ -263,10 +271,10 @@ basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& _
             __is.tie()->flush();
         if (!__noskipws && (__is.flags() & ios_base::skipws))
         {
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
+            typedef istreambuf_iterator<_CharT, _Traits> _Ip;
             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
-            _I __i(__is);
-            _I __eof;
+            _Ip __i(__is);
+            _Ip __eof;
             for (; __i != __eof; ++__i)
                 if (!__ct.is(__ct.space, *__i))
                     break;
@@ -287,7 +295,7 @@ basic_istream<_CharT, _Traits>::basic_istream(basic_streambuf<char_type, traits_
     this->init(__sb);
 }
 
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits>
 inline _LIBCPP_INLINE_VISIBILITY
@@ -307,7 +315,7 @@ basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
     return *this;
 }
 
-#endif
+#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits>
 basic_istream<_CharT, _Traits>::~basic_istream()
@@ -319,7 +327,7 @@ inline _LIBCPP_INLINE_VISIBILITY
 void
 basic_istream<_CharT, _Traits>::swap(basic_istream& __rhs)
 {
-    _STD::swap(__gc_, __rhs.__gc_);
+    _VSTD::swap(__gc_, __rhs.__gc_);
     basic_ios<char_type, traits_type>::swap(__rhs);
 }
 
@@ -330,14 +338,14 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -346,7 +354,7 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -357,14 +365,14 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -373,7 +381,7 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -384,14 +392,14 @@ basic_istream<_CharT, _Traits>::operator>>(long& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -400,7 +408,7 @@ basic_istream<_CharT, _Traits>::operator>>(long& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -411,14 +419,14 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -427,7 +435,7 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -438,14 +446,14 @@ basic_istream<_CharT, _Traits>::operator>>(long long& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -454,7 +462,7 @@ basic_istream<_CharT, _Traits>::operator>>(long long& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -465,14 +473,14 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -481,7 +489,7 @@ basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -492,14 +500,14 @@ basic_istream<_CharT, _Traits>::operator>>(float& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -508,7 +516,7 @@ basic_istream<_CharT, _Traits>::operator>>(float& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -519,14 +527,14 @@ basic_istream<_CharT, _Traits>::operator>>(double& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -535,7 +543,7 @@ basic_istream<_CharT, _Traits>::operator>>(double& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -546,14 +554,14 @@ basic_istream<_CharT, _Traits>::operator>>(long double& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -562,7 +570,7 @@ basic_istream<_CharT, _Traits>::operator>>(long double& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -573,14 +581,14 @@ basic_istream<_CharT, _Traits>::operator>>(bool& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -589,7 +597,7 @@ basic_istream<_CharT, _Traits>::operator>>(bool& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -600,14 +608,14 @@ basic_istream<_CharT, _Traits>::operator>>(void*& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __n);
             this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -616,7 +624,7 @@ basic_istream<_CharT, _Traits>::operator>>(void*& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -627,15 +635,15 @@ basic_istream<_CharT, _Traits>::operator>>(short& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
             long __temp;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __temp);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __temp);
             if (__temp < numeric_limits<short>::min())
             {
                 __err |= ios_base::failbit;
@@ -656,7 +664,7 @@ basic_istream<_CharT, _Traits>::operator>>(short& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -667,15 +675,15 @@ basic_istream<_CharT, _Traits>::operator>>(int& __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this);
         if (__s)
         {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            typedef num_get<char_type, _I> _F;
+            typedef istreambuf_iterator<char_type, traits_type> _Ip;
+            typedef num_get<char_type, _Ip> _Fp;
             ios_base::iostate __err = ios_base::goodbit;
             long __temp;
-            use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __temp);
+            use_facet<_Fp>(this->getloc()).get(_Ip(*this), _Ip(), *this, __err, __temp);
             if (__temp < numeric_limits<int>::min())
             {
                 __err |= ios_base::failbit;
@@ -696,7 +704,7 @@ basic_istream<_CharT, _Traits>::operator>>(int& __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -734,30 +742,33 @@ operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
         if (__sen)
         {
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
             streamsize __n = __is.width();
-            if (__n == 0)
+            if (__n <= 0)
                 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
             streamsize __c = 0;
             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
-            _I __i(__is);
-            _I __eof;
-            for (; __i != __eof && __c < __n-1; ++__i, ++__s, ++__c)
+            ios_base::iostate __err = ios_base::goodbit;
+            while (__c < __n-1)
             {
-                _CharT __ch = *__i;
+                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
+                if (_Traits::eq_int_type(__i, _Traits::eof()))
+                {
+                   __err |= ios_base::eofbit;
+                   break;
+                }
+                _CharT __ch = _Traits::to_char_type(__i);
                 if (__ct.is(__ct.space, __ch))
                     break;
-                *__s = __ch;
+                *__s++ = __ch;
+                ++__c;
+                 __is.rdbuf()->sbumpc();
             }
             *__s = _CharT();
             __is.width(0);
-            ios_base::iostate __err = ios_base::goodbit;
-            if (__i == __eof)
-               __err |= ios_base::eofbit;
             if (__c == 0)
                __err |= ios_base::failbit;
             __is.setstate(__err);
@@ -768,7 +779,7 @@ operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
     {
         __is.__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __is;
 }
 
@@ -795,21 +806,15 @@ operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
         if (__sen)
         {
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
-            _I __i(__is);
-            _I __eof;
-            if (__i != __eof)
-            {
-                __c = *__i;
-                if (++__i == __eof)
-                    __is.setstate(ios_base::eofbit);
-            }
-            else
+            typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
+            if (_Traits::eq_int_type(__i, _Traits::eof()))
                 __is.setstate(ios_base::eofbit | ios_base::failbit);
+            else
+                __c = _Traits::to_char_type(__i);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -817,7 +822,7 @@ operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
     {
         __is.__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __is;
 }
 
@@ -845,46 +850,46 @@ basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_typ
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this, true);
         if (__s)
         {
-            streamsize __c = 0;
             if (__sb)
             {
 #ifndef _LIBCPP_NO_EXCEPTIONS
                 try
                 {
-#endif
-                    typedef istreambuf_iterator<char_type, traits_type> _I;
-                    typedef ostreambuf_iterator<char_type, traits_type> _O;
-                    _I __i(*this);
-                    _I __eof;
-                    _O __o(__sb);
-                    for (; __i != __eof; ++__i, ++__o, ++__c)
+#endif  // _LIBCPP_NO_EXCEPTIONS
+                    ios_base::iostate __err = ios_base::goodbit;
+                    while (true)
                     {
-                        *__o = *__i;
-                        if (__o.failed())
+                        typename traits_type::int_type __i = this->rdbuf()->sgetc();
+                        if (traits_type::eq_int_type(__i, _Traits::eof()))
+                        {
+                           __err |= ios_base::eofbit;
+                           break;
+                        }
+                        if (traits_type::eq_int_type(
+                                __sb->sputc(traits_type::to_char_type(__i)),
+                                traits_type::eof()))
                             break;
+                        ++__gc_;
+                        this->rdbuf()->sbumpc();
                     }
-                    ios_base::iostate __err = ios_base::goodbit;
-                    if (__i == __eof)
-                       __err |= ios_base::eofbit;
-                    if (__c == 0)
+                    if (__gc_ == 0)
                        __err |= ios_base::failbit;
                     this->setstate(__err);
 #ifndef _LIBCPP_NO_EXCEPTIONS
                 }
                 catch (...)
                 {
-                    if (__c == 0)
+                    if (__gc_ == 0)
                         this->__set_failbit_and_consider_rethrow();
                 }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
             }
             else
                 this->setstate(ios_base::failbit);
-            __gc_ = __c;
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -892,7 +897,7 @@ basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_typ
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -905,26 +910,15 @@ basic_istream<_CharT, _Traits>::get()
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __s(*this, true);
         if (__s)
         {
-            streamsize __c = 0;
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            _I __i(*this);
-            _I __eof;
-            ios_base::iostate __err = ios_base::goodbit;
-            if (__i != __eof)
-            {
-                __r = traits_type::to_int_type(*__i);
-                ++__c;
-                if (++__i == __eof)
-                    __err |= ios_base::eofbit;
-            }
+            __r = this->rdbuf()->sbumpc();
+            if (traits_type::eq_int_type(__r, traits_type::eof()))
+               this->setstate(ios_base::failbit | ios_base::eofbit);
             else
-                __err |= ios_base::failbit | ios_base::eofbit;
-            this->setstate(__err);
-            __gc_ = __c;
+                __gc_ = 1;
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -932,7 +926,7 @@ basic_istream<_CharT, _Traits>::get()
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __r;
 }
 
@@ -955,34 +949,35 @@ basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
         {
-            streamsize __c = 0;
             if (__n > 0)
             {
-                typedef istreambuf_iterator<char_type, traits_type> _I;
-                _I __i(*this);
-                _I __eof;
-                for (; __i != __eof && __n > 1; ++__i, ++__s, ++__c)
+                ios_base::iostate __err = ios_base::goodbit;
+                while (__gc_ < __n-1)
                 {
-                    char_type __ch = *__i;
+                    int_type __i = this->rdbuf()->sgetc();
+                    if (traits_type::eq_int_type(__i, traits_type::eof()))
+                    {
+                       __err |= ios_base::eofbit;
+                       break;
+                    }
+                    char_type __ch = traits_type::to_char_type(__i);
                     if (traits_type::eq(__ch, __dlm))
                         break;
-                    *__s = __ch;
+                    *__s++ = __ch;
+                    ++__gc_;
+                     this->rdbuf()->sbumpc();
                 }
                 *__s = char_type();
-                ios_base::iostate __err = ios_base::goodbit;
-                if (__i == __eof)
-                   __err |= ios_base::eofbit;
-                if (__c == 0)
+                if (__gc_ == 0)
                    __err |= ios_base::failbit;
                 this->setstate(__err);
             }
             else
                 this->setstate(ios_base::failbit);
-            __gc_ = __c;
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -990,7 +985,7 @@ basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1011,42 +1006,40 @@ basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __s
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
         {
-            streamsize __c = 0;
             ios_base::iostate __err = ios_base::goodbit;
 #ifndef _LIBCPP_NO_EXCEPTIONS
             try
             {
-#endif
-                typedef istreambuf_iterator<char_type, traits_type> _I;
-                typedef ostreambuf_iterator<char_type, traits_type> _O;
-                _I __i(*this);
-                _I __eof;
-                _O __o(&__sb);
-                for (; __i != __eof; ++__i, ++__o, ++__c)
+#endif  // _LIBCPP_NO_EXCEPTIONS
+                while (true)
                 {
-                    char_type __ch = *__i;
+                    typename traits_type::int_type __i = this->rdbuf()->sgetc();
+                    if (traits_type::eq_int_type(__i, traits_type::eof()))
+                    {
+                       __err |= ios_base::eofbit;
+                       break;
+                    }
+                    char_type __ch = traits_type::to_char_type(__i);
                     if (traits_type::eq(__ch, __dlm))
                         break;
-                    *__o = __ch;
-                    if (__o.failed())
+                    if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
                         break;
+                    ++__gc_;
+                    this->rdbuf()->sbumpc();
                 }
-                if (__i == __eof)
-                   __err |= ios_base::eofbit;
 #ifndef _LIBCPP_NO_EXCEPTIONS
             }
             catch (...)
             {
             }
-#endif
-            if (__c == 0)
+#endif  // _LIBCPP_NO_EXCEPTIONS
+            if (__gc_ == 0)
                __err |= ios_base::failbit;
             this->setstate(__err);
-            __gc_ = __c;
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -1054,7 +1047,7 @@ basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __s
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1074,37 +1067,40 @@ basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_typ
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
         {
-            streamsize __c = 0;
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            _I __i(*this);
-            _I __eof;
-            for (; __i != __eof; ++__s, --__n)
+            ios_base::iostate __err = ios_base::goodbit;
+            while (true)
             {
-                char_type __ch = *__i;
-                ++__i;
-                ++__c;
+                typename traits_type::int_type __i = this->rdbuf()->sgetc();
+                if (traits_type::eq_int_type(__i, traits_type::eof()))
+                {
+                   __err |= ios_base::eofbit;
+                   break;
+                }
+                char_type __ch = traits_type::to_char_type(__i);
                 if (traits_type::eq(__ch, __dlm))
+                {
+                    this->rdbuf()->sbumpc();
+                    ++__gc_;
                     break;
-                if (__n < 2)
+                }
+                if (__gc_ >= __n-1)
                 {
-                    this->setstate(ios_base::failbit);
+                    __err |= ios_base::failbit;
                     break;
                 }
-                *__s = __ch;
+                *__s++ = __ch;
+                this->rdbuf()->sbumpc();
+                ++__gc_;
             }
-            if (__n)
+            if (__n > 0)
                 *__s = char_type();
-            ios_base::iostate __err = ios_base::goodbit;
-            if (__i == __eof)
-               __err |= ios_base::eofbit;
-            if (__c == 0)
+            if (__gc_ == 0)
                __err |= ios_base::failbit;
             this->setstate(__err);
-            __gc_ = __c;
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -1112,7 +1108,7 @@ basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_typ
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1132,39 +1128,44 @@ basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
         {
-            streamsize __c = 0;
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            _I __i(*this);
-            _I __eof;
-            if (__n != numeric_limits<streamsize>::max())
+            ios_base::iostate __err = ios_base::goodbit;
+            if (__n == numeric_limits<streamsize>::max())
             {
-                for (; __n > 0 && __i != __eof; --__n)
+                while (true)
                 {
-                    char_type __ch = *__i;
-                    ++__i;
-                    ++__c;
-                    if (traits_type::eq(__ch, __dlm))
+                    typename traits_type::int_type __i = this->rdbuf()->sbumpc();
+                    if (traits_type::eq_int_type(__i, traits_type::eof()))
+                    {
+                       __err |= ios_base::eofbit;
+                       break;
+                    }
+                    ++__gc_;
+                    char_type __ch = traits_type::to_char_type(__i);
+                    if (traits_type::eq(__ch, static_cast<char_type>(__dlm)))
                         break;
                 }
             }
             else
             {
-                while (__i != __eof)
+                while (__gc_ < __n)
                 {
-                    char_type __ch = *__i;
-                    ++__i;
-                    ++__c;
-                    if (traits_type::eq(__ch, __dlm))
+                    typename traits_type::int_type __i = this->rdbuf()->sbumpc();
+                    if (traits_type::eq_int_type(__i, traits_type::eof()))
+                    {
+                       __err |= ios_base::eofbit;
+                       break;
+                    }
+                    ++__gc_;
+                    char_type __ch = traits_type::to_char_type(__i);
+                    if (traits_type::eq(__ch, static_cast<char_type>(__dlm)))
                         break;
                 }
             }
-            if (__i == __eof)
-                this->setstate(ios_base::eofbit);
-            __gc_ = __c;
+            this->setstate(__err);
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -1172,7 +1173,7 @@ basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1185,7 +1186,7 @@ basic_istream<_CharT, _Traits>::peek()
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
             __r = this->rdbuf()->sgetc();
@@ -1195,7 +1196,7 @@ basic_istream<_CharT, _Traits>::peek()
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __r;
 }
 
@@ -1207,24 +1208,20 @@ basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
         {
-            streamsize __c = 0;
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            _I __i(*this);
-            _I __eof;
-            for (; __i != __eof && __n > 0; ++__i, ++__s, ++__c, --__n)
-                *__s = *__i;
-            if (__i == __eof)
+            for (; __gc_ < __n; ++__gc_)
             {
-                ios_base::iostate __err = ios_base::eofbit;
-                if (__n > 0)
-                    __err |= ios_base::failbit;
-                this->setstate(__err);
+                typename traits_type::int_type __i = this->rdbuf()->sbumpc();
+                if (traits_type::eq_int_type(__i, traits_type::eof()))
+                {
+                   this->setstate(ios_base::failbit | ios_base::eofbit);
+                   break;
+                }
+                *__s++ = traits_type::to_char_type(__i);
             }
-            __gc_ = __c;
         }
         else
             this->setstate(ios_base::failbit);
@@ -1234,7 +1231,7 @@ basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1242,45 +1239,19 @@ template<class _CharT, class _Traits>
 streamsize
 basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
 {
-    __gc_ = 0;
-    streamsize __c = 0;
-#ifndef _LIBCPP_NO_EXCEPTIONS
-    try
+    streamsize __c = this->rdbuf()->in_avail();
+    switch (__c)
     {
-#endif
-        sentry __sen(*this, true);
-        if (__sen)
-        {
-            typedef istreambuf_iterator<char_type, traits_type> _I;
-            _I __i(*this);
-            _I __eof;
-            __c = this->rdbuf()->in_avail();
-            switch (__c)
-            {
-            case -1:
-                __i = __eof;
-                break;
-            case 0:
-                break;
-            default:
-                __c = min(__c, __n);
-                for (streamsize __k = 0; __k < __c; ++__k, ++__s, ++__i)
-                    *__s = *__i;
-            }
-            if (__i == __eof)
-                this->setstate(ios_base::eofbit);
-            __gc_ = __c;
-        }
-        else
-            this->setstate(ios_base::failbit);
-#ifndef _LIBCPP_NO_EXCEPTIONS
+    case -1:
+        this->setstate(ios_base::eofbit);
+        break;
+    case 0:
+        break;
+    default:
+        read(__s, _VSTD::min(__c, __n));
+        break;
     }
-    catch (...)
-    {
-        this->__set_badbit_and_consider_rethrow();
-    }
-#endif
-    return __c;
+    return __gc_;
 }
 
 template<class _CharT, class _Traits>
@@ -1291,7 +1262,8 @@ basic_istream<_CharT, _Traits>::putback(char_type __c)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
+        this->clear(this->rdstate() & ~ios_base::eofbit);
         sentry __sen(*this, true);
         if (__sen)
         {
@@ -1306,7 +1278,7 @@ basic_istream<_CharT, _Traits>::putback(char_type __c)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1318,7 +1290,8 @@ basic_istream<_CharT, _Traits>::unget()
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
+        this->clear(this->rdstate() & ~ios_base::eofbit);
         sentry __sen(*this, true);
         if (__sen)
         {
@@ -1333,7 +1306,7 @@ basic_istream<_CharT, _Traits>::unget()
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1345,7 +1318,7 @@ basic_istream<_CharT, _Traits>::sync()
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
         {
@@ -1363,7 +1336,7 @@ basic_istream<_CharT, _Traits>::sync()
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __r;
 }
 
@@ -1375,7 +1348,7 @@ basic_istream<_CharT, _Traits>::tellg()
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
             __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
@@ -1385,7 +1358,7 @@ basic_istream<_CharT, _Traits>::tellg()
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __r;
 }
 
@@ -1396,7 +1369,8 @@ basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
+        this->clear(this->rdstate() & ~ios_base::eofbit);
         sentry __sen(*this, true);
         if (__sen)
             if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
@@ -1407,7 +1381,7 @@ basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1418,7 +1392,7 @@ basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         sentry __sen(*this, true);
         if (__sen)
             this->rdbuf()->pubseekoff(__off, __dir, ios_base::in);
@@ -1428,7 +1402,7 @@ basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
     {
         this->__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return *this;
 }
 
@@ -1439,19 +1413,23 @@ ws(basic_istream<_CharT, _Traits>& __is)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
         if (__sen)
         {
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
-            _I __i(__is);
-            _I __eof;
-            for (; __i != __eof; ++__i)
-                if (!__ct.is(__ct.space, *__i))
+            while (true)
+            {
+                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
+                if (_Traits::eq_int_type(__i, _Traits::eof()))
+                {
+                   __is.setstate(ios_base::eofbit);
+                   break;
+                }
+                if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
                     break;
-            if (__i == __eof)
-                __is.setstate(ios_base::failbit | ios_base::eofbit);
+                __is.rdbuf()->sbumpc();
+            }
         }
 #ifndef _LIBCPP_NO_EXCEPTIONS
     }
@@ -1459,11 +1437,11 @@ ws(basic_istream<_CharT, _Traits>& __is)
     {
         __is.__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __is;
 }
 
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits, class _Tp>
 inline _LIBCPP_INLINE_VISIBILITY
@@ -1474,10 +1452,10 @@ operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
     return __is;
 }
 
-#endif
+#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits>
-class basic_iostream
+class _LIBCPP_VISIBLE basic_iostream
     : public basic_istream<_CharT, _Traits>,
       public basic_ostream<_CharT, _Traits>
 {
@@ -1493,12 +1471,14 @@ public:
     explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb);
     virtual ~basic_iostream();
 protected:
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+    _LIBCPP_INLINE_VISIBILITY
     basic_iostream(basic_iostream&& __rhs);
 #endif
 
     // assign/swap
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
+    _LIBCPP_INLINE_VISIBILITY
     basic_iostream& operator=(basic_iostream&& __rhs);
 #endif
     void swap(basic_iostream& __rhs);
@@ -1512,12 +1492,12 @@ basic_iostream<_CharT, _Traits>::basic_iostream(basic_streambuf<char_type, trait
 {
 }
 
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits>
 inline _LIBCPP_INLINE_VISIBILITY
 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
-    : basic_istream<_CharT, _Traits>(_STD::move(__rhs))
+    : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
 {
 }
 
@@ -1530,7 +1510,7 @@ basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
     return *this;
 }
 
-#endif
+#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits>
 basic_iostream<_CharT, _Traits>::~basic_iostream()
@@ -1553,32 +1533,35 @@ operator>>(basic_istream<_CharT, _Traits>& __is,
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
         if (__sen)
         {
             __str.clear();
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
             streamsize __n = __is.width();
-            if (__n == 0)
+            if (__n <= 0)
                 __n = __str.max_size();
-            if (__n < 0)
+            if (__n <= 0)
                 __n = numeric_limits<streamsize>::max();
             streamsize __c = 0;
             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
-            _I __i(__is);
-            _I __eof;
-            for (; __i != __eof && __c < __n; ++__i, ++__c)
+            ios_base::iostate __err = ios_base::goodbit;
+            while (__c < __n)
             {
-                _CharT __ch = *__i;
+                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
+                if (_Traits::eq_int_type(__i, _Traits::eof()))
+                {
+                   __err |= ios_base::eofbit;
+                   break;
+                }
+                _CharT __ch = _Traits::to_char_type(__i);
                 if (__ct.is(__ct.space, __ch))
                     break;
                 __str.push_back(__ch);
+                ++__c;
+                 __is.rdbuf()->sbumpc();
             }
             __is.width(0);
-            ios_base::iostate __err = ios_base::goodbit;
-            if (__i == __eof)
-               __err |= ios_base::eofbit;
             if (__c == 0)
                __err |= ios_base::failbit;
             __is.setstate(__err);
@@ -1591,48 +1574,45 @@ operator>>(basic_istream<_CharT, _Traits>& __is,
     {
         __is.__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __is;
 }
 
 template<class _CharT, class _Traits, class _Allocator>
-basic_istream<_CharT, _Traits>& 
+basic_istream<_CharT, _Traits>&
 getline(basic_istream<_CharT, _Traits>& __is,
         basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
 {
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
         if (__sen)
         {
             __str.clear();
-            streamsize __c = 0;
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
-            _I __i(__is);
-            _I __eof;
-            streamsize __n = __str.max_size();
-            if (__n < 0)
-                __n = numeric_limits<streamsize>::max();
-            for (; __i != __eof;)
+            ios_base::iostate __err = ios_base::goodbit;
+            streamsize __extr = 0;
+            while (true)
             {
-                _CharT __ch = *__i;
-                ++__i;
-                ++__c;
+                typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
+                if (_Traits::eq_int_type(__i, _Traits::eof()))
+                {
+                   __err |= ios_base::eofbit;
+                   break;
+                }
+                ++__extr;
+                _CharT __ch = _Traits::to_char_type(__i);
                 if (_Traits::eq(__ch, __dlm))
                     break;
-                if (__c == __n)
+                __str.push_back(__ch);
+                if (__str.size() == __str.max_size())
                 {
-                    __is.setstate(ios_base::failbit);
+                    __err |= ios_base::failbit;
                     break;
                 }
-                __str.push_back(__ch);
             }
-            ios_base::iostate __err = ios_base::goodbit;
-            if (__i == __eof)
-               __err |= ios_base::eofbit;
-            if (__c == 0)
+            if (__extr == 0)
                __err |= ios_base::failbit;
             __is.setstate(__err);
         }
@@ -1642,24 +1622,24 @@ getline(basic_istream<_CharT, _Traits>& __is,
     {
         __is.__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __is;
 }
 
 template<class _CharT, class _Traits, class _Allocator>
-inline
-basic_istream<_CharT, _Traits>& 
+inline _LIBCPP_INLINE_VISIBILITY
+basic_istream<_CharT, _Traits>&
 getline(basic_istream<_CharT, _Traits>& __is,
         basic_string<_CharT, _Traits, _Allocator>& __str)
 {
     return getline(__is, __str, __is.widen('\n'));
 }
 
-#ifdef _LIBCPP_MOVE
+#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template<class _CharT, class _Traits, class _Allocator>
-inline
-basic_istream<_CharT, _Traits>& 
+inline _LIBCPP_INLINE_VISIBILITY
+basic_istream<_CharT, _Traits>&
 getline(basic_istream<_CharT, _Traits>&& __is,
         basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
 {
@@ -1667,15 +1647,15 @@ getline(basic_istream<_CharT, _Traits>&& __is,
 }
 
 template<class _CharT, class _Traits, class _Allocator>
-inline
-basic_istream<_CharT, _Traits>& 
+inline _LIBCPP_INLINE_VISIBILITY
+basic_istream<_CharT, _Traits>&
 getline(basic_istream<_CharT, _Traits>&& __is,
         basic_string<_CharT, _Traits, _Allocator>& __str)
 {
     return getline(__is, __str, __is.widen('\n'));
 }
 
-#endif
+#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
 
 template <class _CharT, class _Traits, size_t _Size>
 basic_istream<_CharT, _Traits>&
@@ -1684,30 +1664,32 @@ operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
 #ifndef _LIBCPP_NO_EXCEPTIONS
     try
     {
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
         if (__sen)
         {
             basic_string<_CharT, _Traits> __str;
             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
-            typedef istreambuf_iterator<_CharT, _Traits> _I;
             streamsize __c = 0;
+            ios_base::iostate __err = ios_base::goodbit;
             _CharT __zero = __ct.widen('0');
             _CharT __one = __ct.widen('1');
-            _I __i(__is);
-            _I __eof;
-            for (; __i != __eof && __c < _Size; ++__i, ++__c)
+            while (__c < _Size)
             {
-                _CharT __ch = *__i;
-                if (__ch != __zero && __ch != __one)
+                typename _Traits::int_type __i = __is.rdbuf()->sgetc();
+                if (_Traits::eq_int_type(__i, _Traits::eof()))
+                {
+                   __err |= ios_base::eofbit;
+                   break;
+                }
+                _CharT __ch = _Traits::to_char_type(__i);
+                if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
                     break;
                 __str.push_back(__ch);
+                ++__c;
+                 __is.rdbuf()->sbumpc();
             }
-            __is.width(0);
             __x = bitset<_Size>(__str);
-            ios_base::iostate __err = ios_base::goodbit;
-            if (__i == __eof)
-               __err |= ios_base::eofbit;
             if (__c == 0)
                __err |= ios_base::failbit;
             __is.setstate(__err);
@@ -1720,7 +1702,7 @@ operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
     {
         __is.__set_badbit_and_consider_rethrow();
     }
-#endif
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __is;
 }