Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72185 - in branches/release: boost libs/conversion libs/conversion/test
From: antoshkka_at_[hidden]
Date: 2011-05-26 12:27:53


Author: apolukhin
Date: 2011-05-26 12:27:53 EDT (Thu, 26 May 2011)
New Revision: 72185
URL: http://svn.boost.org/trac/boost/changeset/72185

Log:
Merge from trunk: r72184 (a lot of optimizations, new tests, fix inspection warnings).

Text files modified:
   branches/release/boost/lexical_cast.hpp | 563 +++++++++++++++++++++++++++++++++++++--
   branches/release/libs/conversion/lexical_cast.htm | 8
   branches/release/libs/conversion/lexical_cast_test.cpp | 163 ++++++++++
   branches/release/libs/conversion/test/lexical_cast_vc8_bug_test.cpp | 14
   4 files changed, 703 insertions(+), 45 deletions(-)

Modified: branches/release/boost/lexical_cast.hpp
==============================================================================
--- branches/release/boost/lexical_cast.hpp (original)
+++ branches/release/boost/lexical_cast.hpp 2011-05-26 12:27:53 EDT (Thu, 26 May 2011)
@@ -11,8 +11,8 @@
 // enhanced with contributions from Terje Slettebo,
 // with additional fixes and suggestions from Gennaro Prota,
 // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
-// Alexander Nasonov and other Boosters
-// when: November 2000, March 2003, June 2005, June 2006
+// Alexander Nasonov, Antony Polukhin and other Boosters
+// when: November 2000, March 2003, June 2005, June 2006, March 2011
 
 #include <climits>
 #include <cstddef>
@@ -20,12 +20,18 @@
 #include <string>
 #include <typeinfo>
 #include <exception>
+#include <cmath>
 #include <boost/config.hpp>
 #include <boost/limits.hpp>
 #include <boost/mpl/if.hpp>
 #include <boost/throw_exception.hpp>
 #include <boost/type_traits/is_pointer.hpp>
+#include <boost/type_traits/is_integral.hpp>
+#include <boost/type_traits/is_arithmetic.hpp>
+#include <boost/numeric/conversion/cast.hpp>
+#include <boost/type_traits/ice.hpp>
 #include <boost/type_traits/make_unsigned.hpp>
+#include <boost/type_traits/is_signed.hpp>
 #include <boost/call_traits.hpp>
 #include <boost/static_assert.hpp>
 #include <boost/detail/lcast_precision.hpp>
@@ -457,7 +463,7 @@
 #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
     }
 
- namespace detail // '0' and '-' constants
+ namespace detail // '0', '+' and '-' constants
     {
         template<typename CharT> struct lcast_char_constants;
 
@@ -466,6 +472,7 @@
         {
             BOOST_STATIC_CONSTANT(char, zero = '0');
             BOOST_STATIC_CONSTANT(char, minus = '-');
+ BOOST_STATIC_CONSTANT(char, plus = '+');
         };
 
 #ifndef BOOST_LCAST_NO_WCHAR_T
@@ -474,6 +481,7 @@
         {
             BOOST_STATIC_CONSTANT(wchar_t, zero = L'0');
             BOOST_STATIC_CONSTANT(wchar_t, minus = L'-');
+ BOOST_STATIC_CONSTANT(wchar_t, plus = L'+');
         };
 #endif
     }
@@ -571,6 +579,86 @@
         }
     }
 
