|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r72203 - in trunk: boost libs/conversion
From: antoshkka_at_[hidden]
Date: 2011-05-27 00:22:23
Author: apolukhin
Date: 2011-05-27 00:22:18 EDT (Fri, 27 May 2011)
New Revision: 72203
URL: http://svn.boost.org/trac/boost/changeset/72203
Log:
Fixes #5564. Adds tests on #5564 and more tests on #5557
Text files modified:
trunk/boost/lexical_cast.hpp | 138 ++++++++++++++++++++++++++++++++++-----
trunk/libs/conversion/lexical_cast.htm | 1
trunk/libs/conversion/lexical_cast_test.cpp | 54 +++++++++++++++
3 files changed, 174 insertions(+), 19 deletions(-)
Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2011-05-27 00:22:18 EDT (Fri, 27 May 2011)
@@ -247,13 +247,42 @@
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*>
+ 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() {}
+ };
#ifndef BOOST_LCAST_NO_WCHAR_T
template<>
@@ -821,9 +850,13 @@
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);
@@ -1050,6 +1083,8 @@
}
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
@@ -1091,6 +1126,34 @@
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<<(
@@ -1256,6 +1319,34 @@
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>>(
@@ -1312,13 +1403,9 @@
struct lcast_streambuf_for_target
{
BOOST_STATIC_CONSTANT(bool, value =
- (
- ::boost::type_traits::ice_or<
- ::boost::type_traits::ice_not< is_integral<Target>::value >::value,
- is_same<Target, signed char>::value,
- is_same<Target, unsigned char>::value
- >::value
- )
+ (
+ ::boost::type_traits::ice_not< is_integral<Target>::value >::value
+ )
);
};
@@ -1472,16 +1559,29 @@
);
};
+ /*
+ * 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_and<
- is_same<Source,Target>::value,
- is_char_or_wchar<Target>::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
- )
+ )
);
};
Modified: trunk/libs/conversion/lexical_cast.htm
==============================================================================
--- trunk/libs/conversion/lexical_cast.htm (original)
+++ trunk/libs/conversion/lexical_cast.htm 2011-05-27 00:22:18 EDT (Fri, 27 May 2011)
@@ -267,6 +267,7 @@
<h2><a name="changes">Changes</a></h2>
<h3>May 2011:</h3>
<ul type="square">
+ <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>
Modified: trunk/libs/conversion/lexical_cast_test.cpp
==============================================================================
--- trunk/libs/conversion/lexical_cast_test.cpp (original)
+++ trunk/libs/conversion/lexical_cast_test.cpp 2011-05-27 00:22:18 EDT (Fri, 27 May 2011)
@@ -95,6 +95,7 @@
void test_allocator();
void test_wallocator();
#endif
+void test_char_types_conversions();
unit_test::test_suite *init_unit_test_suite(int, char *[])
{
@@ -137,6 +138,8 @@
suite->add(BOOST_TEST_CASE(&test_wallocator));
#endif
+ suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
+
return suite;
}
@@ -730,6 +733,12 @@
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;
@@ -785,6 +794,11 @@
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()
@@ -923,3 +937,43 @@
#endif
+#include <iostream>
+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
+}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk