From 74fc01da7e20f21b3e6af3651ab03cc1e736e913 Mon Sep 17 00:00:00 2001 From: nobody Date: Tue, 21 Mar 2006 02:26:31 +0000 Subject: [PATCH 01/16] This commit was manufactured by cvs2svn to create branch 'RC_1_34_0'. [SVN r33417] From 06e7d986d297f1affe58bcd0e5e6a8c3c69808d0 Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Mon, 17 Jul 2006 03:08:48 +0000 Subject: [PATCH 02/16] fixed library name, copyright notice, removed a tab char and a space-only line; NOTE: is the copyright notice autogenerated? [SVN r34573] --- test/Jamfile | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/test/Jamfile b/test/Jamfile index 8af8d65..b0c960c 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -1,17 +1,10 @@ -# Signals library +# Conversion library # Copyright (C) 2001-2003 Douglas Gregor -# Permission to copy, use, sell and distribute this software is granted -# provided this copyright notice appears in all copies. Permission to modify -# the code and to distribute modified code is granted provided this copyright -# notice appears in all copies, and a notice that the code was modified is -# included with the copyright notice. This software is provided "as is" -# without express or implied warranty, and with no claim as to its suitability -# for any purpose. - -# For more information, see http://www.boost.org/ - +# 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) # Testing Jamfile autogenerated from XML source subproject libs/conversion/test ; @@ -27,8 +20,8 @@ DEPENDS all : test ; : [ run implicit_cast.cpp ] [ compile-fail implicit_cast_fail.cpp ] [ run ../cast_test.cpp ] - [ run ../numeric_cast_test.cpp ] + [ run ../numeric_cast_test.cpp ] [ run ../lexical_cast_test.cpp ../../test/build/boost_unit_test_framework ] ; } - + From abf3aa23de68b44e8437d6f5eac74400afee5d05 Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Mon, 17 Jul 2006 04:17:11 +0000 Subject: [PATCH 03/16] removed erroneous macro version [SVN r34578] --- include/boost/implicit_cast.hpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/include/boost/implicit_cast.hpp b/include/boost/implicit_cast.hpp index 2593cb4..7043085 100755 --- a/include/boost/implicit_cast.hpp +++ b/include/boost/implicit_cast.hpp @@ -23,12 +23,6 @@ inline T implicit_cast (typename mpl::identity::type x) { //template //void implicit_cast (...); -// Macro for when you need a constant expression (Gennaro Prota) -#define BOOST_IMPLICIT_CAST(dst_type, expr) \ - ( sizeof( implicit_cast(expr) ) \ - , \ - static_cast(expr) \ - ) } // namespace boost From 78f1acc5ff51e34216cf9105e63b88b0fbfd2338 Mon Sep 17 00:00:00 2001 From: Rene Rivera Date: Mon, 6 Nov 2006 17:10:46 +0000 Subject: [PATCH 04/16] Remove obsolete Boost.Build v1 files. [SVN r35880] --- test/Jamfile | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 test/Jamfile diff --git a/test/Jamfile b/test/Jamfile deleted file mode 100644 index b0c960c..0000000 --- a/test/Jamfile +++ /dev/null @@ -1,27 +0,0 @@ -# Conversion library - -# Copyright (C) 2001-2003 Douglas Gregor - -# 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) - -# Testing Jamfile autogenerated from XML source -subproject libs/conversion/test ; - -# bring in rules for testing -import testing ; - -# Make tests run by default. -DEPENDS all : test ; - -{ - test-suite conversion - : [ run implicit_cast.cpp ] - [ compile-fail implicit_cast_fail.cpp ] - [ run ../cast_test.cpp ] - [ run ../numeric_cast_test.cpp ] - [ run ../lexical_cast_test.cpp ../../test/build/boost_unit_test_framework ] - ; -} - From bfeb60c400c09da36114c07698fe634c811df377 Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Fri, 10 Nov 2006 19:59:52 +0000 Subject: [PATCH 05/16] Merge from HEAD. Allow building of shared versions of some Boost.Test libraries. Adjust tests to use always use static linking to Boost.Test, since linking to the shared version requires test changes. Patch from Juergen Hunold. [SVN r35990] --- test/Jamfile.v2 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 58ae133..0e59404 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -20,7 +20,8 @@ test-suite conversion [ compile-fail implicit_cast_fail.cpp ] [ run ../cast_test.cpp ] [ run ../numeric_cast_test.cpp ] - [ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework ] + [ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_loopback_test.cpp ../../test/build//boost_unit_test_framework/static ] ; - \ No newline at end of file + From 37d07617d13154d9ab8281a027686441fb1ad553 Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Fri, 10 Nov 2006 21:03:16 +0000 Subject: [PATCH 06/16] Fix [SVN r35993] --- test/Jamfile.v2 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 0e59404..56a03d8 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -21,7 +21,6 @@ test-suite conversion [ run ../cast_test.cpp ] [ run ../numeric_cast_test.cpp ] [ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework/static ] - [ run lexical_cast_loopback_test.cpp ../../test/build//boost_unit_test_framework/static ] ; From d6eea4ef813d7df541f522c00a90b3f2dbe94392 Mon Sep 17 00:00:00 2001 From: nobody Date: Tue, 24 Jul 2007 19:28:14 +0000 Subject: [PATCH 07/16] This commit was manufactured by cvs2svn to create tag 'Version_1_34_1'. [SVN r38286] From 4224fa5deb2709fd14239fef634a75ba081b4cbc Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 5 Oct 2007 14:25:06 +0000 Subject: [PATCH 08/16] Starting point for releases [SVN r39706] From ac6e41d676328940dbe656cc47f8b6affd8b95f4 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Fri, 23 Nov 2007 17:03:14 +0000 Subject: [PATCH 09/16] config, detail, filesystem, system, tools, at 41278. [SVN r41316] --- include/boost/detail/lcast_precision.hpp | 184 +++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 include/boost/detail/lcast_precision.hpp diff --git a/include/boost/detail/lcast_precision.hpp b/include/boost/detail/lcast_precision.hpp new file mode 100644 index 0000000..d40ca21 --- /dev/null +++ b/include/boost/detail/lcast_precision.hpp @@ -0,0 +1,184 @@ +// Copyright Alexander Nasonov & Paul A. Bristow 2006. + +// 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) + +#ifndef BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED +#define BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED + +#include +#include +#include + +#include +#include + +#ifndef BOOST_NO_IS_ABSTRACT +// Fix for SF:1358600 - lexical_cast & pure virtual functions & VC 8 STL +#include +#include +#endif + +#if defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS) || \ + (defined(BOOST_MSVC) && (BOOST_MSVC<1310)) + +#define BOOST_LCAST_NO_COMPILE_TIME_PRECISION +#endif + +#ifdef BOOST_LCAST_NO_COMPILE_TIME_PRECISION +#include +#else +#include +#endif + +namespace boost { namespace detail { + +class lcast_abstract_stub {}; + +#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION +// Calculate an argument to pass to std::ios_base::precision from +// lexical_cast. See alternative implementation for broken standard +// libraries in lcast_get_precision below. Keep them in sync, please. +template +struct lcast_precision +{ +#ifdef BOOST_NO_IS_ABSTRACT + typedef std::numeric_limits limits; // No fix for SF:1358600. +#else + typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_< + boost::is_abstract + , std::numeric_limits + , std::numeric_limits + >::type limits; +#endif + + BOOST_STATIC_CONSTANT(bool, use_default_precision = + !limits::is_specialized || limits::is_exact + ); + + BOOST_STATIC_CONSTANT(bool, is_specialized_bin = + !use_default_precision && + limits::radix == 2 && limits::digits > 0 + ); + + BOOST_STATIC_CONSTANT(bool, is_specialized_dec = + !use_default_precision && + limits::radix == 10 && limits::digits10 > 0 + ); + + BOOST_STATIC_CONSTANT(std::streamsize, streamsize_max = + boost::integer_traits::const_max + ); + + BOOST_STATIC_CONSTANT(unsigned int, precision_dec = limits::digits10 + 1U); + + BOOST_STATIC_ASSERT(!is_specialized_dec || + precision_dec <= streamsize_max + 0UL + ); + + BOOST_STATIC_CONSTANT(unsigned long, precision_bin = + 2UL + limits::digits * 30103UL / 100000UL + ); + + BOOST_STATIC_ASSERT(!is_specialized_bin || + (limits::digits + 0UL < ULONG_MAX / 30103UL && + precision_bin > limits::digits10 + 0UL && + precision_bin <= streamsize_max + 0UL) + ); + + BOOST_STATIC_CONSTANT(std::streamsize, value = + is_specialized_bin ? precision_bin + : is_specialized_dec ? precision_dec : 6 + ); +}; +#endif + +template +inline std::streamsize lcast_get_precision(T* = 0) +{ +#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION + return lcast_precision::value; +#else // Follow lcast_precision algorithm at run-time: + +#ifdef BOOST_NO_IS_ABSTRACT + typedef std::numeric_limits limits; // No fix for SF:1358600. +#else + typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_< + boost::is_abstract + , std::numeric_limits + , std::numeric_limits + >::type limits; +#endif + + bool const use_default_precision = + !limits::is_specialized || limits::is_exact; + + if(!use_default_precision) + { // Includes all built-in floating-point types, float, double ... + // and UDT types for which digits (significand bits) is defined (not zero) + + bool const is_specialized_bin = + limits::radix == 2 && limits::digits > 0; + bool const is_specialized_dec = + limits::radix == 10 && limits::digits10 > 0; + std::streamsize const streamsize_max = + (boost::integer_traits::max)(); + + if(is_specialized_bin) + { // Floating-point types with + // limits::digits defined by the specialization. + + unsigned long const digits = limits::digits; + unsigned long const precision = 2UL + digits * 30103UL / 100000UL; + // unsigned long is selected because it is at least 32-bits + // and thus ULONG_MAX / 30103UL is big enough for all types. + BOOST_ASSERT( + digits < ULONG_MAX / 30103UL && + precision > limits::digits10 + 0UL && + precision <= streamsize_max + 0UL + ); + return precision; + } + else if(is_specialized_dec) + { // Decimal Floating-point type, most likely a User Defined Type + // rather than a real floating-point hardware type. + unsigned int const precision = limits::digits10 + 1U; + BOOST_ASSERT(precision <= streamsize_max + 0UL); + return precision; + } + } + + // Integral type (for which precision has no effect) + // or type T for which limits is NOT specialized, + // so assume stream precision remains the default 6 decimal digits. + // Warning: if your User-defined Floating-point type T is NOT specialized, + // then you may lose accuracy by only using 6 decimal digits. + // To avoid this, you need to specialize T with either + // radix == 2 and digits == the number of significand bits, + // OR + // radix = 10 and digits10 == the number of decimal digits. + + return 6; +#endif +} + +template +inline void lcast_set_precision(std::ios_base& stream, T*) +{ + stream.precision(lcast_get_precision()); +} + +template +inline void lcast_set_precision(std::ios_base& stream, Source*, Target*) +{ + std::streamsize const s = lcast_get_precision((Source*)0); + std::streamsize const t = lcast_get_precision((Target*)0); + stream.precision(s > t ? s : t); +} + +}} + +#endif // BOOST_DETAIL_LCAST_PRECISION_HPP_INCLUDED + From d44a6fcf8bace4216b57ddf61951952a44268c3f Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sun, 25 Nov 2007 18:07:19 +0000 Subject: [PATCH 10/16] Full merge from trunk at revision 41356 of entire boost-root tree. [SVN r41369] --- include/boost/implicit_cast.hpp | 2 +- include/boost/lexical_cast.hpp | 902 +++++++++++++++++++++++++++++++- 2 files changed, 876 insertions(+), 28 deletions(-) diff --git a/include/boost/implicit_cast.hpp b/include/boost/implicit_cast.hpp index 7043085..5b1cd92 100755 --- a/include/boost/implicit_cast.hpp +++ b/include/boost/implicit_cast.hpp @@ -23,7 +23,7 @@ inline T implicit_cast (typename mpl::identity::type x) { //template //void implicit_cast (...); - } // namespace boost + #endif // IMPLICIT_CAST_DWA200356_HPP diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 926b95e..754aecf 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -3,24 +3,31 @@ // Boost lexical_cast.hpp header -------------------------------------------// // -// See http://www.boost.org for most recent version including documentation. +// See http://www.boost.org/libs/converston for documentation. // See end of this header for rights and permissions. // // what: lexical_cast custom keyword cast // who: contributed by Kevlin Henney, -// enhanced with contributions from Terje Slettebų, +// enhanced with contributions from Terje Slettebo, // with additional fixes and suggestions from Gennaro Prota, // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov, -// and other Boosters -// when: November 2000, March 2003, June 2005 +// Alexander Nasonov and other Boosters +// when: November 2000, March 2003, June 2005, June 2006 +#include #include +#include +#include #include #include #include #include +#include #include #include +#include +#include +#include #ifdef BOOST_NO_STRINGSTREAM #include @@ -45,9 +52,9 @@ namespace boost { } bad_lexical_cast( - const std::type_info &source_type, - const std::type_info &target_type) : - source(&source_type), target(&target_type) + const std::type_info &source_type_arg, + const std::type_info &target_type_arg) : + source(&source_type_arg), target(&target_type_arg) { } const std::type_info &source_type() const @@ -79,8 +86,8 @@ namespace boost typedef char type; }; - #ifndef DISABLE_WIDE_CHAR_SUPPORT -#if !defined(BOOST_NO_INTRINSIC_WCHAR_T) +#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_NO_INTRINSIC_WCHAR_T template<> struct stream_char { @@ -105,7 +112,7 @@ namespace boost { typedef wchar_t type; }; - #endif +#endif template struct widest_char @@ -119,7 +126,393 @@ namespace boost typedef wchar_t type; }; } - + + namespace detail // lcast_src_length + { + // Return max. length of string representation of Source; + // 0 if unlimited (with exceptions for some types, see below). + // Values with limited string representation are placed to + // the buffer locally defined in lexical_cast function. + // 1 is returned for few types such as CharT const* or + // std::basic_string that already have an internal + // buffer ready to be reused by lexical_stream_limited_src. + // Each specialization should have a correspondent operator<< + // defined in lexical_stream_limited_src. + template< class CharT // A result of widest_char transformation. + , class Source // Source type of lexical_cast. + > + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 0); + // To check coverage, build the test with + // bjam --v2 profile optimization=off + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + + // No specializations for: + // lcast_src_length + // lcast_src_length + // lcast_src_length + // lcast_src_length + // lcast_src_length + // lcast_src_length + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + +#ifndef BOOST_NO_INTRINSIC_WCHAR_T + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; +#endif +#endif + + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; +#endif + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + template + struct lcast_src_length< CharT, std::basic_string > + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; +#else + template<> + struct lcast_src_length< char, std::basic_string > + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template<> + struct lcast_src_length< wchar_t, std::basic_string > + { + BOOST_STATIC_CONSTANT(std::size_t, value = 1); + static void check_coverage() {} + }; +#endif +#endif + + // Helper for integral types. + // Notes on length calculation: + // Max length for 32bit int with grouping "\1" and thousands_sep ',': + // "-2,1,4,7,4,8,3,6,4,7" + // ^ - is_signed + // ^ - 1 digit not counted by digits10 + // ^^^^^^^^^^^^^^^^^^ - digits10 * 2 + // + // Constant is_specialized is used instead of constant 1 + // to prevent buffer overflow in a rare case when + // doesn't add missing specialization for + // numeric_limits for some integral type T. + // When is_specialized is false, the whole expression is 0. + template + struct lcast_src_length_integral + { +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS + BOOST_STATIC_CONSTANT(std::size_t, value = + std::numeric_limits::is_signed + + std::numeric_limits::is_specialized + // == 1 + std::numeric_limits::digits10 * 2 + ); +#else + BOOST_STATIC_CONSTANT(std::size_t, value = 156); + BOOST_STATIC_ASSERT(sizeof(Source) * CHAR_BIT <= 256); +#endif + }; + +#define BOOST_AUX_LEXICAL_CAST_DEF1(CharT, T) template<> \ + struct lcast_src_length : lcast_src_length_integral \ + { static void check_coverage() {} }; + +#ifdef DISABLE_WIDE_CHAR_SUPPORT +#define BOOST_AUX_LEXICAL_CAST_DEF(T) BOOST_AUX_LEXICAL_CAST_DEF1(char, T) +#else +#define BOOST_AUX_LEXICAL_CAST_DEF(T) \ + BOOST_AUX_LEXICAL_CAST_DEF1(char, T) \ + BOOST_AUX_LEXICAL_CAST_DEF1(wchar_t, T) +#endif + + BOOST_AUX_LEXICAL_CAST_DEF(short) + BOOST_AUX_LEXICAL_CAST_DEF(unsigned short) + BOOST_AUX_LEXICAL_CAST_DEF(int) + BOOST_AUX_LEXICAL_CAST_DEF(unsigned int) + BOOST_AUX_LEXICAL_CAST_DEF(long) + BOOST_AUX_LEXICAL_CAST_DEF(unsigned long) +#if defined(BOOST_HAS_LONG_LONG) + BOOST_AUX_LEXICAL_CAST_DEF(boost::ulong_long_type) + BOOST_AUX_LEXICAL_CAST_DEF(boost::long_long_type ) +#elif defined(BOOST_HAS_MS_INT64) + BOOST_AUX_LEXICAL_CAST_DEF(unsigned __int64) + BOOST_AUX_LEXICAL_CAST_DEF( __int64) +#endif + +#undef BOOST_AUX_LEXICAL_CAST_DEF +#undef BOOST_AUX_LEXICAL_CAST_DEF1 + +#ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION +// This #if is in sync with lcast_precision + + // Helper for floating point types. + // -1.23456789e-123456 + // ^ sign + // ^ leading digit + // ^ decimal point + // ^^^^^^^^ lcast_precision::value + // ^ "e" + // ^ exponent sign + // ^^^^^^ exponent (assumed 6 or less digits) + // sign + leading digit + decimal point + "e" + exponent sign == 5 + template + struct lcast_src_length_floating + { + BOOST_STATIC_ASSERT( + std::numeric_limits::max_exponent10 <= 999999L && + std::numeric_limits::min_exponent10 >= -999999L + ); + BOOST_STATIC_CONSTANT(std::size_t, value = + 5 + lcast_precision::value + 6 + ); + }; + + template<> + struct lcast_src_length + : lcast_src_length_floating + { + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + : lcast_src_length_floating + { + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + : lcast_src_length_floating + { + static void check_coverage() {} + }; + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template<> + struct lcast_src_length + : lcast_src_length_floating + { + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + : lcast_src_length_floating + { + static void check_coverage() {} + }; + + template<> + struct lcast_src_length + : lcast_src_length_floating + { + static void check_coverage() {} + }; + +#endif // #ifndef DISABLE_WIDE_CHAR_SUPPORT +#endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION + } + + namespace detail // '0' and '-' constants + { + template struct lcast_char_constants; + + template<> + struct lcast_char_constants + { + BOOST_STATIC_CONSTANT(char, zero = '0'); + BOOST_STATIC_CONSTANT(char, minus = '-'); + }; + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template<> + struct lcast_char_constants + { + BOOST_STATIC_CONSTANT(wchar_t, zero = L'0'); + BOOST_STATIC_CONSTANT(wchar_t, minus = L'-'); + }; +#endif + } + + namespace detail // lexical_streambuf_fake + { + struct lexical_streambuf_fake + { + }; + } + + namespace detail // lcast_to_unsigned + { +#if (defined _MSC_VER) +# pragma warning( push ) +// C4146: unary minus operator applied to unsigned type, result still unsigned +# pragma warning( disable : 4146 ) +#endif + + inline unsigned int lcast_to_unsigned(int value) + { + unsigned int uval = value; + return value < 0 ? -uval : uval; + } + + inline unsigned long lcast_to_unsigned(long value) + { + unsigned long uval = value; + return value < 0 ? -uval : uval; + } + +#if defined(BOOST_HAS_LONG_LONG) + inline boost::ulong_long_type lcast_to_unsigned(boost::long_long_type v) + { + boost::ulong_long_type uval = v; + return v < 0 ? -uval : uval; + } +#elif defined(BOOST_HAS_MS_INT64) + inline unsigned __int64 lcast_to_unsigned(__int64 value) + { + unsigned __int64 uval = value; + return value < 0 ? -uval : uval; + } +#endif + +#if (defined _MSC_VER) +# pragma warning( pop ) // C4146: unary minus operator applied to unsigned type, + // result still unsigned +#endif + } + + namespace detail // lcast_put_unsigned + { + // I'd personally put lcast_put_unsigned in .cpp file if not + // boost practice for header-only libraries (Alexander Nasonov). + template + CharT* lcast_put_unsigned(T n, CharT* finish) + { +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS + BOOST_STATIC_ASSERT(!std::numeric_limits::is_signed); +#endif + CharT thousands_sep = 0; + +#ifdef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE + char const* grouping = ""; + std::size_t const grouping_size = 0; +#else + std::locale loc; + typedef std::numpunct numpunct; + numpunct const& np = BOOST_USE_FACET(numpunct, loc); + std::string const& grouping = np.grouping(); + std::string::size_type const grouping_size = grouping.size(); + + if(grouping_size) + thousands_sep = np.thousands_sep(); +#endif + + std::string::size_type group = 0; // current group number + char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0]; + // a) Since grouping is const, grouping[grouping.size()] returns 0. + // b) It's safe to assume here and below that CHAR_MAX + // is equivalent to unlimited grouping: +#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS + BOOST_STATIC_ASSERT(std::numeric_limits::digits10 < CHAR_MAX); +#endif + + char left = last_grp_size; + + do + { + if(left == 0) + { + ++group; + if(group < grouping_size) + { + char const grp_size = grouping[group]; + last_grp_size = grp_size <= 0 ? CHAR_MAX : grp_size; + } + + left = last_grp_size; + --finish; + *finish = thousands_sep; + } + + --left; + --finish; + int const digit = static_cast(n % 10); + int const cdigit = digit + lcast_char_constants::zero; + *finish = static_cast(cdigit); + n /= 10; + } while(n); + + return finish; + } + } + namespace detail // stream wrapper for handling lexical conversions { template @@ -131,14 +524,10 @@ namespace boost typename stream_char::type>::type char_type; public: - lexical_stream() + lexical_stream(char_type* = 0, char_type* = 0) { stream.unsetf(std::ios::skipws); - - if(std::numeric_limits::is_specialized) - stream.precision(std::numeric_limits::digits10 + 1); - else if(std::numeric_limits::is_specialized) - stream.precision(std::numeric_limits::digits10 + 1); + lcast_set_precision(stream, (Source*)0, (Target*)0); } ~lexical_stream() { @@ -171,13 +560,13 @@ namespace boost #if defined(BOOST_NO_STRINGSTREAM) stream << '\0'; #endif - output = stream.str(); + stream.str().swap(output); return true; } #ifndef DISABLE_WIDE_CHAR_SUPPORT bool operator>>(std::wstring &output) { - output = stream.str(); + stream.str().swap(output); return true; } #endif @@ -192,6 +581,419 @@ namespace boost }; } + namespace detail // optimized stream wrapper + { + // String representation of Source has an upper limit. + template< class CharT // a result of widest_char transformation + , class Base // lexical_streambuf_fake or basic_streambuf + > + class lexical_stream_limited_src : public Base + { + // A string representation of Source is written to [start, finish). + // Currently, it is assumed that [start, finish) is big enough + // to hold a string representation of any Source value. + CharT* start; + CharT* finish; + + private: + + static void widen_and_assign(char*p, char ch) + { + *p = ch; + } + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + static void widen_and_assign(wchar_t* p, char ch) + { + std::locale loc; + *p = BOOST_USE_FACET(std::ctype, loc).widen(ch); + } + + static void widen_and_assign(wchar_t* p, wchar_t ch) + { + *p = ch; + } + + static void widen_and_assign(char*, wchar_t ch); // undefined +#endif + + template + bool lcast_put(const OutputStreamable& input) + { + this->setp(start, finish); + std::basic_ostream stream(static_cast(this)); + lcast_set_precision(stream, (OutputStreamable*)0); + bool const result = !(stream << input).fail(); + finish = this->pptr(); + return result; + } + + // Undefined: + lexical_stream_limited_src(lexical_stream_limited_src const&); + void operator=(lexical_stream_limited_src const&); + + public: + + lexical_stream_limited_src(CharT* start, CharT* finish) + : start(start) + , finish(finish) + {} + + public: // output + + template + bool operator<<(std::basic_string const& str) + { + start = const_cast(str.data()); + finish = start + str.length(); + return true; + } + + bool operator<<(bool); + bool operator<<(char); +#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + bool operator<<(wchar_t); +#endif + bool operator<<(CharT const*); + bool operator<<(short); + bool operator<<(int); + bool operator<<(long); + bool operator<<(unsigned short); + bool operator<<(unsigned int); + bool operator<<(unsigned long); +#if defined(BOOST_HAS_LONG_LONG) + bool operator<<(boost::ulong_long_type); + bool operator<<(boost::long_long_type ); +#elif defined(BOOST_HAS_MS_INT64) + bool operator<<(unsigned __int64); + bool operator<<( __int64); +#endif + // These three operators use ostream and streambuf. + // lcast_streambuf_for_source::value is true. + bool operator<<(float); + bool operator<<(double); + bool operator<<(long double); + + public: // input + + // Generic istream-based algorithm. + // lcast_streambuf_for_target::value is true. + template + bool operator>>(InputStreamable& output) + { +#if (defined _MSC_VER) +# pragma warning( push ) + // conditional expression is constant +# pragma warning( disable : 4127 ) +#endif + if(is_pointer::value) + return false; + + this->setg(start, start, finish); + std::basic_istream stream(static_cast(this)); + stream.unsetf(std::ios::skipws); + lcast_set_precision(stream, (InputStreamable*)0); +#if (defined _MSC_VER) +# pragma warning( pop ) +#endif + return stream >> output && + stream.get() == +#if defined(__GNUC__) && (__GNUC__<3) && defined(BOOST_NO_STD_WSTRING) + // GCC 2.9x lacks std::char_traits<>::eof(). + // We use BOOST_NO_STD_WSTRING to filter out STLport and libstdc++-v3 + // configurations, which do provide std::char_traits<>::eof(). + + EOF; +#else + std::char_traits::eof(); +#endif + } + + bool operator>>(CharT&); + +#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION +// This #if is in sync with lcast_streambuf_for_target + + bool operator>>(std::string&); + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + bool operator>>(std::wstring&); +#endif + +#else + template + bool operator>>(std::basic_string& str) + { + str.assign(start, finish); + return true; + } +#endif + }; + + template + inline bool lexical_stream_limited_src::operator<<( + bool value) + { + *start = value + lcast_char_constants::zero; + finish = start + 1; + return true; + } + + template + inline bool lexical_stream_limited_src::operator<<(char ch) + { + widen_and_assign(start, ch); + finish = start + 1; + return true; + } + +#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + template + inline bool lexical_stream_limited_src::operator<<( + wchar_t ch) + { + widen_and_assign(start, ch); + finish = start + 1; + return true; + } +#endif + + template + inline bool lexical_stream_limited_src::operator<<(short n) + { + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + if(n < 0) + *--start = lcast_char_constants::minus; + return true; + } + + template + inline bool lexical_stream_limited_src::operator<<(int n) + { + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + if(n < 0) + *--start = lcast_char_constants::minus; + return true; + } + + template + inline bool lexical_stream_limited_src::operator<<(long n) + { + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + if(n < 0) + *--start = lcast_char_constants::minus; + return true; + } + +#if defined(BOOST_HAS_LONG_LONG) + template + inline bool lexical_stream_limited_src::operator<<( + boost::long_long_type n) + { + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + if(n < 0) + *--start = lcast_char_constants::minus; + return true; + } +#elif defined(BOOST_HAS_MS_INT64) + template + inline bool lexical_stream_limited_src::operator<<( + __int64 n) + { + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + if(n < 0) + *--start = lcast_char_constants::minus; + return true; + } +#endif + + template + inline bool lexical_stream_limited_src::operator<<( + unsigned short n) + { + start = lcast_put_unsigned(+n, finish); + return true; + } + + template + inline bool lexical_stream_limited_src::operator<<( + unsigned int n) + { + start = lcast_put_unsigned(n, finish); + return true; + } + + template + inline bool lexical_stream_limited_src::operator<<( + unsigned long n) + { + start = lcast_put_unsigned(n, finish); + return true; + } + +#if defined(BOOST_HAS_LONG_LONG) + template + inline bool lexical_stream_limited_src::operator<<( + boost::ulong_long_type n) + { + start = lcast_put_unsigned(n, finish); + return true; + } +#elif defined(BOOST_HAS_MS_INT64) + template + inline bool lexical_stream_limited_src::operator<<( + unsigned __int64 n) + { + start = lcast_put_unsigned(n, finish); + return true; + } +#endif + + template + inline bool lexical_stream_limited_src::operator<<( + float val) + { + return this->lcast_put(val); + } + + template + inline bool lexical_stream_limited_src::operator<<( + double val) + { + return this->lcast_put(val); + } + + template + inline bool lexical_stream_limited_src::operator<<( + long double val) + { + return this->lcast_put(val); + } + + template + inline bool lexical_stream_limited_src::operator<<( + CharT const* str) + { + start = const_cast(str); + finish = start + std::char_traits::length(str); + return true; + } + + template + inline bool lexical_stream_limited_src::operator>>( + CharT& output) + { + bool const ok = (finish - start == 1); + if(ok) + output = *start; + return ok; + } + +#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + template + inline bool lexical_stream_limited_src::operator>>( + std::string& str) + { + str.assign(start, finish); + return true; + } + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template + inline bool lexical_stream_limited_src::operator>>( + std::wstring& str) + { + str.assign(start, finish); + return true; + } +#endif +#endif + } + + namespace detail // lcast_streambuf_for_source + { + // Returns true if optimized stream wrapper needs ostream for writing. + template + struct lcast_streambuf_for_source + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; + + template<> + struct lcast_streambuf_for_source + { + BOOST_STATIC_CONSTANT(bool, value = true); + }; + + template<> + struct lcast_streambuf_for_source + { + BOOST_STATIC_CONSTANT(bool, value = true); + }; + + template<> + struct lcast_streambuf_for_source + { + BOOST_STATIC_CONSTANT(bool, value = true); + }; + } + + namespace detail // lcast_streambuf_for_target + { + // Returns true if optimized stream wrapper needs istream for reading. + template + struct lcast_streambuf_for_target + { + BOOST_STATIC_CONSTANT(bool, value = true); + }; + + template<> + struct lcast_streambuf_for_target + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; + +#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + template<> + struct lcast_streambuf_for_target + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; +#endif + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + template + struct lcast_streambuf_for_target< + std::basic_string > + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template + struct lcast_streambuf_for_target< + std::basic_string > + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; +#endif +#else + template<> + struct lcast_streambuf_for_target + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; + +#ifndef DISABLE_WIDE_CHAR_SUPPORT + template<> + struct lcast_streambuf_for_target + { + BOOST_STATIC_CONSTANT(bool, value = false); + }; +#endif +#endif + } + #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION // call-by-const reference version @@ -209,19 +1011,64 @@ namespace boost { typedef const T * type; }; + + template< typename Target + , typename Source + , bool Unlimited // string representation of Source is unlimited + , typename CharT + > + Target lexical_cast( + BOOST_DEDUCED_TYPENAME boost::call_traits::param_type arg, + CharT* buf, std::size_t src_len) + { + typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c< + lcast_streambuf_for_target::value || + lcast_streambuf_for_source::value + , std::basic_streambuf + , lexical_streambuf_fake + >::type base; + + BOOST_DEDUCED_TYPENAME boost::mpl::if_c< + Unlimited + , detail::lexical_stream + , detail::lexical_stream_limited_src + >::type interpreter(buf, buf + src_len); + + // The original form, reproduced below, is more elegant + // but yields a spurious C4701 warning ("possible use of + // "result" before initialization") with VC7.1 (/W4). +// +// Target result; +// +// if(!(interpreter << arg && interpreter >> result)) +// throw_exception(bad_lexical_cast(typeid(Source), typeid(Target))); +// return result; + + if(interpreter << arg) { + Target result; + if (interpreter >> result) + return result; + } + throw_exception(bad_lexical_cast(typeid(Source), typeid(Target))); + return Target(); // normally never reached (throw_exception) + } } template - Target lexical_cast(const Source &arg) + inline Target lexical_cast(const Source &arg) { - typedef typename detail::array_to_pointer_decay::type NewSource; + typedef typename detail::array_to_pointer_decay::type src; - detail::lexical_stream interpreter; - Target result; + typedef typename detail::widest_char< + typename detail::stream_char::type + , typename detail::stream_char::type + >::type char_type; - if(!(interpreter << arg && interpreter >> result)) - throw_exception(bad_lexical_cast(typeid(NewSource), typeid(Target))); - return result; + typedef detail::lcast_src_length lcast_src_length; + std::size_t const src_len = lcast_src_length::value; + char_type buf[src_len + 1]; + lcast_src_length::check_coverage(); + return detail::lexical_cast(arg, buf, src_len); } #else @@ -242,7 +1089,8 @@ namespace boost #endif } -// Copyright Kevlin Henney, 2000-2005. All rights reserved. +// Copyright Kevlin Henney, 2000-2005. +// Copyright Alexander Nasonov, 2006-2007. // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at From ee3f643c5e9ed40a4bd0d1e24a2dd99204b91e92 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sun, 25 Nov 2007 18:38:02 +0000 Subject: [PATCH 11/16] Full merge from trunk at revision 41356 of entire boost-root tree. [SVN r41370] --- lexical_cast.htm | 40 +++- lexical_cast_test.cpp | 288 +++++++++++++++++++++++++ test/Jamfile.v2 | 3 + test/lexical_cast_abstract_test.cpp | 61 ++++++ test/lexical_cast_loopback_test.cpp | 98 +++++++++ test/lexical_cast_noncopyable_test.cpp | 54 +++++ 6 files changed, 542 insertions(+), 2 deletions(-) create mode 100644 test/lexical_cast_abstract_test.cpp create mode 100644 test/lexical_cast_loopback_test.cpp create mode 100644 test/lexical_cast_noncopyable_test.cpp diff --git a/lexical_cast.htm b/lexical_cast.htm index 949b70c..2a08541 100644 --- a/lexical_cast.htm +++ b/lexical_cast.htm @@ -20,6 +20,10 @@ lexical_cast
  • bad_lexical_cast
  • +
  • + Frequently Asked Questions
  • +
  • + References
  • Changes
  • @@ -160,7 +164,7 @@ void log_errno(int yoko) and an instance of the result type on the right.
  • - Both Source and Target are CopyConstructible [20.1.3]. + Target is CopyConstructible [20.1.3].
  • Target is DefaultConstructible, meaning that it is possible @@ -192,7 +196,39 @@ public: failure.
    +

    Frequently Asked Questions

    +

    Q: Why does lexical_cast<int8_t>("127") throw bad_lexical_cast? +
    A: The type int8_t is a typedef to char or signed char. + Lexical conversion to these types is simply reading a byte from source but since the source has + more than one byte, the exception is thrown. +

    Please use other integer types such as int or short int. If bounds checking + is important, you can also call numeric_cast: + +

    numeric_cast<int8_t>(lexical_cast<int>("127"));
    + +

    Q: What does lexical_cast<std::string> of an int8_t or uint8_t not do what I expect? +
    A: As above, note that int8_t and uint8_t are actually chars and are formatted as such. To avoid this, cast to an integer type first: + +

    lexical_cast<std::string>(static_cast<int>(n));
    + +

    Q: The implementation always resets the ios_base::skipws flag of an underlying stream object. It breaks my operator>> that works only in presence of this flag. Can you remove code that resets the flag? +
    A: May be in a future version. There is no requirement in [N1973] to reset the flag but remember that [N1973] is not yet accepted by the committee. By the way, it's a great opportunity to make your operator>> conform to the standard. Read a good C++ book, study std::sentry and ios_state_saver. + +

    References

    +
      +
    • [N1973] Kevlin Henney, Beman Dawes, Lexical Conversion Library Proposal for TR2, + N1973. +
    • [Tuning] Alexander Nasonov, Fine Tuning for lexical_cast, + Overload #74, + August 2006.
    • +

    Changes

    +

    August, October 2006:

    +
      +
    • Better performance for many combinations of Source and Target + types. Refer to [Tuning] for more details. +
    • +

    June 2005:

    • Call-by-const reference for the parameters. This requires partial specialization @@ -231,4 +267,4 @@ public:
      © Copyright Kevlin Henney, 2000–2005
      - \ No newline at end of file + diff --git a/lexical_cast_test.cpp b/lexical_cast_test.cpp index 4182942..6ba26a3 100644 --- a/lexical_cast_test.cpp +++ b/lexical_cast_test.cpp @@ -3,6 +3,7 @@ // See http://www.boost.org for most recent version, including documentation. // // Copyright Terje Slettebų and Kevlin Henney, 2005. +// Copyright Alexander Nasonov, 2006. // // Distributed under the Boost // Software License, Version 1.0. (See accompanying file @@ -29,6 +30,18 @@ #define DISABLE_WIDE_CHAR_SUPPORT #endif +#if (defined(BOOST_HAS_LONG_LONG) || defined(BOOST_HAS_MS_INT64)) \ + && !(defined(BOOST_MSVC) && BOOST_MSVC < 1300) +#define LCAST_TEST_LONGLONG +#endif + +// Test all 65536 values if true: +bool const lcast_test_small_integral_types_completely = false; + +// lcast_integral_test_counter: use when testing all values of an integral +// types is not possible. Max. portable value is 32767. +int const lcast_integral_test_counter=1000; + using namespace boost; void test_conversion_to_char(); @@ -44,6 +57,16 @@ void test_conversion_from_wstring(); void test_conversion_to_wstring(); void test_bad_lexical_cast(); void test_no_whitespace_stripping(); +void test_conversion_from_short(); +void test_conversion_from_ushort(); +void test_conversion_from_int(); +void test_conversion_from_uint(); +void test_conversion_from_long(); +void test_conversion_from_ulong(); +#ifdef LCAST_TEST_LONGLONG +void test_conversion_from_longlong(); +void test_conversion_from_ulonglong(); +#endif unit_test::test_suite *init_unit_test_suite(int, char *[]) { @@ -64,6 +87,17 @@ unit_test::test_suite *init_unit_test_suite(int, char *[]) #endif suite->add(BOOST_TEST_CASE(test_bad_lexical_cast)); suite->add(BOOST_TEST_CASE(test_no_whitespace_stripping)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_short)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_ushort)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_int)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_uint)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_ulong)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_long)); + #ifdef LCAST_TEST_LONGLONG + suite->add(BOOST_TEST_CASE(&test_conversion_from_longlong)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_ulonglong)); + #endif + return suite; } @@ -167,6 +201,9 @@ void test_conversion_to_bool() void test_conversion_to_string() { + char buf[] = "hello"; + char* str = buf; + BOOST_CHECK_EQUAL(str, lexical_cast(str)); BOOST_CHECK_EQUAL("A", lexical_cast('A')); BOOST_CHECK_EQUAL(" ", lexical_cast(' ')); BOOST_CHECK_EQUAL("123", lexical_cast(123)); @@ -239,6 +276,8 @@ void test_conversion_to_wchar_t() #if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) BOOST_CHECK_EQUAL(L'1', lexical_cast(1)); BOOST_CHECK_EQUAL(L'0', lexical_cast(0)); + BOOST_CHECK_EQUAL(L'1', lexical_cast('1')); + BOOST_CHECK_EQUAL(L'0', lexical_cast('0')); BOOST_CHECK_THROW(lexical_cast(123), bad_lexical_cast); BOOST_CHECK_EQUAL(L'1', lexical_cast(1.0)); BOOST_CHECK_EQUAL(L'0', lexical_cast(0.0)); @@ -280,6 +319,9 @@ void test_conversion_from_wstring() void test_conversion_to_wstring() { #ifndef DISABLE_WIDE_CHAR_SUPPORT + wchar_t buf[] = L"hello"; + wchar_t* str = buf; + BOOST_CHECK(str == lexical_cast(str)); BOOST_CHECK(L"123" == lexical_cast(123)); BOOST_CHECK(L"1.23" == lexical_cast(1.23)); BOOST_CHECK(L"1.111111111" == lexical_cast(1.111111111)); @@ -288,6 +330,7 @@ void test_conversion_to_wstring() #if !defined(BOOST_NO_INTRINSIC_WCHAR_T) BOOST_CHECK(L"A" == lexical_cast(L'A')); BOOST_CHECK(L" " == lexical_cast(L' ')); + BOOST_CHECK(L"A" == lexical_cast('A')); #endif BOOST_CHECK(L"Test" == lexical_cast(L"Test")); BOOST_CHECK(L" " == lexical_cast(L" ")); @@ -318,3 +361,248 @@ void test_no_whitespace_stripping() BOOST_CHECK_THROW(lexical_cast(" 123"), bad_lexical_cast); BOOST_CHECK_THROW(lexical_cast("123 "), bad_lexical_cast); } + +// Replace "-,999" with "-999". +template +std::basic_string to_str_gcc_workaround(std::basic_string str) +{ + std::locale loc; + std::numpunct const& np = BOOST_USE_FACET(std::numpunct, loc); + std::ctype const& ct = BOOST_USE_FACET(std::ctype, loc); + + if(np.grouping().empty()) + return str; + + CharT prefix[3] = { ct.widen('-'), np.thousands_sep(), CharT() }; + + if(str.find(prefix) != 0) + return str; + + prefix[1] = CharT(); + str.replace(0, 2, prefix); + return str; +} + +template +std::basic_string to_str(T t) +{ + std::basic_ostringstream o; + o << t; + return to_str_gcc_workaround(o.str()); +} + +template +void test_conversion_from_integral_to_char(CharT zero) +{ + BOOST_CHECK(lexical_cast(static_cast(0)) == zero + 0); + BOOST_CHECK(lexical_cast(static_cast(1)) == zero + 1); + BOOST_CHECK(lexical_cast(static_cast(2)) == zero + 2); + BOOST_CHECK(lexical_cast(static_cast(3)) == zero + 3); + BOOST_CHECK(lexical_cast(static_cast(4)) == zero + 4); + BOOST_CHECK(lexical_cast(static_cast(5)) == zero + 5); + BOOST_CHECK(lexical_cast(static_cast(6)) == zero + 6); + BOOST_CHECK(lexical_cast(static_cast(7)) == zero + 7); + BOOST_CHECK(lexical_cast(static_cast(8)) == zero + 8); + BOOST_CHECK(lexical_cast(static_cast(9)) == zero + 9); + + BOOST_CHECK_THROW(lexical_cast(static_cast(10)), bad_lexical_cast); + + T t = std::numeric_limits::max(); + BOOST_CHECK_THROW(lexical_cast(t), bad_lexical_cast); +} + +template +void test_conversion_from_integral_to_integral() +{ + T t = 0; + BOOST_CHECK(lexical_cast(t) == t); + + // Next two variables are used to supress warnings. + int st = 32767; unsigned int ut = st; + t = st; + BOOST_CHECK(lexical_cast(t) == st); + BOOST_CHECK(lexical_cast(t) == ut); + BOOST_CHECK(lexical_cast(t) == st); + BOOST_CHECK(lexical_cast(t) == ut); + BOOST_CHECK(lexical_cast(t) == st); + BOOST_CHECK(lexical_cast(t) == ut); + + t = std::numeric_limits::max(); + BOOST_CHECK(lexical_cast(t) == t); + + t = std::numeric_limits::min(); + BOOST_CHECK(lexical_cast(t) == t); +} + +template +void test_conversion_from_integral_to_string(CharT) +{ + typedef std::numeric_limits limits; + typedef std::basic_string string_type; + + T t; + + t = limits::min(); + BOOST_CHECK(lexical_cast(t) == to_str(t)); + + t = limits::max(); + BOOST_CHECK(lexical_cast(t) == to_str(t)); + + if(limits::digits <= 16 && lcast_test_small_integral_types_completely) + for(t = 1 + limits::min(); t != limits::max(); ++t) + BOOST_CHECK(lexical_cast(t) == to_str(t)); + else + { + T const min_val = limits::min(); + T const max_val = limits::max(); + T const half_max_val = max_val / 2; + T const cnt = lcast_integral_test_counter; // to supress warnings + unsigned int const counter = cnt < half_max_val ? cnt : half_max_val; + + unsigned int i; + + // Test values around min: + t = min_val; + for(i = 0; i < counter; ++i, ++t) + BOOST_CHECK(lexical_cast(t) == to_str(t)); + + // Test values around max: + t = max_val; + for(i = 0; i < counter; ++i, --t) + BOOST_CHECK(lexical_cast(t) == to_str(t)); + + // Test values around zero: + if(limits::is_signed) + for(t = -counter; t < static_cast(counter); ++t) + BOOST_CHECK(lexical_cast(t) == to_str(t)); + + // Test values around 100, 1000, 10000, ... + T ten_power = 100; + for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10) + { + // I believe that (ten_power + 100) never overflows + for(t = ten_power - 100; t != ten_power + 100; ++t) + BOOST_CHECK(lexical_cast(t) == to_str(t)); + } + } +} + +template +void test_conversion_from_integral_for_locale() +{ + test_conversion_from_integral_to_integral(); + test_conversion_from_integral_to_string('0'); +#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + test_conversion_from_integral_to_string(L'0'); +#endif +} + +struct restore_oldloc +{ + std::locale oldloc; + ~restore_oldloc() { std::locale::global(oldloc); } +}; + +template +void test_conversion_from_integral() +{ + char const zero = '0'; + signed char const szero = '0'; + unsigned char const uzero = '0'; + test_conversion_from_integral_to_char(zero); + test_conversion_from_integral_to_char(szero); + test_conversion_from_integral_to_char(uzero); +#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) + wchar_t const wzero = L'0'; + test_conversion_from_integral_to_char(wzero); +#endif + + // test_conversion_from_integral_for_locale + + typedef std::numpunct numpunct; + + restore_oldloc guard; + std::locale const& oldloc = guard.oldloc; + + std::string grouping1 = BOOST_USE_FACET(numpunct, oldloc).grouping(); + std::string grouping2(grouping1); + + test_conversion_from_integral_for_locale(); + + try + { + std::locale newloc(""); + std::locale::global(newloc); + + grouping2 = BOOST_USE_FACET(numpunct, newloc).grouping(); + } + catch(std::exception const& ex) + { + std::string msg("Failed to set system locale: "); + msg += ex.what(); + BOOST_TEST_MESSAGE(msg); + } + + if(grouping1 != grouping2) + test_conversion_from_integral_for_locale(); + + if(grouping1.empty() && grouping2.empty()) + BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested"); +} + +void test_conversion_from_short() +{ + test_conversion_from_integral(); +} + +void test_conversion_from_ushort() +{ + test_conversion_from_integral(); +} + +void test_conversion_from_int() +{ + test_conversion_from_integral(); +} + +void test_conversion_from_uint() +{ + test_conversion_from_integral(); +} + +void test_conversion_from_ulong() +{ + test_conversion_from_integral(); +} + +void test_conversion_from_long() +{ + test_conversion_from_integral(); +} + +#if defined(BOOST_HAS_LONG_LONG) + +void test_conversion_from_longlong() +{ + test_conversion_from_integral(); +} + +void test_conversion_from_ulonglong() +{ + test_conversion_from_integral(); +} + +#elif defined(LCAST_TEST_LONGLONG) + +void test_conversion_from_longlong() +{ + test_conversion_from_integral<__int64>(); +} + +void test_conversion_from_ulonglong() +{ + test_conversion_from_integral(); +} + +#endif + diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 56a03d8..af68510 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -21,6 +21,9 @@ test-suite conversion [ run ../cast_test.cpp ] [ run ../numeric_cast_test.cpp ] [ run ../lexical_cast_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_loopback_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_abstract_test.cpp ../../test/build//boost_unit_test_framework/static ] + [ run lexical_cast_noncopyable_test.cpp ../../test/build//boost_unit_test_framework/static ] ; diff --git a/test/lexical_cast_abstract_test.cpp b/test/lexical_cast_abstract_test.cpp new file mode 100644 index 0000000..207d3a5 --- /dev/null +++ b/test/lexical_cast_abstract_test.cpp @@ -0,0 +1,61 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Sergey Shandar 2005, Alexander Nasonov, 2007. +// +// 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). +// +// Test abstract class. Bug 1358600: +// http://sf.net/tracker/?func=detail&aid=1358600&group_id=7586&atid=107586 + +#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_abstract(); + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test_framework::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast unit test"); + suite->add(BOOST_TEST_CASE(&test_abstract)); + + return suite; +} + +class A +{ +public: + virtual void out(std::ostream &) const = 0; +}; + +class B: public A +{ +public: + virtual void out(std::ostream &O) const { O << "B"; } +}; + +std::ostream &operator<<(std::ostream &O, const A &a) +{ + a.out(O); + return O; +}; + +void test_abstract() +{ + const A &a = B(); + BOOST_CHECK(boost::lexical_cast(a) == "B"); +} + diff --git a/test/lexical_cast_loopback_test.cpp b/test/lexical_cast_loopback_test.cpp new file mode 100644 index 0000000..cd058fe --- /dev/null +++ b/test/lexical_cast_loopback_test.cpp @@ -0,0 +1,98 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Alexander Nasonov, 2006. +// +// 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). +// +// Test round-tripping conversion FPT -> string -> FPT, +// where FPT is Floating Point Type. + +#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_round_conversion_float(); +void test_round_conversion_double(); +void test_round_conversion_long_double(); + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test_framework::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast unit test"); + suite->add(BOOST_TEST_CASE(&test_round_conversion_float)); + suite->add(BOOST_TEST_CASE(&test_round_conversion_double)); + suite->add(BOOST_TEST_CASE(&test_round_conversion_long_double)); + + return suite; +} + +template +void test_round_conversion() +{ + T epsilon = std::numeric_limits::epsilon(); + std::string const epsilon_s = boost::lexical_cast(epsilon); + BOOST_CHECK(epsilon == lexical_cast(epsilon_s)); + + T max_ = (std::numeric_limits::max)(); + std::string const max_s = boost::lexical_cast(max_); + BOOST_CHECK(max_ == lexical_cast(max_s)); + + T min_ = (std::numeric_limits::min)(); + std::string const min_s = boost::lexical_cast(min_); + BOOST_CHECK(min_ == lexical_cast(min_s)); + + T max_div137 = max_ / 137; + std::string max_div137_s = boost::lexical_cast(max_div137); + BOOST_CHECK(max_div137 == lexical_cast(max_div137_s)); + + T epsilon_mult137 = epsilon * 137; + std::string epsilon_mult137_s(lexical_cast(epsilon_mult137)); + BOOST_CHECK(epsilon_mult137 == lexical_cast(epsilon_mult137_s)); + +} + +#if defined(BOOST_MSVC) +// See bug http://tinyurl.com/vhpvo +template +void test_msvc_magic_values() +{ + T magic_msvc = 0.00010000433948393407; + std::string magic_msvc_s = boost::lexical_cast(magic_msvc); + BOOST_CHECK(magic_msvc == lexical_cast(magic_msvc_s)); +} +#endif + +void test_round_conversion_float() +{ + test_round_conversion(); +} + +void test_round_conversion_double() +{ + test_round_conversion(); +#if defined(BOOST_MSVC) + test_msvc_magic_values(); +#endif +} + +void test_round_conversion_long_double() +{ + test_round_conversion(); +#if defined(BOOST_MSVC) + test_msvc_magic_values(); +#endif +} + diff --git a/test/lexical_cast_noncopyable_test.cpp b/test/lexical_cast_noncopyable_test.cpp new file mode 100644 index 0000000..6284b14 --- /dev/null +++ b/test/lexical_cast_noncopyable_test.cpp @@ -0,0 +1,54 @@ +// Unit test for boost::lexical_cast. +// +// See http://www.boost.org for most recent version, including documentation. +// +// Copyright Alexander Nasonov, 2007. +// +// 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). +// +// Test that Source can be non-copyable. + +#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_noncopyable(); + +unit_test::test_suite *init_unit_test_suite(int, char *[]) +{ + unit_test_framework::test_suite *suite = + BOOST_TEST_SUITE("lexical_cast unit test"); + suite->add(BOOST_TEST_CASE(&test_noncopyable)); + + return suite; +} + +class Noncopyable : private boost::noncopyable +{ +public: + Noncopyable() {} +}; + +inline std::ostream &operator<<(std::ostream &out, const Noncopyable&) +{ + return out << "Noncopyable"; +} + +void test_noncopyable() +{ + Noncopyable x; + BOOST_CHECK(boost::lexical_cast(x) == "Noncopyable"); +} + From e9a5ac6d7a95b34fc2113b9dfa6338333dd7f44b Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sat, 15 Dec 2007 14:39:18 +0000 Subject: [PATCH 12/16] Merge conversion library at rev 41517 [SVN r42070] --- include/boost/lexical_cast.hpp | 267 +++++++++++++++++++++++---------- lexical_cast_test.cpp | 245 +++++++++++++++++++++++------- 2 files changed, 380 insertions(+), 132 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 754aecf..44c7a66 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -3,7 +3,7 @@ // Boost lexical_cast.hpp header -------------------------------------------// // -// See http://www.boost.org/libs/converston for documentation. +// See http://www.boost.org/libs/conversion for documentation. // See end of this header for rights and permissions. // // what: lexical_cast custom keyword cast @@ -86,6 +86,14 @@ namespace boost typedef char type; }; +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + template + struct stream_char< std::basic_string > + { + typedef CharT type; + }; +#endif + #ifndef DISABLE_WIDE_CHAR_SUPPORT #ifndef BOOST_NO_INTRINSIC_WCHAR_T template<> @@ -107,11 +115,13 @@ namespace boost typedef wchar_t type; }; +#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION template<> struct stream_char { typedef wchar_t type; }; +#endif #endif template @@ -127,6 +137,44 @@ namespace boost }; } + namespace detail // deduce_char_traits template + { +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + template + struct deduce_char_traits + { + typedef std::char_traits type; + }; + + template + struct deduce_char_traits< CharT + , std::basic_string + , Source + > + { + typedef Traits type; + }; + + template + struct deduce_char_traits< CharT + , Target + , std::basic_string + > + { + typedef Traits type; + }; + + template + struct deduce_char_traits< CharT + , std::basic_string + , std::basic_string + > + { + typedef Traits type; + }; +#endif + } + namespace detail // lcast_src_length { // Return max. length of string representation of Source; @@ -309,8 +357,6 @@ namespace boost #undef BOOST_AUX_LEXICAL_CAST_DEF1 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION -// This #if is in sync with lcast_precision - // Helper for floating point types. // -1.23456789e-123456 // ^ sign @@ -450,9 +496,7 @@ namespace boost namespace detail // lcast_put_unsigned { - // I'd personally put lcast_put_unsigned in .cpp file if not - // boost practice for header-only libraries (Alexander Nasonov). - template + template CharT* lcast_put_unsigned(T n, CharT* finish) { #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS @@ -482,6 +526,9 @@ namespace boost #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS BOOST_STATIC_ASSERT(std::numeric_limits::digits10 < CHAR_MAX); #endif + typedef typename Traits::int_type int_type; + CharT const czero = lcast_char_constants::zero; + int_type const zero = Traits::to_int_type(czero); char left = last_grp_size; @@ -498,14 +545,13 @@ namespace boost left = last_grp_size; --finish; - *finish = thousands_sep; + Traits::assign(*finish, thousands_sep); } --left; --finish; - int const digit = static_cast(n % 10); - int const cdigit = digit + lcast_char_constants::zero; - *finish = static_cast(cdigit); + int_type const digit = static_cast(n % 10U); + Traits::assign(*finish, Traits::to_char_type(zero + digit)); n /= 10; } while(n); @@ -515,7 +561,7 @@ namespace boost namespace detail // stream wrapper for handling lexical conversions { - template + template class lexical_stream { private: @@ -523,6 +569,8 @@ namespace boost typename stream_char::type, typename stream_char::type>::type char_type; + typedef Traits traits_type; + public: lexical_stream(char_type* = 0, char_type* = 0) { @@ -552,9 +600,12 @@ namespace boost EOF; #else - std::char_traits::eof(); + traits_type::eof(); #endif } + +#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + bool operator>>(std::string &output) { #if defined(BOOST_NO_STRINGSTREAM) @@ -570,13 +621,29 @@ namespace boost return true; } #endif + +#else + bool operator>>(std::basic_string& output) + { + stream.str().swap(output); + return true; + } + + template + bool operator>>(std::basic_string& out) + { + std::basic_string str(stream.str()); + out.assign(str.begin(), str.end()); + return true; + } +#endif private: #if defined(BOOST_NO_STRINGSTREAM) std::strstream stream; #elif defined(BOOST_NO_STD_LOCALE) std::stringstream stream; #else - std::basic_stringstream stream; + std::basic_stringstream stream; #endif }; } @@ -586,6 +653,7 @@ namespace boost // String representation of Source has an upper limit. template< class CharT // a result of widest_char transformation , class Base // lexical_streambuf_fake or basic_streambuf + , class Traits // usually char_traits > class lexical_stream_limited_src : public Base { @@ -599,19 +667,20 @@ namespace boost static void widen_and_assign(char*p, char ch) { - *p = ch; + Traits::assign(*p, ch); } #ifndef DISABLE_WIDE_CHAR_SUPPORT static void widen_and_assign(wchar_t* p, char ch) { std::locale loc; - *p = BOOST_USE_FACET(std::ctype, loc).widen(ch); + wchar_t w = BOOST_USE_FACET(std::ctype, loc).widen(ch); + Traits::assign(*p, w); } static void widen_and_assign(wchar_t* p, wchar_t ch) { - *p = ch; + Traits::assign(*p, ch); } static void widen_and_assign(char*, wchar_t ch); // undefined @@ -641,8 +710,8 @@ namespace boost public: // output - template - bool operator<<(std::basic_string const& str) + template + bool operator<<(std::basic_string const& str) { start = const_cast(str.data()); finish = start + str.length(); @@ -705,7 +774,7 @@ namespace boost EOF; #else - std::char_traits::eof(); + Traits::eof(); #endif } @@ -721,7 +790,7 @@ namespace boost #endif #else - template + template bool operator>>(std::basic_string& str) { str.assign(start, finish); @@ -730,17 +799,21 @@ namespace boost #endif }; - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( bool value) { - *start = value + lcast_char_constants::zero; + typedef typename Traits::int_type int_type; + CharT const czero = lcast_char_constants::zero; + int_type const zero = Traits::to_int_type(czero); + Traits::assign(*start, Traits::to_char_type(zero + value)); finish = start + 1; return true; } - template - inline bool lexical_stream_limited_src::operator<<(char ch) + template + inline bool lexical_stream_limited_src::operator<<( + char ch) { widen_and_assign(start, ch); finish = start + 1; @@ -748,8 +821,8 @@ namespace boost } #if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( wchar_t ch) { widen_and_assign(start, ch); @@ -758,140 +831,163 @@ namespace boost } #endif - template - inline bool lexical_stream_limited_src::operator<<(short n) + template + inline bool lexical_stream_limited_src::operator<<( + short n) { - start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); if(n < 0) - *--start = lcast_char_constants::minus; + { + --start; + CharT const minus = lcast_char_constants::minus; + Traits::assign(*start, minus); + } return true; } - template - inline bool lexical_stream_limited_src::operator<<(int n) + template + inline bool lexical_stream_limited_src::operator<<( + int n) { - start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); if(n < 0) - *--start = lcast_char_constants::minus; + { + --start; + CharT const minus = lcast_char_constants::minus; + Traits::assign(*start, minus); + } return true; } - template - inline bool lexical_stream_limited_src::operator<<(long n) + template + inline bool lexical_stream_limited_src::operator<<( + long n) { - start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); if(n < 0) - *--start = lcast_char_constants::minus; + { + --start; + CharT const minus = lcast_char_constants::minus; + Traits::assign(*start, minus); + } return true; } #if defined(BOOST_HAS_LONG_LONG) - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( boost::long_long_type n) { - start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); if(n < 0) - *--start = lcast_char_constants::minus; + { + --start; + CharT const minus = lcast_char_constants::minus; + Traits::assign(*start, minus); + } return true; } #elif defined(BOOST_HAS_MS_INT64) - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( __int64 n) { - start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); if(n < 0) - *--start = lcast_char_constants::minus; + { + --start; + CharT const minus = lcast_char_constants::minus; + Traits::assign(*start, minus); + } return true; } #endif - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( unsigned short n) { - start = lcast_put_unsigned(+n, finish); + start = lcast_put_unsigned(lcast_to_unsigned(n), finish); return true; } - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( unsigned int n) { - start = lcast_put_unsigned(n, finish); + start = lcast_put_unsigned(n, finish); return true; } - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( unsigned long n) { - start = lcast_put_unsigned(n, finish); + start = lcast_put_unsigned(n, finish); return true; } #if defined(BOOST_HAS_LONG_LONG) - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( boost::ulong_long_type n) { - start = lcast_put_unsigned(n, finish); + start = lcast_put_unsigned(n, finish); return true; } #elif defined(BOOST_HAS_MS_INT64) - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( unsigned __int64 n) { - start = lcast_put_unsigned(n, finish); + start = lcast_put_unsigned(n, finish); return true; } #endif - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( float val) { return this->lcast_put(val); } - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( double val) { return this->lcast_put(val); } - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( long double val) { return this->lcast_put(val); } - template - inline bool lexical_stream_limited_src::operator<<( + template + inline bool lexical_stream_limited_src::operator<<( CharT const* str) { start = const_cast(str); - finish = start + std::char_traits::length(str); + finish = start + Traits::length(str); return true; } - template - inline bool lexical_stream_limited_src::operator>>( + template + inline bool lexical_stream_limited_src::operator>>( CharT& output) { bool const ok = (finish - start == 1); if(ok) - output = *start; + Traits::assign(output, *start); return ok; } #ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION - template - inline bool lexical_stream_limited_src::operator>>( + template + inline bool lexical_stream_limited_src::operator>>( std::string& str) { str.assign(start, finish); @@ -899,8 +995,8 @@ namespace boost } #ifndef DISABLE_WIDE_CHAR_SUPPORT - template - inline bool lexical_stream_limited_src::operator>>( + template + inline bool lexical_stream_limited_src::operator>>( std::wstring& str) { str.assign(start, finish); @@ -1021,6 +1117,9 @@ namespace boost BOOST_DEDUCED_TYPENAME boost::call_traits::param_type arg, CharT* buf, std::size_t src_len) { + typedef BOOST_DEDUCED_TYPENAME + deduce_char_traits::type traits; + typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_c< lcast_streambuf_for_target::value || lcast_streambuf_for_source::value @@ -1030,8 +1129,8 @@ namespace boost BOOST_DEDUCED_TYPENAME boost::mpl::if_c< Unlimited - , detail::lexical_stream - , detail::lexical_stream_limited_src + , detail::lexical_stream + , detail::lexical_stream_limited_src >::type interpreter(buf, buf + src_len); // The original form, reproduced below, is more elegant @@ -1078,7 +1177,13 @@ namespace boost template Target lexical_cast(Source arg) { - detail::lexical_stream interpreter; + typedef typename detail::widest_char< + BOOST_DEDUCED_TYPENAME detail::stream_char::type + , BOOST_DEDUCED_TYPENAME detail::stream_char::type + >::type char_type; + + typedef std::char_traits traits; + detail::lexical_stream interpreter; Target result; if(!(interpreter << arg && interpreter >> result)) diff --git a/lexical_cast_test.cpp b/lexical_cast_test.cpp index 6ba26a3..04478e0 100644 --- a/lexical_cast_test.cpp +++ b/lexical_cast_test.cpp @@ -24,6 +24,9 @@ #include #include +#include +#include + #if defined(BOOST_NO_STRINGSTREAM) || \ defined(BOOST_NO_STD_WSTRING) || \ defined(BOOST_NO_STD_LOCALE) @@ -35,6 +38,16 @@ #define LCAST_TEST_LONGLONG #endif +template +struct my_traits : std::char_traits +{ +}; + +template +struct my_allocator : std::allocator +{ +}; + // Test all 65536 values if true: bool const lcast_test_small_integral_types_completely = false; @@ -57,15 +70,21 @@ void test_conversion_from_wstring(); void test_conversion_to_wstring(); void test_bad_lexical_cast(); void test_no_whitespace_stripping(); -void test_conversion_from_short(); -void test_conversion_from_ushort(); -void test_conversion_from_int(); -void test_conversion_from_uint(); -void test_conversion_from_long(); -void test_conversion_from_ulong(); +void test_conversion_from_to_short(); +void test_conversion_from_to_ushort(); +void test_conversion_from_to_int(); +void test_conversion_from_to_uint(); +void test_conversion_from_to_long(); +void test_conversion_from_to_ulong(); #ifdef LCAST_TEST_LONGLONG -void test_conversion_from_longlong(); -void test_conversion_from_ulonglong(); +void test_conversion_from_to_longlong(); +void test_conversion_from_to_ulonglong(); +#endif +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION +void test_traits(); +void test_wtraits(); +void test_allocator(); +void test_wallocator(); #endif unit_test::test_suite *init_unit_test_suite(int, char *[]) @@ -87,15 +106,21 @@ unit_test::test_suite *init_unit_test_suite(int, char *[]) #endif suite->add(BOOST_TEST_CASE(test_bad_lexical_cast)); suite->add(BOOST_TEST_CASE(test_no_whitespace_stripping)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_short)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_ushort)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_int)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_uint)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_ulong)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_long)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_short)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ushort)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_int)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_uint)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulong)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_long)); #ifdef LCAST_TEST_LONGLONG - suite->add(BOOST_TEST_CASE(&test_conversion_from_longlong)); - suite->add(BOOST_TEST_CASE(&test_conversion_from_ulonglong)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_longlong)); + suite->add(BOOST_TEST_CASE(&test_conversion_from_to_ulonglong)); + #endif + #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION + suite->add(BOOST_TEST_CASE(&test_traits)); + suite->add(BOOST_TEST_CASE(&test_wtraits)); + suite->add(BOOST_TEST_CASE(&test_allocator)); + suite->add(BOOST_TEST_CASE(&test_wallocator)); #endif return suite; @@ -129,10 +154,15 @@ void test_conversion_to_int() BOOST_CHECK_EQUAL(0, lexical_cast('0')); BOOST_CHECK_THROW(lexical_cast('A'), bad_lexical_cast); BOOST_CHECK_EQUAL(1, lexical_cast(1)); + BOOST_CHECK_EQUAL(1, lexical_cast(1.0)); + BOOST_CHECK_EQUAL( (std::numeric_limits::max)(), lexical_cast((std::numeric_limits::max)())); - BOOST_CHECK_EQUAL(1, lexical_cast(1.0)); + + BOOST_CHECK_EQUAL( + (std::numeric_limits::min)(), + lexical_cast((std::numeric_limits::min)())); BOOST_CHECK_THROW(lexical_cast(1.23), bad_lexical_cast); @@ -407,7 +437,7 @@ void test_conversion_from_integral_to_char(CharT zero) BOOST_CHECK_THROW(lexical_cast(static_cast(10)), bad_lexical_cast); - T t = std::numeric_limits::max(); + T t = (std::numeric_limits::max)(); BOOST_CHECK_THROW(lexical_cast(t), bad_lexical_cast); } @@ -427,10 +457,10 @@ void test_conversion_from_integral_to_integral() BOOST_CHECK(lexical_cast(t) == st); BOOST_CHECK(lexical_cast(t) == ut); - t = std::numeric_limits::max(); + t = (std::numeric_limits::max)(); BOOST_CHECK(lexical_cast(t) == t); - t = std::numeric_limits::min(); + t = (std::numeric_limits::min)(); BOOST_CHECK(lexical_cast(t) == t); } @@ -442,19 +472,20 @@ void test_conversion_from_integral_to_string(CharT) T t; - t = limits::min(); + t = (limits::min)(); BOOST_CHECK(lexical_cast(t) == to_str(t)); - t = limits::max(); + t = (limits::max)(); BOOST_CHECK(lexical_cast(t) == to_str(t)); if(limits::digits <= 16 && lcast_test_small_integral_types_completely) - for(t = 1 + limits::min(); t != limits::max(); ++t) + // min and max have already been tested. + for(t = 1 + (limits::min)(); t != (limits::max)(); ++t) BOOST_CHECK(lexical_cast(t) == to_str(t)); else { - T const min_val = limits::min(); - T const max_val = limits::max(); + T const min_val = (limits::min)(); + T const max_val = (limits::max)(); T const half_max_val = max_val / 2; T const cnt = lcast_integral_test_counter; // to supress warnings unsigned int const counter = cnt < half_max_val ? cnt : half_max_val; @@ -480,20 +511,75 @@ void test_conversion_from_integral_to_string(CharT) T ten_power = 100; for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10) { - // I believe that (ten_power + 100) never overflows + // ten_power + 100 probably never overflows for(t = ten_power - 100; t != ten_power + 100; ++t) BOOST_CHECK(lexical_cast(t) == to_str(t)); } } } +template +void test_conversion_from_string_to_integral(CharT) +{ + typedef std::numeric_limits limits; + + T t; + + t = (limits::min)(); + BOOST_CHECK(lexical_cast(to_str(t)) == t); + + t = (limits::max)(); + BOOST_CHECK(lexical_cast(to_str(t)) == t); + + if(limits::digits <= 16 && lcast_test_small_integral_types_completely) + // min and max have already been tested. + for(t = 1 + (limits::min)(); t != (limits::max)(); ++t) + BOOST_CHECK(lexical_cast(to_str(t)) == t); + else + { + T const min_val = (limits::min)(); + T const max_val = (limits::max)(); + T const half_max_val = max_val / 2; + T const cnt = lcast_integral_test_counter; // to supress warnings + unsigned int const counter = cnt < half_max_val ? cnt : half_max_val; + + unsigned int i; + + // Test values around min: + t = min_val; + for(i = 0; i < counter; ++i, ++t) + BOOST_CHECK(lexical_cast(to_str(t)) == t); + + // Test values around max: + t = max_val; + for(i = 0; i < counter; ++i, --t) + BOOST_CHECK(lexical_cast(to_str(t)) == t); + + // Test values around zero: + if(limits::is_signed) + for(t = -counter; t < static_cast(counter); ++t) + BOOST_CHECK(lexical_cast(to_str(t)) == t); + + // Test values around 100, 1000, 10000, ... + T ten_power = 100; + for(int e = 2; e <= limits::digits10; ++e, ten_power *= 10) + { + // ten_power + 100 probably never overflows + for(t = ten_power - 100; t != ten_power + 100; ++t) + BOOST_CHECK(lexical_cast(to_str(t)) == t); + } + } +} + template -void test_conversion_from_integral_for_locale() +void test_conversion_from_to_integral_for_locale() { test_conversion_from_integral_to_integral(); test_conversion_from_integral_to_string('0'); + test_conversion_from_string_to_integral('0'); #if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) test_conversion_from_integral_to_string(L'0'); + test_conversion_from_string_to_integral(L'0'); #endif } @@ -504,7 +590,7 @@ struct restore_oldloc }; template -void test_conversion_from_integral() +void test_conversion_from_to_integral() { char const zero = '0'; signed char const szero = '0'; @@ -517,7 +603,7 @@ void test_conversion_from_integral() test_conversion_from_integral_to_char(wzero); #endif - // test_conversion_from_integral_for_locale + // test_conversion_from_to_integral_for_locale typedef std::numpunct numpunct; @@ -527,7 +613,7 @@ void test_conversion_from_integral() std::string grouping1 = BOOST_USE_FACET(numpunct, oldloc).grouping(); std::string grouping2(grouping1); - test_conversion_from_integral_for_locale(); + test_conversion_from_to_integral_for_locale(); try { @@ -544,64 +630,121 @@ void test_conversion_from_integral() } if(grouping1 != grouping2) - test_conversion_from_integral_for_locale(); + test_conversion_from_to_integral_for_locale(); if(grouping1.empty() && grouping2.empty()) BOOST_TEST_MESSAGE("Formatting with thousands_sep has not been tested"); } -void test_conversion_from_short() +void test_conversion_from_to_short() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } -void test_conversion_from_ushort() +void test_conversion_from_to_ushort() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } -void test_conversion_from_int() +void test_conversion_from_to_int() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } -void test_conversion_from_uint() +void test_conversion_from_to_uint() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } -void test_conversion_from_ulong() +void test_conversion_from_to_ulong() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } -void test_conversion_from_long() +void test_conversion_from_to_long() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } #if defined(BOOST_HAS_LONG_LONG) -void test_conversion_from_longlong() +void test_conversion_from_to_longlong() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } -void test_conversion_from_ulonglong() +void test_conversion_from_to_ulonglong() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); } #elif defined(LCAST_TEST_LONGLONG) -void test_conversion_from_longlong() +void test_conversion_from_to_longlong() { - test_conversion_from_integral<__int64>(); + test_conversion_from_to_integral<__int64>(); } -void test_conversion_from_ulonglong() +void test_conversion_from_to_ulonglong() { - test_conversion_from_integral(); + test_conversion_from_to_integral(); +} + +#endif + +#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION +void test_traits() +{ + typedef std::basic_string > my_string; + + my_string const s("s"); + BOOST_CHECK(boost::lexical_cast(s) == s[0]); + BOOST_CHECK(boost::lexical_cast(s) == s); + BOOST_CHECK(boost::lexical_cast(-1) == "-1"); +} + +void test_wtraits() +{ + typedef std::basic_string > my_string; + + my_string const s(L"s"); + BOOST_CHECK(boost::lexical_cast(s) == s[0]); + BOOST_CHECK(boost::lexical_cast(s) == s); + //BOOST_CHECK(boost::lexical_cast(-1) == L"-1"); + // Commented out because gcc 3.3 doesn't support this: + // basic_ostream > o; o << -1; +} + +void test_allocator() +{ + typedef std::basic_string< char + , std::char_traits + , my_allocator + > my_string; + + my_string s("s"); + BOOST_CHECK(boost::lexical_cast(s) == s[0]); + BOOST_CHECK(boost::lexical_cast(s) == "s"); + BOOST_CHECK(boost::lexical_cast(s) == s); + BOOST_CHECK(boost::lexical_cast(1) == "1"); + BOOST_CHECK(boost::lexical_cast("s") == s); + BOOST_CHECK(boost::lexical_cast(std::string("s")) == s); +} + +void test_wallocator() +{ + typedef std::basic_string< wchar_t + , std::char_traits + , my_allocator + > my_string; + + my_string s(L"s"); + BOOST_CHECK(boost::lexical_cast(s) == s[0]); + BOOST_CHECK(boost::lexical_cast(s) == L"s"); + BOOST_CHECK(boost::lexical_cast(s) == s); + BOOST_CHECK(boost::lexical_cast(1) == L"1"); + BOOST_CHECK(boost::lexical_cast(L"s") == s); + BOOST_CHECK(boost::lexical_cast(std::wstring(L"s")) == s); } #endif From 49c0b2f333fb1da92127d994d9cb3bd1253ced11 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 10 Feb 2008 16:39:38 +0000 Subject: [PATCH 13/16] Merged revisions 43206,43208-43213 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r43206 | danieljames | 2008-02-10 09:55:03 +0000 (Sun, 10 Feb 2008) | 1 line Fix some broken links. ........ r43209 | danieljames | 2008-02-10 14:56:22 +0000 (Sun, 10 Feb 2008) | 1 line Link to people pages on the website, as they've been removed from the download. ........ r43210 | danieljames | 2008-02-10 15:02:17 +0000 (Sun, 10 Feb 2008) | 1 line Point links to the pages that used to be in 'more' to the site. ........ r43212 | danieljames | 2008-02-10 16:10:16 +0000 (Sun, 10 Feb 2008) | 1 line Fix links on the home page as well. ........ r43213 | danieljames | 2008-02-10 16:21:22 +0000 (Sun, 10 Feb 2008) | 1 line Generated documentation which is no longer generated. ........ [SVN r43214] --- cast.htm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cast.htm b/cast.htm index 587d08a..cb8add6 100644 --- a/cast.htm +++ b/cast.htm @@ -120,10 +120,10 @@ void f( Fruit * fruit ) {

      polymorphic_cast was suggested by Bjarne Stroustrup in "The C++ Programming Language".
      polymorphic_downcast was contributed by Dave Abrahams.
      + "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams.
      An old numeric_cast
      that was contributed by Kevlin Henney is now superseeded by the Boost Numeric Conversion Library

      + "http://www.boost.org/people/kevlin_henney.htm">Kevlin Henney is now superseeded by the Boost Numeric Conversion Library


      Revised From 2ce20c872e49d0cfc85be1db56d17b47e12fdd86 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Thu, 7 Aug 2008 22:12:31 +0000 Subject: [PATCH 14/16] Fix link to numeric conversion library Merged revisions 48003 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r48003 | danieljames | 2008-08-06 16:28:17 +0100 (Wed, 06 Aug 2008) | 1 line Fix link to numeric conversion library. ........ [SVN r48026] --- cast.htm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cast.htm b/cast.htm index cb8add6..a1580c3 100644 --- a/cast.htm +++ b/cast.htm @@ -123,7 +123,7 @@ void f( Fruit * fruit ) { "http://www.boost.org/people/dave_abrahams.htm">Dave Abrahams.
      An old numeric_cast
      that was contributed by Kevlin Henney is now superseeded by the Boost Numeric Conversion Library

      + "http://www.boost.org/people/kevlin_henney.htm">Kevlin Henney is now superseeded by the Boost Numeric Conversion Library


      Revised From 39329bc5b99a8c36d3c7bbb0c7ad7c207ffea4af Mon Sep 17 00:00:00 2001 From: Daniel James Date: Fri, 10 Oct 2008 09:29:21 +0000 Subject: [PATCH 15/16] Merge quickbook workaround and fix some links. Merged revisions 48987,49230-49231 via svnmerge from https://svn.boost.org/svn/boost/trunk ........ r48987 | danieljames | 2008-09-28 13:21:39 +0100 (Sun, 28 Sep 2008) | 1 line Clean up some link errors. ........ r49230 | danieljames | 2008-10-09 23:13:48 +0100 (Thu, 09 Oct 2008) | 1 line position_iterator is meant to be a forward iterator, so avoid using operator+ with it. ........ r49231 | danieljames | 2008-10-09 23:14:14 +0100 (Thu, 09 Oct 2008) | 4 lines Work around the problems with window newlines in position_iterator. (I'm about to fix them, but this will get quickbook working immediately). Fixes #2155 ........ [SVN r49242] --- lexical_cast.htm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lexical_cast.htm b/lexical_cast.htm index 2a08541..c9d37be 100644 --- a/lexical_cast.htm +++ b/lexical_cast.htm @@ -81,7 +81,7 @@ conversions, such as where precision or formatting need tighter control than is offered by the default behavior of lexical_cast, the conventional stringstream approach is recommended. Where the conversions are - numeric to numeric, numeric_cast + numeric to numeric, numeric_cast may offer more reasonable behavior than lexical_cast.

      For a good discussion of the options and issues involved in string-based @@ -202,9 +202,9 @@ public: Lexical conversion to these types is simply reading a byte from source but since the source has more than one byte, the exception is thrown.

      Please use other integer types such as int or short int. If bounds checking - is important, you can also call numeric_cast: + is important, you can also call numeric_cast: -

      numeric_cast<int8_t>(lexical_cast<int>("127"));
      +
      numeric_cast<int8_t>(lexical_cast<int>("127"));

      Q: What does lexical_cast<std::string> of an int8_t or uint8_t not do what I expect?
      A: As above, note that int8_t and uint8_t are actually chars and are formatted as such. To avoid this, cast to an integer type first: From 0c54d6a0377ec91f724ae6f50eccc59cfdb40308 Mon Sep 17 00:00:00 2001 From: Alexander Nasonov Date: Mon, 13 Oct 2008 23:54:01 +0000 Subject: [PATCH 16/16] Merge lexical_cast from trunk r49262 [SVN r49325] --- include/boost/lexical_cast.hpp | 158 ++++++++++++++++----------------- 1 file changed, 77 insertions(+), 81 deletions(-) diff --git a/include/boost/lexical_cast.hpp b/include/boost/lexical_cast.hpp index 44c7a66..bc678fa 100644 --- a/include/boost/lexical_cast.hpp +++ b/include/boost/lexical_cast.hpp @@ -17,17 +17,23 @@ #include #include #include -#include #include #include +#include #include #include #include #include #include +#include #include #include #include +#include + +#ifndef BOOST_NO_STD_LOCALE +#include +#endif #ifdef BOOST_NO_STRINGSTREAM #include @@ -38,13 +44,19 @@ #if defined(BOOST_NO_STRINGSTREAM) || \ defined(BOOST_NO_STD_WSTRING) || \ defined(BOOST_NO_STD_LOCALE) -#define DISABLE_WIDE_CHAR_SUPPORT +#define BOOST_LCAST_NO_WCHAR_T #endif namespace boost { // exception used to indicate runtime lexical_cast failure class bad_lexical_cast : public std::bad_cast + +#if defined(__BORLANDC__) && BOOST_WORKAROUND( __BORLANDC__, < 0x560 ) + // under bcc32 5.5.1 bad_cast doesn't derive from exception + , public std::exception +#endif + { public: bad_lexical_cast() : @@ -94,7 +106,7 @@ namespace boost }; #endif -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T #ifndef BOOST_NO_INTRINSIC_WCHAR_T template<> struct stream_char @@ -219,7 +231,7 @@ namespace boost // lcast_src_length // lcast_src_length -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template<> struct lcast_src_length { @@ -258,7 +270,7 @@ namespace boost static void check_coverage() {} }; -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template<> struct lcast_src_length { @@ -289,7 +301,7 @@ namespace boost static void check_coverage() {} }; -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template<> struct lcast_src_length< wchar_t, std::basic_string > { @@ -327,34 +339,35 @@ namespace boost #endif }; -#define BOOST_AUX_LEXICAL_CAST_DEF1(CharT, T) template<> \ - struct lcast_src_length : lcast_src_length_integral \ +#define BOOST_LCAST_DEF1(CharT, T) \ + template<> struct lcast_src_length \ + : lcast_src_length_integral \ { static void check_coverage() {} }; -#ifdef DISABLE_WIDE_CHAR_SUPPORT -#define BOOST_AUX_LEXICAL_CAST_DEF(T) BOOST_AUX_LEXICAL_CAST_DEF1(char, T) +#ifdef BOOST_LCAST_NO_WCHAR_T +#define BOOST_LCAST_DEF(T) BOOST_LCAST_DEF1(char, T) #else -#define BOOST_AUX_LEXICAL_CAST_DEF(T) \ - BOOST_AUX_LEXICAL_CAST_DEF1(char, T) \ - BOOST_AUX_LEXICAL_CAST_DEF1(wchar_t, T) +#define BOOST_LCAST_DEF(T) \ + BOOST_LCAST_DEF1(char, T) \ + BOOST_LCAST_DEF1(wchar_t, T) #endif - BOOST_AUX_LEXICAL_CAST_DEF(short) - BOOST_AUX_LEXICAL_CAST_DEF(unsigned short) - BOOST_AUX_LEXICAL_CAST_DEF(int) - BOOST_AUX_LEXICAL_CAST_DEF(unsigned int) - BOOST_AUX_LEXICAL_CAST_DEF(long) - BOOST_AUX_LEXICAL_CAST_DEF(unsigned long) + BOOST_LCAST_DEF(short) + BOOST_LCAST_DEF(unsigned short) + BOOST_LCAST_DEF(int) + BOOST_LCAST_DEF(unsigned int) + BOOST_LCAST_DEF(long) + BOOST_LCAST_DEF(unsigned long) #if defined(BOOST_HAS_LONG_LONG) - BOOST_AUX_LEXICAL_CAST_DEF(boost::ulong_long_type) - BOOST_AUX_LEXICAL_CAST_DEF(boost::long_long_type ) + BOOST_LCAST_DEF(boost::ulong_long_type) + BOOST_LCAST_DEF(boost::long_long_type ) #elif defined(BOOST_HAS_MS_INT64) - BOOST_AUX_LEXICAL_CAST_DEF(unsigned __int64) - BOOST_AUX_LEXICAL_CAST_DEF( __int64) + BOOST_LCAST_DEF(unsigned __int64) + BOOST_LCAST_DEF( __int64) #endif -#undef BOOST_AUX_LEXICAL_CAST_DEF -#undef BOOST_AUX_LEXICAL_CAST_DEF1 +#undef BOOST_LCAST_DEF +#undef BOOST_LCAST_DEF1 #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION // Helper for floating point types. @@ -400,7 +413,7 @@ namespace boost static void check_coverage() {} }; -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template<> struct lcast_src_length : lcast_src_length_floating @@ -422,7 +435,7 @@ namespace boost static void check_coverage() {} }; -#endif // #ifndef DISABLE_WIDE_CHAR_SUPPORT +#endif // #ifndef BOOST_LCAST_NO_WCHAR_T #endif // #ifndef BOOST_LCAST_NO_COMPILE_TIME_PRECISION } @@ -437,7 +450,7 @@ namespace boost BOOST_STATIC_CONSTANT(char, minus = '-'); }; -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template<> struct lcast_char_constants { @@ -460,37 +473,21 @@ namespace boost # pragma warning( push ) // C4146: unary minus operator applied to unsigned type, result still unsigned # pragma warning( disable : 4146 ) +#elif defined( __BORLANDC__ ) +# pragma option push -w-8041 #endif - - inline unsigned int lcast_to_unsigned(int value) + template + inline + BOOST_DEDUCED_TYPENAME make_unsigned::type lcast_to_unsigned(T value) { - unsigned int uval = value; - return value < 0 ? -uval : uval; + typedef BOOST_DEDUCED_TYPENAME make_unsigned::type result_type; + result_type uvalue = static_cast(value); + return value < 0 ? -uvalue : uvalue; } - - inline unsigned long lcast_to_unsigned(long value) - { - unsigned long uval = value; - return value < 0 ? -uval : uval; - } - -#if defined(BOOST_HAS_LONG_LONG) - inline boost::ulong_long_type lcast_to_unsigned(boost::long_long_type v) - { - boost::ulong_long_type uval = v; - return v < 0 ? -uval : uval; - } -#elif defined(BOOST_HAS_MS_INT64) - inline unsigned __int64 lcast_to_unsigned(__int64 value) - { - unsigned __int64 uval = value; - return value < 0 ? -uval : uval; - } -#endif - #if (defined _MSC_VER) -# pragma warning( pop ) // C4146: unary minus operator applied to unsigned type, - // result still unsigned +# pragma warning( pop ) +#elif defined( __BORLANDC__ ) +# pragma option pop #endif } @@ -502,22 +499,15 @@ namespace boost #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS BOOST_STATIC_ASSERT(!std::numeric_limits::is_signed); #endif - CharT thousands_sep = 0; -#ifdef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE - char const* grouping = ""; - std::size_t const grouping_size = 0; -#else +#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE + // TODO: use BOOST_NO_STD_LOCALE std::locale loc; typedef std::numpunct numpunct; numpunct const& np = BOOST_USE_FACET(numpunct, loc); std::string const& grouping = np.grouping(); std::string::size_type const grouping_size = grouping.size(); - - if(grouping_size) - thousands_sep = np.thousands_sep(); -#endif - + CharT thousands_sep = grouping_size ? np.thousands_sep() : 0; std::string::size_type group = 0; // current group number char last_grp_size = grouping[0] <= 0 ? CHAR_MAX : grouping[0]; // a) Since grouping is const, grouping[grouping.size()] returns 0. @@ -526,14 +516,17 @@ namespace boost #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS BOOST_STATIC_ASSERT(std::numeric_limits::digits10 < CHAR_MAX); #endif + + char left = last_grp_size; +#endif + typedef typename Traits::int_type int_type; CharT const czero = lcast_char_constants::zero; int_type const zero = Traits::to_int_type(czero); - char left = last_grp_size; - do { +#ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE if(left == 0) { ++group; @@ -549,6 +542,8 @@ namespace boost } --left; +#endif + --finish; int_type const digit = static_cast(n % 10U); Traits::assign(*finish, Traits::to_char_type(zero + digit)); @@ -614,7 +609,7 @@ namespace boost stream.str().swap(output); return true; } - #ifndef DISABLE_WIDE_CHAR_SUPPORT + #ifndef BOOST_LCAST_NO_WCHAR_T bool operator>>(std::wstring &output) { stream.str().swap(output); @@ -670,9 +665,10 @@ namespace boost Traits::assign(*p, ch); } -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T static void widen_and_assign(wchar_t* p, char ch) { + // TODO: use BOOST_NO_STD_LOCALE std::locale loc; wchar_t w = BOOST_USE_FACET(std::ctype, loc).widen(ch); Traits::assign(*p, w); @@ -703,9 +699,9 @@ namespace boost public: - lexical_stream_limited_src(CharT* start, CharT* finish) - : start(start) - , finish(finish) + lexical_stream_limited_src(CharT* sta, CharT* fin) + : start(sta) + , finish(fin) {} public: // output @@ -720,7 +716,7 @@ namespace boost bool operator<<(bool); bool operator<<(char); -#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) +#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) bool operator<<(wchar_t); #endif bool operator<<(CharT const*); @@ -785,7 +781,7 @@ namespace boost bool operator>>(std::string&); -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T bool operator>>(std::wstring&); #endif @@ -820,7 +816,7 @@ namespace boost return true; } -#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) +#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) template inline bool lexical_stream_limited_src::operator<<( wchar_t ch) @@ -907,7 +903,7 @@ namespace boost inline bool lexical_stream_limited_src::operator<<( unsigned short n) { - start = lcast_put_unsigned(lcast_to_unsigned(n), finish); + start = lcast_put_unsigned(n, finish); return true; } @@ -994,7 +990,7 @@ namespace boost return true; } -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template inline bool lexical_stream_limited_src::operator>>( std::wstring& str) @@ -1049,7 +1045,7 @@ namespace boost BOOST_STATIC_CONSTANT(bool, value = false); }; -#if !defined(DISABLE_WIDE_CHAR_SUPPORT) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) +#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) template<> struct lcast_streambuf_for_target { @@ -1065,7 +1061,7 @@ namespace boost BOOST_STATIC_CONSTANT(bool, value = false); }; -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template struct lcast_streambuf_for_target< std::basic_string > @@ -1080,7 +1076,7 @@ namespace boost BOOST_STATIC_CONSTANT(bool, value = false); }; -#ifndef DISABLE_WIDE_CHAR_SUPPORT +#ifndef BOOST_LCAST_NO_WCHAR_T template<> struct lcast_streambuf_for_target { @@ -1201,5 +1197,5 @@ namespace boost // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#undef DISABLE_WIDE_CHAR_SUPPORT +#undef BOOST_LCAST_NO_WCHAR_T #endif