+ namespace detail // lcast_ret_unsigned
+ {
+ template<class Traits, class T, class CharT>
+ inline bool lcast_ret_unsigned(T& value, const CharT* const begin, const CharT* end)
+ {
+#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
+ BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
+#endif
+ typedef typename Traits::int_type int_type;
+ CharT const czero = lcast_char_constants<CharT>::zero;
+ --end;
+ value = 0;
+
+ if ( *end < czero || *end >= czero + 10 || begin > end)
+ return false;
+ value = *end - czero;
+ --end;
+ T multiplier = 1;
+
+#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
+ // TODO: use BOOST_NO_STD_LOCALE
+ std::locale loc;
+ typedef std::numpunct<CharT> numpunct;
+ numpunct const& np = BOOST_USE_FACET(numpunct, loc);
+ std::string const& grouping = np.grouping();
+ std::string::size_type const grouping_size = grouping.size();
+
+ /* According to [22.2.2.1.2] of Programming languages - C++
+ * we MUST check for correct grouping
+ */
+ if (grouping_size)
+ {
+ unsigned char current_grouping = 0;
+ CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
+ char remained = grouping[current_grouping] - 1;
+
+ for(;end>=begin; --end)
+ {
+ if (remained) {
+ T const new_sub_value = multiplier * 10 * (*end - czero);
+
+ if (*end < czero || *end >= czero + 10
+ /* detecting overflow */
+ || new_sub_value/10 != multiplier * (*end - czero)
+ || static_cast<T>((std::numeric_limits<T>::max)()-new_sub_value) < value
+ )
+ return false;
+
+ value += new_sub_value;
+ multiplier *= 10;
+ --remained;
+ } else {
+ if ( !Traits::eq(*end, thousands_sep) || begin == end ) return false;
+ if (current_grouping < grouping_size-1 ) ++current_grouping;
+ remained = grouping[current_grouping];
+ }
+ }
+ } else
+#endif
+ {
+ while ( begin <= end )
+ {
+ T const new_sub_value = multiplier * 10 * (*end - czero);
+
+ if (*end < czero || *end >= czero + 10
+ /* detecting overflow */
+ || new_sub_value/10 != multiplier * (*end - czero)
+ || static_cast<T>((std::numeric_limits<T>::max)()-new_sub_value) < value
+ )
+ return false;
+
+ value += new_sub_value;
+ multiplier *= 10;
+ --end;
+ }
+ }
+ return true;
+ }
+ }
+
     namespace detail // stream wrapper for handling lexical conversions
     {
         template<typename Target, typename Source, typename Traits>
@@ -756,8 +844,178 @@
             bool operator<<(double);
             bool operator<<(long double);
 
+ private:
+
+ template <typename Type>
+ bool input_operator_helper_unsigned(Type& output)
+ {
+ CharT const minus = lcast_char_constants<CharT>::minus;
+ CharT const plus = lcast_char_constants<CharT>::plus;
+ bool has_minus = false;
+
+ /* We won`t use `start' any more, so no need in decrementing it after */
+ if ( Traits::eq(minus,*start) )
+ {
+ ++start;
+ has_minus = true;
+ } else if ( Traits::eq( plus, *start ) )
+ {
+ ++start;
+ }
+
+ bool const succeed = lcast_ret_unsigned<Traits>(output, start, finish);
+#if (defined _MSC_VER)
+# pragma warning( push )
+// C4146: unary minus operator applied to unsigned type, result still unsigned
+# pragma warning( disable : 4146 )
+#elif defined( __BORLANDC__ )
+# pragma option push -w-8041
+#endif
+ if (has_minus) output = static_cast<Type>(-output);
+#if (defined _MSC_VER)
+# pragma warning( pop )
+#elif defined( __BORLANDC__ )
+# pragma option pop
+#endif
+ return succeed;
+ }
+
+ template <typename Type>
+ bool input_operator_helper_signed(Type& output)
+ {
+ CharT const minus = lcast_char_constants<CharT>::minus;
+ CharT const plus = lcast_char_constants<CharT>::plus;
+ typedef BOOST_DEDUCED_TYPENAME make_unsigned<Type>::type utype;
+ utype out_tmp =0;
+ bool has_minus = false;
+
+ /* We won`t use `start' any more, so no need in decrementing it after */
+ if ( Traits::eq(minus,*start) )
+ {
+ ++start;
+ has_minus = true;
+ } else if ( Traits::eq(plus, *start) )
+ {
+ ++start;
+ }
+
+ bool succeed = lcast_ret_unsigned<Traits>(out_tmp, start, finish);
+ if (has_minus) {
+#if (defined _MSC_VER)
+# pragma warning( push )
+// C4146: unary minus operator applied to unsigned type, result still unsigned
+# pragma warning( disable : 4146 )
+#elif defined( __BORLANDC__ )
+# pragma option push -w-8041
+#endif
+ utype const comp_val = static_cast<utype>(-(std::numeric_limits<Type>::min)());
+ succeed = succeed && out_tmp<=comp_val;
+ output = -out_tmp;
+#if (defined _MSC_VER)
+# pragma warning( pop )
+#elif defined( __BORLANDC__ )
+# pragma option pop
+#endif
+ } else {
+ utype const comp_val = static_cast<utype>((std::numeric_limits<Type>::max)());
+ succeed = succeed && out_tmp<=comp_val;
+ output = out_tmp;
+ }
+ return succeed;
+ }
+
         public: // input
 
+ bool operator>>(unsigned short& output)
+ {
+ return input_operator_helper_unsigned(output);
+ }
+
+ bool operator>>(unsigned int& output)
+ {
+ return input_operator_helper_unsigned(output);
+ }
+
+ bool operator>>(unsigned long int& output)
+ {
+ return input_operator_helper_unsigned(output);
+ }
+
+ bool operator>>(short& output)
+ {
+ return input_operator_helper_signed(output);
+ }
+
+ bool operator>>(int& output)
+ {
+ return input_operator_helper_signed(output);
+ }
+
+ bool operator>>(long int& output)
+ {
+ return input_operator_helper_signed(output);
+ }
+
+
+#if defined(BOOST_HAS_LONG_LONG)
+ bool operator>>( boost::ulong_long_type& output)
+ {
+ return input_operator_helper_unsigned(output);
+ }
+
+ bool operator>>(boost::long_long_type& output)
+ {
+ return input_operator_helper_signed(output);
+ }
+
+#elif defined(BOOST_HAS_MS_INT64)
+ bool operator>>(unsigned __int64& output)
+ {
+ return input_operator_helper_unsigned(output);
+ }
+
+ bool operator>>(__int64& output)
+ {
+ return input_operator_helper_signed(output);
+ }
+
+#endif
+
+ /*
+ * case "-0" || "0" || "+0" : output = false; return true;
+ * case "1" || "+1": output = true; return true;
+ * default: return false;
+ */
+ bool operator>>(bool& output)
+ {
+ CharT const zero = lcast_char_constants<CharT>::zero;
+ CharT const plus = lcast_char_constants<CharT>::plus;
+ CharT const minus = lcast_char_constants<CharT>::minus;
+
+ switch(finish-start)
+ {
+ case 1:
+ output = Traits::eq(start[0], zero+1);
+ return output || Traits::eq(start[0], zero );
+ case 2:
+ if ( Traits::eq( plus, *start) )
+ {
+ ++start;
+ output = Traits::eq(start[0], zero +1);
+ return output || Traits::eq(start[0], zero );
+ } else
+ {
+ output = false;
+ return Traits::eq( minus, *start)
+ && Traits::eq( zero, start[1]);
+ }
+ default:
+ output = false; // Suppress warning about uninitalized variable
+ return false;
+ }
+ }
+
+
             // Generic istream-based algorithm.
             // lcast_streambuf_for_target<InputStreamable>::value is true.
             template<typename InputStreamable>
@@ -1053,23 +1311,17 @@
         template<class Target>
         struct lcast_streambuf_for_target
         {
- BOOST_STATIC_CONSTANT(bool, value = true);
- };
-
- template<>
- struct lcast_streambuf_for_target<char>
- {
- BOOST_STATIC_CONSTANT(bool, value = false);
+ BOOST_STATIC_CONSTANT(bool, value =
+ (
+ ::boost::type_traits::ice_or<
+ ::boost::type_traits::ice_not< is_integral<Target>::value >::value,
+ is_same<Target, signed char>::value,
+ is_same<Target, unsigned char>::value
+ >::value
+ )
+ );
         };
 
-#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
- template<>
- struct lcast_streambuf_for_target<wchar_t>
- {
- BOOST_STATIC_CONSTANT(bool, value = false);
- };
-#endif
-
 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
         template<class Traits, class Alloc>
         struct lcast_streambuf_for_target<
@@ -1161,23 +1413,273 @@
 #if (defined _MSC_VER)
 # pragma warning( pop )
 #endif
+
+ template<typename T>
+ struct is_stdstring
+ {
+ BOOST_STATIC_CONSTANT(bool, value = false );
+ };
+
+ template<typename CharT, typename Traits, typename Alloc>
+ struct is_stdstring< std::basic_string<CharT, Traits, Alloc> >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true );
+ };
+
+ template<typename T>
+ struct is_char_or_wchar
+ {
+#ifndef BOOST_LCAST_NO_WCHAR_T
+ BOOST_STATIC_CONSTANT(bool, value =
+ (
+ ::boost::type_traits::ice_or<
+ is_same< T, char >::value,
+ is_same< T, wchar_t >::value,
+ is_same< T, unsigned char >::value,
+ is_same< T, signed char >::value
+ >::value
+ )
+ );
+#else
+ BOOST_STATIC_CONSTANT(bool, value =
+ (
+ ::boost::type_traits::ice_or<
+ is_same< T, char >::value,
+ is_same< T, unsigned char >::value,
+ is_same< T, signed char >::value
+ >::value
+ )
+ );
+#endif
+ };
+
+ template<typename Target, typename Source>
+ struct is_arithmetic_and_not_xchars
+ {
+ BOOST_STATIC_CONSTANT(bool, value =
+ (
+ ::boost::type_traits::ice_and<
+ is_arithmetic<Source>::value,
+ is_arithmetic<Target>::value,
+ ::boost::type_traits::ice_not<
+ detail::is_char_or_wchar<Target>::value
+ >::value,
+ ::boost::type_traits::ice_not<
+ detail::is_char_or_wchar<Source>::value
+ >::value
+ >::value
+ )
+ );
+ };
+
+ template<typename Target, typename Source>
+ struct is_xchar_to_xchar
+ {
+ BOOST_STATIC_CONSTANT(bool, value =
+ (
+ ::boost::type_traits::ice_and<
+ is_same<Source,Target>::value,
+ is_char_or_wchar<Target>::value
+ >::value
+ )
+ );
+ };
+
+ template<typename Target, typename Source>
+ struct is_char_array_to_stdstring
+ {
+ BOOST_STATIC_CONSTANT(bool, value = false );
+ };
+
+ template<typename CharT, typename Traits, typename Alloc>
+ struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, CharT* >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true );
+ };
+
+ template<typename CharT, typename Traits, typename Alloc>
+ struct is_char_array_to_stdstring< std::basic_string<CharT, Traits, Alloc>, const CharT* >
+ {
+ BOOST_STATIC_CONSTANT(bool, value = true );
+ };
+
+ template<typename Target, typename Source>
+ struct lexical_cast_do_cast
+ {
+ static inline Target lexical_cast_impl(const Source &arg)
+ {
+ typedef typename detail::array_to_pointer_decay<Source>::type src;
+
+ typedef typename detail::widest_char<
+ typename detail::stream_char<Target>::type
+ , typename detail::stream_char<src>::type
+ >::type char_type;
+
+ typedef detail::lcast_src_length<char_type, src> lcast_src_length;
+ std::size_t const src_len = lcast_src_length::value;
+ char_type buf[src_len + 1];
+ lcast_src_length::check_coverage();
+ return detail::lexical_cast<Target, src, !src_len>(arg, buf, src_len);
+ }
+ };
+
+ template<typename Source>
+ struct lexical_cast_copy
+ {
+ static inline Source lexical_cast_impl(const Source &arg)
+ {
+ return arg;
+ }
+ };
+
+ class precision_loss_error : public boost::numeric::bad_numeric_cast
+ {
+ public:
+ virtual const char * what() const throw()
+ { return "bad numeric conversion: precision loss error"; }
+ };
+
+ template<class S >
+ struct throw_on_precision_loss
+ {
+ typedef boost::numeric::Trunc<S> Rounder;
+ typedef S source_type ;
+
+ typedef typename mpl::if_< is_arithmetic<S>,S,S const&>::type argument_type ;
+
+ static source_type nearbyint ( argument_type s )
+ {
+ source_type orig_div_round = s / Rounder::nearbyint(s);
+
+ if ( (orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > std::numeric_limits<source_type>::epsilon() )
+ BOOST_THROW_EXCEPTION( precision_loss_error() );
+ return s ;
+ }
+
+ typedef typename Rounder::round_style round_style;
+ } ;
+
+ template<typename Target, typename Source>
+ struct lexical_cast_dynamic_num_not_ignoring_minus
+ {
+ static inline Target lexical_cast_impl(const Source &arg)
+ {
+ try{
+ typedef boost::numeric::converter<
+ Target,
+ Source,
+ boost::numeric::conversion_traits<Target,Source>,
+ boost::numeric::def_overflow_handler,
+ throw_on_precision_loss<Source>
+ > Converter ;
+
+ return Converter::convert(arg);
+ } catch( ::boost::numeric::bad_numeric_cast const& ) {
+ BOOST_LCAST_THROW_BAD_CAST(Source, Target);
+ }
+ }
+ };
+
+ template<typename Target, typename Source>
+ struct lexical_cast_dynamic_num_ignoring_minus
+ {
+ static inline Target lexical_cast_impl(const Source &arg)
+ {
+ try{
+ typedef boost::numeric::converter<
+ Target,
+ Source,
+ boost::numeric::conversion_traits<Target,Source>,
+ boost::numeric::def_overflow_handler,
+ throw_on_precision_loss<Source>
+ > Converter ;
+
+ bool has_minus = ( arg < 0);
+ if ( has_minus ) {
+ return static_cast<Target>(-Converter::convert(-arg));
+ } else {
+ return Converter::convert(arg);
+ }
+ } catch( ::boost::numeric::bad_numeric_cast const& ) {
+ BOOST_LCAST_THROW_BAD_CAST(Source, Target);
+ }
+ }
+ };
+
+ /*
+ * lexical_cast_dynamic_num follows the rules:
+ * 1) If Source can be converted to Target without precision loss and
+ * without overflows, then assign Source to Target and return
+ *
+ * 2) If Source is less than 0 and Target is an unsigned integer,
+ * then negate Source, check the requirements of rule 1) and if
+ * successful, assign static_casted Source to Target and return
+ *
+ * 3) Otherwise throw a bad_lexical_cast exception
+ *
+ *
+ * Rule 2) required because boost::lexical_cast has the behavior of
+ * stringstream, which uses the rules of scanf for conversions. And
+ * in the C99 standard for unsigned input value minus sign is
+ * optional, so if a negative number is read, no errors will arise
+ * and the result will be the two's complement.
+ */
+ template<typename Target, typename Source>
+ struct lexical_cast_dynamic_num
+ {
+ static inline Target lexical_cast_impl(const Source &arg)
+ {
+ typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
+ ::boost::type_traits::ice_and<
+ ::boost::type_traits::ice_or<
+ ::boost::is_signed<Source>::value,
+ ::boost::is_float<Source>::value
+ >::value,
+ ::boost::type_traits::ice_not<
+ is_same<Source, bool>::value
+ >::value,
+ ::boost::type_traits::ice_not<
+ is_same<Target, bool>::value
+ >::value,
+ ::boost::is_unsigned<Target>::value
+ >::value,
+ lexical_cast_dynamic_num_ignoring_minus<Target, Source>,
+ lexical_cast_dynamic_num_not_ignoring_minus<Target, Source>
+ >::type caster_type;
+
+ return caster_type::lexical_cast_impl(arg);
+ }
+ };
     }
 
     template<typename Target, typename Source>
     inline Target lexical_cast(const Source &arg)
     {
- typedef typename detail::array_to_pointer_decay<Source>::type src;
+ typedef BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
+
+ typedef BOOST_DEDUCED_TYPENAME ::boost::type_traits::ice_or<
+ detail::is_xchar_to_xchar<Target, src>::value,
+ detail::is_char_array_to_stdstring<Target,src>::value,
+ ::boost::type_traits::ice_and<
+ is_same<Target, src>::value,
+ detail::is_stdstring<Target>::value
+ >::value
+ > do_copy_type;
+
+ typedef BOOST_DEDUCED_TYPENAME
+ detail::is_arithmetic_and_not_xchars<Target, src> do_copy_with_dynamic_check_type;
+
+ typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
+ do_copy_type::value,
+ detail::lexical_cast_copy<src>,
+ BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
+ do_copy_with_dynamic_check_type::value,
+ detail::lexical_cast_dynamic_num<Target, src>,
+ detail::lexical_cast_do_cast<Target, src>
+ >::type
+ >::type caster_type;
 
- typedef typename detail::widest_char<
- typename detail::stream_char<Target>::type
- , typename detail::stream_char<src>::type
- >::type char_type;
-
- typedef detail::lcast_src_length<char_type, src> lcast_src_length;
- std::size_t const src_len = lcast_src_length::value;
- char_type buf[src_len + 1];
- lcast_src_length::check_coverage();
- return detail::lexical_cast<Target, src, !src_len>(arg, buf, src_len);
+ return caster_type::lexical_cast_impl(arg);
     }
 
     #else
