std::basic_istream<stream_char_type, stream_char_traits> &
		operator >>(std::basic_istream<stream_char_type, stream_char_traits> & is,
		            basic_string_facade<storage, char_traits> & str)
	{
		typedef std::ctype<stream_char_type> ctype_type;
		typedef std::basic_istream<stream_char_type, stream_char_traits> istream_type;
		typedef ext::basic_string_facade<storage, char_traits> string_type;
		typedef typename string_type::size_type size_type;

		std::ios_base::iostate state = std::ios_base::goodbit;
		const typename istream_type::sentry ok(is);

		if (ok)
		{
			const auto & lc = is.getloc();
			auto * buf = is.rdbuf();

			const ctype_type & ctype_fac = std::use_facet<ctype_type>(lc);
			str.clear();

			std::streamsize w = is.width();
			size_type size =
				0 < w && static_cast<size_type>(w) < str.max_size()
					? w
					: str.max_size();

			try {
				auto meta = buf->sgetc();
				for (; 0 < size; --size, meta = buf->snextc())
				{
					if (stream_char_traits::eq_int_type(stream_char_traits::eof(), meta))
					{
						// end of file
						state |= std::ios_base::eofbit;
						break;
					}

					auto ch = stream_char_traits::to_char_type(meta);
					if (ctype_fac.is(ctype_type::space, ch))
					{
						break;
					}

					str.push_back(ch);
				} // for
			}
			catch (...)
			{
				is.setstate(std::ios::badbit);
			}
		} // if ok
		
		is.width(0);
		if (str.empty()) // read nothing
			state |= std::ios_base::failbit;
		is.setstate(state);
		return is;
	}
示例#2
0
文件: io.hpp 项目: Fadis/Sprout
	inline SPROUT_NON_CONSTEXPR std::basic_istream<T, StreamTraits>&
	operator>>(std::basic_istream<T, StreamTraits>& lhs, sprout::basic_string<T, N, Traits>& rhs) {
		typedef T elem_type;
		typedef StreamTraits traits_type;
		typedef std::basic_istream<T, StreamTraits> istream_type;
		typedef sprout::basic_string<T, N, Traits> string_type;
		typedef std::ctype<elem_type> ctype_type;
		typedef typename string_type::size_type size_type;
		std::ios_base::iostate state = std::ios_base::goodbit;
		bool changed = false;
		size_type current = 0;
		if (typename istream_type::sentry(lhs)) {
			ctype_type const& ctype_fac = std::use_facet<ctype_type>(lhs.getloc());
			try {
				size_type remain = 0 < lhs.width() && static_cast<size_type>(lhs.width()) < rhs.max_size()
					? static_cast<size_type>(lhs.width())
					: rhs.max_size()
					;
				typename traits_type::int_type meta = lhs.rdbuf()->sgetc();
				for (; remain; --remain, meta = lhs.rdbuf()->snextc())
					if (traits_type::eq_int_type(traits_type::eof(), meta)) {
						state |= std::ios_base::eofbit;
						break;
					} else if (ctype_fac.is(ctype_type::space, traits_type::to_char_type(meta))) {
						break;
					} else {
						rhs[current] = traits_type::to_char_type(meta);
						changed = true;
						++current;
					}
			} catch (...) {
				state |= std::ios_base::badbit;
			}
		}
		lhs.width(0);
		if (!changed) {
			state |= std::ios_base::failbit;
		}
		lhs.setstate(state);
		rhs.resize(current);
		return lhs;
	}