mirror of
https://github.com/boostorg/conversion.git
synced 2025-08-03 22:44:32 +02:00
Quickbook: Merge from trunk to quickbook-dev.
[SVN r76255]
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 :]
|
||||
|
||||
* Restored 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,8 +17,8 @@
|
||||
// enhanced with contributions from Terje Slettebo,
|
||||
// with additional fixes and suggestions from Gennaro Prota,
|
||||
// Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
|
||||
// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann
|
||||
// and other Boosters
|
||||
// Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
|
||||
// Cheng Yang, Matthew Bradbury and other Boosters
|
||||
// when: November 2000, March 2003, June 2005, June 2006, March 2011
|
||||
|
||||
#include <climits>
|
||||
@@ -586,7 +586,7 @@ namespace boost
|
||||
--end;
|
||||
value = 0;
|
||||
|
||||
if ( *end < czero || *end >= czero + 10 || begin > end)
|
||||
if (begin > end || *end < czero || *end >= czero + 10)
|
||||
return false;
|
||||
value = *end - czero;
|
||||
--end;
|
||||
@@ -684,6 +684,7 @@ namespace boost
|
||||
, const CharT opening_brace, const CharT closing_brace)
|
||||
{
|
||||
using namespace std;
|
||||
if (begin == end) return false;
|
||||
const CharT minus = lcast_char_constants<CharT>::minus;
|
||||
const CharT plus = lcast_char_constants<CharT>::plus;
|
||||
const int inifinity_size = 8;
|
||||
@@ -743,6 +744,26 @@ namespace boost
|
||||
, L'(', L')');
|
||||
}
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR16_T
|
||||
template <class T>
|
||||
bool parse_inf_nan(const char16_t* begin, const char16_t* end, T& value)
|
||||
{
|
||||
return parse_inf_nan_impl(begin, end, value
|
||||
, u"NAN", u"nan"
|
||||
, u"INFINITY", u"infinity"
|
||||
, u'(', u')');
|
||||
}
|
||||
#endif
|
||||
#ifndef BOOST_NO_CHAR32_T
|
||||
template <class T>
|
||||
bool parse_inf_nan(const char32_t* begin, const char32_t* end, T& value)
|
||||
{
|
||||
return parse_inf_nan_impl(begin, end, value
|
||||
, U"NAN", U"nan"
|
||||
, U"INFINITY", U"infinity"
|
||||
, U'(', U')');
|
||||
}
|
||||
#endif
|
||||
|
||||
template <class CharT, class T>
|
||||
bool parse_inf_nan(const CharT* begin, const CharT* end, T& value)
|
||||
@@ -863,7 +884,7 @@ namespace boost
|
||||
CharT const thousands_sep = grouping_size ? np.thousands_sep() : 0;
|
||||
CharT const decimal_point = np.decimal_point();
|
||||
bool found_grouping = false;
|
||||
unsigned int last_grouping_pos = grouping_size - 1;
|
||||
std::string::size_type last_grouping_pos = grouping_size - 1;
|
||||
#else
|
||||
CharT const decimal_point = lcast_char_constants<CharT>::c_decimal_separator;
|
||||
#endif
|
||||
@@ -1183,7 +1204,7 @@ namespace boost
|
||||
bool const result = !(stream << input).fail();
|
||||
start = stringbuffer.pbase();
|
||||
finish = stringbuffer.pptr();
|
||||
return result && (start != finish);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
@@ -1354,9 +1375,11 @@ namespace boost
|
||||
|
||||
/************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
|
||||
private:
|
||||
|
||||
template <typename Type>
|
||||
bool shr_unsigned(Type& output)
|
||||
{
|
||||
if (start == finish) return false;
|
||||
CharT const minus = lcast_char_constants<CharT>::minus;
|
||||
CharT const plus = lcast_char_constants<CharT>::plus;
|
||||
bool has_minus = false;
|
||||
@@ -1391,6 +1414,7 @@ namespace boost
|
||||
template <typename Type>
|
||||
bool shr_signed(Type& output)
|
||||
{
|
||||
if (start == finish) return false;
|
||||
CharT const minus = lcast_char_constants<CharT>::minus;
|
||||
CharT const plus = lcast_char_constants<CharT>::plus;
|
||||
typedef BOOST_DEDUCED_TYPENAME make_unsigned<Type>::type utype;
|
||||
@@ -1480,7 +1504,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); }
|
||||
@@ -1493,11 +1517,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
|
||||
|
@@ -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 ]
|
||||
@@ -30,6 +35,7 @@ test-suite conversion
|
||||
[ 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 ]
|
||||
;
|
||||
|
||||
|
||||
|
@@ -22,29 +22,120 @@
|
||||
|
||||
using namespace boost;
|
||||
|
||||
void test_empty_iterator_range()
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
|
||||
#define BOOST_LCAST_NO_WCHAR_T
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
void do_test_on_empty_input(T& v)
|
||||
{
|
||||
boost::iterator_range<const char*> v;
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<float>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<long double>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned int>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<std::string>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned short>(v), bad_lexical_cast);
|
||||
#if defined(BOOST_HAS_LONG_LONG)
|
||||
BOOST_CHECK_THROW(lexical_cast<boost::ulong_long_type>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<boost::long_long_type>(v), bad_lexical_cast);
|
||||
#elif defined(BOOST_HAS_MS_INT64)
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned __int64>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<__int64>(v), bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_empty_iterator_range()
|
||||
{
|
||||
|
||||
boost::iterator_range<char*> v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(v), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
|
||||
boost::iterator_range<const char*> cv;
|
||||
do_test_on_empty_input(cv);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(cv), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(cv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(cv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(cv), bad_lexical_cast);
|
||||
|
||||
const boost::iterator_range<const char*> ccv;
|
||||
do_test_on_empty_input(ccv);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(ccv), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(ccv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(ccv), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(ccv), bad_lexical_cast);
|
||||
}
|
||||
|
||||
void test_empty_string()
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(std::string()), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<float>(std::string()), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(std::string()), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<long double>(std::string()), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned int>(std::string()), bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(lexical_cast<std::string>(std::string()), std::string());
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned short>(std::string()), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(std::string()), bad_lexical_cast);
|
||||
std::string v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
|
||||
#ifndef BOOST_LCAST_NO_WCHAR_T
|
||||
std::wstring vw;
|
||||
do_test_on_empty_input(vw);
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(vw), bad_lexical_cast);
|
||||
#endif
|
||||
|
||||
// Currently, no compiler and STL library fully support char16_t and char32_t
|
||||
//#ifndef BOOST_NO_CHAR16_T
|
||||
// std::basic_string<char16_t> v16w;
|
||||
// do_test_on_empty_input(v16w);
|
||||
// BOOST_CHECK_THROW(lexical_cast<char16_t>(v16w), bad_lexical_cast);
|
||||
//#endif
|
||||
//#ifndef BOOST_NO_CHAR32_T
|
||||
// std::basic_string<char32_t> v32w;
|
||||
// do_test_on_empty_input(v32w);
|
||||
// BOOST_CHECK_THROW(lexical_cast<char32_t>(v32w), bad_lexical_cast);
|
||||
//#endif
|
||||
}
|
||||
|
||||
struct Escape
|
||||
{
|
||||
Escape(const std::string& s)
|
||||
: str_(s)
|
||||
{}
|
||||
|
||||
std::string str_;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<< (std::ostream& o, const Escape& rhs)
|
||||
{
|
||||
return o << rhs.str_;
|
||||
}
|
||||
|
||||
void test_empty_user_class()
|
||||
{
|
||||
Escape v("");
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
namespace std {
|
||||
inline std::ostream & operator<<(std::ostream & out, const std::vector<long> & v)
|
||||
{
|
||||
std::ostream_iterator<long> it(out);
|
||||
std::copy(v.begin(), v.end(), it);
|
||||
assert(out);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
void test_empty_vector()
|
||||
{
|
||||
std::vector<long> v;
|
||||
do_test_on_empty_input(v);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<unsigned char>(v), bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<signed char>(v), bad_lexical_cast);
|
||||
}
|
||||
|
||||
unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
@@ -53,6 +144,8 @@ unit_test::test_suite *init_unit_test_suite(int, char *[])
|
||||
BOOST_TEST_SUITE("lexical_cast. Empty input unit test");
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_iterator_range));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_string));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_user_class));
|
||||
suite->add(BOOST_TEST_CASE(&test_empty_vector));
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
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;
|
||||
}
|
Reference in New Issue
Block a user