@@ -1205,7 +1707,8 @@
 }
 
 // Copyright Kevlin Henney, 2000-2005.
-// Copyright Alexander Nasonov, 2006-2007.
+// Copyright Alexander Nasonov, 2006-2010.
+// Copyright Antony Polukhin, 2011.
 //
 // Distributed under the Boost Software License, Version 1.0. (See
 // accompanying file LICENSE_1_0.txt or copy at

Modified: branches/release/libs/conversion/lexical_cast.htm
==============================================================================
--- branches/release/libs/conversion/lexical_cast.htm (original)
+++ branches/release/libs/conversion/lexical_cast.htm 2011-05-26 12:27:53 EDT (Thu, 26 May 2011)
@@ -265,6 +265,12 @@
       August 2006.</li>
 </ul>
 <h2><a name="changes">Changes</a></h2>
+<h3>May 2011:</h3>
+<ul type="square">
+ <li>Better performance and less memory usage for conversions to arithmetic types.</li>
+ <li>Better performance and less memory usage for conversions from arithmetic type to arithmetic type.</li>
+ <li>Directly construct <code>Target</code> from <code>Source</code> on some conversions (like conversions from string to string, from char array to string, from char to char and others).</li>
+</ul>
 <h3>August, October 2006:</h3>
 <ul type="square">
     <li>Better performance for many combinations of <code>Source</code> and <code>Target</code>
