Compare commits

..

1 Commits

Author SHA1 Message Date
Beman Dawes 8a0a6c1942 Release
[SVN r43921]
2008-03-29 11:50:24 +00:00
3 changed files with 85 additions and 81 deletions
+1 -1
View File
@@ -123,7 +123,7 @@ void f( Fruit * fruit ) {
"http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams</a>.<code><br>
An old
numeric_cast</code> that was contributed by <a href=
"http://www.boost.org/people/kevlin_henney.htm">Kevlin Henney</a> is now superseeded by the <a href="../numeric/conversion/doc/html/index.html">Boost Numeric Conversion Library</a></p>
"http://www.boost.org/people/kevlin_henney.htm">Kevlin Henney</a> is now superseeded by the <a href="../numeric/conversion/doc/index.html">Boost Numeric Conversion Library</a></p>
<hr>
<p>Revised
+81 -77
View File
@@ -17,23 +17,17 @@
#include <climits>
#include <cstddef>
#include <istream>
#include <locale>
#include <string>
#include <typeinfo>
#include <exception>
#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/make_unsigned.hpp>
#include <boost/call_traits.hpp>
#include <boost/static_assert.hpp>
#include <boost/detail/lcast_precision.hpp>
#include <boost/detail/workaround.hpp>
#ifndef BOOST_NO_STD_LOCALE
#include <locale>
#endif
#ifdef BOOST_NO_STRINGSTREAM
#include <strstream>
@@ -44,19 +38,13 @@
#if defined(BOOST_NO_STRINGSTREAM) || \
defined(BOOST_NO_STD_WSTRING) || \
defined(BOOST_NO_STD_LOCALE)
#define BOOST_LCAST_NO_WCHAR_T
#define DISABLE_WIDE_CHAR_SUPPORT
#endif
namespace boost
{
// exception used to indicate runtime lexical_cast failure
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:
bad_lexical_cast() :
@@ -106,7 +94,7 @@ namespace boost
};
#endif
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
#ifndef BOOST_NO_INTRINSIC_WCHAR_T
template<>
struct stream_char<wchar_t>
@@ -231,7 +219,7 @@ namespace boost
// lcast_src_length<char, signed char const*>
// lcast_src_length<char, unsigned char const*>
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>
struct lcast_src_length<wchar_t, bool>
{
@@ -270,7 +258,7 @@ namespace boost
static void check_coverage() {}
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>
struct lcast_src_length<wchar_t, wchar_t const*>
{
@@ -301,7 +289,7 @@ namespace boost
static void check_coverage() {}
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>
struct lcast_src_length< wchar_t, std::basic_string<wchar_t> >
{
@@ -339,35 +327,34 @@ namespace boost
#endif
};
#define BOOST_LCAST_DEF1(CharT, T) \
template<> struct lcast_src_length<CharT, T> \
: lcast_src_length_integral<T> \
#define BOOST_AUX_LEXICAL_CAST_DEF1(CharT, T) template<> \
struct lcast_src_length<CharT, T> : lcast_src_length_integral<T> \
{ static void check_coverage() {} };
#ifdef BOOST_LCAST_NO_WCHAR_T
#define BOOST_LCAST_DEF(T) BOOST_LCAST_DEF1(char, T)
#ifdef DISABLE_WIDE_CHAR_SUPPORT
#define BOOST_AUX_LEXICAL_CAST_DEF(T) BOOST_AUX_LEXICAL_CAST_DEF1(char, T)
#else
#define BOOST_LCAST_DEF(T) \
BOOST_LCAST_DEF1(char, T) \
BOOST_LCAST_DEF1(wchar_t, T)
#define BOOST_AUX_LEXICAL_CAST_DEF(T) \
BOOST_AUX_LEXICAL_CAST_DEF1(char, T) \
BOOST_AUX_LEXICAL_CAST_DEF1(wchar_t, T)
#endif
BOOST_LCAST_DEF(short)
BOOST_LCAST_DEF(unsigned short)
BOOST_LCAST_DEF(int)
BOOST_LCAST_DEF(unsigned int)
BOOST_LCAST_DEF(long)
BOOST_LCAST_DEF(unsigned long)
BOOST_AUX_LEXICAL_CAST_DEF(short)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned short)
BOOST_AUX_LEXICAL_CAST_DEF(int)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned int)
BOOST_AUX_LEXICAL_CAST_DEF(long)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned long)
#if defined(BOOST_HAS_LONG_LONG)
BOOST_LCAST_DEF(boost::ulong_long_type)
BOOST_LCAST_DEF(boost::long_long_type )
BOOST_AUX_LEXICAL_CAST_DEF(boost::ulong_long_type)
BOOST_AUX_LEXICAL_CAST_DEF(boost::long_long_type )
#elif defined(BOOST_HAS_MS_INT64)
BOOST_LCAST_DEF(unsigned __int64)
BOOST_LCAST_DEF( __int64)
BOOST_AUX_LEXICAL_CAST_DEF(unsigned __int64)
BOOST_AUX_LEXICAL_CAST_DEF( __int64)
#endif
#undef BOOST_LCAST_DEF
#undef BOOST_LCAST_DEF1
#undef BOOST_AUX_LEXICAL_CAST_DEF
#undef BOOST_AUX_LEXICAL_CAST_DEF1
#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
// Helper for floating point types.
@@ -413,7 +400,7 @@ namespace boost
static void check_coverage() {}
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>
struct lcast_src_length<wchar_t,float>
: lcast_src_length_floating<float>
@@ -435,7 +422,7 @@ namespace boost
static void check_coverage() {}
};
#endif // #ifndef BOOST_LCAST_NO_WCHAR_T
#endif // #ifndef DISABLE_WIDE_CHAR_SUPPORT
#endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION
}
@@ -450,7 +437,7 @@ namespace boost
BOOST_STATIC_CONSTANT(char, minus = '-');
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>
struct lcast_char_constants<wchar_t>
{
@@ -473,21 +460,37 @@ namespace boost
# 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
template<class T>
inline
BOOST_DEDUCED_TYPENAME make_unsigned<T>::type lcast_to_unsigned(T value)
inline unsigned int lcast_to_unsigned(int value)
{
typedef BOOST_DEDUCED_TYPENAME make_unsigned<T>::type result_type;
result_type uvalue = static_cast<result_type>(value);
return value < 0 ? -uvalue : uvalue;
unsigned int uval = value;
return value < 0 ? -uval : uval;
}
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)
# pragma warning( pop )
#elif defined( __BORLANDC__ )
# pragma option pop
# pragma warning( pop ) // C4146: unary minus operator applied to unsigned type,
// result still unsigned
#endif
}
@@ -499,15 +502,22 @@ namespace boost
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
BOOST_STATIC_ASSERT(!std::numeric_limits<T>::is_signed);
#endif
CharT thousands_sep = 0;
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
// TODO: use BOOST_NO_STD_LOCALE
#ifdef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
char const* grouping = "";
std::size_t const grouping_size = 0;
#else
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;
if(grouping_size)
thousands_sep = np.thousands_sep();
#endif
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.
@@ -516,17 +526,14 @@ namespace boost
#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);
char left = last_grp_size;
do
{
#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
if(left == 0)
{
++group;
@@ -542,8 +549,6 @@ namespace boost
}
--left;
#endif
--finish;
int_type const digit = static_cast<int_type>(n % 10U);
Traits::assign(*finish, Traits::to_char_type(zero + digit));
@@ -609,7 +614,7 @@ namespace boost
stream.str().swap(output);
return true;
}
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
bool operator>>(std::wstring &output)
{
stream.str().swap(output);
@@ -665,10 +670,9 @@ namespace boost
Traits::assign(*p, ch);
}
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
static void widen_and_assign(wchar_t* p, char ch)
{
// TODO: use BOOST_NO_STD_LOCALE
std::locale loc;
wchar_t w = BOOST_USE_FACET(std::ctype<wchar_t>, loc).widen(ch);
Traits::assign(*p, w);
@@ -699,9 +703,9 @@ namespace boost
public:
lexical_stream_limited_src(CharT* sta, CharT* fin)
: start(sta)
, finish(fin)
lexical_stream_limited_src(CharT* start, CharT* finish)
: start(start)
, finish(finish)
{}
public: // output
@@ -716,7 +720,7 @@ namespace boost
bool operator<<(bool);
bool operator<<(char);
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
bool operator<<(wchar_t);
#endif
bool operator<<(CharT const*);
@@ -781,7 +785,7 @@ namespace boost
bool operator>>(std::string&);
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
bool operator>>(std::wstring&);
#endif
@@ -816,7 +820,7 @@ namespace boost
return true;
}
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
wchar_t ch)
@@ -903,7 +907,7 @@ namespace boost
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator<<(
unsigned short n)
{
start = lcast_put_unsigned<Traits>(n, finish);
start = lcast_put_unsigned<Traits>(lcast_to_unsigned(n), finish);
return true;
}
@@ -990,7 +994,7 @@ namespace boost
return true;
}
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<typename CharT, class Base, class Traits>
inline bool lexical_stream_limited_src<CharT,Base,Traits>::operator>>(
std::wstring& str)
@@ -1045,7 +1049,7 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = false);
};
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
template<>
struct lcast_streambuf_for_target<wchar_t>
{
@@ -1061,7 +1065,7 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = false);
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<class Traits, class Alloc>
struct lcast_streambuf_for_target<
std::basic_string<wchar_t,Traits,Alloc> >
@@ -1076,7 +1080,7 @@ namespace boost
BOOST_STATIC_CONSTANT(bool, value = false);
};
#ifndef BOOST_LCAST_NO_WCHAR_T
#ifndef DISABLE_WIDE_CHAR_SUPPORT
template<>
struct lcast_streambuf_for_target<std::wstring>
{
@@ -1197,5 +1201,5 @@ namespace boost
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#undef BOOST_LCAST_NO_WCHAR_T
#undef DISABLE_WIDE_CHAR_SUPPORT
#endif
+3 -3
View File
@@ -81,7 +81,7 @@
conversions, such as where precision or formatting need tighter control than is
offered by the default behavior of <code>lexical_cast</code>, the conventional <code>
stringstream</code> approach is recommended. Where the conversions are
numeric to numeric, <code><a href="../numeric/conversion/doc/html/boost_numericconversion/improved_numeric_cast__.html">numeric_cast</a></code>
numeric to numeric, <code><a href="../numeric/conversion/doc/numeric_cast.html">numeric_cast</a></code>
may offer more reasonable behavior than <code>lexical_cast</code>.
<p>
For a good discussion of the options and issues involved in string-based
@@ -202,9 +202,9 @@ public:
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.
<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>:
is important, you can also call <a href="../../libs/numeric/conversion/doc/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>
<pre><a href="../../libs/numeric/conversion/doc/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: