From 4ddb0bfb9564c43e61f1ba764db137794ad4f157 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 3 Nov 2011 04:33:58 +0000 Subject: [PATCH 01/28] Fix for error noticed by Olaf van der Spek (ticket #6083) Added test case for that error [SVN r75283] --- include/boost/lexical_cast.hpp | 2 +- test/Jamfile.v2 | 1 + test/lexical_cast_empty_input_test.cpp | 58 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100755 test/lexical_cast_empty_input_test.cpp diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 62f0f66..3d65297 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -1181,7 +1181,7 @@ namespace boost bool const result = !(stream << input).fail(); start = stringbuffer.pbase(); finish = stringbuffer.pptr(); - return result; + return result && (start != finish); } template diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7e5eacf..6eebe72 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -29,6 +29,7 @@ test-suite conversion [ run lexical_cast_float_types_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/static ] ; diff --git a/test/lexical_cast_empty_input_test.cpp b/test/lexical_cast_empty_input_test.cpp new file mode 100755 index 0000000..42e7cec --- /dev/null +++ b/test/lexical_cast_empty_input_test.cpp @@ -0,0 +1,58 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 2011. +// +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). + +#include + +#if defined(__INTEL_COMPILER) +#pragma warning(disable: 193 383 488 981 1418 1419) +#elif defined(BOOST_MSVC) +#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) +#endif + +#include +#include +#include + +using namespace boost; + +void test_empty_iterator_range() +{ + boost::iterator_range v; + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +} + +void test_empty_string() +{ + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + BOOST_CHECK_EQUAL(lexical_cast(std::string()), std::string()); + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); +} + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast. Empty input unit test"); + suite->add(BOOST_TEST_CASE(&test_empty_iterator_range)); + suite->add(BOOST_TEST_CASE(&test_empty_string)); + + return suite; +} From 7dd63d08d64a3343830eef49e878c4f8fec6e594 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 5 Dec 2011 16:17:09 +0000 Subject: [PATCH 02/28] Fixes #6159 (compilation on platforms without cwchar) [SVN r75812] --- include/boost/lexical_cast.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 3d65297..4cfa298 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -17,7 +17,7 @@ // enhanced with contributions from Terje Slettebo, // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, -// Alexander Nasonov, Antony Polukhin and other Boosters +// Alexander Nasonov, Antony Polukhin, Justin Viiret and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 #include @@ -47,8 +47,9 @@ #include #include #include -#include - +#ifndef BOOST_NO_CWCHAR +# include +#endif #ifndef BOOST_NO_STD_LOCALE # include From 2312691bdebad981cbe8461f3f93c3b23bc2406a Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 5 Dec 2011 16:29:45 +0000 Subject: [PATCH 03/28] Fixes #6127 (wchar_t usage outside of #ifndef BOOST_LCAST_NO_WCHAR_T ) [SVN r75813] --- include/boost/lexical_cast.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 4cfa298..e7ab248 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -17,7 +17,8 @@ // enhanced with contributions from Terje Slettebo, // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, -// Alexander Nasonov, Antony Polukhin, Justin Viiret and other Boosters +// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann +// and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 #include @@ -683,8 +684,8 @@ namespace boost , const CharT opening_brace, const CharT closing_brace) { using namespace std; - const wchar_t minus = lcast_char_constants::minus; - const wchar_t plus = lcast_char_constants::plus; + const CharT minus = lcast_char_constants::minus; + const CharT plus = lcast_char_constants::plus; const int inifinity_size = 8; bool has_minus = false; From a9697b88fd3b946d6e8a4a7f678d5b0f7533843d Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 8 Dec 2011 15:29:44 +0000 Subject: [PATCH 04/28] Fixes #6186 (lexical_cast compliation error fixed, when wchar_t is a typedef for unsigned short. Test added) [SVN r75864] --- include/boost/lexical_cast.hpp | 15 ++++++- test/Jamfile.v2 | 7 ++- test/lexical_cast_typedefed_wchar_test.cpp | 50 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100755 test/lexical_cast_typedefed_wchar_test.cpp diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index e7ab248..480d87a 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -1093,6 +1093,8 @@ namespace boost namespace detail // optimized stream wrapper { + struct type_used_as_workaround_for_typedefed_wchar_ts{}; + // String representation of Source has an upper limit. template< class CharT // a result of widest_char transformation , class Traits // usually char_traits @@ -1354,6 +1356,8 @@ namespace boost /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/ private: + bool shr_unsigned(type_used_as_workaround_for_typedefed_wchar_ts /*var*/) {return true;} + template bool shr_unsigned(Type& output) { @@ -1480,8 +1484,15 @@ namespace boost } /************************************ OPERATORS >> ( ... ) ********************************/ - public: - bool operator>>(unsigned short& output) { return shr_unsigned(output); } + + typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c< + ::boost::is_same::value + , type_used_as_workaround_for_typedefed_wchar_ts + , unsigned short + >::type ushort_ambiguity_workaround; + + public: + bool operator>>(ushort_ambiguity_workaround& output){ return shr_unsigned(output); } bool operator>>(unsigned int& output) { return shr_unsigned(output); } bool operator>>(unsigned long int& output) { return shr_unsigned(output); } bool operator>>(short& output) { return shr_signed(output); } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 6eebe72..e7408f3 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -14,7 +14,12 @@ # bring in rules for testing import testing ; +import feature ; +feature.feature nowchar : on : + composite optional propagated link-incompatible ; +feature.compose on : /Zc:wchar_t- ; + test-suite conversion : [ run implicit_cast.cpp ] [ compile-fail implicit_cast_fail.cpp ] @@ -30,6 +35,6 @@ test-suite conversion [ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_typedefed_wchar_test.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on : : ] ; - diff --git a/test/lexical_cast_typedefed_wchar_test.cpp b/test/lexical_cast_typedefed_wchar_test.cpp new file mode 100755 index 0000000..ae84678 --- /dev/null +++ b/test/lexical_cast_typedefed_wchar_test.cpp @@ -0,0 +1,50 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 2011. +// +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). + +#include + +#if defined(__INTEL_COMPILER) +#pragma warning(disable: 193 383 488 981 1418 1419) +#elif defined(BOOST_MSVC) +#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) +#endif + +#include +#include + +void test_typedefed_wchar_t(); + +using namespace boost; + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast unit test for typedefed wchar_t (mainly for MSVC)"); + suite->add(BOOST_TEST_CASE(&test_typedefed_wchar_t)); + + return suite; +} + + +void test_typedefed_wchar_t() +{ +#ifdef BOOST_MSVC + BOOST_CHECK((boost::is_same::value)); +#endif + + BOOST_CHECK_EQUAL(lexical_cast(L"1000"), 1000); +} + + + + + + + From f32fb4b5e538be898323e8ba4f6dde683cf699e1 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 12 Dec 2011 18:09:53 +0000 Subject: [PATCH 05/28] Fixes #6186 test failures [SVN r75921] --- test/Jamfile.v2 | 2 +- test/lexical_cast_typedefed_wchar_test.cpp | 33 +++------------------- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index e7408f3..96ce8f6 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -35,6 +35,6 @@ test-suite conversion [ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/static ] - [ run lexical_cast_typedefed_wchar_test.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on : : ] + [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] ; diff --git a/test/lexical_cast_typedefed_wchar_test.cpp b/test/lexical_cast_typedefed_wchar_test.cpp index ae84678..752c3e5 100755 --- a/test/lexical_cast_typedefed_wchar_test.cpp +++ b/test/lexical_cast_typedefed_wchar_test.cpp @@ -10,41 +10,16 @@ #include -#if defined(__INTEL_COMPILER) -#pragma warning(disable: 193 383 488 981 1418 1419) -#elif defined(BOOST_MSVC) -#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) -#endif - +#include #include -#include -void test_typedefed_wchar_t(); - -using namespace boost; - -unit_test::test_suite *init_unit_test_suite(int, char *[]) -{ - unit_test::test_suite *suite = - BOOST_TEST_SUITE("lexical_cast unit test for typedefed wchar_t (mainly for MSVC)"); - suite->add(BOOST_TEST_CASE(&test_typedefed_wchar_t)); - - return suite; -} - - -void test_typedefed_wchar_t() +int main() { #ifdef BOOST_MSVC - BOOST_CHECK((boost::is_same::value)); + BOOST_STATIC_ASSERT((boost::is_same::value)); #endif - BOOST_CHECK_EQUAL(lexical_cast(L"1000"), 1000); + return ::boost::lexical_cast(L"1000") == 1000; } - - - - - From 8627f8bb8ac5dcc83b1a51d50fcd3078c098caed Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 14 Dec 2011 16:36:34 +0000 Subject: [PATCH 06/28] Fixes #6186 (treat conversions to/from single wchar_t character as conversions to/from unsigned short. Test added, documentation updated) [SVN r75937] --- doc/lexical_cast.qbk | 12 +++++ include/boost/lexical_cast.hpp | 28 +++++------ test/Jamfile.v2 | 1 + ...ical_cast_typedefed_wchar_test_runtime.cpp | 48 +++++++++++++++++++ 4 files changed, 74 insertions(+), 15 deletions(-) create mode 100755 test/lexical_cast_typedefed_wchar_test_runtime.cpp diff --git a/doc/lexical_cast.qbk b/doc/lexical_cast.qbk index b540267..0532b56 100644 --- a/doc/lexical_cast.qbk +++ b/doc/lexical_cast.qbk @@ -159,9 +159,21 @@ Read a good C++ book, study `std::sentry` and [@boost:libs/io/doc/ios_state.html the rules of `scanf` for conversions. And in the C99 standard for unsigned input value minus sign is optional, so if a negative number is read, no errors will arise and the result will be the two's complement. +[pre +] + +* [*Question:] Why `boost::lexical_cast(L'A');` outputs 65 and `boost::lexical_cast(L"65");` does not throw? + * [*Answer:] If you are using an old version of Visual Studio or compile code with /Zc:wchar_t- flag, +`boost::lexical_cast` sees single `wchar_t` character as `unsigned short`. It is not a `boost::lexical_cast` mistake, but a +limitation of compiler options that you use. + [endsect] [section Changes] +* [*boost 1.49.0 :] + + * Added code to work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio). + * [*boost 1.48.0 :] * Added code to work with Inf and NaN on any platform. diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 480d87a..5fa6a96 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -17,8 +17,8 @@ // enhanced with contributions from Terje Slettebo, // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, -// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann -// and other Boosters +// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, +// Cheng Yang and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 #include @@ -1093,8 +1093,6 @@ namespace boost namespace detail // optimized stream wrapper { - struct type_used_as_workaround_for_typedefed_wchar_ts{}; - // String representation of Source has an upper limit. template< class CharT // a result of widest_char transformation , class Traits // usually char_traits @@ -1356,7 +1354,6 @@ namespace boost /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/ private: - bool shr_unsigned(type_used_as_workaround_for_typedefed_wchar_ts /*var*/) {return true;} template bool shr_unsigned(Type& output) @@ -1484,15 +1481,8 @@ namespace boost } /************************************ OPERATORS >> ( ... ) ********************************/ - - typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c< - ::boost::is_same::value - , type_used_as_workaround_for_typedefed_wchar_ts - , unsigned short - >::type ushort_ambiguity_workaround; - public: - bool operator>>(ushort_ambiguity_workaround& output){ return shr_unsigned(output); } + bool operator>>(unsigned short& output) { return shr_unsigned(output); } bool operator>>(unsigned int& output) { return shr_unsigned(output); } bool operator>>(unsigned long int& output) { return shr_unsigned(output); } bool operator>>(short& output) { return shr_signed(output); } @@ -1504,11 +1494,19 @@ namespace boost #elif defined(BOOST_HAS_MS_INT64) bool operator>>(unsigned __int64& output) { return shr_unsigned(output); } bool operator>>(__int64& output) { return shr_signed(output); } - #endif - bool operator>>(CharT& output) { return shr_xchar(output); } + bool operator>>(char& output) { return shr_xchar(output); } bool operator>>(unsigned char& output) { return shr_xchar(output); } bool operator>>(signed char& output) { return shr_xchar(output); } +#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + bool operator>>(wchar_t& output) { return shr_xchar(output); } +#endif +#ifndef BOOST_NO_CHAR16_T + bool operator>>(char16_t& output) { return shr_xchar(output); } +#endif +#ifndef BOOST_NO_CHAR32_T + bool operator>>(char32_t& output) { return shr_xchar(output); } +#endif #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION bool operator>>(std::string& str) { str.assign(start, finish); return true; } # ifndef BOOST_LCAST_NO_WCHAR_T diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 96ce8f6..8718301 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -36,5 +36,6 @@ test-suite conversion [ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/static ] [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] + [ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on ] ; diff --git a/test/lexical_cast_typedefed_wchar_test_runtime.cpp b/test/lexical_cast_typedefed_wchar_test_runtime.cpp new file mode 100755 index 0000000..d01700a --- /dev/null +++ b/test/lexical_cast_typedefed_wchar_test_runtime.cpp @@ -0,0 +1,48 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 2011. +// +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). + +#include + +#if defined(__INTEL_COMPILER) +#pragma warning(disable: 193 383 488 981 1418 1419) +#elif defined(BOOST_MSVC) +#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) +#endif + +#include + +#include +using namespace boost; + +void test_typedefed_wchar_t_runtime() +{ +#ifndef BOOST_LCAST_NO_WCHAR_T +#ifdef BOOST_MSVC + BOOST_STATIC_ASSERT((boost::is_same::value)); + + + BOOST_CHECK_EQUAL(boost::lexical_cast(L'A'), 65); + BOOST_CHECK_EQUAL(boost::lexical_cast(L'B'), 66); + + BOOST_CHECK_EQUAL(boost::lexical_cast(L"65"), 65); + BOOST_CHECK_EQUAL(boost::lexical_cast(L"66"), 66); +#endif +#endif + BOOST_CHECK(1); +} + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast typedefed wchar_t runtime test"); + suite->add(BOOST_TEST_CASE(&test_typedefed_wchar_t_runtime)); + + return suite; +} From 5d288580ee1c9a30ea2a56995438196ddd92a332 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 21 Dec 2011 17:03:52 +0000 Subject: [PATCH 07/28] Fixes #6298 Fixes #6264 [SVN r76096] --- include/boost/lexical_cast.hpp | 7 ++- test/lexical_cast_empty_input_test.cpp | 81 ++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 5fa6a96..11ee183 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -586,7 +586,7 @@ namespace boost --end; value = 0; - if ( *end < czero || *end >= czero + 10 || begin > end) + if (begin > end || *end < czero || *end >= czero + 10) return false; value = *end - czero; --end; @@ -684,6 +684,7 @@ namespace boost , const CharT opening_brace, const CharT closing_brace) { using namespace std; + if (begin == end) return false; const CharT minus = lcast_char_constants::minus; const CharT plus = lcast_char_constants::plus; const int inifinity_size = 8; @@ -1183,7 +1184,7 @@ namespace boost bool const result = !(stream << input).fail(); start = stringbuffer.pbase(); finish = stringbuffer.pptr(); - return result && (start != finish); + return result; } template @@ -1358,6 +1359,7 @@ namespace boost template bool shr_unsigned(Type& output) { + if (start == finish) return false; CharT const minus = lcast_char_constants::minus; CharT const plus = lcast_char_constants::plus; bool has_minus = false; @@ -1392,6 +1394,7 @@ namespace boost template bool shr_signed(Type& output) { + if (start == finish) return false; CharT const minus = lcast_char_constants::minus; CharT const plus = lcast_char_constants::plus; typedef BOOST_DEDUCED_TYPENAME make_unsigned::type utype; diff --git a/test/lexical_cast_empty_input_test.cpp b/test/lexical_cast_empty_input_test.cpp index 42e7cec..0c67542 100755 --- a/test/lexical_cast_empty_input_test.cpp +++ b/test/lexical_cast_empty_input_test.cpp @@ -24,27 +24,86 @@ using namespace boost; void test_empty_iterator_range() { - boost::iterator_range v; + boost::iterator_range v; BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); } void test_empty_string() { - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); - BOOST_CHECK_EQUAL(lexical_cast(std::string()), std::string()); - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(std::string()), bad_lexical_cast); + std::string v; + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +} + +struct Escape +{ + Escape(const std::string& s) + : str_(s) + {} + + std::string str_; +}; + +inline std::ostream& operator<< (std::ostream& o, const Escape& rhs) +{ + return o << rhs.str_; +} + +void test_empty_user_class() +{ + Escape v(""); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +} + +namespace std { +inline std::ostream & operator<<(std::ostream & out, const std::vector & v) +{ + std::ostream_iterator it(out); + std::copy(v.begin(), v.end(), it); + assert(out); + return out; +} +} + +void test_empty_vector() +{ + std::vector v; + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); } unit_test::test_suite *init_unit_test_suite(int, char *[]) @@ -53,6 +112,8 @@ unit_test::test_suite *init_unit_test_suite(int, char *[]) BOOST_TEST_SUITE("lexical_cast. Empty input unit test"); suite->add(BOOST_TEST_CASE(&test_empty_iterator_range)); suite->add(BOOST_TEST_CASE(&test_empty_string)); + suite->add(BOOST_TEST_CASE(&test_empty_user_class)); + suite->add(BOOST_TEST_CASE(&test_empty_vector)); return suite; } From 085bd6f93c56d55ebd952086926449536162937f Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sun, 25 Dec 2011 18:55:33 +0000 Subject: [PATCH 08/28] Fixes #6290 Tiny documentation update Updated char16_t and char32_t support (still some work required) lexical_cast_empty_input_test.cpp refactored [SVN r76154] --- doc/lexical_cast.qbk | 2 +- include/boost/lexical_cast.hpp | 24 +++++++- test/lexical_cast_empty_input_test.cpp | 80 ++++++++++++++++++-------- 3 files changed, 79 insertions(+), 27 deletions(-) diff --git a/doc/lexical_cast.qbk b/doc/lexical_cast.qbk index 0532b56..bf6b562 100644 --- a/doc/lexical_cast.qbk +++ b/doc/lexical_cast.qbk @@ -172,7 +172,7 @@ limitation of compiler options that you use. [section Changes] * [*boost 1.49.0 :] - * Added code to work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio). + * Restored work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio). * [*boost 1.48.0 :] diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 11ee183..b9bc984 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -18,7 +18,7 @@ // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, -// Cheng Yang and other Boosters +// Cheng Yang, Matthew Bradbury and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 #include @@ -744,6 +744,26 @@ namespace boost , L'(', L')'); } #endif +#ifndef BOOST_NO_CHAR16_T + template + bool parse_inf_nan(const char16_t* begin, const char16_t* end, T& value) + { + return parse_inf_nan_impl(begin, end, value + , u"NAN", u"nan" + , u"INFINITY", u"infinity" + , u'(', u')'); + } +#endif +#ifndef BOOST_NO_CHAR32_T + template + bool parse_inf_nan(const char32_t* begin, const char32_t* end, T& value) + { + return parse_inf_nan_impl(begin, end, value + , U"NAN", U"nan" + , U"INFINITY", U"infinity" + , U'(', U')'); + } +#endif template bool parse_inf_nan(const CharT* begin, const CharT* end, T& value) @@ -864,7 +884,7 @@ namespace boost CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0; CharT const decimal_point = np.decimal_point(); bool found_grouping = false; - unsigned int last_grouping_pos = grouping_size - 1; + std::string::size_type last_grouping_pos = grouping_size - 1; #else CharT const decimal_point = lcast_char_constants::c_decimal_separator; #endif diff --git a/test/lexical_cast_empty_input_test.cpp b/test/lexical_cast_empty_input_test.cpp index 0c67542..5a5881b 100755 --- a/test/lexical_cast_empty_input_test.cpp +++ b/test/lexical_cast_empty_input_test.cpp @@ -22,34 +22,78 @@ using namespace boost; -void test_empty_iterator_range() +#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) +#define BOOST_LCAST_NO_WCHAR_T +#endif + +template +void do_test_on_empty_input(T& v) { - boost::iterator_range v; BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +#if defined(BOOST_HAS_LONG_LONG) + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +#elif defined(BOOST_HAS_MS_INT64) + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast); +#endif +} + +void test_empty_iterator_range() +{ + + boost::iterator_range v; + do_test_on_empty_input(v); + BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + + boost::iterator_range cv; + do_test_on_empty_input(cv); + BOOST_CHECK_EQUAL(lexical_cast(cv), std::string()); + BOOST_CHECK_THROW(lexical_cast(cv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(cv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(cv), bad_lexical_cast); + + const boost::iterator_range ccv; + do_test_on_empty_input(ccv); + BOOST_CHECK_EQUAL(lexical_cast(ccv), std::string()); + BOOST_CHECK_THROW(lexical_cast(ccv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(ccv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(ccv), bad_lexical_cast); } void test_empty_string() { std::string v; - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + do_test_on_empty_input(v); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + +#ifndef BOOST_LCAST_NO_WCHAR_T + std::wstring vw; + do_test_on_empty_input(vw); + BOOST_CHECK_THROW(lexical_cast(vw), bad_lexical_cast); +#endif + +// Currently, no compiler and STL library fully support char16_t and char32_t +//#ifndef BOOST_NO_CHAR16_T +// std::basic_string v16w; +// do_test_on_empty_input(v16w); +// BOOST_CHECK_THROW(lexical_cast(v16w), bad_lexical_cast); +//#endif +//#ifndef BOOST_NO_CHAR32_T +// std::basic_string v32w; +// do_test_on_empty_input(v32w); +// BOOST_CHECK_THROW(lexical_cast(v32w), bad_lexical_cast); +//#endif } struct Escape @@ -69,13 +113,7 @@ inline std::ostream& operator<< (std::ostream& o, const Escape& rhs) void test_empty_user_class() { Escape v(""); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + do_test_on_empty_input(v); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); @@ -94,13 +132,7 @@ inline std::ostream & operator<<(std::ostream & out, const std::vector & v void test_empty_vector() { std::vector v; - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); - BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); - BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + do_test_on_empty_input(v); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); From d754143308c482d381aba9ee9834fd8ab6e2058b Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 4 Jan 2012 16:06:37 +0000 Subject: [PATCH 09/28] Fixes #6132 Fixes #6182 [SVN r76305] --- include/boost/lexical_cast.hpp | 27 ++++++-- test/Jamfile.v2 | 1 + test/lexical_cast_pointers_test.cpp | 96 +++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 6 deletions(-) create mode 100755 test/lexical_cast_pointers_test.cpp diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index b9bc984..cec2c50 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -19,7 +19,7 @@ // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, // Cheng Yang, Matthew Bradbury and other Boosters -// when: November 2000, March 2003, June 2005, June 2006, March 2011 +// when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2012 #include #include @@ -1822,6 +1822,24 @@ namespace boost deduce_char_traits::type traits; typedef BOOST_DEDUCED_TYPENAME remove_pointer::type removed_ptr_t; + + // is_char_types_match variable value can be computed via + // sizeof(char_type) == sizeof(removed_ptr_t). But when + // removed_ptr_t is an incomplete type or void*, compilers + // produce warnings or errors. + const bool is_char_types_match = + (::boost::type_traits::ice_or< + ::boost::type_traits::ice_and< + ::boost::type_traits::ice_eq::value, + ::boost::type_traits::ice_or< + ::boost::is_same::value, + ::boost::is_same::value, + ::boost::is_same::value + >::value + >::value, + is_same::value + >::value); + const bool requires_stringbuf = !( ::boost::type_traits::ice_or< @@ -1830,10 +1848,7 @@ namespace boost ::boost::type_traits::ice_and< is_pointer::value, is_char_or_wchar::value, - ::boost::type_traits::ice_eq< - sizeof(char_type), - sizeof(removed_ptr_t) - >::value + is_char_types_match >::value >::value ); @@ -2111,7 +2126,7 @@ namespace boost // Copyright Kevlin Henney, 2000-2005. // Copyright Alexander Nasonov, 2006-2010. -// Copyright Antony Polukhin, 2011. +// Copyright Antony Polukhin, 2011-2012. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 8718301..c311420 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -35,6 +35,7 @@ test-suite conversion [ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_pointers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] [ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on ] ; diff --git a/test/lexical_cast_pointers_test.cpp b/test/lexical_cast_pointers_test.cpp new file mode 100755 index 0000000..d24a895 --- /dev/null +++ b/test/lexical_cast_pointers_test.cpp @@ -0,0 +1,96 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 2012. +// +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). + +#include + +#if defined(__INTEL_COMPILER) +#pragma warning(disable: 193 383 488 981 1418 1419) +#elif defined(BOOST_MSVC) +#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) +#endif + +#include +#include + +using namespace boost; + +#if defined(BOOST_NO_STRINGSTREAM) + typedef std::strstream ss_t; +#else + typedef std::stringstream ss_t; +#endif + +void test_void_pointers_conversions() +{ + void *p_to_null = NULL; + const void *cp_to_data = "Some data"; + char nonconst_data[5]; + void *p_to_data = nonconst_data; + ss_t ss; + + ss << p_to_null; + BOOST_CHECK_EQUAL(boost::lexical_cast(p_to_null), ss.str()); + ss.str(std::string()); + + ss << cp_to_data; + BOOST_CHECK_EQUAL(boost::lexical_cast(cp_to_data), ss.str()); + ss.str(std::string()); + + ss << p_to_data; + BOOST_CHECK_EQUAL(boost::lexical_cast(p_to_data), ss.str()); + ss.str(std::string()); +} + +struct incomplete_type; + +void test_incomplete_type_pointers_conversions() +{ + incomplete_type *p_to_null = NULL; + const incomplete_type *cp_to_data = NULL; + char nonconst_data[5]; + incomplete_type *p_to_data = reinterpret_cast(nonconst_data); + ss_t ss; + + ss << p_to_null; + BOOST_CHECK_EQUAL(boost::lexical_cast(p_to_null), ss.str()); + ss.str(std::string()); + + ss << cp_to_data; + BOOST_CHECK_EQUAL(boost::lexical_cast(cp_to_data), ss.str()); + ss.str(std::string()); + + ss << p_to_data; + BOOST_CHECK_EQUAL(boost::lexical_cast(p_to_data), ss.str()); + ss.str(std::string()); +} + +struct ble; +typedef struct ble *meh; +std::ostream& operator <<(std::ostream &o, meh) { + o << "yay"; + return o; +} + +void test_inomplete_type_with_overloaded_ostream_op() { + meh heh = NULL; + ss_t ss; + ss << heh; + BOOST_CHECK_EQUAL(boost::lexical_cast(heh), ss.str()); +} + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast pinters test"); + suite->add(BOOST_TEST_CASE(&test_void_pointers_conversions)); + suite->add(BOOST_TEST_CASE(&test_incomplete_type_pointers_conversions)); + suite->add(BOOST_TEST_CASE(&test_inomplete_type_with_overloaded_ostream_op)); + return suite; +} From 9a6d983ee55ba495b3b5f9055fa86c7fd7018c6c Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 5 Jan 2012 12:09:15 +0000 Subject: [PATCH 10/28] Fixes #6193 [SVN r76318] --- include/boost/lexical_cast.hpp | 21 ++++++++++++++++----- lexical_cast_test.cpp | 11 +++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index cec2c50..0927e39 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -18,7 +18,7 @@ // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann, -// Cheng Yang, Matthew Bradbury and other Boosters +// Cheng Yang, Matthew Bradbury, David W. Birdsall and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2012 #include @@ -591,6 +591,7 @@ namespace boost value = *end - czero; --end; T multiplier = 1; + bool multiplier_overflowed = false; #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE std::locale loc; @@ -613,12 +614,17 @@ namespace boost for(;end>=begin; --end) { if (remained) { - T const new_sub_value = multiplier * 10 * (*end - czero); + T const multiplier_10 = multiplier * 10; + if (multiplier_10 / 10 != multiplier) multiplier_overflowed = true; + + T const dig_value = *end - czero; + T const new_sub_value = multiplier_10 * dig_value; if (*end < czero || *end >= czero + 10 /* detecting overflow */ - || new_sub_value/10 != multiplier * (*end - czero) + || (dig_value && new_sub_value / dig_value != multiplier_10) || static_cast((std::numeric_limits::max)()-new_sub_value) < value + || (multiplier_overflowed && dig_value) ) return false; @@ -656,12 +662,17 @@ namespace boost { while ( begin <= end ) { - T const new_sub_value = multiplier * 10 * (*end - czero); + T const multiplier_10 = multiplier * 10; + if (multiplier_10 / 10 != multiplier) multiplier_overflowed = true; + + T const dig_value = *end - czero; + T const new_sub_value = multiplier_10 * dig_value; if (*end < czero || *end >= czero + 10 /* detecting overflow */ - || new_sub_value/10 != multiplier * (*end - czero) + || (dig_value && new_sub_value / dig_value != multiplier_10) || static_cast((std::numeric_limits::max)()-new_sub_value) < value + || (multiplier_overflowed && dig_value) ) return false; diff --git a/lexical_cast_test.cpp b/lexical_cast_test.cpp index faeaa93..4e9ec90 100644 --- a/lexical_cast_test.cpp +++ b/lexical_cast_test.cpp @@ -796,6 +796,17 @@ void test_conversion_from_to_integral() BOOST_CHECK_THROW(lexical_cast("+-9"), bad_lexical_cast); // test_conversion_from_to_integral_for_locale + // Overflow test case from David W. Birdsall + std::string must_owerflow_str = "160000000000000000000"; + std::string must_owerflow_negative_str = "-160000000000000000000"; + for (int i = 0; i < 15; ++i) { + BOOST_CHECK_THROW(lexical_cast(must_owerflow_str), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(must_owerflow_negative_str), bad_lexical_cast); + + must_owerflow_str += '0'; + must_owerflow_negative_str += '0'; + } + typedef std::numpunct numpunct; restore_oldloc guard; From 7bdc45327a2a1f5cefe513752d7548ba370145c6 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sun, 8 Jan 2012 09:21:00 +0000 Subject: [PATCH 11/28] Tiny documentation update [SVN r76356] --- doc/lexical_cast.qbk | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/lexical_cast.qbk b/doc/lexical_cast.qbk index bf6b562..4715803 100644 --- a/doc/lexical_cast.qbk +++ b/doc/lexical_cast.qbk @@ -172,7 +172,8 @@ limitation of compiler options that you use. [section Changes] * [*boost 1.49.0 :] - * Restored work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio). + * Restored work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio). + * Better performance and less memory usage for `boost::container::basic_string` conversions. * [*boost 1.48.0 :] From 5ed7bfd280aad6e060952b184826aa644a09f2b8 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Fri, 20 Jan 2012 19:33:04 +0000 Subject: [PATCH 12/28] Attempt to fix #6251 [SVN r76599] --- include/boost/lexical_cast.hpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 0927e39..cd7d512 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -21,6 +21,32 @@ // Cheng Yang, Matthew Bradbury, David W. Birdsall and other Boosters // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2012 +#include +#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) +#define BOOST_LCAST_NO_WCHAR_T +#endif + +#if (defined(__MINGW32__) || defined(__MINGW64__)) && (__GNUC__ == 4) \ + && ((__GNUC_MINOR__ == 4) || (__GNUC_MINOR__ == 5)) && defined(__STRICT_ANSI__) \ + && !defined(BOOST_LCAST_NO_WCHAR_T) + +// workaround for a mingw bug +// http://sourceforge.net/tracker/index.php?func=detail&aid=2373234&group_id=2435&atid=102435 +#include <_mingw.h> +#if (__GNUC_MINOR__ == 4) +extern "C" { +_CRTIMP int __cdecl swprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , ...); +_CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , ...); +} +#endif +#if (__GNUC_MINOR__ == 5) +extern "C" { +_CRTIMP int __cdecl swprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , ...); +_CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restrict__ , va_list); +} +#endif +#endif + #include #include #include @@ -30,7 +56,6 @@ #include #include #include -#include #include #include #include @@ -67,10 +92,6 @@ #include #endif -#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) -#define BOOST_LCAST_NO_WCHAR_T -#endif - #ifdef BOOST_NO_TYPEID #define BOOST_LCAST_THROW_BAD_CAST(S, T) throw_exception(bad_lexical_cast()) #else From 84d9706182fd1ba05d3791f1a884ee58e76451e8 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 26 Jan 2012 17:47:56 +0000 Subject: [PATCH 13/28] Update tests with example from #6452 [SVN r76707] --- test/lexical_cast_empty_input_test.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/lexical_cast_empty_input_test.cpp b/test/lexical_cast_empty_input_test.cpp index 5a5881b..3b94f5f 100755 --- a/test/lexical_cast_empty_input_test.cpp +++ b/test/lexical_cast_empty_input_test.cpp @@ -138,6 +138,20 @@ void test_empty_vector() BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); } + +struct my_string { + friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) { + return sout << ""; + } +}; + +void test_empty_zero_terminated_string() +{ + my_string st; + std::string st2 = boost::lexical_cast(st); + (void)st2; +} + unit_test::test_suite *init_unit_test_suite(int, char *[]) { unit_test::test_suite *suite = @@ -146,6 +160,7 @@ unit_test::test_suite *init_unit_test_suite(int, char *[]) suite->add(BOOST_TEST_CASE(&test_empty_string)); suite->add(BOOST_TEST_CASE(&test_empty_user_class)); suite->add(BOOST_TEST_CASE(&test_empty_vector)); + suite->add(BOOST_TEST_CASE(&test_empty_zero_terminated_string)); return suite; } From efe20fcf765955a7a1ce8ff88ccbc15ce6186a64 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 26 Jan 2012 17:51:17 +0000 Subject: [PATCH 14/28] Tiny change to test case from #6452 [SVN r76708] --- test/lexical_cast_empty_input_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/lexical_cast_empty_input_test.cpp b/test/lexical_cast_empty_input_test.cpp index 3b94f5f..df05981 100755 --- a/test/lexical_cast_empty_input_test.cpp +++ b/test/lexical_cast_empty_input_test.cpp @@ -148,8 +148,7 @@ struct my_string { void test_empty_zero_terminated_string() { my_string st; - std::string st2 = boost::lexical_cast(st); - (void)st2; + BOOST_CHECK_EQUAL(boost::lexical_cast(st), std::string());; } unit_test::test_suite *init_unit_test_suite(int, char *[]) From a0cbfa8af636efc4ad2fc45cf5f751ac1e8f7d9a Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Tue, 31 Jan 2012 16:05:22 +0000 Subject: [PATCH 15/28] New test case from ticket #5793 [SVN r76809] --- test/lexical_cast_typedefed_wchar_test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/lexical_cast_typedefed_wchar_test.cpp b/test/lexical_cast_typedefed_wchar_test.cpp index 752c3e5..82f7cc9 100755 --- a/test/lexical_cast_typedefed_wchar_test.cpp +++ b/test/lexical_cast_typedefed_wchar_test.cpp @@ -13,12 +13,26 @@ #include #include +#include +#include + +void parseDate() +{ + std::locale locale; + boost::date_time::format_date_parser parser(L"", locale); + boost::date_time::special_values_parser svp; + + boost::gregorian::date date = parser.parse_date(L"", L"", svp); +} + + int main() { #ifdef BOOST_MSVC BOOST_STATIC_ASSERT((boost::is_same::value)); #endif + parseDate(); return ::boost::lexical_cast(L"1000") == 1000; } From 132663404bdaa634838d205b52da2eee226639eb Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 1 Feb 2012 04:22:21 +0000 Subject: [PATCH 16/28] Added tests for ticket #6453 [SVN r76817] --- test/lexical_cast_containers_test.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/lexical_cast_containers_test.cpp b/test/lexical_cast_containers_test.cpp index 5f98ac8..0c6315b 100644 --- a/test/lexical_cast_containers_test.cpp +++ b/test/lexical_cast_containers_test.cpp @@ -13,6 +13,7 @@ #include void testing_boost_containers_basic_string(); +void testing_boost_containers_string_std_string(); using namespace boost; @@ -21,6 +22,7 @@ boost::unit_test::test_suite *init_unit_test_suite(int, char *[]) unit_test::test_suite *suite = BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::container::string"); suite->add(BOOST_TEST_CASE(testing_boost_containers_basic_string)); + suite->add(BOOST_TEST_CASE(testing_boost_containers_string_std_string)); return suite; } @@ -35,4 +37,24 @@ void testing_boost_containers_basic_string() BOOST_CHECK(1000 == lexical_cast(str)); } +#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING) +#define BOOST_LCAST_NO_WCHAR_T +#endif +void testing_boost_containers_string_std_string() +{ + std::string std_str("std_str"); + boost::container::string boost_str("boost_str"); + BOOST_CHECK(boost::lexical_cast(boost_str) == "boost_str"); + BOOST_CHECK(boost::lexical_cast(std_str) == "std_str"); + +#ifndef BOOST_LCAST_NO_WCHAR_T + std::wstring std_wstr(L"std_wstr"); + boost::container::wstring boost_wstr(L"boost_wstr"); + + BOOST_CHECK(boost::lexical_cast(boost_wstr) == L"boost_wstr"); + BOOST_CHECK(boost::lexical_cast(std_wstr) == L"std_wstr"); + +#endif + +} From c9863cd254c667dfb635f348674f7fb379f6737b Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 1 Feb 2012 04:25:21 +0000 Subject: [PATCH 17/28] FIxes #6453 (compile error due to deduce_char_traits ambiguity) [SVN r76818] --- include/boost/lexical_cast.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index cd7d512..b69d5a0 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -324,6 +324,24 @@ namespace boost { typedef Traits type; }; + + template + struct deduce_char_traits< CharT + , ::boost::container::basic_string + , std::basic_string + > + { + typedef Traits type; + }; + + template + struct deduce_char_traits< CharT + , std::basic_string + , ::boost::container::basic_string + > + { + typedef Traits type; + }; #endif } From 7a48fea517180d332f3f45c33cb6cfd772f53937 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Wed, 1 Feb 2012 17:08:10 +0000 Subject: [PATCH 18/28] Disabling some optimisations for sun cc Bug described in #6462 [SVN r76824] --- include/boost/lexical_cast.hpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index b69d5a0..5a3d4f0 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -72,7 +72,9 @@ _CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restric #include #include #include +#if !defined(__SUNPRO_CC) #include +#endif // !defined(__SUNPRO_CC) #ifndef BOOST_NO_CWCHAR # include #endif @@ -170,11 +172,13 @@ namespace boost typedef CharT type; }; +#if !defined(__SUNPRO_CC) template struct stream_char< ::boost::container::basic_string > { typedef CharT type; }; +#endif // !defined(__SUNPRO_CC) #endif #ifndef BOOST_LCAST_NO_WCHAR_T @@ -289,6 +293,7 @@ namespace boost typedef Traits type; }; +#if !defined(__SUNPRO_CC) template struct deduce_char_traits< CharT , ::boost::container::basic_string @@ -342,6 +347,7 @@ namespace boost { typedef Traits type; }; +#endif // !defined(__SUNPRO_CC) #endif } @@ -1364,6 +1370,7 @@ namespace boost return true; } +#if !defined(__SUNPRO_CC) template bool operator<<(::boost::container::basic_string const& str) { @@ -1371,7 +1378,7 @@ namespace boost finish = start + str.length(); return true; } - +#endif // !defined(__SUNPRO_CC) bool operator<<(bool value) { CharT const czero = lcast_char_constants::zero; @@ -1588,9 +1595,10 @@ namespace boost #else template bool operator>>(std::basic_string& str) { str.assign(start, finish); return true; } - +#if !defined(__SUNPRO_CC) template bool operator>>(::boost::container::basic_string& str) { str.assign(start, finish); return true; } +#endif // !defined(__SUNPRO_CC) #endif /* * case "-0" || "0" || "+0" : output = false; return true; @@ -1726,13 +1734,13 @@ namespace boost { BOOST_STATIC_CONSTANT(bool, value = true ); }; - +#if !defined(__SUNPRO_CC) template struct is_stdstring< ::boost::container::basic_string > { BOOST_STATIC_CONSTANT(bool, value = true ); }; - +#endif // !defined(__SUNPRO_CC) template struct is_char_or_wchar { @@ -1832,7 +1840,7 @@ namespace boost { BOOST_STATIC_CONSTANT(bool, value = true ); }; - +#if !defined(__SUNPRO_CC) template struct is_char_array_to_stdstring< ::boost::container::basic_string, CharT* > { @@ -1844,6 +1852,7 @@ namespace boost { BOOST_STATIC_CONSTANT(bool, value = true ); }; +#endif // !defined(__SUNPRO_CC) #if (defined _MSC_VER) # pragma warning( push ) From 04f441e989ef073a69334ae313ee53c78724c348 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Tue, 7 Feb 2012 17:20:03 +0000 Subject: [PATCH 19/28] Commited sources of performance testing program and Jamfile.v2 to build and run it [SVN r76936] --- perf/Jamfile.v2 | 29 ++++ perf/performance_test.cpp | 309 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 338 insertions(+) create mode 100644 perf/Jamfile.v2 create mode 100644 perf/performance_test.cpp diff --git a/perf/Jamfile.v2 b/perf/Jamfile.v2 new file mode 100644 index 0000000..78176be --- /dev/null +++ b/perf/Jamfile.v2 @@ -0,0 +1,29 @@ +#============================================================================== +# Copyright (c) 2012 Antony Polukhin +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +#============================================================================== + +# performance tests +import testing ; +import path ; + +path-constant TEST_DIR : . ; + +project performance/test + : source-location ./ + : requirements +# /boost/chrono//boost_chrono +# /boost/system//boost_system + static + freebsd:"-lrt" + linux:"-lrt" + gcc:-fvisibility=hidden + intel-linux:-fvisibility=hidden + sun:-xldscope=hidden + : default-build release + ; + +run performance_test.cpp : $(TEST_DIR) ; + diff --git a/perf/performance_test.cpp b/perf/performance_test.cpp new file mode 100644 index 0000000..ff1eb1d --- /dev/null +++ b/perf/performance_test.cpp @@ -0,0 +1,309 @@ +// (C) Copyright Antony Polukhin 2012. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See http://www.boost.org/libs/config for most recent version. + +// +// Testing lexical_cast<> performance +// + +#define BOOST_ERROR_CODE_HEADER_ONLY +#define BOOST_CHRONO_HEADER_ONLY + +#include +#include +#include +#include + +// File to output data +std::fstream fout; + +template +static inline void test_lexical(const InT& in_val) { + OutT out_val = boost::lexical_cast(in_val); + (void)out_val; +} + +template +static inline void test_ss_constr(const InT& in_val) { + OutT out_val; + std::stringstream ss; + ss << in_val; + if (ss.fail()) throw std::logic_error("descr"); + ss >> out_val; + if (ss.fail()) throw std::logic_error("descr"); +} + +template +static inline void test_ss_noconstr(StringStreamT& ss, const InT& in_val) { + OutT out_val; + ss << in_val; // ss is an instance of std::stringstream + if (ss.fail()) throw std::logic_error("descr"); + ss >> out_val; + if (ss.fail()) throw std::logic_error("descr"); + /* reseting std::stringstream to use it again */ + ss.str(std::string()); + ss.clear(); +} + +struct structure_sprintf { + template + static inline void test(BufferT* buffer, const InT& in_val, const char* const conv) { + sprintf(buffer, conv, in_val); + OutT out_val(buffer); + } + + template + static inline void test(BufferT* buffer, const std::string& in_val, const char* const conv) { + sprintf(buffer, conv, in_val.c_str()); + OutT out_val(buffer); + } +}; + +struct structure_sscanf { + template + static inline void test(BufferT* /*buffer*/, const InT& in_val, const char* const conv) { + OutT out_val; + sscanf(reinterpret_cast(in_val), conv, &out_val); + } + + template + static inline void test(BufferT* /*buffer*/, const std::string& in_val, const char* const conv) { + OutT out_val; + sscanf(in_val.c_str(), conv, &out_val); + } +}; + +struct structure_fake { + template + static inline void test(BufferT* /*buffer*/, const InT& /*in_val*/, const char* const /*conv*/) {} +}; + +static const int fake_test_value = 9999; + +template +static inline void min_fancy_output(T v1, T v2, T v3, T v4) { + const char beg_mark[] = "!!! *"; + const char end_mark[] = "* !!!"; + const char no_mark[] = ""; + + unsigned int res = 4; + if (v1 < v2 && v1 < v3 && v1 < v4) res = 1; + if (v2 < v1 && v2 < v3 && v2 < v4) res = 2; + if (v3 < v1 && v3 < v2 && v3 < v4) res = 3; + + fout << "[ " + << (res == 1 ? beg_mark : no_mark) + ; + + if (v1) fout << v1; + else fout << "<1"; + + fout << (res == 1 ? end_mark : no_mark) + << " ][ " + << (res == 2 ? beg_mark : no_mark) + ; + + if (v2) fout << v2; + else fout << "<1"; + + fout << (res == 2 ? end_mark : no_mark) + << " ][ " + << (res == 3 ? beg_mark : no_mark) + ; + + if (v3) fout << v3; + else fout << "<1"; + + fout << (res == 3 ? end_mark : no_mark) + << " ][ " + << (res == 4 ? beg_mark : no_mark) + ; + + if (!v4) fout << "<1"; + else if (v4 == fake_test_value) fout << "---"; + else fout << v4; + + fout + << (res == 4 ? end_mark : no_mark) + << " ]"; +} + +template +static inline void perf_test_impl(const FromT& in_val, const char* const conv) { + + typedef boost::chrono::steady_clock test_clock; + test_clock::time_point start; + typedef boost::chrono::milliseconds duration_t; + duration_t lexical_cast_time, ss_constr_time, ss_noconstr_time, printf_time; + + start = test_clock::now(); + for (unsigned int i = 0; i < IetartionsCountV; ++i) { + test_lexical(in_val); + test_lexical(in_val); + test_lexical(in_val); + test_lexical(in_val); + } + lexical_cast_time = boost::chrono::duration_cast(test_clock::now() - start); + + + start = test_clock::now(); + for (unsigned int i = 0; i < IetartionsCountV; ++i) { + test_ss_constr(in_val); + test_ss_constr(in_val); + test_ss_constr(in_val); + test_ss_constr(in_val); + } + ss_constr_time = boost::chrono::duration_cast(test_clock::now() - start); + + std::stringstream ss; + start = test_clock::now(); + for (unsigned int i = 0; i < IetartionsCountV; ++i) { + test_ss_noconstr(ss, in_val); + test_ss_noconstr(ss, in_val); + test_ss_noconstr(ss, in_val); + test_ss_noconstr(ss, in_val); + } + ss_noconstr_time = boost::chrono::duration_cast(test_clock::now() - start); + + + char buffer[128]; + start = test_clock::now(); + for (unsigned int i = 0; i < IetartionsCountV; ++i) { + SprintfT::template test(buffer, in_val, conv); + SprintfT::template test(buffer, in_val, conv); + SprintfT::template test(buffer, in_val, conv); + SprintfT::template test(buffer, in_val, conv); + } + printf_time = boost::chrono::duration_cast(test_clock::now() - start); + + min_fancy_output( + lexical_cast_time.count(), + ss_constr_time.count(), + ss_noconstr_time.count(), + boost::is_same::value ? fake_test_value : printf_time.count() + ); +} + +template +static inline void perf_test(const std::string& test_name, const FromT& in_val, const char* const conv) { + const unsigned int ITERATIONSCOUNT = 100000; + fout << " [[ " << test_name << " ]"; + + perf_test_impl(in_val, conv); + + fout << "]\n"; +} + + +template +void string_like_test_set(const std::string& from) { + typedef structure_sscanf ssc_t; + ConverterT conv; + + perf_test(from + "->char", conv("c"), "%c"); + perf_test(from + "->signed char", conv("c"), "%hhd"); + perf_test(from + "->unsigned char", conv("c"), "%hhu"); + + perf_test(from + "->int", conv("100"), "%d"); + perf_test(from + "->short", conv("100"), "%hd"); + perf_test(from + "->long int", conv("100"), "%ld"); + perf_test(from + "->long long", conv("100"), "%lld"); + + perf_test(from + "->unsigned int", conv("100"), "%u"); + perf_test(from + "->unsigned short", conv("100"), "%hu"); + perf_test(from + "->unsigned long int", conv("100"), "%lu"); + perf_test(from + "->unsigned long long", conv("100"), "%llu"); + + // perf_test(from + "->bool", conv("1"), "%"); + + perf_test(from + "->float", conv("1.123"), "%f"); + perf_test(from + "->double", conv("1.123"), "%lf"); + perf_test(from + "->long double", conv("1.123"), "%Lf"); + + + perf_test(from + "->string", conv("string"), "%Lf"); + perf_test(from + "->container::string" + , conv("string"), "%Lf"); + +} + +struct to_string_conv { + std::string operator()(const char* const c) const { + return c; + } +}; + +struct to_char_conv { + const char* operator()(const char* const c) const { + return c; + } +}; + +struct to_uchar_conv { + const unsigned char* operator()(const char* const c) const { + return reinterpret_cast(c); + } +}; + + +struct to_schar_conv { + const signed char* operator()(const char* const c) const { + return reinterpret_cast(c); + } +}; + +int main(int argc, char** argv) { + BOOST_ASSERT(argc >= 2); + std::string output_path(argv[1]); + output_path += "/results.txt"; + fout.open(output_path.c_str(), std::fstream::in | std::fstream::out | std::fstream::app); + BOOST_ASSERT(fout); + + fout << "[section " << BOOST_COMPILER << "]\n" + << "[table:id Performance Table ( "<< BOOST_COMPILER << ")\n" + << "[[From->To] [lexical_cast] [std::stringstream with construction] " + << "[std::stringstream without construction][scanf/printf]]\n"; + + + // From std::string to ... + string_like_test_set("string"); + + // From ... to std::string + perf_test("string->char", 'c', "%c"); + perf_test("string->signed char", static_cast('c'), "%hhd"); + perf_test("string->unsigned char", static_cast('c'), "%hhu"); + + perf_test("int->string", 100, "%d"); + perf_test("short->string", static_cast(100), "%hd"); + perf_test("long int->string", 100l, "%ld"); + perf_test("long long->string", 100ll, "%lld"); + + perf_test("unsigned int->string", static_cast(100u), "%u"); + perf_test("unsigned short->string", 100u, "%hu"); + perf_test("unsigned long int->string", 100ul, "%lu"); + perf_test("unsigned long long->string", static_cast(100), "%llu"); + + // perf_test("bool->string", std::string("1"), "%"); + + perf_test("float->string", 1.123f, "%f"); + perf_test("double->string", 1.123, "%lf"); + perf_test("long double->string", 1.123L, "%Lf"); + + + string_like_test_set("char*"); + string_like_test_set("unsigned char*"); + string_like_test_set("signed char*"); + + perf_test("int->int", 100, ""); + perf_test("float->double", 100.0f, ""); + perf_test("char->signed char", 'c', ""); + + fout << "]\n" + << "[endsect]\n\n"; + return 0; +} + + From 646b958a92a559cb49211c5a6c179930f89fcc1e Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 16 Feb 2012 17:56:27 +0000 Subject: [PATCH 20/28] FIx #5689 (issue from 5th comment) [SVN r77039] --- include/boost/lexical_cast.hpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 5a3d4f0..be27890 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -732,6 +732,15 @@ namespace boost namespace detail { + template + bool lc_iequal(const CharT* val, const CharT* lcase, const CharT* ucase, unsigned int len) { + for( unsigned int i=0; i < len; ++i ) { + if ( val[i] != lcase[i] && val[i] != ucase[i] ) return false; + } + + return true; + } + /* Returns true and sets the correct value if found NaN or Inf. */ template inline bool parse_inf_nan_impl(const CharT* begin, const CharT* end, T& value @@ -755,7 +764,7 @@ namespace boost else if( *begin == plus ) ++begin; if( end-begin < 3 ) return false; - if( !memcmp(begin, lc_nan, 3*sizeof(CharT)) || !memcmp(begin, lc_NAN, 3*sizeof(CharT)) ) + if( lc_iequal(begin, lc_nan, lc_NAN, 3) ) { begin += 3; if (end != begin) /* It is 'nan(...)' or some bad input*/ @@ -772,13 +781,13 @@ namespace boost if (( /* 'INF' or 'inf' */ end-begin==3 && - (!memcmp(begin, lc_infinity, 3*sizeof(CharT)) || !memcmp(begin, lc_INFINITY, 3*sizeof(CharT))) + lc_iequal(begin, lc_infinity, lc_INFINITY, 3) ) || ( /* 'INFINITY' or 'infinity' */ end-begin==inifinity_size && - (!memcmp(begin, lc_infinity, inifinity_size)|| !memcmp(begin, lc_INFINITY, inifinity_size)) + lc_iequal(begin, lc_infinity, lc_INFINITY, inifinity_size) ) ) { From 80e858b182fc2cef5425e97d8e792f0cbfaaf01a Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Thu, 16 Feb 2012 17:57:52 +0000 Subject: [PATCH 21/28] More tests for fixed #5689 (issue mentiond in 5th comment) [SVN r77040] --- test/lexical_cast_inf_nan_test.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/lexical_cast_inf_nan_test.cpp b/test/lexical_cast_inf_nan_test.cpp index bb4331a..af1caa0 100755 --- a/test/lexical_cast_inf_nan_test.cpp +++ b/test/lexical_cast_inf_nan_test.cpp @@ -85,6 +85,12 @@ void test_inf_nan_templated() BOOST_CHECK( is_pos_inf( lexical_cast("+infinity") ) ); BOOST_CHECK( is_pos_inf( lexical_cast("+INFINITY") ) ); + BOOST_CHECK( is_pos_inf( lexical_cast("iNfiNity") ) ); + BOOST_CHECK( is_pos_inf( lexical_cast("INfinity") ) ); + + BOOST_CHECK( is_neg_inf( lexical_cast("-inFINITY") ) ); + BOOST_CHECK( is_neg_inf( lexical_cast("-INFINITY") ) ); + BOOST_CHECK( is_pos_nan( lexical_cast("nan") ) ); BOOST_CHECK( is_pos_nan( lexical_cast("NAN") ) ); @@ -94,6 +100,15 @@ void test_inf_nan_templated() BOOST_CHECK( is_pos_nan( lexical_cast("+nan") ) ); BOOST_CHECK( is_pos_nan( lexical_cast("+NAN") ) ); + BOOST_CHECK( is_pos_nan( lexical_cast("nAn") ) ); + BOOST_CHECK( is_pos_nan( lexical_cast("NaN") ) ); + + BOOST_CHECK( is_neg_nan( lexical_cast("-nAn") ) ); + BOOST_CHECK( is_neg_nan( lexical_cast("-NaN") ) ); + + BOOST_CHECK( is_pos_nan( lexical_cast("+Nan") ) ); + BOOST_CHECK( is_pos_nan( lexical_cast("+nAN") ) ); + BOOST_CHECK( is_pos_nan( lexical_cast("nan()") ) ); BOOST_CHECK( is_pos_nan( lexical_cast("NAN(some string)") ) ); BOOST_CHECK_THROW( lexical_cast("NAN(some string"), bad_lexical_cast ); @@ -127,6 +142,12 @@ void test_inf_nan_templated() BOOST_CHECK( is_pos_inf( lexical_cast(L"+infinity") ) ); BOOST_CHECK( is_pos_inf( lexical_cast(L"+INFINITY") ) ); + BOOST_CHECK( is_neg_inf( lexical_cast(L"-infINIty") ) ); + BOOST_CHECK( is_neg_inf( lexical_cast(L"-INFiniTY") ) ); + + BOOST_CHECK( is_pos_inf( lexical_cast(L"+inFINIty") ) ); + BOOST_CHECK( is_pos_inf( lexical_cast(L"+INfinITY") ) ); + BOOST_CHECK( is_pos_nan( lexical_cast(L"nan") ) ); BOOST_CHECK( is_pos_nan( lexical_cast(L"NAN") ) ); From d9baa71302b86b5d9eb2ec1c74e57a72abe1d75a Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Fri, 17 Feb 2012 18:51:05 +0000 Subject: [PATCH 22/28] Updated INF/NAN support for char16_t and char32_t [SVN r77058] --- include/boost/lexical_cast.hpp | 117 ++++++++++++++++----------------- 1 file changed, 55 insertions(+), 62 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index be27890..4658ab6 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -799,6 +799,41 @@ namespace boost return false; } + template + bool put_inf_nan_impl(CharT* begin, CharT*& end, const T& value + , const CharT* lc_nan + , const CharT* lc_infinity) + { + using namespace std; + const CharT minus = lcast_char_constants::minus; + if ( (boost::math::isnan)(value) ) + { + if ( (boost::math::signbit)(value) ) + { + *begin = minus; + ++ begin; + } + + memcpy(begin, lc_nan, 3 * sizeof(CharT)); + end = begin + 3; + return true; + } else if ( (boost::math::isinf)(value) ) + { + if ( (boost::math::signbit)(value) ) + { + *begin = minus; + ++ begin; + } + + memcpy(begin, lc_infinity, 3 * sizeof(CharT)); + end = begin + 3; + return true; + } + + return false; + } + + #ifndef BOOST_LCAST_NO_WCHAR_T template bool parse_inf_nan(const wchar_t* begin, const wchar_t* end, T& value) @@ -808,6 +843,13 @@ namespace boost , L"INFINITY", L"infinity" , L'(', L')'); } + + template + bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value) + { + return put_inf_nan_impl(begin, end, value, L"nan", L"infinity"); + } + #endif #ifndef BOOST_NO_CHAR16_T template @@ -818,6 +860,12 @@ namespace boost , u"INFINITY", u"infinity" , u'(', u')'); } + + template + bool put_inf_nan(char16_t* begin, char16_t*& end, const T& value) + { + return put_inf_nan_impl(begin, end, value, u"nan", u"infinity"); + } #endif #ifndef BOOST_NO_CHAR32_T template @@ -828,6 +876,12 @@ namespace boost , U"INFINITY", U"infinity" , U'(', U')'); } + + template + bool put_inf_nan(char32_t* begin, char32_t*& end, const T& value) + { + return put_inf_nan_impl(begin, end, value, U"nan", U"infinity"); + } #endif template @@ -838,73 +892,12 @@ namespace boost , "INFINITY", "infinity" , '(', ')'); } -#ifndef BOOST_LCAST_NO_WCHAR_T - template - bool put_inf_nan(wchar_t* begin, wchar_t*& end, const T& value) - { - using namespace std; - if ( (boost::math::isnan)(value) ) - { - if ( (boost::math::signbit)(value) ) - { - memcpy(begin,L"-nan", sizeof(L"-nan")); - end = begin + 4; - } else - { - memcpy(begin,L"nan", sizeof(L"nan")); - end = begin + 3; - } - return true; - } else if ( (boost::math::isinf)(value) ) - { - if ( (boost::math::signbit)(value) ) - { - memcpy(begin,L"-inf", sizeof(L"-inf")); - end = begin + 4; - } else - { - memcpy(begin,L"inf", sizeof(L"inf")); - end = begin + 3; - } - return true; - } - return false; - } -#endif template bool put_inf_nan(CharT* begin, CharT*& end, const T& value) { - using namespace std; - if ( (boost::math::isnan)(value) ) - { - if ( (boost::math::signbit)(value) ) - { - memcpy(begin,"-nan", sizeof("-nan")); - end = begin + 4; - } else - { - memcpy(begin,"nan", sizeof("nan")); - end = begin + 3; - } - return true; - } else if ( (boost::math::isinf)(value) ) - { - if ( (boost::math::signbit)(value) ) - { - memcpy(begin,"-inf", sizeof("-inf")); - end = begin + 4; - } else - { - memcpy(begin,"inf", sizeof("inf")); - end = begin + 3; - } - return true; - } - - return false; + return put_inf_nan_impl(begin, end, value, "nan", "infinity"); } - } From b71fb9aada4fa8aee2decf40b17a7f957d9bf4ff Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 20 Feb 2012 16:20:09 +0000 Subject: [PATCH 23/28] Fixes #6441 (compilation error with BOOST_NO_STD_LOCALE defined) [SVN r77077] --- include/boost/lexical_cast.hpp | 53 +++++-- test/Jamfile.v2 | 1 + test/lexical_cast_no_locale_test.cpp | 166 +++++++++++++++++++++ test/lexical_cast_typedefed_wchar_test.cpp | 1 + 4 files changed, 205 insertions(+), 16 deletions(-) create mode 100755 test/lexical_cast_no_locale_test.cpp diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 4658ab6..398fa25 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -1167,7 +1167,7 @@ namespace boost namespace detail { - struct do_not_construct_stringbuffer_t{}; + struct do_not_construct_out_stream_t{}; } namespace detail // optimized stream wrapper @@ -1179,25 +1179,27 @@ namespace boost > class lexical_stream_limited_src { - typedef stl_buf_unlocker, CharT > local_streambuffer_t; #if defined(BOOST_NO_STRINGSTREAM) - typedef stl_buf_unlocker local_stringbuffer_t; + typedef std::ostrstream out_stream_t; + typedef stl_buf_unlocker unlocked_but_t; #elif defined(BOOST_NO_STD_LOCALE) - typedef stl_buf_unlocker local_stringbuffer_t; + typedef std::ostringstream out_stream_t; + typedef stl_buf_unlocker unlocked_but_t; #else - typedef stl_buf_unlocker, CharT > local_stringbuffer_t; + typedef std::basic_ostringstream out_stream_t; + typedef stl_buf_unlocker, CharT> unlocked_but_t; #endif typedef BOOST_DEDUCED_TYPENAME ::boost::mpl::if_c< RequiresStringbuffer, - local_stringbuffer_t, - do_not_construct_stringbuffer_t - >::type deduced_stringbuffer_t; + out_stream_t, + do_not_construct_out_stream_t + >::type deduced_out_stream_t; // A string representation of Source is written to [start, finish). CharT* start; CharT* finish; - deduced_stringbuffer_t stringbuffer; + deduced_out_stream_t out_stream; public: lexical_stream_limited_src(CharT* sta, CharT* fin) @@ -1258,10 +1260,16 @@ namespace boost template bool shl_input_streamable(InputStreamable& input) { - std::basic_ostream stream(&stringbuffer); - bool const result = !(stream << input).fail(); - start = stringbuffer.pbase(); - finish = stringbuffer.pptr(); +#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE) + // If you have compilation error at this point, than your STL library + // unsupports such conversions. Try updating it. + BOOST_STATIC_ASSERT((boost::is_same::value)); +#endif + bool const result = !(out_stream << input).fail(); + const unlocked_but_t* const p + = static_cast(out_stream.rdbuf()) ; + start = p->pbase(); + finish = p->pptr(); return result; } @@ -1526,9 +1534,22 @@ namespace boost if(is_pointer::value) return false; - local_streambuffer_t bb; - bb.setg(start, start, finish); - std::basic_istream stream(&bb); +#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE) + // If you have compilation error at this point, than your STL library + // unsupports such conversions. Try updating it. + BOOST_STATIC_ASSERT((boost::is_same::value)); +#endif + +#if defined(BOOST_NO_STRINGSTREAM) + std::istrstream stream(start, finish - start); +#elif defined(BOOST_NO_STD_LOCALE) + std::istringstream stream; +#else + std::basic_istringstream stream; +#endif + static_cast(stream.rdbuf()) + ->setg(start, start, finish); + stream.unsetf(std::ios::skipws); lcast_set_precision(stream, static_cast(0)); #if (defined _MSC_VER) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index c311420..0618516 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -38,5 +38,6 @@ test-suite conversion [ run lexical_cast_pointers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] [ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on ] + [ run lexical_cast_no_locale_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_STD_LOCALE ] ; diff --git a/test/lexical_cast_no_locale_test.cpp b/test/lexical_cast_no_locale_test.cpp new file mode 100755 index 0000000..3404481 --- /dev/null +++ b/test/lexical_cast_no_locale_test.cpp @@ -0,0 +1,166 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 2012. +// +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). + +#include + +#if defined(__INTEL_COMPILER) +#pragma warning(disable: 193 383 488 981 1418 1419) +#elif defined(BOOST_MSVC) +#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) +#endif + +#include +#include +#include + +using namespace boost; + +// Testing compilation and some basic usage with BOOST_NO_STD_LOCALE +// Tests are mainly copyied from lexical_cast_empty_input_test.cpp (something +// new added to test_empty_3) + +#ifndef BOOST_NO_STD_LOCALE +#error "This test must be compiled with -DBOOST_NO_STD_LOCALE" +#endif + + +template +void do_test_on_empty_input(T& v) +{ + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +#if defined(BOOST_HAS_LONG_LONG) + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +#elif defined(BOOST_HAS_MS_INT64) + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast); +#endif +} + +void test_empty_1() +{ + boost::iterator_range v; + do_test_on_empty_input(v); + BOOST_CHECK_EQUAL(lexical_cast(v), std::string()); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + + boost::iterator_range cv; + do_test_on_empty_input(cv); + BOOST_CHECK_EQUAL(lexical_cast(cv), std::string()); + BOOST_CHECK_THROW(lexical_cast(cv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(cv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(cv), bad_lexical_cast); + + const boost::iterator_range ccv; + do_test_on_empty_input(ccv); + BOOST_CHECK_EQUAL(lexical_cast(ccv), std::string()); + BOOST_CHECK_THROW(lexical_cast(ccv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(ccv), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(ccv), bad_lexical_cast); +} + +void test_empty_2() +{ + std::string v; + do_test_on_empty_input(v); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +} + +struct Escape +{ + Escape(){} + Escape(const std::string& s) + : str_(s) + {} + + std::string str_; +}; + +inline std::ostream& operator<< (std::ostream& o, const Escape& rhs) +{ + return o << rhs.str_; +} + +inline std::istream& operator>> (std::istream& i, Escape& rhs) +{ + return i >> rhs.str_; +} + +void test_empty_3() +{ + Escape v(""); + do_test_on_empty_input(v); + + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + + v = lexical_cast(100); + BOOST_CHECK_EQUAL(lexical_cast(v), 100); + BOOST_CHECK_EQUAL(lexical_cast(v), 100); + + v = lexical_cast(0.0); + BOOST_CHECK_EQUAL(lexical_cast(v), 0.0); +} + +namespace std { +inline std::ostream & operator<<(std::ostream & out, const std::vector & v) +{ + std::ostream_iterator it(out); + std::copy(v.begin(), v.end(), it); + assert(out); + return out; +} +} + +void test_empty_4() +{ + std::vector v; + do_test_on_empty_input(v); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); + BOOST_CHECK_THROW(lexical_cast(v), bad_lexical_cast); +} + + +struct my_string { + friend std::ostream &operator<<(std::ostream& sout, my_string const&/* st*/) { + return sout << ""; + } +}; + +void test_empty_5() +{ + my_string st; + BOOST_CHECK_EQUAL(boost::lexical_cast(st), std::string());; +} + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast. Testing with BOOST_NO_STD_LOCALE"); + suite->add(BOOST_TEST_CASE(&test_empty_1)); + suite->add(BOOST_TEST_CASE(&test_empty_2)); + suite->add(BOOST_TEST_CASE(&test_empty_3)); + suite->add(BOOST_TEST_CASE(&test_empty_4)); + suite->add(BOOST_TEST_CASE(&test_empty_5)); + + return suite; +} + diff --git a/test/lexical_cast_typedefed_wchar_test.cpp b/test/lexical_cast_typedefed_wchar_test.cpp index 82f7cc9..2dc0098 100755 --- a/test/lexical_cast_typedefed_wchar_test.cpp +++ b/test/lexical_cast_typedefed_wchar_test.cpp @@ -23,6 +23,7 @@ void parseDate() boost::date_time::special_values_parser svp; boost::gregorian::date date = parser.parse_date(L"", L"", svp); + (void)date; } From 25d880f5ebc1e22ea0c85c86ac6f4a2358107f6d Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sat, 25 Feb 2012 16:58:46 +0000 Subject: [PATCH 24/28] Updates #5800 (partially fix lexical cast compilation with disabled exceptions) [SVN r77116] --- doc/lexical_cast.qbk | 7 +- include/boost/lexical_cast.hpp | 88 ++++++++++------------ test/Jamfile.v2 | 5 +- test/lexical_cast_no_exceptions_test.cpp | 95 ++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 50 deletions(-) create mode 100755 test/lexical_cast_no_exceptions_test.cpp diff --git a/doc/lexical_cast.qbk b/doc/lexical_cast.qbk index 4715803..5d45446 100644 --- a/doc/lexical_cast.qbk +++ b/doc/lexical_cast.qbk @@ -3,7 +3,7 @@ [version 1.0] [copyright 2000-2005 Kevlin Henney] [copyright 2006-2010 Alexander Nasonov] - [copyright 2011 Antony Polukhin] + [copyright 2011-2012 Antony Polukhin] [category String and text processing] [category Miscellaneous] [license @@ -170,6 +170,11 @@ limitation of compiler options that you use. [endsect] [section Changes] +* [*boost 1.50.0 :] + + * `boost::bad_lexical_cast` exception is now globaly visible and can be catched even if code is compiled with -fvisibility=hidden. + * Now it is possible to compile library with disabled exceptions. + * [*boost 1.49.0 :] * Restored work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio). diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 398fa25..a68152b 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -104,7 +104,7 @@ _CRTIMP int __cdecl vswprintf(wchar_t * __restrict__ , const wchar_t * __restric namespace boost { // exception used to indicate runtime lexical_cast failure - class bad_lexical_cast : + class BOOST_SYMBOL_VISIBLE bad_lexical_cast : // workaround MSVC bug with std::bad_cast when _HAS_EXCEPTIONS == 0 #if defined(BOOST_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS public std::exception @@ -1958,52 +1958,52 @@ namespace boost } }; - class precision_loss_error : public boost::numeric::bad_numeric_cast + template + struct detect_precision_loss { - public: - virtual const char * what() const throw() - { return "bad numeric conversion: precision loss error"; } - }; + typedef boost::numeric::Trunc Rounder; + typedef Source source_type ; - template - struct throw_on_precision_loss - { - typedef boost::numeric::Trunc Rounder; - typedef S source_type ; - - typedef typename mpl::if_< is_arithmetic,S,S const&>::type argument_type ; + typedef BOOST_DEDUCED_TYPENAME mpl::if_< + is_arithmetic, Source, Source const& + >::type argument_type ; static source_type nearbyint ( argument_type s ) { - source_type orig_div_round = s / Rounder::nearbyint(s); + const source_type orig_div_round = s / Rounder::nearbyint(s); + const source_type eps = std::numeric_limits::epsilon(); + + if ((orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > eps) + BOOST_LCAST_THROW_BAD_CAST(Source, Target); - if ( (orig_div_round > 1 ? orig_div_round - 1 : 1 - orig_div_round) > std::numeric_limits::epsilon() ) - BOOST_THROW_EXCEPTION( precision_loss_error() ); return s ; } typedef typename Rounder::round_style round_style; } ; + template + struct nothrow_overflow_handler + { + void operator() ( boost::numeric::range_check_result r ) + { + if (r != boost::numeric::cInRange) + BOOST_LCAST_THROW_BAD_CAST(Source, Target); + } + } ; + template struct lexical_cast_dynamic_num_not_ignoring_minus { static inline Target lexical_cast_impl(const Source &arg) { - try{ - typedef boost::numeric::converter< - Target, - Source, - boost::numeric::conversion_traits, - boost::numeric::def_overflow_handler, - throw_on_precision_loss - > Converter ; - - return Converter::convert(arg); - } catch( ::boost::numeric::bad_numeric_cast const& ) { - BOOST_LCAST_THROW_BAD_CAST(Source, Target); - } - BOOST_UNREACHABLE_RETURN(static_cast(0)); + return boost::numeric::converter< + Target, + Source, + boost::numeric::conversion_traits, + nothrow_overflow_handler, + detect_precision_loss + >::convert(arg); } }; @@ -2012,25 +2012,17 @@ namespace boost { static inline Target lexical_cast_impl(const Source &arg) { - try{ - typedef boost::numeric::converter< - Target, - Source, - boost::numeric::conversion_traits, - boost::numeric::def_overflow_handler, - throw_on_precision_loss - > Converter ; + typedef boost::numeric::converter< + Target, + Source, + boost::numeric::conversion_traits, + nothrow_overflow_handler, + detect_precision_loss + > converter_t; - bool has_minus = ( arg < 0); - if ( has_minus ) { - return static_cast(-Converter::convert(-arg)); - } else { - return Converter::convert(arg); - } - } catch( ::boost::numeric::bad_numeric_cast const& ) { - BOOST_LCAST_THROW_BAD_CAST(Source, Target); - } - BOOST_UNREACHABLE_RETURN(static_cast(0)); + return ( + arg < 0 ? -converter_t::convert(-arg) : converter_t::convert(arg) + ); } }; diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 0618516..5078436 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -39,5 +39,8 @@ test-suite conversion [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] [ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on ] [ run lexical_cast_no_locale_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_STD_LOCALE ] + [ run lexical_cast_no_exceptions_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_EXCEPTIONS ] ; - +#Add missing flags, when numeric_cast compilation with exceptions disabled will be fixed: +# [ run lexical_cast_no_exceptions_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_EXCEPTIONS gcc:-fno-exceptions ] + diff --git a/test/lexical_cast_no_exceptions_test.cpp b/test/lexical_cast_no_exceptions_test.cpp new file mode 100755 index 0000000..8431c3a --- /dev/null +++ b/test/lexical_cast_no_exceptions_test.cpp @@ -0,0 +1,95 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Antony Polukhin, 2012. +// +// Distributed under the Boost +// Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). + +#include + +#if defined(__INTEL_COMPILER) +#pragma warning(disable: 193 383 488 981 1418 1419) +#elif defined(BOOST_MSVC) +#pragma warning(disable: 4097 4100 4121 4127 4146 4244 4245 4511 4512 4701 4800) +#endif + +#include +#include +#include + +#ifndef BOOST_NO_EXCEPTIONS +#error "This test must be compiled with -DBOOST_NO_EXCEPTIONS" +#endif + +bool g_was_exception = false; + +namespace boost { + +void throw_exception(std::exception const & ) { + g_was_exception = true; +} + +} + +using namespace boost; + + +struct Escape +{ + Escape(){} + Escape(const std::string& s) + : str_(s) + {} + + std::string str_; +}; + +inline std::ostream& operator<< (std::ostream& o, const Escape& rhs) +{ + return o << rhs.str_; +} + +inline std::istream& operator>> (std::istream& i, Escape& rhs) +{ + return i >> rhs.str_; +} + +void test_exceptions_off() +{ + Escape v(""); + + g_was_exception = false; + lexical_cast(v); + BOOST_CHECK(g_was_exception); + + g_was_exception = false; + lexical_cast(v); + BOOST_CHECK(g_was_exception); + + v = lexical_cast(100); + BOOST_CHECK_EQUAL(lexical_cast(v), 100); + BOOST_CHECK_EQUAL(lexical_cast(v), 100u); + + v = lexical_cast(0.0); + BOOST_CHECK_EQUAL(lexical_cast(v), 0.0); + + BOOST_CHECK_EQUAL(lexical_cast(100), 100); + BOOST_CHECK_EQUAL(lexical_cast(0.0), 0.0); + + g_was_exception = false; + lexical_cast(700000); + BOOST_CHECK(g_was_exception); +} + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast. Testing with BOOST_NO_EXCEPTIONS"); + suite->add(BOOST_TEST_CASE(&test_exceptions_off)); + + return suite; +} + From 0dded6d3a7d5dd313cb04f7717b6e453f5675799 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sat, 25 Feb 2012 17:32:26 +0000 Subject: [PATCH 25/28] Update for fixed #6441 (unable to compile lexical_cast with BOOST_NO_STD_LOCALE defined) Now test must compile under VC, removed some warnings. [SVN r77117] --- test/Jamfile.v2 | 2 +- test/lexical_cast_no_locale_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 5078436..54c1e42 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -38,7 +38,7 @@ test-suite conversion [ run lexical_cast_pointers_test.cpp ../../test/build//boost_unit_test_framework/static ] [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] [ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on ] - [ run lexical_cast_no_locale_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_STD_LOCALE ] + [ run lexical_cast_no_locale_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_STD_LOCALE BOOST_LEXICAL_CAST_ASSUME_C_LOCALE ] [ run lexical_cast_no_exceptions_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_EXCEPTIONS ] ; #Add missing flags, when numeric_cast compilation with exceptions disabled will be fixed: diff --git a/test/lexical_cast_no_locale_test.cpp b/test/lexical_cast_no_locale_test.cpp index 3404481..f3defb3 100755 --- a/test/lexical_cast_no_locale_test.cpp +++ b/test/lexical_cast_no_locale_test.cpp @@ -113,7 +113,7 @@ void test_empty_3() v = lexical_cast(100); BOOST_CHECK_EQUAL(lexical_cast(v), 100); - BOOST_CHECK_EQUAL(lexical_cast(v), 100); + BOOST_CHECK_EQUAL(lexical_cast(v), 100u); v = lexical_cast(0.0); BOOST_CHECK_EQUAL(lexical_cast(v), 0.0); From 83639bd9aee73ef88ea9c9aba075ba427287ce99 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sun, 26 Feb 2012 09:19:55 +0000 Subject: [PATCH 26/28] Fixes #6571 (documentation update, added 'why lexical_cast does not understand "-1.#IND"' question and answer to it) [SVN r77118] --- doc/lexical_cast.qbk | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/lexical_cast.qbk b/doc/lexical_cast.qbk index 5d45446..4ad8336 100644 --- a/doc/lexical_cast.qbk +++ b/doc/lexical_cast.qbk @@ -167,6 +167,14 @@ if a negative number is read, no errors will arise and the result will be the tw `boost::lexical_cast` sees single `wchar_t` character as `unsigned short`. It is not a `boost::lexical_cast` mistake, but a limitation of compiler options that you use. +[pre +] + +* [*Question:] Why `boost::lexical_cast("-1.#IND");` throws `boost::bad_lexical_cast`? + * [*Answer:] `"-1.#IND"` is a compiler extension, that violates standard. You shall input `"-nan"`, `"nan"`, `"inf"` +, `"-inf"` (case insensitive) strings to get NaN and Inf values. `boost::lexical_cast` outputs `"-nan"`, `"nan"`, +`"inf"`, `"-inf"` strings, when has NaN or Inf input values. + [endsect] [section Changes] From 7e7f494dd12230982c1082c350eac29c3649037c Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sun, 4 Mar 2012 17:59:03 +0000 Subject: [PATCH 27/28] Attempt to supress warning described in #6645 (implicit conversion shortens 64-bit value into a 32-bit value) [SVN r77221] --- include/boost/lexical_cast.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index a68152b..b73c741 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -953,7 +953,7 @@ namespace boost CharT const capital_e = lcast_char_constants::capital_e; CharT const lowercase_e = lcast_char_constants::lowercase_e; - value = 0.0; + value = static_cast(0); if (parse_inf_nan(begin, end, value)) return true; From 676bde6e1f3b9e658fd7890b13f203624c14fce5 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Mon, 5 Mar 2012 18:44:31 +0000 Subject: [PATCH 28/28] More strict tests for disabled exceptions (for fixed #5800) [SVN r77236] --- test/Jamfile.v2 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 54c1e42..019c9e7 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -39,8 +39,11 @@ test-suite conversion [ compile lexical_cast_typedefed_wchar_test.cpp : msvc:on ] [ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/static : : : msvc:on ] [ run lexical_cast_no_locale_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_STD_LOCALE BOOST_LEXICAL_CAST_ASSUME_C_LOCALE ] - [ run lexical_cast_no_exceptions_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_EXCEPTIONS ] - ; -#Add missing flags, when numeric_cast compilation with exceptions disabled will be fixed: -# [ run lexical_cast_no_exceptions_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_EXCEPTIONS gcc:-fno-exceptions ] + [ run lexical_cast_no_exceptions_test.cpp ../../test/build//boost_unit_test_framework/static : : : BOOST_NO_EXCEPTIONS + gcc-4.3:-fno-exceptions + gcc-4.4:-fno-exceptions + gcc-4.5:-fno-exceptions + gcc-4.6:-fno-exceptions + ] + ;