Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r80291 - in trunk: boost libs/conversion libs/conversion/doc libs/conversion/test
From: antoshkka_at_[hidden]
Date: 2012-08-28 16:31:17


Author: apolukhin
Date: 2012-08-28 16:31:16 EDT (Tue, 28 Aug 2012)
New Revision: 80291
URL: http://svn.boost.org/trac/boost/changeset/80291

Log:
Added lexical_cast(const CharType* chars, std::size_t count) function overload (refs #6430 and refs #6663)
Fixed GCC warning in numeric_cast_test.cpp
Text files modified:
   trunk/boost/lexical_cast.hpp | 11 +++++++++++
   trunk/libs/conversion/doc/lexical_cast.qbk | 23 +++++++++++++++++++++--
   trunk/libs/conversion/numeric_cast_test.cpp | 1 +
   trunk/libs/conversion/test/lexical_cast_iterator_range_test.cpp | 13 +++++++++++++
   4 files changed, 46 insertions(+), 2 deletions(-)

Modified: trunk/boost/lexical_cast.hpp
==============================================================================
--- trunk/boost/lexical_cast.hpp (original)
+++ trunk/boost/lexical_cast.hpp 2012-08-28 16:31:16 EDT (Tue, 28 Aug 2012)
@@ -2300,6 +2300,17 @@
         return caster_type::lexical_cast_impl(arg);
     }
 
+ template <typename Target, typename CharType>
+ inline Target lexical_cast(const CharType* chars, std::size_t count)
+ {
+ BOOST_STATIC_ASSERT_MSG(::boost::detail::is_char_or_wchar<CharType>::value,
+ "CharType must be a character or wide character type");
+
+ return ::boost::lexical_cast<Target>(
+ ::boost::iterator_range<const CharType*>(chars, chars + count)
+ );
+ }
+
 } // namespace boost
 
 #else // #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION

Modified: trunk/libs/conversion/doc/lexical_cast.qbk
==============================================================================
--- trunk/libs/conversion/doc/lexical_cast.qbk (original)
+++ trunk/libs/conversion/doc/lexical_cast.qbk 2012-08-28 16:31:16 EDT (Tue, 28 Aug 2012)
@@ -79,6 +79,13 @@
     buf_t buffer = boost::lexical_cast<buf_t>(i); // No dynamic memory allocation
     puts(buffer.begin(), file);
 ``
+Following example takes part of the string and converts it to `int`:
+``
+int convert_strings_part(const std::string& s, std::size_t pos, std::size_t n)
+{
+ return boost::lexical_cast<int>(s.c_str() + pos, n);
+}
+``
 [endsect]
 
 [section Synopsis]
@@ -99,7 +106,13 @@
 ``
 Returns the result of streaming arg into a standard library string-based stream and then out as a Target object. Where Target is either `std::string` or `std::wstring`, stream extraction takes the whole content of the string, including spaces, rather than relying on the default `operator>>` behavior. If the conversion is unsuccessful, a `bad_lexical_cast` exception is thrown.
 
-The requirements on the argument and result types are:
+``
+ template <typename Target, typename CharType>
+ Target lexical_cast(const CharType* chars, std::size_t count);
+``
+Takes an array of `count` characters as input parameter and streams them out as a Target object. If the conversion is unsuccessful, a `bad_lexical_cast` exception is thrown. This call may be useful for processing nonzero terminated array of characters or processing just some part of character array.
+
+The requirements on the argument and result types for both functions are:
 
 * Source is OutputStreamable, meaning that an `operator<<` is defined that takes a `std::ostream` or `std::wostream` object on the left hand side and an instance of the argument type on the right.
 * Target is InputStreamable, meaning that an `operator>>` is defined that takes a `std::istream` or `std::wistream` object on the left hand side and an instance of the result type on the right.
@@ -114,6 +127,7 @@
 * `boost::iterator_range<WideCharPtr>`, where `WideCharPtr` is a pointer to wide-character or pointer to const wide-character
 * `boost::array<CharT, N>` and `std::array<CharT, N>`, `boost::array<const CharT, N>` and `std::array<const CharT, N>`
 
+
 [important Many compilers and runtime libraries fail to make conversions using new Unicode characters. Make sure that the following code compiles and outputs nonzero values, before using new types:
 ``
     std::cout
@@ -253,12 +267,17 @@
 ]
 
 * [*Question:] What is the fastest way to convert a non zero terminated string or a substring using `boost::lexical_cast`?