@@ -312,7 +318,7 @@
 <div align="right"><small><i>Copyright &copy; Antony Polukhin, 2011</i></small></div>
 <div align="right"><small><i>
   Distributed under the Boost Software License, Version 1.0. (See accompanying
- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</i></small>
+ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)</i></small>
 </div>
         </body>
 </html>

Modified: branches/release/libs/conversion/lexical_cast_test.cpp
==============================================================================
--- branches/release/libs/conversion/lexical_cast_test.cpp (original)
+++ branches/release/libs/conversion/lexical_cast_test.cpp 2011-05-26 12:27:53 EDT (Thu, 26 May 2011)
@@ -2,8 +2,9 @@
 //
 // See http://www.boost.org for most recent version, including documentation.
 //
-// Copyright Terje Slettebø and Kevlin Henney, 2005.
+// Copyright Terje Sletteb and Kevlin Henney, 2005.
 // Copyright Alexander Nasonov, 2006.
+// Copyright Antony Polukhin, 2011.
 //
 // Distributed under the Boost
 // Software License, Version 1.0. (See accompanying file
@@ -32,6 +33,7 @@
 #include <boost/test/unit_test.hpp>
 #include <boost/test/floating_point_comparison.hpp>
 
+#include <boost/type_traits/integral_promotion.hpp>
 #include <string>
 #include <memory>
 
@@ -84,6 +86,9 @@
 void test_conversion_from_to_longlong();
 void test_conversion_from_to_ulonglong();
 #endif
+void test_conversion_from_to_float();
+void test_conversion_from_to_double();
+void test_conversion_from_to_long_double();
 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
 void test_traits();
 void test_wtraits();
@@ -122,6 +127,9 @@
     suite->add(BOOST_TEST_CASE(&test_conversion_from_to_longlong));
     suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulonglong));
 #endif
+ suite->add(BOOST_TEST_CASE(&test_conversion_from_to_float));
+ suite->add(BOOST_TEST_CASE(&test_conversion_from_to_double));
+ suite->add(BOOST_TEST_CASE(&test_conversion_from_to_long_double));
 #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
     suite->add(BOOST_TEST_CASE(&test_traits));
     suite->add(BOOST_TEST_CASE(&test_wtraits));
@@ -229,10 +237,24 @@
     BOOST_CHECK_EQUAL(false, lexical_cast<bool>("0"));
     BOOST_CHECK_EQUAL(true, lexical_cast<bool>(std::string("1")));
     BOOST_CHECK_EQUAL(false, lexical_cast<bool>(std::string("0")));
+
+ BOOST_CHECK_THROW(lexical_cast<bool>(1.0001L), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<bool>(2), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<bool>(2u), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<bool>(-1), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<bool>(-2), bad_lexical_cast);
+
+
     BOOST_CHECK_THROW(
         lexical_cast<bool>(std::string("")), bad_lexical_cast);
     BOOST_CHECK_THROW(
         lexical_cast<bool>(std::string("Test")), bad_lexical_cast);
+
+ BOOST_CHECK(lexical_cast<bool>("+1") == true );
+ BOOST_CHECK(lexical_cast<bool>("+0") == false );
+ BOOST_CHECK(lexical_cast<bool>("-0") == false );
+ BOOST_CHECK_THROW(lexical_cast<bool>("--0"), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<bool>("-+-0"), bad_lexical_cast);
 }
 
 void test_conversion_to_string()
@@ -447,6 +469,24 @@
     BOOST_CHECK_THROW(lexical_cast<CharT>(t), bad_lexical_cast);
 }
 
+template<class T, class CharT>
+void test_conversion_from_char_to_integral(CharT zero)
+{
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 0)) == static_cast<T>(0) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 1)) == static_cast<T>(1) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 2)) == static_cast<T>(2) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 3)) == static_cast<T>(3) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 4)) == static_cast<T>(4) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 5)) == static_cast<T>(5) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 6)) == static_cast<T>(6) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 7)) == static_cast<T>(7) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 8)) == static_cast<T>(8) );
+ BOOST_CHECK(lexical_cast<T>( static_cast<CharT>(zero + 9)) == static_cast<T>(9) );
+
+ BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero + 10)), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>( static_cast<CharT>(zero - 1)), bad_lexical_cast);
+}
+
 template<class T>
 void test_conversion_from_integral_to_integral()
 {
@@ -540,25 +580,46 @@
     BOOST_CHECK_EQUAL(lexical_cast<T>(s), min_val);
     if(limits::is_signed)
     {
-#if defined(BOOST_MSVC) && BOOST_MSVC == 1400
- // VC++ 8.0 bug, see libs/conversion/test/lexical_cast_vc8_bug_test.cpp
- if(sizeof(T) < sizeof(boost::intmax_t))
-#endif
- {
- BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
- BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
- }
+ BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
     }
 
     s = to_str<CharT>(max_val);
     BOOST_CHECK_EQUAL(lexical_cast<T>(s), max_val);
-#if defined(BOOST_MSVC) && BOOST_MSVC == 1400
- // VC++ 8.0 bug, see libs/conversion/test/lexical_cast_vc8_bug_test.cpp
- if(sizeof(T) != sizeof(boost::intmax_t))
-#endif
     {
         BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
         BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
+
+ s = to_str<CharT>(max_val);
+ for (int i =1; i <=10; ++i) {
+ s[s.size()-1] += 1;
+ BOOST_CHECK_THROW(lexical_cast<T>( s ), bad_lexical_cast);
+ }
+
+ s = to_str<CharT>(max_val);
+ std::locale loc;
+ typedef std::numpunct<char> numpunct;
+ if ( BOOST_USE_FACET(numpunct, loc).grouping().empty() ) {
+ // Following tests work well for locale C
+ BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+s), max_val);
+ BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+to_str<CharT>(0)+s), max_val);
+ BOOST_CHECK_EQUAL(lexical_cast<T>(to_str<CharT>(0)+to_str<CharT>(0)+to_str<CharT>(0)+s), max_val);
+ }
+
+ for (int i =1; i <=256; ++i) {
+ BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(i)+s ), bad_lexical_cast);
+ }
+
+ typedef BOOST_DEDUCED_TYPENAME boost::integral_promotion<T>::type promoted;
+ if ( !(boost::is_same<T, promoted>::value) )
+ {
+ promoted prom = max_val;
+ s = to_str<CharT>(max_val);
+ for (int i =1; i <=256; ++i) {
+ BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(prom+i) ), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>( to_str<CharT>(i)+s ), bad_lexical_cast);
+ }
+ }
     }
 
     if(limits::digits <= 16 && lcast_test_small_integral_types_completely)
@@ -603,6 +664,18 @@
 template<class T>
 void test_conversion_from_to_integral_for_locale()
 {
+ std::locale current_locale;
+ typedef std::numpunct<char> numpunct;
+ numpunct const& np = BOOST_USE_FACET(numpunct, current_locale);
+ if ( !np.grouping().empty() )
+ {
+ BOOST_CHECK_THROW(
+ lexical_cast<T>( std::string("100") + np.thousands_sep() + np.thousands_sep() + "0" )
+ , bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>( std::string("100") + np.thousands_sep() ), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>( np.thousands_sep() + std::string("100") ), bad_lexical_cast);
+ }
+
     test_conversion_from_integral_to_integral<T>();
     test_conversion_from_integral_to_string<T>('0');
     test_conversion_from_string_to_integral<T>('0');
@@ -625,13 +698,38 @@
     signed char const szero = '0';
     unsigned char const uzero = '0';
     test_conversion_from_integral_to_char<T>(zero);
+ test_conversion_from_char_to_integral<T>(zero);
     test_conversion_from_integral_to_char<T>(szero);
+ test_conversion_from_char_to_integral<T>(szero);
     test_conversion_from_integral_to_char<T>(uzero);
+ test_conversion_from_char_to_integral<T>(uzero);
 #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
     wchar_t const wzero = L'0';
     test_conversion_from_integral_to_char<T>(wzero);
+ test_conversion_from_char_to_integral<T>(wzero);
 #endif
 
+ BOOST_CHECK(lexical_cast<T>("-1") == static_cast<T>(-1));
+ BOOST_CHECK(lexical_cast<T>("-9") == static_cast<T>(-9));
+ BOOST_CHECK(lexical_cast<T>(-1) == static_cast<T>(-1));
+ BOOST_CHECK(lexical_cast<T>(-9) == static_cast<T>(-9));
+
+ BOOST_CHECK_THROW(lexical_cast<T>("-1.0"), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>("-9.0"), bad_lexical_cast);
+ BOOST_CHECK(lexical_cast<T>(-1.0) == static_cast<T>(-1));
+ BOOST_CHECK(lexical_cast<T>(-9.0) == static_cast<T>(-9));
+
+ BOOST_CHECK(lexical_cast<T>(static_cast<T>(1)) == static_cast<T>(1));
+ BOOST_CHECK(lexical_cast<T>(static_cast<T>(9)) == static_cast<T>(9));
+ BOOST_CHECK_THROW(lexical_cast<T>(1.1f), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>(1.1), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>(1.1L), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>(1.0001f), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>(1.0001), bad_lexical_cast);
+ BOOST_CHECK_THROW(lexical_cast<T>(1.0001L), bad_lexical_cast);
+
+ BOOST_CHECK(lexical_cast<T>("+1") == static_cast<T>(1) );
+ BOOST_CHECK(lexical_cast<T>("+9") == static_cast<T>(9) );
     // test_conversion_from_to_integral_for_locale
 
     typedef std::numpunct<char> numpunct;
@@ -665,6 +763,30 @@
         BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested");
 }
 
