Merge lexical_cast from trunk r49262

[SVN r49325]
This commit is contained in:
Alexander Nasonov
2008-10-13 23:54:01 +00:00
parent 39329bc5b9
commit 0c54d6a037

View File

@@ -17,17 +17,23 @@
#include <climits> #include <climits>
#include <cstddef> #include <cstddef>
#include <istream> #include <istream>
#include <locale>
#include <string> #include <string>
#include <typeinfo> #include <typeinfo>
#include <exception>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/limits.hpp> #include <boost/limits.hpp>
#include <boost/mpl/if.hpp> #include <boost/mpl/if.hpp>
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
#include <boost/type_traits/is_pointer.hpp> #include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include <boost/call_traits.hpp> #include <boost/call_traits.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/detail/lcast_precision.hpp> #include <boost/detail/lcast_precision.hpp>
#include <boost/detail/workaround.hpp>
#ifndef BOOST_NO_STD_LOCALE
#include <locale>
#endif
#ifdef BOOST_NO_STRINGSTREAM #ifdef BOOST_NO_STRINGSTREAM
#include <strstream> #include <strstream>
@@ -38,13 +44,19 @@
#if defined(BOOST_NO_STRINGSTREAM) || \ #if defined(BOOST_NO_STRINGSTREAM) || \
defined(BOOST_NO_STD_WSTRING) || \ defined(BOOST_NO_STD_WSTRING) || \
defined(BOOST_NO_STD_LOCALE) defined(BOOST_NO_STD_LOCALE)
#define DISABLE_WIDE_CHAR_SUPPORT #define BOOST_LCAST_NO_WCHAR_T
#endif #endif
namespace boost namespace boost
{ {
// exception used to indicate runtime lexical_cast failure // exception used to indicate runtime lexical_cast failure
class bad_lexical_cast : public std::bad_cast 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
, public std::exception
#endif
{ {
public: public:
bad_lexical_cast() : bad_lexical_cast() :
@@ -94,7 +106,7 @@ namespace boost
}; };
#endif #endif
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef BOOST_NO_INTRINSIC_WCHAR_T #ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<> template<>
struct stream_char<wchar_t> struct stream_char<wchar_t>
@@ -219,7 +231,7 @@ namespace boost
// lcast_src_length<char, signed char const*> // lcast_src_length<char, signed char const*>
// lcast_src_length<char, unsigned char const*> // lcast_src_length<char, unsigned char const*>
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<> template<>
struct lcast_src_length<wchar_t, bool> struct lcast_src_length<wchar_t, bool>
{ {
@@ -258,7 +270,7 @@ namespace boost
static void check_coverage() {} static void check_coverage() {}
}; };
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<> template<>
struct lcast_src_length<wchar_t, wchar_t const*> struct lcast_src_length<wchar_t, wchar_t const*>
{ {
@@ -289,7 +301,7 @@ namespace boost
static void check_coverage() {} static void check_coverage() {}
}; };
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<> template<>
struct lcast_src_length< wchar_t, std::basic_string<wchar_t> > struct lcast_src_length< wchar_t, std::basic_string<wchar_t> >
{ {
@@ -327,34 +339,35 @@ namespace boost
#endif #endif
}; };
#define BOOST_AUX_LEXICAL_CAST_DEF1(CharT, T) template<> \ #define BOOST_LCAST_DEF1(CharT, T) \
struct lcast_src_length<CharT, T> : lcast_src_length_integral<T> \ template<> struct lcast_src_length<CharT, T> \
: lcast_src_length_integral<T> \
{ static void check_coverage() {} }; { static void check_coverage() {} };
#ifdef DISABLE_WIDE_CHAR_SUPPORT #ifdef BOOST_LCAST_NO_WCHAR_T
#define BOOST_AUX_LEXICAL_CAST_DEF(T) BOOST_AUX_LEXICAL_CAST_DEF1(char, T) #define BOOST_LCAST_DEF(T) BOOST_LCAST_DEF1(char, T)
#else #else
#define BOOST_AUX_LEXICAL_CAST_DEF(T) \ #define BOOST_LCAST_DEF(T) \
BOOST_AUX_LEXICAL_CAST_DEF1(char, T) \ BOOST_LCAST_DEF1(char, T) \
BOOST_AUX_LEXICAL_CAST_DEF1(wchar_t, T) BOOST_LCAST_DEF1(wchar_t, T)
#endif #endif
BOOST_AUX_LEXICAL_CAST_DEF(short) BOOST_LCAST_DEF(short)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned short) BOOST_LCAST_DEF(unsigned short)
BOOST_AUX_LEXICAL_CAST_DEF(int) BOOST_LCAST_DEF(int)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned int) BOOST_LCAST_DEF(unsigned int)
BOOST_AUX_LEXICAL_CAST_DEF(long) BOOST_LCAST_DEF(long)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned long) BOOST_LCAST_DEF(unsigned long)
#if defined(BOOST_HAS_LONG_LONG) #if defined(BOOST_HAS_LONG_LONG)
BOOST_AUX_LEXICAL_CAST_DEF(boost::ulong_long_type) BOOST_LCAST_DEF(boost::ulong_long_type)
BOOST_AUX_LEXICAL_CAST_DEF(boost::long_long_type ) BOOST_LCAST_DEF(boost::long_long_type )
#elif defined(BOOST_HAS_MS_INT64) #elif defined(BOOST_HAS_MS_INT64)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned __int64) BOOST_LCAST_DEF(unsigned __int64)
BOOST_AUX_LEXICAL_CAST_DEF( __int64) BOOST_LCAST_DEF( __int64)
#endif #endif
#undef BOOST_AUX_LEXICAL_CAST_DEF #undef BOOST_LCAST_DEF
#undef BOOST_AUX_LEXICAL_CAST_DEF1 #undef BOOST_LCAST_DEF1
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
// Helper for floating point types. // Helper for floating point types.
@@ -400,7 +413,7 @@ namespace boost
static void check_coverage() {} static void check_coverage() {}
}; };
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<> template<>
struct lcast_src_length<wchar_t,float> struct lcast_src_length<wchar_t,float>
: lcast_src_length_floating<float> : lcast_src_length_floating<float>
@@ -422,7 +435,7 @@ namespace boost
static void check_coverage() {} static void check_coverage() {}
}; };
#endif // #ifndef DISABLE_WIDE_CHAR_SUPPORT #endif // #ifndef BOOST_LCAST_NO_WCHAR_T
#endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
} }
@@ -437,7 +450,7 @@ namespace boost
BOOST_STATIC_CONSTANT(char, minus = '-'); BOOST_STATIC_CONSTANT(char, minus = '-');
}; };
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<> template<>
struct lcast_char_constants<wchar_t> struct lcast_char_constants<wchar_t>
{ {
@@ -460,37 +473,21 @@ namespace boost
# pragma warning( push ) # pragma warning( push )
// C4146: unary minus operator applied to unsigned type, result still unsigned // C4146: unary minus operator applied to unsigned type, result still unsigned
# pragma warning( disable : 4146 ) # pragma warning( disable : 4146 )
#elif defined( __BORLANDC__ )
# pragma option push -w-8041
#endif #endif
template<class T>
inline unsigned int lcast_to_unsigned(int value) inline
BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
{ {
unsigned int uval = value; typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
return value < 0 ? -uval : uval; result_type uvalue = static_cast<result_type>(value);
return value < 0 ? -uvalue : uvalue;
} }
inline unsigned long lcast_to_unsigned(long value)
{
unsigned long uval = value;
return value < 0 ? -uval : uval;
}
#if defined(BOOST_HAS_LONG_LONG)
inline boost::ulong_long_type lcast_to_unsigned(boost::long_long_type v)
{
boost::ulong_long_type uval = v;
return v < 0 ? -uval : uval;
}
#elif defined(BOOST_HAS_MS_INT64)
inline unsigned __int64 lcast_to_unsigned(__int64 value)
{
unsigned __int64 uval = value;
return value < 0 ? -uval : uval;
}
#endif
#if (defined _MSC_VER) #if (defined _MSC_VER)
# pragma warning( pop ) // C4146: unary minus operator applied to unsigned type, # pragma warning( pop )
// result still unsigned #elif defined( __BORLANDC__ )
# pragma option pop
#endif #endif
} }
@@ -502,22 +499,15 @@ namespace boost
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed); BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
#endif #endif
CharT thousands_sep = 0;
#ifdef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
char const* grouping = ""; // TODO: use BOOST_NO_STD_LOCALE
std::size_t const grouping_size = 0;
#else
std::locale loc; std::locale loc;
typedef std::numpunct<CharT> numpunct; typedef std::numpunct<CharT> numpunct;
numpunct const& np = BOOST_USE_FACET(numpunct, loc); numpunct const& np = BOOST_USE_FACET(numpunct, loc);
std::string const& grouping = np.grouping(); std::string const& grouping = np.grouping();
std::string::size_type const grouping_size = grouping.size(); std::string::size_type const grouping_size = grouping.size();
CharT thousands_sep = grouping_size ? np.thousands_sep() : 0;
if(grouping_size)
thousands_sep = np.thousands_sep();
#endif
std::string::size_type group = 0; // current group number std::string::size_type group = 0; // current group number
char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0]; char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0];
// a) Since grouping is const, grouping[grouping.size()] returns 0. // a) Since grouping is const, grouping[grouping.size()] returns 0.
@@ -526,14 +516,17 @@ namespace boost
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX); BOOST_STATIC_ASSERT(std::numeric_limits<T>::digits10 < CHAR_MAX);
#endif #endif
char left = last_grp_size;
#endif
typedef typename Traits::int_type int_type; typedef typename Traits::int_type int_type;
CharT const czero = lcast_char_constants<CharT>::zero; CharT const czero = lcast_char_constants<CharT>::zero;
int_type const zero = Traits::to_int_type(czero); int_type const zero = Traits::to_int_type(czero);
char left = last_grp_size;
do do
{ {
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
if(left == 0) if(left == 0)
{ {
++group; ++group;
@@ -549,6 +542,8 @@ namespace boost
} }
--left; --left;
#endif
--finish; --finish;
int_type const digit = static_cast<int_type>(n % 10U); int_type const digit = static_cast<int_type>(n % 10U);
Traits::assign(*finish, Traits::to_char_type(zero + digit)); Traits::assign(*finish, Traits::to_char_type(zero + digit));
@@ -614,7 +609,7 @@ namespace boost
stream.str().swap(output); stream.str().swap(output);
return true; return true;
} }
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
bool operator>>(std::wstring &output) bool operator>>(std::wstring &output)
{ {
stream.str().swap(output); stream.str().swap(output);
@@ -670,9 +665,10 @@ namespace boost
Traits::assign(*p, ch); Traits::assign(*p, ch);
} }
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
static void widen_and_assign(wchar_t* p, char ch) static void widen_and_assign(wchar_t* p, char ch)
{ {
// TODO: use BOOST_NO_STD_LOCALE
std::locale loc; std::locale loc;
wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch); wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch);
Traits::assign(*p, w); Traits::assign(*p, w);
@@ -703,9 +699,9 @@ namespace boost
public: public:
lexical_stream_limited_src(CharT* start, CharT* finish) lexical_stream_limited_src(CharT* sta, CharT* fin)
: start(start) : start(sta)
, finish(finish) , finish(fin)
{} {}
public: // output public: // output
@@ -720,7 +716,7 @@ namespace boost
bool operator<<(bool); bool operator<<(bool);
bool operator<<(char); bool operator<<(char);
#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
bool operator<<(wchar_t); bool operator<<(wchar_t);
#endif #endif
bool operator<<(CharT const*); bool operator<<(CharT const*);
@@ -785,7 +781,7 @@ namespace boost
bool operator>>(std::string&); bool operator>>(std::string&);
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
bool operator>>(std::wstring&); bool operator>>(std::wstring&);
#endif #endif
@@ -820,7 +816,7 @@ namespace boost
return true; return true;
} }
#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<typename CharT, class Base, class Traits> template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<( inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
wchar_t ch) wchar_t ch)
@@ -907,7 +903,7 @@ namespace boost
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<( inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned short n) unsigned short n)
{ {
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish); start = lcast_put_unsigned<Traits>(n, finish);
return true; return true;
} }
@@ -994,7 +990,7 @@ namespace boost
return true; return true;
} }
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<typename CharT, class Base, class Traits> template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>( inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
std::wstring& str) std::wstring& str)
@@ -1049,7 +1045,7 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = false); BOOST_STATIC_CONSTANT(bool, value = false);
}; };
#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<> template<>
struct lcast_streambuf_for_target<wchar_t> struct lcast_streambuf_for_target<wchar_t>
{ {
@@ -1065,7 +1061,7 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = false); BOOST_STATIC_CONSTANT(bool, value = false);
}; };
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<class Traits, class Alloc> template<class Traits, class Alloc>
struct lcast_streambuf_for_target< struct lcast_streambuf_for_target<
std::basic_string<wchar_t,Traits,Alloc> > std::basic_string<wchar_t,Traits,Alloc> >
@@ -1080,7 +1076,7 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = false); BOOST_STATIC_CONSTANT(bool, value = false);
}; };
#ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_LCAST_NO_WCHAR_T
template<> template<>
struct lcast_streambuf_for_target<std::wstring> struct lcast_streambuf_for_target<std::wstring>
{ {
@@ -1201,5 +1197,5 @@ namespace boost
// accompanying file LICENSE_1_0.txt or copy at // accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
#undef DISABLE_WIDE_CHAR_SUPPORT #undef BOOST_LCAST_NO_WCHAR_T
#endif #endif