- * [*Answer:] Use `boost::iterator_range` for conversion. For example, if you whant to convert to `int` two characters from a string `str`, you shall write `lexacal_cast<int>(make_iterator_range(str.c_str(), str.c_str() + 2));`.
+ * [*Answer:] Use `boost::iterator_range` for conversion or `lexical_cast` overload with two parameters. For example, if you whant to convert to `int` two characters from a string `str`, you shall write `lexical_cast<int>(make_iterator_range(str.c_str(), str.c_str() + 2));` or `lexical_cast<int>(str.c_str(), 2);`.
 
 [endsect]
 
 [section Changes]
 
+* [*boost 1.52.0 :]
+
+ * Restored compilation on MSVC-2003 (was broken in 1.51.0).
+ * Added `lexical_cast(const CharType* chars, std::size_t count)` function overload.
+
 * [*boost 1.51.0 :]
 
     * Better performance, less memory usage for `boost::array<character_type, N>` and `std::array<character_type, N>` conversions.

Modified: trunk/libs/conversion/numeric_cast_test.cpp
==============================================================================
--- trunk/libs/conversion/numeric_cast_test.cpp (original)
+++ trunk/libs/conversion/numeric_cast_test.cpp 2012-08-28 16:31:16 EDT (Tue, 28 Aug 2012)
@@ -96,5 +96,6 @@
         { cout<<"caught bad_numeric_cast #5\n"; caught_exception = true; }
     BOOST_CHECK ( caught_exception );
 
+ (void)ul; // Supressing GCC warning about set but unused wariable
     return 0 ;
 }

Modified: trunk/libs/conversion/test/lexical_cast_iterator_range_test.cpp
==============================================================================
--- trunk/libs/conversion/test/lexical_cast_iterator_range_test.cpp (original)
+++ trunk/libs/conversion/test/lexical_cast_iterator_range_test.cpp 2012-08-28 16:31:16 EDT (Tue, 28 Aug 2012)
@@ -46,24 +46,37 @@
 void do_test_iterator_range_impl(const RngT& rng)
 {
     BOOST_CHECK_EQUAL(lexical_cast<int>(rng), 1);
+ BOOST_CHECK_EQUAL(lexical_cast<int>(rng.begin(), rng.size()), 1);
     BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(rng), 1u);
+ BOOST_CHECK_EQUAL(lexical_cast<unsigned int>(rng.begin(), rng.size()), 1u);
     BOOST_CHECK_EQUAL(lexical_cast<short>(rng), 1);
+ BOOST_CHECK_EQUAL(lexical_cast<short>(rng.begin(), rng.size()), 1);
     BOOST_CHECK_EQUAL(lexical_cast<unsigned short>(rng), 1u);
+ BOOST_CHECK_EQUAL(lexical_cast<unsigned short>(rng.begin(), rng.size()), 1u);
     BOOST_CHECK_EQUAL(lexical_cast<long int>(rng), 1);
+ BOOST_CHECK_EQUAL(lexical_cast<long int>(rng.begin(), rng.size()), 1);
     BOOST_CHECK_EQUAL(lexical_cast<unsigned long int>(rng), 1u);
+ BOOST_CHECK_EQUAL(lexical_cast<unsigned long int>(rng.begin(), rng.size()), 1u);
 
 #ifdef BOOST_STL_SUPPORTS_NEW_UNICODE_LOCALES
     BOOST_CHECK_EQUAL(lexical_cast<float>(rng), 1.0f);
+ BOOST_CHECK_EQUAL(lexical_cast<float>(rng.begin(), rng.size()), 1.0f);
     BOOST_CHECK_EQUAL(lexical_cast<double>(rng), 1.0);
+ BOOST_CHECK_EQUAL(lexical_cast<double>(rng.begin(), rng.size()), 1.0);
     BOOST_CHECK_EQUAL(lexical_cast<long double>(rng), 1.0L);
+ BOOST_CHECK_EQUAL(lexical_cast<long double>(rng.begin(), rng.size()), 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::ulong_long_type>(rng.begin(), rng.size()), 1u);
     BOOST_CHECK_EQUAL(lexical_cast<boost::long_long_type>(rng), 1);
+ BOOST_CHECK_EQUAL(lexical_cast<boost::long_long_type>(rng.begin(), rng.size()), 1);
 #elif defined(BOOST_HAS_MS_INT64)
     BOOST_CHECK_EQUAL(lexical_cast<unsigned __int64>(rng), 1u);
+ BOOST_CHECK_EQUAL(lexical_cast<unsigned __int64>(rng.begin(), rng.size()), 1u);
     BOOST_CHECK_EQUAL(lexical_cast<__int64>(rng), 1);
+ BOOST_CHECK_EQUAL(lexical_cast<__int64>(rng.begin(), rng.size()), 1);
 #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