|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r71958 - in trunk: boost libs/conversion
From: antoshkka_at_[hidden]
Date: 2011-05-15 11:31:02
Author: apolukhin
Date: 2011-05-15 11:31:01 EDT (Sun, 15 May 2011)
New Revision: 71958
URL: http://svn.boost.org/trac/boost/changeset/71958
Log:
Fixes #5417. Fixes #4397
More tests (for conversions to float types, for conversions of negative values to unsigned integers)
Text files modified:
trunk/boost/lexical_cast.hpp | 284 ++++++++++++++++++++++++++++++++++++++-
trunk/libs/conversion/lexical_cast.htm | 5
trunk/libs/conversion/lexical_cast_test.cpp | 83 +++++++++++
3 files changed, 357 insertions(+), 15 deletions(-)
Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2011-05-15 11:31:01 EDT (Sun, 15 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>
@@ -1161,23 +1167,272 @@
#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_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_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 +1460,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: trunk/libs/conversion/lexical_cast.htm
==============================================================================
--- trunk/libs/conversion/lexical_cast.htm (original)
+++ trunk/libs/conversion/lexical_cast.htm 2011-05-15 11:31:01 EDT (Sun, 15 May 2011)
@@ -265,6 +265,11 @@
August 2006.</li>
</ul>
<h2><a name="changes">Changes</a></h2>
+<h3>May 2011:</h3>
+<ul type="square">
+ <li>Better performance 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>
Modified: trunk/libs/conversion/lexical_cast_test.cpp
==============================================================================
--- trunk/libs/conversion/lexical_cast_test.cpp (original)
+++ trunk/libs/conversion/lexical_cast_test.cpp 2011-05-15 11:31:01 EDT (Sun, 15 May 2011)
@@ -2,7 +2,7 @@
//
// 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.
//
// Distributed under the Boost
@@ -84,6 +84,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 +125,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));
@@ -447,6 +453,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()
{
@@ -625,13 +649,36 @@
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);
+
// test_conversion_from_to_integral_for_locale
typedef std::numpunct<char> numpunct;
@@ -665,6 +712,27 @@
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>();
+}
+
void test_conversion_from_to_short()
{
test_conversion_from_to_integral<short>();
@@ -717,6 +785,19 @@
test_conversion_from_to_integral<boost::ulong_long_type>();
}
+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>();
+}
+
#elif defined(LCAST_TEST_LONGLONG)
void test_conversion_from_to_longlong()
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