+template<class T>
+void test_conversion_from_to_float()
+{
+ char const zero = '0';
+ signed char const szero = '0';
+ unsigned char const uzero = '0';
+ test_conversion_from_integral_to_char<T>(zero);
+ test_conversion_from_char_to_integral<T>(zero);
+ test_conversion_from_integral_to_char<T>(szero);
+ test_conversion_from_char_to_integral<T>(szero);
+ test_conversion_from_integral_to_char<T>(uzero);
+ test_conversion_from_char_to_integral<T>(uzero);
+#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
+ wchar_t const wzero = L'0';
+ test_conversion_from_integral_to_char<T>(wzero);
+ test_conversion_from_char_to_integral<T>(wzero);
+#endif
+
+ test_conversion_from_integral_to_integral<T>();
+
+ BOOST_CHECK_CLOSE(lexical_cast<T>("+1"), 1, std::numeric_limits<T>::epsilon() );
+ BOOST_CHECK_CLOSE(lexical_cast<T>("+9"), 9, std::numeric_limits<T>::epsilon()*9 );
+}
+
 void test_conversion_from_to_short()
 {
     test_conversion_from_to_integral<short>();
@@ -705,6 +827,19 @@
     test_conversion_from_to_integral<boost::uintmax_t>();
 }
 
+void test_conversion_from_to_float()
+{
+ test_conversion_from_to_float<float>();
+}
+void test_conversion_from_to_double()
+{
+ test_conversion_from_to_float<double>();
+}
+void test_conversion_from_to_long_double()
+{
+ test_conversion_from_to_float<long double>();
+}
+
 #if defined(BOOST_HAS_LONG_LONG)
 
 void test_conversion_from_to_longlong()
@@ -717,7 +852,7 @@
     test_conversion_from_to_integral<boost::ulong_long_type>();
 }
 
-#elif defined(LCAST_TEST_LONGLONG)
+#elif defined(BOOST_HAS_MS_INT64)
 
 void test_conversion_from_to_longlong()
 {

Modified: branches/release/libs/conversion/test/lexical_cast_vc8_bug_test.cpp
==============================================================================
--- branches/release/libs/conversion/test/lexical_cast_vc8_bug_test.cpp (original)
+++ branches/release/libs/conversion/test/lexical_cast_vc8_bug_test.cpp 2011-05-26 12:27:53 EDT (Thu, 26 May 2011)
@@ -1,3 +1,17 @@
+// Unit test for boost::lexical_cast.
+//
+// See http://www.boost.org for most recent version, including documentation.
+//
+// Copyright Alexander Nasonov, 2007.
+//
+// Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
+//
+// This tests now must pass on vc8, because lexical_cast
+// implementation has changed and it does not use stringstream for casts
+// to integral types
+
 #include <boost/config.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/cstdint.hpp>


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk