forked from boostorg/conversion
Merge from trunk r75937
* fixed a lot of wchar_t errors * Optimizations for boost::containers::basic_string * More tests [SVN r76062]
This commit is contained in:
@@ -159,9 +159,21 @@ Read a good C++ book, study `std::sentry` and [@boost:libs/io/doc/ios_state.html
|
||||
the rules of `scanf` for conversions. And in the C99 standard for unsigned input value minus sign is optional, so
|
||||
if a negative number is read, no errors will arise and the result will be the two's complement.
|
||||
|
||||
[pre
|
||||
]
|
||||
|
||||
* [*Question:] Why `boost::lexical_cast<int>(L'A');` outputs 65 and `boost::lexical_cast<wchar_t>(L"65");` does not throw?
|
||||
* [*Answer:] If you are using an old version of Visual Studio or compile code with /Zc:wchar_t- flag,
|
||||
`boost::lexical_cast` sees single `wchar_t` character as `unsigned short`. It is not a `boost::lexical_cast` mistake, but a
|
||||
limitation of compiler options that you use.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Changes]
|
||||
* [*boost 1.49.0 :]
|
||||
|
||||
* Added code to work with typedefed wchar_t (compilation flag /Zc:wchar_t- for Visual Studio).
|
||||
|
||||
* [*boost 1.48.0 :]
|
||||
|
||||
* Added code to work with Inf and NaN on any platform.
|
||||
|
@@ -17,7 +17,8 @@
|
||||
// enhanced with contributions from Terje Slettebo,
|
||||
// with additional fixes and suggestions from Gennaro Prota,
|
||||
// Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
|
||||
// Alexander Nasonov, Antony Polukhin and other Boosters
|
||||
// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
|
||||
// Cheng Yang and other Boosters
|
||||
// when: November 2000, March 2003, June 2005, June 2006, March 2011
|
||||
|
||||
#include <climits>
|
||||
@@ -46,8 +47,10 @@
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/detail/lcast_precision.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <cwchar>
|
||||
|
||||
#include <boost/container/container_fwd.hpp>
|
||||
#ifndef BOOST_NO_CWCHAR
|
||||
# include <cwchar>
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_NO_STD_LOCALE
|
||||
# include <locale>
|
||||
@@ -145,6 +148,12 @@ namespace boost
|
||||
{
|
||||
typedef CharT type;
|
||||
};
|
||||
|
||||
template<class CharT, class Traits, class Alloc>
|
||||
struct stream_char< ::boost::container::basic_string<CharT,Traits,Alloc> >
|
||||
{
|
||||
typedef CharT type;
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
@@ -259,6 +268,24 @@ namespace boost
|
||||
typedef Traits type;
|
||||
};
|
||||
|
||||
template<class CharT, class Traits, class Alloc, class Source>
|
||||
struct deduce_char_traits< CharT
|
||||
, ::boost::container::basic_string<CharT,Traits,Alloc>
|
||||
, Source
|
||||
>
|
||||
{
|
||||
typedef Traits type;
|
||||
};
|
||||
|
||||
template<class CharT, class Target, class Traits, class Alloc>
|
||||
struct deduce_char_traits< CharT
|
||||
, Target
|
||||
, ::boost::container::basic_string<CharT,Traits,Alloc>
|
||||
>
|
||||
{
|
||||
typedef Traits type;
|
||||
};
|
||||
|
||||
template<class CharT, class Traits, class Alloc1, class Alloc2>
|
||||
struct deduce_char_traits< CharT
|
||||
, std::basic_string<CharT,Traits,Alloc1>
|
||||
@@ -267,6 +294,15 @@ namespace boost
|
||||
{
|
||||
typedef Traits type;
|
||||
};
|
||||
|
||||
template<class CharT, class Traits, class Alloc1, class Alloc2>
|
||||
struct deduce_char_traits< CharT
|
||||
, ::boost::container::basic_string<CharT,Traits,Alloc1>
|
||||
, ::boost::container::basic_string<CharT,Traits,Alloc2>
|
||||
>
|
||||
{
|
||||
typedef Traits type;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -648,8 +684,8 @@ namespace boost
|
||||
, const CharT opening_brace, const CharT closing_brace)
|
||||
{
|
||||
using namespace std;
|
||||
const wchar_t minus = lcast_char_constants<wchar_t>::minus;
|
||||
const wchar_t plus = lcast_char_constants<wchar_t>::plus;
|
||||
const CharT minus = lcast_char_constants<CharT>::minus;
|
||||
const CharT plus = lcast_char_constants<CharT>::plus;
|
||||
const int inifinity_size = 8;
|
||||
|
||||
bool has_minus = false;
|
||||
@@ -1257,6 +1293,14 @@ namespace boost
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class Alloc>
|
||||
bool operator<<(::boost::container::basic_string<CharT,Traits,Alloc> const& str)
|
||||
{
|
||||
start = const_cast<CharT*>(str.data());
|
||||
finish = start + str.length();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator<<(bool value)
|
||||
{
|
||||
CharT const czero = lcast_char_constants<CharT>::zero;
|
||||
@@ -1310,6 +1354,7 @@ namespace boost
|
||||
|
||||
/************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
|
||||
private:
|
||||
|
||||
template <typename Type>
|
||||
bool shr_unsigned(Type& output)
|
||||
{
|
||||
@@ -1436,7 +1481,7 @@ namespace boost
|
||||
}
|
||||
|
||||
/************************************ OPERATORS >> ( ... ) ********************************/
|
||||
public:
|
||||
public:
|
||||
bool operator>>(unsigned short& output) { return shr_unsigned(output); }
|
||||
bool operator>>(unsigned int& output) { return shr_unsigned(output); }
|
||||
bool operator>>(unsigned long int& output) { return shr_unsigned(output); }
|
||||
@@ -1449,11 +1494,19 @@ namespace boost
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
bool operator>>(unsigned __int64& output) { return shr_unsigned(output); }
|
||||
bool operator>>(__int64& output) { return shr_signed(output); }
|
||||
|
||||
#endif
|
||||
bool operator>>(CharT& output) { return shr_xchar(output); }
|
||||
bool operator>>(char& output) { return shr_xchar(output); }
|
||||
bool operator>>(unsigned char& output) { return shr_xchar(output); }
|
||||
bool operator>>(signed char& output) { return shr_xchar(output); }
|
||||
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
bool operator>>(wchar_t& output) { return shr_xchar(output); }
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR16_T
|
||||
bool operator>>(char16_t& output) { return shr_xchar(output); }
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR32_T
|
||||
bool operator>>(char32_t& output) { return shr_xchar(output); }
|
||||
#endif
|
||||
#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
bool operator>>(std::string& str) { str.assign(start, finish); return true; }
|
||||
# ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
@@ -1462,6 +1515,9 @@ namespace boost
|
||||
#else
|
||||
template<class Alloc>
|
||||
bool operator>>(std::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
|
||||
|
||||
template<class Alloc>
|
||||
bool operator>>(::boost::container::basic_string<CharT,Traits,Alloc>& str) { str.assign(start, finish); return true; }
|
||||
#endif
|
||||
/*
|
||||
* case "-0" || "0" || "+0" : output = false; return true;
|
||||
@@ -1598,6 +1654,12 @@ namespace boost
|
||||
BOOST_STATIC_CONSTANT(bool, value = true );
|
||||
};
|
||||
|
||||
template<typename CharT, typename Traits, typename Alloc>
|
||||
struct is_stdstring< ::boost::container::basic_string<CharT, Traits, Alloc> >
|
||||
{
|
||||
BOOST_STATIC_CONSTANT(bool, value = true );
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct is_char_or_wchar
|
||||
{
|
||||
@@ -1698,6 +1760,18 @@ namespace boost
|
||||
BOOST_STATIC_CONSTANT(bool, value = true );
|
||||
};
|
||||
|
||||
template<typename CharT, typename Traits, typename Alloc>
|
||||
struct is_char_array_to_stdstring< ::boost::container::basic_string<CharT, Traits, Alloc>, CharT* >
|
||||
{
|
||||
BOOST_STATIC_CONSTANT(bool, value = true );
|
||||
};
|
||||
|
||||
template<typename CharT, typename Traits, typename Alloc>
|
||||
struct is_char_array_to_stdstring< ::boost::container::basic_string<CharT, Traits, Alloc>, const CharT* >
|
||||
{
|
||||
BOOST_STATIC_CONSTANT(bool, value = true );
|
||||
};
|
||||
|
||||
#if (defined _MSC_VER)
|
||||
# pragma warning( push )
|
||||
# pragma warning( disable : 4701 ) // possible use of ... before initialization
|
||||
|
@@ -110,7 +110,7 @@ void test_char32_conversions();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_char));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_int));
|
||||
|
@@ -14,7 +14,12 @@
|
||||
|
||||
# bring in rules for testing
|
||||
import testing ;
|
||||
import feature ;
|
||||
|
||||
feature.feature nowchar : on :
|
||||
composite optional propagated link-incompatible ;
|
||||
feature.compose <nowchar>on : <cxxflags>/Zc:wchar_t- ;
|
||||
|
||||
test-suite conversion
|
||||
: [ run implicit_cast.cpp ]
|
||||
[ compile-fail implicit_cast_fail.cpp ]
|
||||
@@ -28,7 +33,9 @@ test-suite conversion
|
||||
[ run lexical_cast_wchars_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_float_types_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_inf_nan_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_containers_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ run lexical_cast_empty_input_test.cpp ../../test/build//boost_unit_test_framework/<link>static ]
|
||||
[ compile lexical_cast_typedefed_wchar_test.cpp : <toolset>msvc:<nowchar>on ]
|
||||
[ run lexical_cast_typedefed_wchar_test_runtime.cpp ../../test/build//boost_unit_test_framework/<link>static : : : <toolset>msvc:<nowchar>on ]
|
||||
;
|
||||
|
||||
|
||||
|
@@ -28,7 +28,7 @@ void test_abstract();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_abstract));
|
||||
|
||||
|
38
test/lexical_cast_containers_test.cpp
Normal file
38
test/lexical_cast_containers_test.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Testing boost::lexical_cast with boost::container::string.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/container/string.hpp>
|
||||
|
||||
void testing_boost_containers_basic_string();
|
||||
|
||||
using namespace boost;
|
||||
|
||||
boost::unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("Testing boost::lexical_cast with boost::container::string");
|
||||
suite->add(BOOST_TEST_CASE(testing_boost_containers_basic_string));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
||||
void testing_boost_containers_basic_string()
|
||||
{
|
||||
BOOST_CHECK("100" == lexical_cast<boost::container::string>("100"));
|
||||
BOOST_CHECK(L"100" == lexical_cast<boost::container::wstring>(L"100"));
|
||||
|
||||
BOOST_CHECK("100" == lexical_cast<boost::container::string>(100));
|
||||
boost::container::string str("1000");
|
||||
BOOST_CHECK(1000 == lexical_cast<int>(str));
|
||||
}
|
||||
|
||||
|
@@ -49,7 +49,7 @@ void test_empty_string()
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast. Empty input unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_iterator_range));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_string));
|
||||
|
@@ -31,7 +31,7 @@ using namespace boost;
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast float types unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_float));
|
||||
suite->add(BOOST_TEST_CASE(&test_conversion_from_to_double));
|
||||
|
@@ -170,7 +170,7 @@ void test_inf_nan_long_double()
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast inf anf nan parsing unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_inf_nan_float));
|
||||
suite->add(BOOST_TEST_CASE(&test_inf_nan_double));
|
||||
|
@@ -30,7 +30,7 @@ void test_round_conversion_long_double();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::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));
|
||||
|
@@ -28,7 +28,7 @@ void test_noncopyable();
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_noncopyable));
|
||||
|
||||
|
25
test/lexical_cast_typedefed_wchar_test.cpp
Executable file
25
test/lexical_cast_typedefed_wchar_test.cpp
Executable file
@@ -0,0 +1,25 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/static_assert.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
#ifdef BOOST_MSVC
|
||||
BOOST_STATIC_ASSERT((boost::is_same<wchar_t, unsigned short>::value));
|
||||
#endif
|
||||
|
||||
return ::boost::lexical_cast<int>(L"1000") == 1000;
|
||||
}
|
||||
|
||||
|
48
test/lexical_cast_typedefed_wchar_test_runtime.cpp
Executable file
48
test/lexical_cast_typedefed_wchar_test_runtime.cpp
Executable file
@@ -0,0 +1,48 @@
|
||||
// Unit test for boost::lexical_cast.
|
||||
//
|
||||
// See http://www.boost.org for most recent version, including documentation.
|
||||
//
|
||||
// Copyright Antony Polukhin, 2011.
|
||||
//
|
||||
// Distributed under the Boost
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#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 <boost/lexical_cast.hpp>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
using namespace boost;
|
||||
|
||||
void test_typedefed_wchar_t_runtime()
|
||||
{
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
#ifdef BOOST_MSVC
|
||||
BOOST_STATIC_ASSERT((boost::is_same<wchar_t, unsigned short>::value));
|
||||
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<int>(L'A'), 65);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<int>(L'B'), 66);
|
||||
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(L"65"), 65);
|
||||
BOOST_CHECK_EQUAL(boost::lexical_cast<wchar_t>(L"66"), 66);
|
||||
#endif
|
||||
#endif
|
||||
BOOST_CHECK(1);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast typedefed wchar_t runtime test");
|
||||
suite->add(BOOST_TEST_CASE(&test_typedefed_wchar_t_runtime));
|
||||
|
||||
return suite;
|
||||
}
|
@@ -60,7 +60,7 @@ void test_vc8_bug()
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast vc8 bug unit test");
|
||||
suite->add(BOOST_TEST_CASE(test_vc8_bug));
|
||||
return suite;
|
||||
|
@@ -48,7 +48,7 @@ void test_char_types_conversions()
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
{
|
||||
unit_test_framework::test_suite *suite =
|
||||
unit_test::test_suite *suite =
|
||||
BOOST_TEST_SUITE("lexical_cast char<->wchar_t unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_char_types_conversions));
|
||||
|
||||
|
Reference in New Issue
Block a user