Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r72268 - in branches/release: boost libs/conversion
From: antoshkka_at_[hidden]
Date: 2011-05-29 15:21:29


Author: apolukhin
Date: 2011-05-29 15:21:28 EDT (Sun, 29 May 2011)
New Revision: 72268
URL: http://svn.boost.org/trac/boost/changeset/72268

Log:
mereged lexical_cast from trunk r72267 (more optimizations, updated documentation, added tests)
Text files modified:
   branches/release/boost/lexical_cast.hpp | 211 ++++++++++++++++++++++++++++++---------
   branches/release/libs/conversion/lexical_cast.htm | 31 +++++
   branches/release/libs/conversion/lexical_cast_test.cpp | 53 ++++++++++
   3 files changed, 243 insertions(+), 52 deletions(-)

Modified: branches/release/boost/lexical_cast.hpp
==============================================================================
--- branches/release/boost/lexical_cast.hpp (original)
+++ branches/release/boost/lexical_cast.hpp 2011-05-29 15:21:28 EDT (Sun, 29 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<>
@@ -526,6 +555,10 @@
             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;
+ 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;
@@ -533,47 +566,54 @@
             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_size == 0 || grouping[0] <= 0 ? CHAR_MAX : grouping[0];
+
+ if ( grouping_size && grouping[0] > 0 )
+ {
+
 #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;
 
- 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);
-
- do
- {
-#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
- if(left == 0)
+ do
                 {
- ++group;
- if(group < grouping_size)
+ if(left == 0)
                     {
- char const grp_size = grouping[group];
- last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_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);
                     }
 
- left = last_grp_size;
+ --left;
+
                     --finish;
- Traits::assign(*finish, thousands_sep);
- }
+ int_type const digit = static_cast<int_type>(n % 10U);
+ Traits::assign(*finish, Traits::to_char_type(zero + digit));
+ n /= 10;
+ } while(n);
 
- --left;
+ } else
 #endif
-
- --finish;
- int_type const digit = static_cast<int_type>(n % 10U);
- Traits::assign(*finish, Traits::to_char_type(zero + digit));
- n /= 10;
- } while(n);
+ {
+ 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);
+ }
 
             return finish;
         }
@@ -609,10 +649,10 @@
             /* According to [22.2.2.1.2] of Programming languages - C++
              * we MUST check for correct grouping
              */
- if (grouping_size)
+ if (grouping_size && grouping[0] > 0)
             {
                 unsigned char current_grouping = 0;
- CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
+ CharT const thousands_sep = np.thousands_sep();
                 char remained = grouping[current_grouping] - 1;
 
                 for(;end>=begin; --end)
@@ -821,9 +861,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 +1094,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 +1137,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 +1330,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 +1414,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 +1570,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: branches/release/libs/conversion/lexical_cast.htm
==============================================================================
--- branches/release/libs/conversion/lexical_cast.htm (original)
+++ branches/release/libs/conversion/lexical_cast.htm 2011-05-29 15:21:28 EDT (Sun, 29 May 2011)
@@ -26,6 +26,8 @@
                                 <a href="#references">References</a></li>
                         <li>
                                 <a href="#changes">Changes</a></li>
+ <li>
+ Performance</li>
                 </ul>
                 <hr>
                 <h2><a name="motivation">Motivation</a></h2>
@@ -87,7 +89,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>.
+ <i>The String Formatters of Manor Farm</i></a>. Also, take a look at the Performance section.
                 <p>
                         <hr>
                         <h2><a name="examples">Examples</a></h2>
@@ -267,6 +269,8 @@
 <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>
@@ -312,7 +316,30 @@
 </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>

Modified: branches/release/libs/conversion/lexical_cast_test.cpp
==============================================================================
--- branches/release/libs/conversion/lexical_cast_test.cpp (original)
+++ branches/release/libs/conversion/lexical_cast_test.cpp 2011-05-29 15:21:28 EDT (Sun, 29 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,42 @@
 
 #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
+}


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