Merge lexical_cast library from trunk:

* multiple optimizations and bugfixes for boost::iterator_range (refs #6786, refs #6430, refs #6663)
* documentation update
* new Unicode characters support updated
* much more tests, removed incorrect tests

[SVN r78059]
This commit is contained in:
Antony Polukhin
2012-04-18 04:09:49 +00:00
parent 1e58331baf
commit eb4ad73caf
7 changed files with 459 additions and 364 deletions

View File

@@ -97,7 +97,21 @@ The requirements on the argument and result types are:
* Target is CopyConstructible [20.1.3].
* Target is DefaultConstructible, meaning that it is possible to default-initialize an object of that type [8.5, 20.1.4].
The character type of the underlying stream is assumed to be char unless either the Source or the Target requires wide-character streaming, in which case the underlying stream uses `wchar_t`. Source types that require wide-character streaming are `wchar_t`, `wchar_t *`, and `std::wstring`. Target types that require wide-character streaming are `wchar_t` and `std::wstring`.
The character type of the underlying stream is assumed to be `char` unless either the `Source` or the `Target` requires wide-character streaming, in which case the underlying stream uses `wchar_t`, `char16_t` or `char32_t`. Wide-character streaming is currently detected for:
* Single character: `wchar_t`, `char16_t`, `char32_t`
* Arrays of characters: `wchar_t *`, `char16_t *`, `char32_t *`, `const wchar_t *`, `const char16_t *`, `const char32_t *`
* Strings: `std::basic_string`, `boost::containers::basic_string`
* `boost::iterator_range<WideCharPtr>`, where `WideCharPtr` is a pointer to wide-character or pointer to const wide-character
[important Many compilers and runtime libraries fail to make conversions using new Unicode characters. Make shure that the following code compiles and outputs nonzero values, before using new types:
``
std::cout
<< booat::lexical_cast<std::u32string>(1.0).size()
<< " "
<< booat::lexical_cast<std::u16string>(1.0).size();
``
]
Where a higher degree of control is required over conversions, `std::stringstream` and `std::wstringstream` offer a more appropriate path. Where non-stream-based conversions are required, `lexical_cast` is the wrong tool for the job and is not special-cased for such scenarios.
[endsect]
@@ -115,6 +129,7 @@ Exception used to indicate runtime lexical_cast failure.
[endsect]
[/ Commenting out bad advise (this will break the ability to get correct function pointers via &lexical_cast<Target, Source>)
[section Tuning classes for fast lexical conversions]
Because of `boost::lexical_cast` optimizations for `boost::iterator_range<character_type*>`, it is possibile to make very fast lexical conversions for non zero terminated strings, substrings and user-defined classes.
@@ -145,9 +160,9 @@ Consider the following example:
This is a good generic solution for most use cases.
But we can make it even faster for some performance critical applications. During conversion, we loose speed at:
* `std::ostream` construction (it makes some heap allocations)
* `operator <<` (it copyies one by one all the symbols to an instance of `std::ostream`)
* `std::ostream` destruction (it makes some heap deallocations)
* `std::ostream` construction (it makes some heap allocations)
* `operator <<` (it copyies one by one all the symbols to an instance of `std::ostream`)
* `std::ostream` destruction (it makes some heap deallocations)
We can avoid all of this, by specifieng an overload for `boost::lexical_cast`:
``
@@ -162,7 +177,7 @@ namespace boost {
``
Now `boost::lexical_cast<some_type>(example_class_instance)` conversions won't copy data and construct heavy STL stream objects. See [link boost_lexical_cast.performance Performance] section for info on `boost::iterator_range` conversion performance.
[endsect]
]
[section Frequently Asked Questions]

View File

@@ -28,36 +28,20 @@
#include <climits>
#include <cstddef>
#include <istream>
#include <string>
#include <cstring>
#include <cstdio>
#include <typeinfo>
#include <exception>
#include <cmath>
#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/type_traits/remove_pointer.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/math/special_functions/sign.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/static_assert.hpp>
#include <boost/detail/lcast_precision.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/range/iterator_range_core.hpp>
#if !defined(__SUNPRO_CC)
#include <boost/container/container_fwd.hpp>
#endif // !defined(__SUNPRO_CC)
#ifndef BOOST_NO_CWCHAR
# include <cwchar>
#endif
#ifndef BOOST_NO_STD_LOCALE
# include <locale>
@@ -137,118 +121,9 @@ namespace boost
const std::type_info *target;
};
namespace detail // selectors for choosing stream character type
namespace detail // widest_char
{
template<typename Type>
struct stream_char
{
typedef char type;
};
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<typename CharT>
struct stream_char<iterator_range<CharT*> >
{
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
};
template<typename CharT>
struct stream_char<iterator_range<const CharT*> >
{
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
};
template<class CharT, class Traits, class Alloc>
struct stream_char< std::basic_string<CharT,Traits,Alloc> >
{
typedef CharT type;
};
#if !defined(__SUNPRO_CC)
template<class CharT, class Traits, class Alloc>
struct stream_char< ::boost::container::basic_string<CharT,Traits,Alloc> >
{
typedef CharT type;
};
#endif // !defined(__SUNPRO_CC)
#endif
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<>
struct stream_char<wchar_t>
{
typedef wchar_t type;
};
#endif
template<>
struct stream_char<wchar_t *>
{
typedef wchar_t type;
};
template<>
struct stream_char<const wchar_t *>
{
typedef wchar_t type;
};
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<>
struct stream_char<std::wstring>
{
typedef wchar_t type;
};
#endif
#endif
#ifndef BOOST_NO_CHAR16_T
template<>
struct stream_char<char16_t>
{
typedef char16_t type;
};
template<>
struct stream_char<char16_t *>
{
typedef char16_t type;
};
template<>
struct stream_char<const char16_t *>
{
typedef char16_t type;
};
#endif
#ifndef BOOST_NO_CHAR32_T
template<>
struct stream_char<char32_t>
{
typedef char32_t type;
};
template<>
struct stream_char<char32_t *>
{
typedef char32_t type;
};
template<>
struct stream_char<const char32_t *>
{
typedef char32_t type;
};
#endif
template<typename TargetChar, typename SourceChar>
template <typename TargetChar, typename SourceChar>
struct widest_char
{
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
@@ -257,10 +132,162 @@ namespace boost
, SourceChar >::type type;
};
}
} // namespace boost
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
#include <cmath>
#include <istream>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/type_traits/is_signed.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/math/special_functions/sign.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/range/iterator_range_core.hpp>
#if !defined(__SUNPRO_CC)
#include <boost/container/container_fwd.hpp>
#endif // !defined(__SUNPRO_CC)
#ifndef BOOST_NO_CWCHAR
# include <cwchar>
#endif
namespace boost {
namespace detail // widest_char<...> (continuation)
{
struct not_a_character_type{};
template <typename CharT>
struct widest_char<not_a_character_type, CharT >
{
typedef CharT type;
};
template <typename CharT>
struct widest_char< CharT, not_a_character_type >
{
typedef CharT type;
};
template <>
struct widest_char< not_a_character_type, not_a_character_type >
{
typedef char type;
};
}
namespace detail // is_char_or_wchar<...> and stream_char<...> templates
{
// returns true, if T is one of the character types
template <typename T>
struct is_char_or_wchar
{
typedef ::boost::type_traits::ice_or<
::boost::is_same< T, char >::value,
#ifndef BOOST_LCAST_NO_WCHAR_T
::boost::is_same< T, wchar_t >::value,
#endif
#ifndef BOOST_NO_CHAR16_T
::boost::is_same< T, char16_t >::value,
#endif
#ifndef BOOST_NO_CHAR32_T
::boost::is_same< T, char32_t >::value,
#endif
::boost::is_same< T, unsigned char >::value,
::boost::is_same< T, signed char >::value
> result_type;
BOOST_STATIC_CONSTANT(bool, value = (result_type::value) );
};
// selectors for choosing stream character type
// returns one of char, wchar_t, char16_t, char32_t or not_a_character_type types
template <typename Type>
struct stream_char
{
typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c<
is_char_or_wchar<Type >::value,
Type,
boost::detail::not_a_character_type
>::type type;
};
template <>
struct stream_char<unsigned char>
{
typedef char type;
};
template <>
struct stream_char<signed char>
{
typedef char type;
};
template<typename CharT>
struct stream_char<CharT*>
{
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
};
template<typename CharT>
struct stream_char<const CharT*>
{
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT>::type type;
};
template<typename CharT>
struct stream_char<iterator_range<CharT*> >
{
typedef BOOST_DEDUCED_TYPENAME stream_char<CharT*>::type type;
};
template<typename CharT>
struct stream_char<iterator_range<const CharT*> >
{
typedef BOOST_DEDUCED_TYPENAME stream_char<const CharT*>::type type;
};
template<class CharT, class Traits, class Alloc>
struct stream_char< std::basic_string<CharT, Traits, Alloc> >
{
typedef CharT type;
};
#if !defined(__SUNPRO_CC)
template<class CharT, class Traits, class Alloc>
struct stream_char< ::boost::container::basic_string<CharT, Traits, Alloc> >
{
typedef CharT type;
};
#endif // !defined(__SUNPRO_CC)
#if !defined(BOOST_LCAST_NO_WCHAR_T) && defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<>
struct stream_char<wchar_t>
{
typedef boost::detail::not_a_character_type type;
};
template<>
struct stream_char<wchar_t*>
{
typedef wchar_t type;
};
template<>
struct stream_char<const wchar_t*>
{
typedef wchar_t type;
};
#endif
}
namespace detail // deduce_char_traits template
{
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<class CharT, class Target, class Source>
struct deduce_char_traits
{
@@ -325,7 +352,7 @@ namespace boost
template<class CharT, class Traits, class Alloc1, class Alloc2>
struct deduce_char_traits< CharT
, ::boost::container::basic_string<CharT,Traits,Alloc1>
, std::basic_string<CharT,Traits,Alloc2>
, ::std::basic_string<CharT,Traits,Alloc2>
>
{
typedef Traits type;
@@ -333,14 +360,13 @@ namespace boost
template<class CharT, class Traits, class Alloc1, class Alloc2>
struct deduce_char_traits< CharT
, std::basic_string<CharT,Traits,Alloc1>
, ::std::basic_string<CharT,Traits,Alloc1>
, ::boost::container::basic_string<CharT,Traits,Alloc2>
>
{
typedef Traits type;
};
#endif // !defined(__SUNPRO_CC)
#endif
}
namespace detail // lcast_src_length
@@ -383,7 +409,7 @@ namespace boost
BOOST_STATIC_ASSERT(sizeof(Source) * CHAR_BIT <= 256);
#endif
};
// TODO: FIX for char16_t, char32_t, we can ignore CharT
#define BOOST_LCAST_DEF(T) \
template<> struct lcast_src_length<T> \
: lcast_src_length_integral<T> \
@@ -1217,7 +1243,7 @@ namespace boost
bool shl_char(T ch)
{
BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)) ,
"boost::lexical_cast does not support conversions from wchar_t to char types."
"boost::lexical_cast does not support conversions from wide character to char types."
"Use boost::locale instead" );
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
std::locale loc;
@@ -1243,7 +1269,7 @@ namespace boost
bool shl_char_array(T const* str)
{
BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)),
"boost::lexical_cast does not support conversions from wchar_t to char types."
"boost::lexical_cast does not support conversions from wide characters to char types."
"Use boost::locale instead" );
return shl_input_streamable(str);
}
@@ -1430,6 +1456,16 @@ namespace boost
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
bool operator<<(wchar_t ch) { return shl_char(ch); }
#endif
#endif
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
bool operator<<(char16_t ch) { return shl_char(ch); }
bool operator<<(char16_t * str) { return shl_char_array(str); }
bool operator<<(char16_t const * str) { return shl_char_array(str); }
#endif
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
bool operator<<(char32_t ch) { return shl_char(ch); }
bool operator<<(char32_t * str) { return shl_char_array(str); }
bool operator<<(char32_t const * str) { return shl_char_array(str); }
#endif
bool operator<<(unsigned char const* ch) { return ((*this) << reinterpret_cast<char const*>(ch)); }
bool operator<<(unsigned char * ch) { return ((*this) << reinterpret_cast<char *>(ch)); }
@@ -1634,19 +1670,13 @@ namespace boost
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
bool operator>>(char32_t& output) { return shr_xchar(output); }
#endif
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
bool operator>>(std::string& str) { str.assign(start, finish); return true; }
# ifndef BOOST_LCAST_NO_WCHAR_T
bool operator>>(std::wstring& str) { str.assign(start, finish); return true; }
# endif
#else
template<class Alloc>
bool operator>>(std::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
#if !defined(__SUNPRO_CC)
template<class Alloc>
bool operator>>(::boost::container::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
#endif // !defined(__SUNPRO_CC)
#endif
/*
* case "-0" || "0" || "+0" : output = false; return true;
* case "1" || "+1": output = true; return true;
@@ -1752,10 +1782,6 @@ namespace boost
};
}
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
// call-by-const reference version
namespace detail
{
template<class T>
@@ -1788,60 +1814,6 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = true );
};
#endif // !defined(__SUNPRO_CC)
template<typename T>
struct is_char_or_wchar
{
private:
#ifndef BOOST_LCAST_NO_WCHAR_T
typedef wchar_t wchar_t_if_supported;
#else
typedef char wchar_t_if_supported;
#endif
#ifndef BOOST_NO_CHAR16_T
typedef char16_t char16_t_if_supported;
#else
typedef char char16_t_if_supported;
#endif
#ifndef BOOST_NO_CHAR32_T
typedef char32_t char32_t_if_supported;
#else
typedef char char32_t_if_supported;
#endif
public:
BOOST_STATIC_CONSTANT(bool, value =
(
::boost::type_traits::ice_or<
is_same< T, char >::value,
is_same< T, wchar_t_if_supported >::value,
is_same< T, char16_t_if_supported >::value,
is_same< T, char32_t_if_supported >::value,
is_same< T, unsigned char >::value,
is_same< T, signed char >::value
>::value
)
);
};
template <typename T>
struct is_char_iterator_range
{
BOOST_STATIC_CONSTANT(bool, value = false );
};
template <typename CharT>
struct is_char_iterator_range<iterator_range<CharT*> >
{
BOOST_STATIC_CONSTANT(bool, value = (is_char_or_wchar<CharT>::value) );
};
template <typename CharT>
struct is_char_iterator_range<iterator_range<const CharT*> >
{
BOOST_STATIC_CONSTANT(bool, value = (is_char_or_wchar<CharT>::value) );
};
template<typename Target, typename Source>
struct is_arithmetic_and_not_xchars
@@ -1970,51 +1942,34 @@ namespace boost
"Your compiler does not have full support for char32_t" );
#endif
typedef detail::lcast_src_length<src> lcast_src_length;
typedef detail::lcast_src_length<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();
typedef BOOST_DEDUCED_TYPENAME
deduce_char_traits<char_type,Target,Source>::type traits;
typedef BOOST_DEDUCED_TYPENAME ::boost::detail::deduce_char_traits<
char_type, Target, Source
>::type traits;
typedef BOOST_DEDUCED_TYPENAME remove_pointer<src>::type removed_ptr_t_1;
typedef BOOST_DEDUCED_TYPENAME remove_cv<removed_ptr_t_1>::type removed_ptr_t;
typedef ::boost::type_traits::ice_and<
::boost::detail::is_char_or_wchar<src_char_type>::value, // source is lexical type
::boost::detail::is_char_or_wchar<target_char_t>::value, // target is a lexical type
::boost::is_same<char, src_char_type>::value, // source is not a wide character based type
::boost::type_traits::ice_ne<sizeof(char), sizeof(target_char_t) >::value // target type is based on wide character
> is_string_widening_required_t;
// is_char_types_match variable value can be computed via
// sizeof(char_type) == sizeof(removed_ptr_t). But when
// removed_ptr_t is an incomplete type or void*, compilers
// produce warnings or errors.
const bool is_char_types_match =
(::boost::type_traits::ice_or<
::boost::type_traits::ice_and<
::boost::type_traits::ice_eq<sizeof(char_type), sizeof(char) >::value,
::boost::type_traits::ice_or<
::boost::is_same<char, src_char_type>::value,
::boost::is_same<unsigned char, src_char_type>::value,
::boost::is_same<signed char, src_char_type>::value
>::value
>::value,
::boost::is_same<char_type, src_char_type>::value
>::value);
const bool requires_stringbuf =
!(
::boost::type_traits::ice_or<
::boost::detail::is_stdstring<src>::value,
typedef ::boost::type_traits::ice_or<
::boost::is_integral<src>::value,
::boost::detail::is_this_float_conversion_optimized<src, char_type >::value,
::boost::type_traits::ice_and<
::boost::detail::is_char_iterator_range<src >::value,
is_char_types_match
>::value,
::boost::type_traits::ice_and<
::boost::is_pointer<src>::value,
::boost::detail::is_char_or_wchar<removed_ptr_t>::value,
is_char_types_match
>::value
>::value
);
::boost::detail::is_char_or_wchar<src_char_type >::value
> is_source_input_optimized_t;
// If we have an optimized conversion for
// Source, we do not need to construct stringbuf.
const bool requires_stringbuf = ::boost::type_traits::ice_or<
is_string_widening_required_t::value,
::boost::type_traits::ice_not< is_source_input_optimized_t::value >::value
>::value;
detail::lexical_stream_limited_src<char_type, traits, requires_stringbuf >
interpreter(buf, buf + src_len);
@@ -2030,7 +1985,7 @@ namespace boost
# pragma warning( pop )
#endif
template<typename Source>
template <typename Source>
struct lexical_cast_copy
{
static inline Source lexical_cast_impl(const Source &arg)
@@ -2039,7 +1994,7 @@ namespace boost
}
};
template<class Source, class Target >
template <class Source, class Target >
struct detect_precision_loss
{
typedef boost::numeric::Trunc<Source> Rounder;
@@ -2063,7 +2018,7 @@ namespace boost
typedef typename Rounder::round_style round_style;
} ;
template<class Source, class Target >
template <class Source, class Target >
struct nothrow_overflow_handler
{
void operator() ( boost::numeric::range_check_result r )
@@ -2073,7 +2028,7 @@ namespace boost
}
} ;
template<typename Target, typename Source>
template <typename Target, typename Source>
struct lexical_cast_dynamic_num_not_ignoring_minus
{
static inline Target lexical_cast_impl(const Source &arg)
@@ -2088,7 +2043,7 @@ namespace boost
}
};
template<typename Target, typename Source>
template <typename Target, typename Source>
struct lexical_cast_dynamic_num_ignoring_minus
{
static inline Target lexical_cast_impl(const Source &arg)
@@ -2125,7 +2080,7 @@ namespace boost
* 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>
template <typename Target, typename Source>
struct lexical_cast_dynamic_num
{
static inline Target lexical_cast_impl(const Source &arg)
@@ -2153,40 +2108,80 @@ namespace boost
};
}
template<typename Target, typename Source>
template <typename Target, typename Source>
inline Target lexical_cast(const Source &arg)
{
typedef BOOST_DEDUCED_TYPENAME ::boost::detail::array_to_pointer_decay<Source>::type src;
typedef BOOST_DEDUCED_TYPENAME ::boost::type_traits::ice_or<
::boost::detail::is_xchar_to_xchar<Target, src>::value,
::boost::detail::is_char_array_to_stdstring<Target,src>::value,
::boost::detail::is_xchar_to_xchar<Target, src >::value,
::boost::detail::is_char_array_to_stdstring<Target, src >::value,
::boost::type_traits::ice_and<
::boost::is_same<Target, src>::value,
::boost::detail::is_stdstring<Target>::value
::boost::is_same<Target, src >::value,
::boost::detail::is_stdstring<Target >::value
>::value
> do_copy_type;
> shall_we_copy_t;
typedef BOOST_DEDUCED_TYPENAME
::boost::detail::is_arithmetic_and_not_xchars<Target, src> do_copy_with_dynamic_check_type;
::boost::detail::is_arithmetic_and_not_xchars<Target, src > shall_we_copy_with_dynamic_check_t;
typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
do_copy_type::value,
detail::lexical_cast_copy<src>,
shall_we_copy_t::value,
::boost::detail::lexical_cast_copy<src >,
BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c<
do_copy_with_dynamic_check_type::value,
::boost::detail::lexical_cast_dynamic_num<Target, src>,
::boost::detail::lexical_cast_do_cast<Target, src>
shall_we_copy_with_dynamic_check_t::value,
::boost::detail::lexical_cast_dynamic_num<Target, src >,
::boost::detail::lexical_cast_do_cast<Target, src >
>::type
>::type caster_type;
return caster_type::lexical_cast_impl(arg);
}
#else
} // namespace boost
namespace detail // stream wrapper for handling lexical conversions
#else // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
namespace boost {
namespace detail
{
// selectors for choosing stream character type
template<typename Type>
struct stream_char
{
typedef char type;
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<>
struct stream_char<wchar_t>
{
typedef wchar_t type;
};
#endif
template<>
struct stream_char<wchar_t *>
{
typedef wchar_t type;
};
template<>
struct stream_char<const wchar_t *>
{
typedef wchar_t type;
};
template<>
struct stream_char<std::wstring>
{
typedef wchar_t type;
};
#endif
// stream wrapper for handling lexical conversions
template<typename Target, typename Source, typename Traits>
class lexical_stream
{
@@ -2276,8 +2271,9 @@ namespace boost
return result;
}
#endif
}
} // namespace boost
#endif
// Copyright Kevlin Henney, 2000-2005.
// Copyright Alexander Nasonov, 2006-2010.
@@ -2287,5 +2283,8 @@ namespace boost
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#undef BOOST_LCAST_THROW_BAD_CAST
#undef BOOST_LCAST_NO_WCHAR_T
#endif
#endif // BOOST_LEXICAL_CAST_INCLUDED

View File

@@ -427,7 +427,6 @@ void test_conversion_to_wstring()
BOOST_CHECK(str == lexical_cast<std::wstring>(str));
BOOST_CHECK(L"123" == lexical_cast<std::wstring>(123));
BOOST_CHECK(L"1.23" == lexical_cast<std::wstring>(1.23));
BOOST_CHECK(L"1.111111111" == lexical_cast<std::wstring>(1.111111111));
BOOST_CHECK(L"1" == lexical_cast<std::wstring>(true));
BOOST_CHECK(L"0" == lexical_cast<std::wstring>(false));
#if !defined(BOOST_NO_INTRINSIC_WCHAR_T)

View File

@@ -12,6 +12,7 @@ project
: requirements
<library>/boost/test//boost_unit_test_framework
<link>static
<toolset>gcc-4.8:<define>BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
;
# Thanks to Steven Watanabe for helping with <nowchar> feature

View File

@@ -14,6 +14,8 @@
void testing_boost_containers_basic_string();
void testing_boost_containers_string_std_string();
void testing_boost_containers_string_widening();
using namespace boost;
@@ -23,6 +25,7 @@ boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::container::string");
suite->add(BOOST_TEST_CASE(testing_boost_containers_basic_string));
suite->add(BOOST_TEST_CASE(testing_boost_containers_string_std_string));
suite->add(BOOST_TEST_CASE(testing_boost_containers_string_widening));
return suite;
}
@@ -58,3 +61,23 @@ void testing_boost_containers_string_std_string()
#endif
}
void testing_boost_containers_string_widening()
{
const char char_array[] = "Test string";
#ifndef BOOST_LCAST_NO_WCHAR_T
const wchar_t wchar_array[] = L"Test string";
BOOST_CHECK(boost::lexical_cast<boost::container::wstring>(char_array) == wchar_array);
#endif
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
const char16_t char16_array[] = u"Test string";
BOOST_CHECK(boost::lexical_cast<boost::container::basic_string<char16_t> >(char_array) == char16_array);
#endif
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
const char32_t char32_array[] = U"Test string";
BOOST_CHECK(boost::lexical_cast<boost::container::basic_string<char32_t> >(char_array) == char32_array);
#endif
}

View File

@@ -43,7 +43,7 @@ inline std::basic_istream<CharT>& operator >> (std::basic_istream<CharT>& istr,
template <class RngT>
void do_test_iterator_range(const RngT& rng)
void do_test_iterator_range_impl(const RngT& rng)
{
BOOST_CHECK_EQUAL(lexical_cast<int>(rng), 1);
BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(rng), 1u);
@@ -51,11 +51,13 @@ void do_test_iterator_range(const RngT& rng)
BOOST_CHECK_EQUAL(lexical_cast<unsigned short>(rng), 1u);
BOOST_CHECK_EQUAL(lexical_cast<long int>(rng), 1);
BOOST_CHECK_EQUAL(lexical_cast<unsigned long int>(rng), 1u);
#ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
BOOST_CHECK_EQUAL(lexical_cast<float>(rng), 1.0f);
BOOST_CHECK_EQUAL(lexical_cast<double>(rng), 1.0);
BOOST_CHECK_EQUAL(lexical_cast<long double>(rng), 1.0L);
BOOST_CHECK_EQUAL(lexical_cast<class_with_user_defined_sream_operators>(rng), 1);
#endif
#if defined(BOOST_HAS_LONG_LONG)
BOOST_CHECK_EQUAL(lexical_cast<boost::ulong_long_type>(rng), 1u);
BOOST_CHECK_EQUAL(lexical_cast<boost::long_long_type>(rng), 1);
@@ -63,115 +65,139 @@ void do_test_iterator_range(const RngT& rng)
BOOST_CHECK_EQUAL(lexical_cast<unsigned __int64>(rng), 1u);
BOOST_CHECK_EQUAL(lexical_cast<__int64>(rng), 1);
#endif
}
template <class CharT>
void test_it_range_using_any_chars(CharT* one, CharT* eleven)
{
typedef CharT test_char_type;
// Zero terminated
iterator_range<test_char_type*> rng1(one, one + 1);
do_test_iterator_range_impl(rng1);
iterator_range<const test_char_type*> crng1(one, one + 1);
do_test_iterator_range_impl(crng1);
// Non zero terminated
iterator_range<test_char_type*> rng2(eleven, eleven + 1);
do_test_iterator_range_impl(rng2);
iterator_range<const test_char_type*> crng2(eleven, eleven + 1);
do_test_iterator_range_impl(crng2);
}
template <class CharT>
void test_it_range_using_char(CharT* one, CharT* eleven)
{
typedef CharT test_char_type;
iterator_range<test_char_type*> rng1(one, one + 1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng1), "1");
iterator_range<const test_char_type*> crng1(one, one + 1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng1), "1");
iterator_range<test_char_type*> rng2(eleven, eleven + 1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng2), "1");
iterator_range<const test_char_type*> crng2(eleven, eleven + 1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng2), "1");
BOOST_CHECK_EQUAL(lexical_cast<float>(rng1), 1.0f);
BOOST_CHECK_EQUAL(lexical_cast<double>(rng1), 1.0);
BOOST_CHECK_EQUAL(lexical_cast<long double>(rng1), 1.0L);
BOOST_CHECK_EQUAL(lexical_cast<class_with_user_defined_sream_operators>(rng1), 1);
BOOST_CHECK_EQUAL(lexical_cast<float>(crng2), 1.0f);
BOOST_CHECK_EQUAL(lexical_cast<double>(crng2), 1.0);
BOOST_CHECK_EQUAL(lexical_cast<long double>(crng2), 1.0L);
BOOST_CHECK_EQUAL(lexical_cast<class_with_user_defined_sream_operators>(crng2), 1);
#ifndef BOOST_LCAST_NO_WCHAR_T
BOOST_CHECK(lexical_cast<std::wstring>(rng) == L"1");
BOOST_CHECK(lexical_cast<std::wstring>(rng1) == L"1");
BOOST_CHECK(lexical_cast<std::wstring>(crng1) == L"1");
BOOST_CHECK(lexical_cast<std::wstring>(rng2) == L"1");
BOOST_CHECK(lexical_cast<std::wstring>(crng2) == L"1");
#endif
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
typedef std::basic_string<char16_t> my_char16_string;
BOOST_CHECK(lexical_cast<my_char16_string>(rng1) == u"1");
BOOST_CHECK(lexical_cast<my_char16_string>(crng1) == u"1");
BOOST_CHECK(lexical_cast<my_char16_string>(rng2) == u"1");
BOOST_CHECK(lexical_cast<my_char16_string>(crng2) == u"1");
#endif
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
typedef std::basic_string<char32_t> my_char32_string;
BOOST_CHECK(lexical_cast<my_char32_string>(rng1) == U"1");
BOOST_CHECK(lexical_cast<my_char32_string>(crng1) == U"1");
BOOST_CHECK(lexical_cast<my_char32_string>(rng2) == U"1");
BOOST_CHECK(lexical_cast<my_char32_string>(crng2) == U"1");
#endif
}
void test_char_iterator_ranges()
{
typedef char test_char_type;
// Zero terminated
test_char_type data1[] = "1";
iterator_range<test_char_type*> rng1(data1, data1 + 1);
do_test_iterator_range(rng1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng1), "1");
const test_char_type cdata1[] = "1";
iterator_range<const test_char_type*> crng1(cdata1, cdata1 + 1);
do_test_iterator_range(crng1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng1), "1");
// Non zero terminated
test_char_type data2[] = "11";
iterator_range<test_char_type*> rng2(data2, data2 + 1);
do_test_iterator_range(rng2);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng2), "1");
const test_char_type cdata2[] = "11";
iterator_range<const test_char_type*> crng2(cdata2, cdata2 + 1);
do_test_iterator_range(crng2);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng2), "1");
test_it_range_using_any_chars(data1, data2);
test_it_range_using_char(data1, data2);
}
void test_unsigned_char_iterator_ranges()
{
typedef unsigned char test_char_type;
// Zero terminated
test_char_type data1[] = "1";
iterator_range<test_char_type*> rng1(data1, data1 + 1);
do_test_iterator_range(rng1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng1), "1");
const test_char_type cdata1[] = "1";
iterator_range<const test_char_type*> crng1(cdata1, cdata1 + 1);
do_test_iterator_range(crng1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng1), "1");
// Non zero terminated
test_char_type data2[] = "11";
iterator_range<test_char_type*> rng2(data2, data2 + 1);
do_test_iterator_range(rng2);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng2), "1");
const test_char_type cdata2[] = "11";
iterator_range<const test_char_type*> crng2(cdata2, cdata2 + 1);
do_test_iterator_range(crng2);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng2), "1");
test_it_range_using_any_chars(data1, data2);
test_it_range_using_char(data1, data2);
}
void test_signed_char_iterator_ranges()
{
typedef signed char test_char_type;
// Zero terminated
test_char_type data1[] = "1";
iterator_range<test_char_type*> rng1(data1, data1 + 1);
do_test_iterator_range(rng1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng1), "1");
const test_char_type cdata1[] = "1";
iterator_range<const test_char_type*> crng1(cdata1, cdata1 + 1);
do_test_iterator_range(crng1);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng1), "1");
// Non zero terminated
test_char_type data2[] = "11";
iterator_range<test_char_type*> rng2(data2, data2 + 1);
do_test_iterator_range(rng2);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(rng2), "1");
const test_char_type cdata2[] = "11";
iterator_range<const test_char_type*> crng2(cdata2, cdata2 + 1);
do_test_iterator_range(crng2);
BOOST_CHECK_EQUAL(lexical_cast<std::string>(crng2), "1");
test_it_range_using_any_chars(data1, data2);
test_it_range_using_char(data1, data2);
}
void test_wide_char_iterator_ranges()
void test_wchar_iterator_ranges()
{
#ifndef BOOST_LCAST_NO_WCHAR_T
typedef wchar_t test_char_type;
// Zero terminated
test_char_type data1[] = L"1";
iterator_range<test_char_type*> rng1(data1, data1 + 1);
do_test_iterator_range(rng1);
const test_char_type cdata1[] = L"1";
iterator_range<const test_char_type*> crng1(cdata1, cdata1 + 1);
do_test_iterator_range(crng1);
// Non zero terminated
test_char_type data2[] = L"11";
iterator_range<test_char_type*> rng2(data2, data2 + 1);
do_test_iterator_range(rng2);
test_it_range_using_any_chars(data1, data2);
#endif
const test_char_type cdata2[] = L"11";
iterator_range<const test_char_type*> crng2(cdata2, cdata2 + 1);
do_test_iterator_range(crng2);
BOOST_CHECK(true);
}
void test_char16_iterator_ranges()
{
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS)
typedef char16_t test_char_type;
test_char_type data1[] = u"1";
test_char_type data2[] = u"11";
test_it_range_using_any_chars(data1, data2);
#endif
BOOST_CHECK(true);
}
void test_char32_iterator_ranges()
{
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS)
typedef char32_t test_char_type;
test_char_type data1[] = U"1";
test_char_type data2[] = U"11";
test_it_range_using_any_chars(data1, data2);
#endif
BOOST_CHECK(true);
@@ -183,7 +209,9 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
suite->add(BOOST_TEST_CASE(&test_char_iterator_ranges));
suite->add(BOOST_TEST_CASE(&test_unsigned_char_iterator_ranges));
suite->add(BOOST_TEST_CASE(&test_signed_char_iterator_ranges));
suite->add(BOOST_TEST_CASE(&test_wide_char_iterator_ranges));
suite->add(BOOST_TEST_CASE(&test_wchar_iterator_ranges));
suite->add(BOOST_TEST_CASE(&test_char16_iterator_ranges));
suite->add(BOOST_TEST_CASE(&test_char32_iterator_ranges));
return suite;
}

View File

@@ -2,7 +2,7 @@
//
// See http://www.boost.org for most recent version, including documentation.
//
// Copyright Antony Polukhin, 2011.
// Copyright Antony Polukhin, 2011-2012.
//
// Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
@@ -17,40 +17,70 @@
#endif
#include <boost/lexical_cast.hpp>
#include <boost/cstdint.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>
using namespace boost;
void test_char_types_conversions()
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
#define BOOST_LCAST_NO_WCHAR_T
#endif
template <class CharT>
void test_impl(const CharT* wc_arr)
{
#ifndef BOOST_LCAST_NO_WCHAR_T
typedef CharT wide_char;
typedef std::basic_string<CharT> wide_string;
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";
const wchar_t wc_arr[] =L"Test array of chars";
// Following tests depend on realization of std::locale
// and pass for popular compilers and STL realizations
BOOST_CHECK(boost::lexical_cast<wchar_t>(c_arr[0]) == wc_arr[0]);
BOOST_CHECK(boost::lexical_cast<std::wstring>(c_arr) == std::wstring(wc_arr));
BOOST_CHECK(boost::lexical_cast<wide_char>(c_arr[0]) == wc_arr[0]);
BOOST_CHECK(boost::lexical_cast<wide_string>(c_arr) == wide_string(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<wide_string>(sc_arr) == wide_string(wc_arr) );
BOOST_CHECK(boost::lexical_cast<wide_string>(uc_arr) == wide_string(wc_arr) );
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(uc_arr[0]), wc_arr[0]);
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(sc_arr[0]), wc_arr[0]);
BOOST_CHECK_EQUAL(boost::lexical_cast<wide_char>(uc_arr[0]), wc_arr[0]);
BOOST_CHECK_EQUAL(boost::lexical_cast<wide_char>(sc_arr[0]), wc_arr[0]);
}
void test_char_types_conversions_wchar_t()
{
#ifndef BOOST_LCAST_NO_WCHAR_T
test_impl(L"Test array of chars");
#endif
BOOST_CHECK(1);
BOOST_CHECK(true);
}
void test_char_types_conversions_char16_t()
{
#if !defined(BOOST_NO_CHAR16_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
test_impl(u"Test array of chars");
#endif
BOOST_CHECK(true);
}
void test_char_types_conversions_char32_t()
{
#if !defined(BOOST_NO_CHAR32_T) && !defined(BOOST_NO_UNICODE_LITERALS) && defined(BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES)
test_impl(U"Test array of chars");
#endif
BOOST_CHECK(true);
}
unit_test::test_suite *init_unit_test_suite(int, char *[])
{
unit_test::test_suite *suite =
BOOST_TEST_SUITE("lexical_cast char<->wchar_t unit test");
suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
BOOST_TEST_SUITE("lexical_cast char => wide characters unit test (widening test)");
suite->add(BOOST_TEST_CASE(&test_char_types_conversions_wchar_t));
suite->add(BOOST_TEST_CASE(&test_char_types_conversions_char16_t));
suite->add(BOOST_TEST_CASE(&test_char_types_conversions_char32_t));
return suite;
}