@@ -1265,41 +1265,49 @@ _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
12651265getline (basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
12661266 ios_base::iostate __state = ios_base::goodbit;
12671267 typename basic_istream<_CharT, _Traits>::sentry __sen (__is, true );
1268- if (__sen) {
1268+ if (!__sen)
1269+ return __is;
12691270# if _LIBCPP_HAS_EXCEPTIONS
1270- try {
1271+ try {
12711272# endif
1272- __str.clear ();
1273- streamsize __extr = 0 ;
1274- while (true ) {
1275- typename _Traits::int_type __i = __is.rdbuf ()->sbumpc ();
1276- if (_Traits::eq_int_type (__i, _Traits::eof ())) {
1277- __state |= ios_base::eofbit;
1278- break ;
1279- }
1280- ++__extr;
1281- _CharT __ch = _Traits::to_char_type (__i);
1282- if (_Traits::eq (__ch, __dlm))
1283- break ;
1284- __str.push_back (__ch);
1285- if (__str.size () == __str.max_size ()) {
1273+ __str.clear ();
1274+
1275+ auto & __buffer = *__is.rdbuf ();
1276+
1277+ auto __next = __buffer.sgetc ();
1278+ for (; !_Traits::eq_int_type (__next, _Traits::eof ()); __next = __buffer.sgetc ()) {
1279+ const auto * __first = __buffer.gptr ();
1280+ const auto * __last = __buffer.egptr ();
1281+ const auto * __match = _Traits::find (__first, __last - __first, __dlm);
1282+ if (__match) {
1283+ if (auto __cap = __str.max_size () - __str.size (); __cap <= static_cast <size_t >(__match - __first)) {
1284+ __str.append (__first, __cap);
1285+ __buffer.__gbump_ptrdiff (__cap);
12861286 __state |= ios_base::failbit;
12871287 break ;
12881288 }
1289+ __str.append (__first, __match);
1290+ __buffer.__gbump_ptrdiff (__match - __first + 1 );
1291+ break ;
12891292 }
1290- if (__extr == 0 )
1291- __state |= ios_base::failbit;
1293+
1294+ __str.append (__first, __last);
1295+ __buffer.__gbump_ptrdiff (__last - __first);
1296+ }
1297+
1298+ if (_Traits::eq_int_type (__next, _Traits::eof ()))
1299+ __state |= ios_base::eofbit | (__str.empty () ? ios_base::failbit : ios_base::goodbit);
1300+
12921301# if _LIBCPP_HAS_EXCEPTIONS
1293- } catch (...) {
1294- __state |= ios_base::badbit;
1295- __is.__setstate_nothrow (__state);
1296- if (__is.exceptions () & ios_base::badbit) {
1297- throw ;
1298- }
1302+ } catch (...) {
1303+ __state |= ios_base::badbit;
1304+ __is.__setstate_nothrow (__state);
1305+ if (__is.exceptions () & ios_base::badbit) {
1306+ throw ;
12991307 }
1300- # endif
1301- __is.setstate (__state);
13021308 }
1309+ # endif
1310+ __is.setstate (__state);
13031311 return __is;
13041312}
13051313
0 commit comments