Changeset View
Changeset View
Standalone View
Standalone View
include/istream
Show First 20 Lines • Show All 511 Lines • ▼ Show 20 Lines | |||||
template <class _CharT, class _Traits> | template <class _CharT, class _Traits> | ||||
basic_istream<_CharT, _Traits>& | basic_istream<_CharT, _Traits>& | ||||
basic_istream<_CharT, _Traits>::operator>>(int& __n) | basic_istream<_CharT, _Traits>::operator>>(int& __n) | ||||
{ | { | ||||
return _VSTD::__input_arithmetic_with_numeric_limits<int>(*this, __n); | return _VSTD::__input_arithmetic_with_numeric_limits<int>(*this, __n); | ||||
} | } | ||||
template<class _CharT, class _Traits> | template<class _CharT, class _Traits> | ||||
_LIBCPP_INLINE_VISIBILITY | |||||
basic_istream<_CharT, _Traits>& | basic_istream<_CharT, _Traits>& | ||||
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) | __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n) | ||||
{ | { | ||||
#ifndef _LIBCPP_NO_EXCEPTIONS | #ifndef _LIBCPP_NO_EXCEPTIONS | ||||
try | try | ||||
{ | { | ||||
#endif // _LIBCPP_NO_EXCEPTIONS | #endif // _LIBCPP_NO_EXCEPTIONS | ||||
typename basic_istream<_CharT, _Traits>::sentry __sen(__is); | typename basic_istream<_CharT, _Traits>::sentry __sen(__is); | ||||
if (__sen) | if (__sen) | ||||
{ | { | ||||
streamsize __n = __is.width(); | auto __s = __p; | ||||
if (__n <= 0) | |||||
__n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1; | |||||
streamsize __c = 0; | |||||
const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc()); | const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc()); | ||||
ios_base::iostate __err = ios_base::goodbit; | ios_base::iostate __err = ios_base::goodbit; | ||||
while (__c < __n-1) | while (__s != __p + (__n-1)) | ||||
{ | { | ||||
typename _Traits::int_type __i = __is.rdbuf()->sgetc(); | typename _Traits::int_type __i = __is.rdbuf()->sgetc(); | ||||
if (_Traits::eq_int_type(__i, _Traits::eof())) | if (_Traits::eq_int_type(__i, _Traits::eof())) | ||||
{ | { | ||||
__err |= ios_base::eofbit; | __err |= ios_base::eofbit; | ||||
break; | break; | ||||
} | } | ||||
_CharT __ch = _Traits::to_char_type(__i); | _CharT __ch = _Traits::to_char_type(__i); | ||||
if (__ct.is(__ct.space, __ch)) | if (__ct.is(__ct.space, __ch)) | ||||
break; | break; | ||||
*__s++ = __ch; | *__s++ = __ch; | ||||
++__c; | |||||
__is.rdbuf()->sbumpc(); | __is.rdbuf()->sbumpc(); | ||||
} | } | ||||
*__s = _CharT(); | *__s = _CharT(); | ||||
__is.width(0); | __is.width(0); | ||||
if (__c == 0) | if (__s == __p) | ||||
__err |= ios_base::failbit; | __err |= ios_base::failbit; | ||||
__is.setstate(__err); | __is.setstate(__err); | ||||
} | } | ||||
#ifndef _LIBCPP_NO_EXCEPTIONS | #ifndef _LIBCPP_NO_EXCEPTIONS | ||||
} | } | ||||
catch (...) | catch (...) | ||||
{ | { | ||||
__is.__set_badbit_and_consider_rethrow(); | __is.__set_badbit_and_consider_rethrow(); | ||||
} | } | ||||
#endif // _LIBCPP_NO_EXCEPTIONS | #endif // _LIBCPP_NO_EXCEPTIONS | ||||
return __is; | return __is; | ||||
} | } | ||||
#if _LIBCPP_STD_VER > 17 | |||||
template<class _CharT, class _Traits, size_t _Np> | |||||
inline _LIBCPP_INLINE_VISIBILITY | |||||
basic_istream<_CharT, _Traits>& | |||||
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np]) | |||||
{ | |||||
auto __n = _Np; | |||||
if (__is.width() > 0) | |||||
__n = _VSTD::min(size_t(__is.width()), _Np); | |||||
return _VSTD::__input_c_string(__is, __buf, __n); | |||||
} | |||||
template<class _Traits, size_t _Np> | |||||
inline _LIBCPP_INLINE_VISIBILITY | |||||
basic_istream<char, _Traits>& | |||||
operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np]) | |||||
{ | |||||
return __is >> (char(&)[_Np])__buf; | |||||
} | |||||
template<class _Traits, size_t _Np> | |||||
inline _LIBCPP_INLINE_VISIBILITY | |||||
basic_istream<char, _Traits>& | |||||
operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np]) | |||||
{ | |||||
return __is >> (char(&)[_Np])__buf; | |||||
} | |||||
#else | |||||
template<class _CharT, class _Traits> | |||||
inline _LIBCPP_INLINE_VISIBILITY | |||||
basic_istream<_CharT, _Traits>& | |||||
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) | |||||
{ | |||||
streamsize __n = __is.width(); | |||||
if (__n <= 0) | |||||
ldionne: Should this instead be `if (__n == 0)`? Can `width()` ever return a negative number? | |||||
Not Done ReplyInline ActionsMy interpretation of 27.5.3.2 is that you can retrieve the value you set through width(streamsize). And the wording here (unmodified) says "If width() is greater than zero, [...]," so the opposite should be width() <= 0. lichray: My interpretation of 27.5.3.2 is that you can retrieve the value you set through `width… | |||||
__n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1; | |||||
return _VSTD::__input_c_string(__is, __s, size_t(__n)); | |||||
} | |||||
template<class _Traits> | template<class _Traits> | ||||
inline _LIBCPP_INLINE_VISIBILITY | inline _LIBCPP_INLINE_VISIBILITY | ||||
basic_istream<char, _Traits>& | basic_istream<char, _Traits>& | ||||
operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) | operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) | ||||
{ | { | ||||
return __is >> (char*)__s; | return __is >> (char*)__s; | ||||
} | } | ||||
template<class _Traits> | template<class _Traits> | ||||
inline _LIBCPP_INLINE_VISIBILITY | inline _LIBCPP_INLINE_VISIBILITY | ||||
basic_istream<char, _Traits>& | basic_istream<char, _Traits>& | ||||
operator>>(basic_istream<char, _Traits>& __is, signed char* __s) | operator>>(basic_istream<char, _Traits>& __is, signed char* __s) | ||||
{ | { | ||||
return __is >> (char*)__s; | return __is >> (char*)__s; | ||||
} | } | ||||
#endif // _LIBCPP_STD_VER > 17 | |||||
template<class _CharT, class _Traits> | template<class _CharT, class _Traits> | ||||
basic_istream<_CharT, _Traits>& | basic_istream<_CharT, _Traits>& | ||||
operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) | operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) | ||||
{ | { | ||||
#ifndef _LIBCPP_NO_EXCEPTIONS | #ifndef _LIBCPP_NO_EXCEPTIONS | ||||
try | try | ||||
{ | { | ||||
#endif // _LIBCPP_NO_EXCEPTIONS | #endif // _LIBCPP_NO_EXCEPTIONS | ||||
▲ Show 20 Lines • Show All 886 Lines • Show Last 20 Lines |
Should this instead be if (__n == 0)? Can width() ever return a negative number? cppreference says this about streamsize:
I couldn't find a matching statement in the standard, however there's this footnote in 30.5.2:
This does not clearly say that negative values are never used, but it does suggest it. Maybe it's safer to still use __n <= 0.