From 51a320e98ab3b765a2e671b99082e31af210fba7 Mon Sep 17 00:00:00 2001 From: jzmaddock Date: Wed, 11 Mar 2015 19:25:14 +0000 Subject: [PATCH] Fix for stream reading: handles more formats now, including things like CSV lists. --- include/boost/multiprecision/number.hpp | 70 ++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/include/boost/multiprecision/number.hpp b/include/boost/multiprecision/number.hpp index c977958e..24b95ad1 100644 --- a/include/boost/multiprecision/number.hpp +++ b/include/boost/multiprecision/number.hpp @@ -24,6 +24,7 @@ #include #include // stream operators #include // EOF +#include // isspace namespace boost{ namespace multiprecision{ @@ -1706,6 +1707,48 @@ inline std::ostream& operator << (std::ostream& os, const expressionsgetc(); + + for(;; c = is.rdbuf()->snextc()) + if(std::istream::traits_type::eq_int_type(std::istream::traits_type::eof(), c)) + { // end of file: + state |= std::ios_base::eofbit; + break; + } + else if(permitted_chars.find_first_of(std::istream::traits_type::to_char_type(c)) == std::string::npos) + { + // Invalid numeric character, stop reading: + is.rdbuf()->sputbackc(c); + break; + } + else + { + result.append(1, std::istream::traits_type::to_char_type(c)); + } + } + + if(!result.size()) + state |= std::ios_base::failbit; + is.setstate(state); + return result; +} } // namespace detail @@ -1715,12 +1758,27 @@ inline std::istream& operator >> (std::istream& is, number> s; - if(hex_format && (number_category::value == number_kind_integer) && ((s[0] != '0') || (s[1] != 'x'))) - s.insert(s.find_first_not_of("+-"), "0x"); - if(oct_format && (number_category::value == number_kind_integer) && (s[0] != '0')) - s.insert(s.find_first_not_of("+-"), "0"); - r.assign(s); + switch(boost::multiprecision::number_category >::value) + { + case boost::multiprecision::number_kind_integer: + s = detail::read_string_while(is, "+-0xX123456789"); + break; + case boost::multiprecision::number_kind_floating_point: + s = detail::read_string_while(is, "+-eE.0123456789infINFnanNANinfinityINFINITY"); + break; + default: + is >> s; + } + if(s.size()) + { + if(hex_format && (number_category::value == number_kind_integer) && ((s[0] != '0') || (s[1] != 'x'))) + s.insert(s.find_first_not_of("+-"), "0x"); + if(oct_format && (number_category::value == number_kind_integer) && (s[0] != '0')) + s.insert(s.find_first_not_of("+-"), "0"); + r.assign(s); + } + else if(!is.fail()) + is.setstate(std::istream::failbit); return is; }