Fix <rdar://problem/10256836> getline of an empty string mistakenly causes failure

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@141506 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2011-10-09 15:20:46 +00:00
parent c6fe8cafc3
commit b97de44471
2 changed files with 11 additions and 3 deletions

View File

@ -1586,6 +1586,7 @@ getline(basic_istream<_CharT, _Traits>& __is,
{ {
__str.clear(); __str.clear();
ios_base::iostate __err = ios_base::goodbit; ios_base::iostate __err = ios_base::goodbit;
streamsize __extr = 0;
while (true) while (true)
{ {
typename _Traits::int_type __i = __is.rdbuf()->sbumpc(); typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
@ -1594,6 +1595,7 @@ getline(basic_istream<_CharT, _Traits>& __is,
__err |= ios_base::eofbit; __err |= ios_base::eofbit;
break; break;
} }
++__extr;
_CharT __ch = _Traits::to_char_type(__i); _CharT __ch = _Traits::to_char_type(__i);
if (_Traits::eq(__ch, __dlm)) if (_Traits::eq(__ch, __dlm))
break; break;
@ -1604,7 +1606,7 @@ getline(basic_istream<_CharT, _Traits>& __is,
break; break;
} }
} }
if (__str.empty()) if (__extr == 0)
__err |= ios_base::failbit; __err |= ios_base::failbit;
__is.setstate(__err); __is.setstate(__err);
} }

View File

@ -21,7 +21,7 @@
int main() int main()
{ {
{ {
std::istringstream in(" abc* def* ghij"); std::istringstream in(" abc* def** ghij");
std::string s("initial text"); std::string s("initial text");
getline(in, s, '*'); getline(in, s, '*');
assert(in.good()); assert(in.good());
@ -30,11 +30,14 @@ int main()
assert(in.good()); assert(in.good());
assert(s == " def"); assert(s == " def");
getline(in, s, '*'); getline(in, s, '*');
assert(in.good());
assert(s == "");
getline(in, s, '*');
assert(in.eof()); assert(in.eof());
assert(s == " ghij"); assert(s == " ghij");
} }
{ {
std::wistringstream in(L" abc* def* ghij"); std::wistringstream in(L" abc* def** ghij");
std::wstring s(L"initial text"); std::wstring s(L"initial text");
getline(in, s, L'*'); getline(in, s, L'*');
assert(in.good()); assert(in.good());
@ -43,6 +46,9 @@ int main()
assert(in.good()); assert(in.good());
assert(s == L" def"); assert(s == L" def");
getline(in, s, L'*'); getline(in, s, L'*');
assert(in.good());
assert(s == L"");
getline(in, s, L'*');
assert(in.eof()); assert(in.eof());
assert(s == L" ghij"); assert(s == L" ghij");
} }