Compare commits

..

1 Commits

Author SHA1 Message Date
Beman Dawes 063feb705c Boost 1.42.0
[SVN r59432]
2010-02-02 20:03:43 +00:00
6 changed files with 118 additions and 1047 deletions
+2 -2
View File
@@ -173,8 +173,8 @@ inline void lcast_set_precision(std::ios_base& stream, T*)
template<class Source, class Target>
inline void lcast_set_precision(std::ios_base& stream, Source*, Target*)
{
std::streamsize const s = lcast_get_precision(static_cast<Source*>(0));
std::streamsize const t = lcast_get_precision(static_cast<Target*>(0));
std::streamsize const s = lcast_get_precision((Source*)0);
std::streamsize const t = lcast_get_precision((Target*)0);
stream.precision(s > t ? s : t);
}
+80 -716
View File
@@ -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, Antony Polukhin and other Boosters
// when: November 2000, March 2003, June 2005, June 2006, March 2011
// Alexander Nasonov and other Boosters
// when: November 2000, March 2003, June 2005, June 2006
#include <climits>
#include <cstddef>
@@ -20,18 +20,12 @@
#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>
@@ -61,13 +55,7 @@
namespace boost
{
// exception used to indicate runtime lexical_cast failure
class bad_lexical_cast :
// workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0
#if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
public std::exception
#else
public std::bad_cast
#endif
class bad_lexical_cast : public std::bad_cast
#if defined(__BORLANDC__) && BOOST_WORKAROUND( __BORLANDC__, < 0x560 )
// under bcc32 5.5.1 bad_cast doesn't derive from exception
@@ -247,42 +235,13 @@ namespace boost
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, signed char>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, unsigned char>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, signed char*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, unsigned char*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, signed char const*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
template<>
struct lcast_src_length<char, unsigned char const*>
{
BOOST_STATIC_CONSTANT(std::size_t, value = 1);
static void check_coverage() {}
};
// No specializations for:
// lcast_src_length<char, signed char>
// lcast_src_length<char, unsigned char>
// lcast_src_length<char, signed char*>
// lcast_src_length<char, unsigned char*>
// lcast_src_length<char, signed char const*>
// lcast_src_length<char, unsigned char const*>
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
@@ -492,7 +451,7 @@ namespace boost
#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;
@@ -501,7 +460,6 @@ namespace boost
{
BOOST_STATIC_CONSTANT(char, zero = '0');
BOOST_STATIC_CONSTANT(char, minus = '-');
BOOST_STATIC_CONSTANT(char, plus = '+');
};
#ifndef BOOST_LCAST_NO_WCHAR_T
@@ -510,7 +468,6 @@ namespace boost
{
BOOST_STATIC_CONSTANT(wchar_t, zero = L'0');
BOOST_STATIC_CONSTANT(wchar_t, minus = L'-');
BOOST_STATIC_CONSTANT(wchar_t, plus = L'+');
};
#endif
}
@@ -555,170 +512,60 @@ namespace boost
BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
#endif
#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();
CharT thousands_sep = grouping_size ? np.thousands_sep() : 0;
std::string::size_type group = 0; // current group number
char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0];
// a) Since grouping is const, grouping[grouping.size()] returns 0.
// b) It's safe to assume here and below that CHAR_MAX
// is equivalent to unlimited grouping:
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
#endif
char left = last_grp_size;
#endif
typedef typename Traits::int_type int_type;
CharT const czero = lcast_char_constants<CharT>::zero;
int_type const zero = Traits::to_int_type(czero);
#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();
if ( grouping_size && grouping[0] > 0 )
do
{
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
// Check that ulimited group is unreachable:
BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
#endif
CharT thousands_sep = np.thousands_sep();
std::string::size_type group = 0; // current group number
char last_grp_size = grouping[0];
char left = last_grp_size;
do
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
if(left == 0)
{
if(left == 0)
++group;
if(group < grouping_size)
{
++group;
if(group < grouping_size)
{
char const grp_size = grouping[group];
last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
}
left = last_grp_size;
--finish;
Traits::assign(*finish, thousands_sep);
char const grp_size = grouping[group];
last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size;
}
--left;
left = last_grp_size;
--finish;
int_type const digit = static_cast<int_type>(n % 10U);
Traits::assign(*finish, Traits::to_char_type(zero + digit));
n /= 10;
} while(n);
Traits::assign(*finish, thousands_sep);
}
} else
--left;
#endif
{
do
{
--finish;
int_type const digit = static_cast<int_type>(n % 10U);
Traits::assign(*finish, Traits::to_char_type(zero + digit));
n /= 10;
} while(n);
}
--finish;
int_type const digit = static_cast<int_type>(n % 10U);
Traits::assign(*finish, Traits::to_char_type(zero + digit));
n /= 10;
} while(n);
return finish;
}
}
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 && grouping[0] > 0)
{
unsigned char current_grouping = 0;
CharT const thousands_sep = np.thousands_sep();
char remained = grouping[current_grouping] - 1;
bool shall_we_return = true;
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;
{
/*
* According to Programming languages - C++
* Digit grouping is checked. That is, the positions of discarded
* separators is examined for consistency with
* use_facet<numpunct<charT> >(loc ).grouping()
*
* BUT what if there is no separators at all and grouping()
* is not empty? Well, we have no extraced separators, so we
* won`t check them for consistency. This will allow us to
* work with "C" locale from other locales
*/
shall_we_return = false;
break;
} else {
if ( begin == end ) return false;
if (current_grouping < grouping_size-1 ) ++current_grouping;
remained = grouping[current_grouping];
}
}
}
if (shall_we_return) return true;
}
#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>
@@ -881,13 +728,9 @@ namespace boost
bool operator<<(bool);
bool operator<<(char);
bool operator<<(unsigned char);
bool operator<<(signed char);
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
bool operator<<(wchar_t);
#endif
bool operator<<(unsigned char const*);
bool operator<<(signed char const*);
bool operator<<(CharT const*);
bool operator<<(short);
bool operator<<(int);
@@ -908,178 +751,8 @@ namespace boost
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>
@@ -1114,8 +787,6 @@ namespace boost
}
bool operator>>(CharT&);
bool operator>>(unsigned char&);
bool operator>>(signed char&);
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// This #if is in sync with lcast_streambuf_for_target
@@ -1157,34 +828,6 @@ namespace boost
return true;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned char ch)
{
return ((*this) << static_cast<char>(ch));
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
signed char ch)
{
return ((*this) << static_cast<char>(ch));
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned char const* ch)
{
return ((*this) << reinterpret_cast<char const*>(ch));
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
signed char const* ch)
{
return ((*this) << reinterpret_cast<char const*>(ch));
}
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
@@ -1350,34 +993,6 @@ namespace boost
return ok;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
unsigned char& output)
{
BOOST_STATIC_ASSERT( sizeof(CharT) == sizeof(unsigned char) );
bool const ok = (finish - start == 1);
if(ok) {
CharT out;
Traits::assign(out, *start);
output = static_cast<signed char>(out);
}
return ok;
}
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
signed char& output)
{
BOOST_STATIC_ASSERT( sizeof(CharT) == sizeof(signed char) );
bool const ok = (finish - start == 1);
if(ok) {
CharT out;
Traits::assign(out, *start);
output = static_cast<signed char>(out);
}
return ok;
}
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
@@ -1433,13 +1048,23 @@ namespace boost
template<class Target>
struct lcast_streambuf_for_target
{
BOOST_STATIC_CONSTANT(bool, value =
(
::boost::type_traits::ice_not< is_integral<Target>::value >::value
)
);
BOOST_STATIC_CONSTANT(bool, value = true);
};
template<>
struct lcast_streambuf_for_target<char>
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
#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<
@@ -1495,7 +1120,6 @@ namespace boost
# pragma warning( push )
# pragma warning( disable : 4701 ) // possible use of ... before initialization
# pragma warning( disable : 4702 ) // unreachable code
# pragma warning( disable : 4267 ) // conversion from 'size_t' to 'unsigned int'
#endif
template< typename Target
@@ -1525,292 +1149,29 @@ namespace boost
Target result;
if(!(interpreter << arg && interpreter >> result))
BOOST_LCAST_THROW_BAD_CAST(Source, Target);
BOOST_LCAST_THROW_BAD_CAST(Source, Target);
return result;
}
#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
)
);
};
/*
* is_xchar_to_xchar<Target, Source>::value is true, when
* Target and Souce are the same char types, or when
* Target and Souce are char types of the same size.
*/
template<typename Target, typename Source>
struct is_xchar_to_xchar
{
BOOST_STATIC_CONSTANT(bool, value =
(
::boost::type_traits::ice_or<
::boost::type_traits::ice_and<
is_same<Source,Target>::value,
is_char_or_wchar<Target>::value
>::value,
::boost::type_traits::ice_and<
::boost::type_traits::ice_eq< sizeof(char),sizeof(Target)>::value,
::boost::type_traits::ice_eq< sizeof(char),sizeof(Source)>::value,
is_char_or_wchar<Target>::value,
is_char_or_wchar<Source>::value
>::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 BOOST_DEDUCED_TYPENAME detail::array_to_pointer_decay<Source>::type src;
typedef 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 typename detail::widest_char<
typename detail::stream_char<Target>::type
, typename detail::stream_char<src>::type
>::type char_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;
return caster_type::lexical_cast_impl(arg);
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);
}
#else
@@ -1830,7 +1191,11 @@ namespace boost
Target result;
if(!(interpreter << arg && interpreter >> result))
BOOST_LCAST_THROW_BAD_CAST(Source, Target);
#ifndef BOOST_NO_TYPEID
throw_exception(bad_lexical_cast(typeid(Source), typeid(Target)));
#else
throw_exception(bad_lexical_cast());
#endif
return result;
}
@@ -1838,8 +1203,7 @@ namespace boost
}
// Copyright Kevlin Henney, 2000-2005.
// Copyright Alexander Nasonov, 2006-2010.
// Copyright Antony Polukhin, 2011.
// Copyright Alexander Nasonov, 2006-2007.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
+21 -102
View File
@@ -26,8 +26,6 @@
<a href="#references">References</a></li>
<li>
<a href="#changes">Changes</a></li>
<li>
<a href="#performance">Performance</a></li>
</ul>
<hr>
<h2><a name="motivation">Motivation</a></h2>
@@ -89,7 +87,7 @@
For a good discussion of the options and issues involved in string-based
formatting, including comparison of <code>stringstream</code>, <code>lexical_cast</code>,
and others, see Herb Sutter's article, <a href="http://www.gotw.ca/publications/mill19.htm">
<i>The String Formatters of Manor Farm</i></a>. Also, take a look at the <a href="#performance">Performance</a> section.
<i>The String Formatters of Manor Farm</i></a>.
<p>
<hr>
<h2><a name="examples">Examples</a></h2>
@@ -196,85 +194,35 @@ public:
</pre>
</blockquote>Exception used to indicate runtime <a href="#lexical_cast"><code>lexical_cast</code></a>
failure.
<hr>
<hr>
<!--
The original design of lexical_cast library does not supports throwing/nonthrowing behaviour, default values,
locales... BOOST_LEXICAL_CAST_ASSUME_C_LOCALE is a good optimization, but it breaks down the original design.
-->
<!--
<h2><a name="BOOST_LEXICAL_CAST_ASSUME_C_LOCALE"><code>BOOST_LEXICAL_CAST_ASSUME_C_LOCALE</code></a></h2>
<blockquote><pre>#define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE</blockquote></pre>
or,
<blockquote><pre>g++ -DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE ... (gcc on Linux/Unix)</blockquote></pre>
<blockquote><pre>cl.exe /DBOOST_LEXICAL_CAST_ASSUME_C_LOCALE ... (Visual C++ on Windows)</blockquote></pre>
</pre>
Eliminate an overhead of <code>std::locale</code> if your program runs in the "C" locale. If the option is set but a program runs in other locale, <code>lexical_cast</code> result is unspecified.
<hr>
-->
<h2><a name="faq">Frequently Asked Questions</a></h2>
<table>
<tr>
<td valign="top"><b>Question:</b></td>
<td>Why does <code>lexical_cast&lt;int8_t&gt;("127")</code> throw <code>bad_lexical_cast</code>?</td>
</tr>
<tr>
<td valign="top"><b>Answer:</b></td>
<td>The type <code>int8_t</code> is a typedef to <code>char</code> or <code>signed char</code>.
<h2><a name="faq">Frequently Asked Questions</h2>
<p> Q: Why does <code>lexical_cast&lt;int8_t&gt;("127")</code> throw <code>bad_lexical_cast</code>?
<br> A: The type <code>int8_t</code> is a typedef to <code>char</code> or <code>signed char</code>.
Lexical conversion to these types is simply reading a byte from source but since the source has
more than one byte, the exception is thrown.
Please use other integer types such as <code>int</code> or <code>short int</code>. If bounds checking
<p>Please use other integer types such as <code>int</code> or <code>short int</code>. If bounds checking
is important, you can also call <a href="../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a>:
<pre><a href="../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a>&lt;int8_t&gt;(lexical_cast&lt;int&gt;("127"));</pre>
</td>
</tr>
<tr>
<td valign="top"><b>Question:</b></td><td>What does <code>lexical_cast&lt;std::string&gt;</code> of an <code>int8_t</code> or <code>uint8_t</code> not do what I expect?</td>
</tr>
<tr>
<td valign="top"><b>Answer:</b></td><td>As above, note that <code>int8_t</code> and <code>uint8_t</code> are actually chars and are formatted as such. To avoid this, cast to an integer type first:
<pre>lexical_cast&lt;std::string&gt;(static_cast&lt;int&gt;(n));</pre>
</td>
</tr>
<tr>
<td valign="top"><b>Question:</b></td>
<td>The implementation always resets the <code>ios_base::skipws</code> flag of an underlying stream object. It breaks my <code>operator&gt;&gt;</code> that works only in presence of this flag. Can you remove code that resets the flag?</td>
</tr>
<tr>
<td valign="top"><b>Answer:</b></td>
<td>May be in a future version. There is no requirement in <a href="#n1973">[N1973]</a> to reset the flag but remember that <a href="#n1973">[N1973]</a> is not yet accepted by the committee. By the way, it's a great opportunity to make your <code>operator&gt;&gt;</code> conform to the standard. Read a good C++ book, study <code>std::sentry</code> and <a href="../../libs/io/doc/ios_state.html">ios_state_saver</a>.
</td>
</tr>
<tr>
<td valign="top"><b>Question:</b></td>
<td>Why <code>std::cout << boost::lexical_cast&lt;unsigned int&gt;("-1");</code> does not throw, but outputs 4294967295?</td>
</tr>
<tr>
<td valign="top"><b>Answer:</b></td>
<td><code>boost::lexical_cast</code> has the behavior of <code>stringstream</code>, which uses <code>num_get</code> functions of <code>std::locale</code> to convert numbers. If we look at the [22.2.2.1.2] of Programming languages &mdash; C++, we'll see, that <code>num_get</code> uses the rules of <code>scanf</code> 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.
</td>
</tr>
</table>
<h2><a name="references">References</a></h2>
<pre><a href="../../libs/numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a>&lt;int8_t&gt;(lexical_cast&lt;int&gt;("127"));</pre>
<p> Q: What does <code>lexical_cast&lt;std::string&gt;</code> of an <code>int8_t</code> or <code>uint8_t</code> not do what I expect?
<br> A: As above, note that <code>int8_t</code> and <code>uint8_t</code> are actually chars and are formatted as such. To avoid this, cast to an integer type first:
<pre>lexical_cast&lt;std::string&gt;(static_cast&lt;int&gt;(n));</pre>
<p> Q: The implementation always resets the <code>ios_base::skipws</code> flag of an underlying stream object. It breaks my <code>operator&gt;&gt;</code> that works only in presence of this flag. Can you remove code that resets the flag?
<br> A: May be in a future version. There is no requirement in <a href="#n1973">[N1973]</a> to reset the flag but remember that <a href="#n1973">[N1973]</a> is not yet accepted by the committee. By the way, it's a great opportunity to make your <code>operator&gt;&gt;</code> conform to the standard. Read a good C++ book, study <code>std::sentry</code> and <a href="../../libs/io/doc/ios_state.html">ios_state_saver</a>.
</ul>
<h2><a name="references">References</h2>
<ul type="square">
<li><a name="n1973"></a>[N1973] Kevlin Henney, Beman Dawes, Lexical Conversion Library Proposal for TR2,
<a name="n1973"></a><li> [N1973] Kevlin Henney, Beman Dawes, Lexical Conversion Library Proposal for TR2,
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1973.html">N1973</a>.
<a name="tuning"></a><li> [Tuning] Alexander Nasonov, Fine Tuning for lexical_cast,
<a href="http://accu.org/index.php/journals/1375">Overload #74</a> (<a href="http://www.accu.org/var/uploads/journals/overload74.pdf">PDF</a>),
<a href="http://www.accu.org/var/uploads/journals/overload74.pdf">Overload #74</a>,
August 2006.</li>
</ul>
<h2><a name="changes">Changes</a></h2>
<h3>May 2011:</h3>
<ul type="square">
<li>Optimizations for "C" and other locales without number grouping.</li>
<li>Better performance and less memory usage for unsigned char and signed char conversions.</li>
<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>
@@ -316,36 +264,7 @@ Eliminate an overhead of <code>std::locale</code> if your program runs in the "C
</ul>
<p>
<hr>
<h2><a name="performance">Performance</a></h2>
This table shows the execution time in milliseconds for 100000 calls of the following string formatters:
<table border="1" width="100%">
<tr>
<tr><td>From->To</td><td> <code>lexical_cast</code> </td><td><code>std::stringstream</code><br>with construction</td><td><code>std::stringstream</code><br>without construction</td><td><code>sscanf</code>/<code>sprintf</code></td></tr>
<tr><td>string->char</td><td bgcolor="#00C000"><1</td><td>91</td><td>7</td><td>10</td></tr>
<tr><td>string->int</td><td bgcolor="#00C000">7</td><td>115</td><td>23</td><td>18</td></tr>
<tr><td>string->unsigned int</td><td bgcolor="#00C000">7</td><td>117</td><td>22</td><td>17</td></tr>
<tr><td>string->bool</td><td bgcolor="#00C000"><1</td><td>104</td><td>19</td><td>10</td></tr>
<tr><td>string->float</td><td>85</td><td>172</td><td>60</td><td bgcolor="#00C000">33</td></tr>
<tr><td>char->string</td><td bgcolor="#00C000">7</td><td>105</td><td>16</td><td>12</td></tr>
<tr><td>int->string</td><td bgcolor="#00C000">15</td><td>131</td><td>21</td><td>17</td></tr>
<tr><td>unsigned int->string</td><td bgcolor="#00C000">14</td><td>125</td><td>21</td><td>17</td></tr>
<tr><td>bool->string</td><td bgcolor="#00C000">7</td><td>122</td><td>24</td><td>12</td></tr>
<tr><td>float->string</td><td>124</td><td>223</td><td>115</td><td bgcolor="#00C000">48</td></tr>
<tr><td>char*->string</td><td bgcolor="#00C000">9</td><td>123</td><td>20</td><td>---</td></tr>
<tr><td>int->int</td><td bgcolor="#00C000"><1</td><td>120</td><td>26</td><td>---</td></tr>
<tr><td>float->float</td><td bgcolor="#00C000"><1</td><td>262</td><td>142</td><td>---</td></tr>
</table>
Fastest results are highlitened with green.
<hr>
<div align="right"><small><i>Copyright &copy; Kevlin Henney, 2000-2005</i></small></div>
<div align="right"><small><i>Copyright &copy; Alexander Nasonov, 2006-2010</i></small></div>
<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 <a href="../../LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</i></small>
</div>
<div align="right"><small><i>&copy; Copyright Kevlin Henney, 2000&#150;2005</i></small></div>
</body>
</html>
+14 -212
View File
@@ -2,9 +2,8 @@
//
// 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
@@ -12,13 +11,6 @@
//
// Note: The unit test no longer compile on MSVC 6, but lexical_cast itself works for it.
//
// We need this #define before any #includes: otherwise msvc will emit warnings
// deep within std::string, resulting from our (perfectly legal) use of basic_string
// with a custom traits class:
//
#define _SCL_SECURE_NO_WARNINGS
#include <boost/config.hpp>
#if defined(__INTEL_COMPILER)
@@ -33,7 +25,6 @@
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include <boost/type_traits/integral_promotion.hpp>
#include <string>
#include <memory>
@@ -86,16 +77,12 @@ void test_conversion_from_to_uintmax_t();
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();
void test_allocator();
void test_wallocator();
#endif
void test_char_types_conversions();
unit_test::test_suite *init_unit_test_suite(int, char *[])
{
@@ -128,9 +115,6 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
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));
@@ -138,8 +122,6 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
suite->add(BOOST_TEST_CASE(&test_wallocator));
#endif
suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
return suite;
}
@@ -240,24 +222,10 @@ void test_conversion_to_bool()
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()
@@ -472,24 +440,6 @@ void test_conversion_from_integral_to_char(CharT zero)
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()
{
@@ -583,46 +533,25 @@ void test_conversion_from_string_to_integral(CharT)
BOOST_CHECK_EQUAL(lexical_cast<T>(s), min_val);
if(limits::is_signed)
{
BOOST_CHECK_THROW(lexical_cast<T>(s + zero), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>(s + nine), bad_lexical_cast);
#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);
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)
@@ -667,21 +596,6 @@ void test_conversion_from_string_to_integral(CharT)
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);
// Exception must not be thrown, when we are using no separators at all
BOOST_CHECK( lexical_cast<T>("10000") == static_cast<T>(10000) );
}
test_conversion_from_integral_to_integral<T>();
test_conversion_from_integral_to_string<T>('0');
test_conversion_from_string_to_integral<T>('0');
@@ -704,44 +618,13 @@ void test_conversion_from_to_integral()
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) );
BOOST_CHECK(lexical_cast<T>("+10") == static_cast<T>(10) );
BOOST_CHECK(lexical_cast<T>("+90") == static_cast<T>(90) );
BOOST_CHECK_THROW(lexical_cast<T>("++1"), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>("-+9"), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>("--1"), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>("+-9"), bad_lexical_cast);
// test_conversion_from_to_integral_for_locale
typedef std::numpunct<char> numpunct;
@@ -775,35 +658,6 @@ void test_conversion_from_to_integral()
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 );
BOOST_CHECK_THROW(lexical_cast<T>("++1"), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>("-+9"), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>("--1"), bad_lexical_cast);
BOOST_CHECK_THROW(lexical_cast<T>("+-9"), bad_lexical_cast);
}
void test_conversion_from_to_short()
{
test_conversion_from_to_integral<short>();
@@ -844,19 +698,6 @@ void test_conversion_from_to_uintmax_t()
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()
@@ -869,7 +710,7 @@ void test_conversion_from_to_ulonglong()
test_conversion_from_to_integral<boost::ulong_long_type>();
}
#elif defined(BOOST_HAS_MS_INT64)
#elif defined(LCAST_TEST_LONGLONG)
void test_conversion_from_to_longlong()
{
@@ -940,42 +781,3 @@ void test_wallocator()
#endif
void test_char_types_conversions()
{
const char c_arr[] = "Test array of chars";
const unsigned char uc_arr[] = "Test array of chars";
const signed char sc_arr[] = "Test array of chars";
BOOST_CHECK(boost::lexical_cast<std::string>(c_arr) == std::string(c_arr));
BOOST_CHECK(boost::lexical_cast<std::string>(uc_arr) == std::string(c_arr));
BOOST_CHECK(boost::lexical_cast<std::string>(sc_arr) == std::string(c_arr));
BOOST_CHECK(boost::lexical_cast<char>(c_arr[0]) == c_arr[0]);
BOOST_CHECK(boost::lexical_cast<char>(uc_arr[0]) == c_arr[0]);
BOOST_CHECK(boost::lexical_cast<char>(sc_arr[0]) == c_arr[0]);
BOOST_CHECK(boost::lexical_cast<unsigned char>(c_arr[0]) == uc_arr[0]);
BOOST_CHECK(boost::lexical_cast<unsigned char>(uc_arr[0]) == uc_arr[0]);
BOOST_CHECK(boost::lexical_cast<unsigned char>(sc_arr[0]) == uc_arr[0]);
BOOST_CHECK(boost::lexical_cast<signed char>(c_arr[0]) == sc_arr[0]);
BOOST_CHECK(boost::lexical_cast<signed char>(uc_arr[0]) == sc_arr[0]);
BOOST_CHECK(boost::lexical_cast<signed char>(sc_arr[0]) == sc_arr[0]);
#ifndef BOOST_LCAST_NO_WCHAR_T
const wchar_t wc_arr[]=L"Test array of chars";
BOOST_CHECK(boost::lexical_cast<std::wstring>(wc_arr) == std::wstring(wc_arr));
BOOST_CHECK(boost::lexical_cast<std::wstring>(c_arr) == std::wstring(wc_arr));
BOOST_CHECK(boost::lexical_cast<std::wstring>(sc_arr) != std::wstring(wc_arr) );
BOOST_CHECK(boost::lexical_cast<std::wstring>(uc_arr) != std::wstring(wc_arr) );
BOOST_CHECK(boost::lexical_cast<wchar_t>(c_arr[0]) == wc_arr[0]);
BOOST_CHECK(boost::lexical_cast<wchar_t>(wc_arr[0]) == wc_arr[0]);
BOOST_CHECK_THROW(boost::lexical_cast<wchar_t>(uc_arr[0]), bad_lexical_cast);
BOOST_CHECK_THROW(boost::lexical_cast<wchar_t>(sc_arr[0]), bad_lexical_cast);
#endif
}
+1 -1
View File
@@ -51,7 +51,7 @@ std::ostream &operator<<(std::ostream &O, const A &a)
{
a.out(O);
return O;
}
};
void test_abstract()
{
-14
View File
@@ -1,17 +1,3 @@
// 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>