mirror of
https://github.com/boostorg/conversion.git
synced 2025-08-02 22:14:28 +02:00
This commit was manufactured by cvs2svn to create tag
'merged_to_RC_1_30_0'. [SVN r19627]
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
#ifndef BOOST_LEXICAL_CAST_INCLUDED
|
||||
#define BOOST_LEXICAL_CAST_INCLUDED
|
||||
|
||||
// boost lexical_cast.hpp header --------------------------------------------//
|
||||
|
||||
// See http://www.boost.org/libs/conversion for documentation.
|
||||
// Boost lexical_cast.hpp header -------------------------------------------//
|
||||
//
|
||||
// See http://www.boost.org for most recent version including 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<65>,
|
||||
// with additional fixes and suggestions from Gennaro Prota,
|
||||
// Dave Abrahams, Daryle Walker, and other Boosters on the list
|
||||
// Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
|
||||
// and other Boosters
|
||||
// when: November 2000, March 2003
|
||||
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/limits.hpp>
|
||||
#include <boost/type_traits.hpp>
|
||||
#include <boost/type_traits/is_pointer.hpp>
|
||||
|
||||
#ifdef BOOST_NO_STRINGSTREAM
|
||||
#include <strstream>
|
||||
@@ -25,6 +26,18 @@
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || \
|
||||
defined(BOOST_NO_STD_WSTRING) || \
|
||||
defined(BOOST_NO_STD_LOCALE) || \
|
||||
defined(BOOST_NO_CWCHAR) || \
|
||||
defined(BOOST_MSVC) && (BOOST_MSVC <= 1200)
|
||||
#define DISABLE_WIDE_CHAR_SUPPORT
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_NO_INTRINSIC_WCHAR_T
|
||||
#include <cwchar>
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
// exception used to indicate runtime lexical_cast failure
|
||||
@@ -69,7 +82,7 @@ namespace boost
|
||||
typedef char type;
|
||||
};
|
||||
|
||||
#ifndef BOOST_NO_STRINGSTREAM
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
template<>
|
||||
struct stream_char<wchar_t>
|
||||
{
|
||||
@@ -140,11 +153,21 @@ namespace boost
|
||||
stream >> output &&
|
||||
(stream >> std::ws).eof();
|
||||
}
|
||||
template<typename Char, typename Traits, typename Allocator>
|
||||
bool operator>>(std::basic_string<Char, Traits, Allocator> &output)
|
||||
bool operator>>(std::string &output)
|
||||
{
|
||||
return std::getline(stream, output, char_type()).eof();
|
||||
#if defined(BOOST_NO_STRINGSTREAM)
|
||||
stream << '\0';
|
||||
#endif
|
||||
output = stream.str();
|
||||
return true;
|
||||
}
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
bool operator>>(std::wstring &output)
|
||||
{
|
||||
output = stream.str();
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
private:
|
||||
typedef typename widest_char<
|
||||
typename stream_char<Target>::type,
|
||||
@@ -152,6 +175,8 @@ namespace boost
|
||||
|
||||
#if defined(BOOST_NO_STRINGSTREAM)
|
||||
std::strstream stream;
|
||||
#elif defined(BOOST_NO_STD_LOCALE)
|
||||
std::stringstream stream;
|
||||
#else
|
||||
std::basic_stringstream<char_type> stream;
|
||||
#endif
|
||||
@@ -178,4 +203,5 @@ namespace boost
|
||||
//
|
||||
// This software is provided "as is" without express or implied warranty.
|
||||
|
||||
#undef DISABLE_WIDE_CHAR_SUPPORT
|
||||
#endif
|
||||
|
@@ -13,7 +13,7 @@
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#if defined(__INTEL_COMPILER)
|
||||
#pragma warning(disable: 383 488 981 1418 1419)
|
||||
#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
|
||||
@@ -22,11 +22,11 @@
|
||||
#include <boost/test/floating_point_comparison.hpp>
|
||||
#include <boost/test/included/unit_test_framework.hpp>
|
||||
|
||||
// If BOOST_NO_STRINGSTREAM is defined wide character support is unavailable,
|
||||
// and all wide character tests are disabled.
|
||||
|
||||
#ifdef BOOST_NO_STRINGSTREAM
|
||||
#define NO_WIDE_CHAR_SUPPORT
|
||||
#if defined(BOOST_NO_STRINGSTREAM) || \
|
||||
defined(BOOST_NO_STD_WSTRING) || \
|
||||
defined(BOOST_NO_STD_LOCALE) || \
|
||||
defined(BOOST_NO_INTRINSIC_WCHAR_T)
|
||||
#define DISABLE_WIDE_CHAR_SUPPORT
|
||||
#endif
|
||||
|
||||
using namespace boost;
|
||||
@@ -36,6 +36,7 @@ void test_conversion_to_int();
|
||||
void test_conversion_to_double();
|
||||
void test_conversion_to_bool();
|
||||
void test_conversion_to_string();
|
||||
void test_conversion_from_to_wchar_t_alias();
|
||||
void test_conversion_to_pointer();
|
||||
void test_conversion_from_wchar_t();
|
||||
void test_conversion_to_wchar_t();
|
||||
@@ -50,9 +51,10 @@ unit_test_framework::test_suite *init_unit_test_suite(int, char **)
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_int));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_double));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_bool));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_from_to_wchar_t_alias));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_pointer));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_string));
|
||||
#ifndef NO_WIDE_CHAR_SUPPORT
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_from_wchar_t));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_to_wchar_t));
|
||||
suite->add(BOOST_TEST_CASE(test_conversion_from_wstring));
|
||||
@@ -75,10 +77,6 @@ void test_conversion_to_char()
|
||||
BOOST_CHECK_EQUAL(' ', lexical_cast<char>(" "));
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL('A', lexical_cast<char>("A"));
|
||||
BOOST_CHECK_EQUAL(' ', lexical_cast<char>(" "));
|
||||
BOOST_CHECK_THROW(lexical_cast<char>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<char>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL('A', lexical_cast<char>(std::string("A")));
|
||||
BOOST_CHECK_EQUAL(' ', lexical_cast<char>(std::string(" ")));
|
||||
BOOST_CHECK_THROW(
|
||||
@@ -109,8 +107,6 @@ void test_conversion_to_int()
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>("123"));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(123,lexical_cast<int>(std::string("123")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::string(" 123")), boost::bad_lexical_cast);
|
||||
@@ -135,9 +131,6 @@ void test_conversion_to_double()
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>("1.23"));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>("1.23"));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>(std::string("1.23")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<double>(std::string("")), boost::bad_lexical_cast);
|
||||
@@ -163,8 +156,6 @@ void test_conversion_to_bool()
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>("1"));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>("0"));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>("Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(std::string("1")));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(std::string("0")));
|
||||
BOOST_CHECK_THROW(
|
||||
@@ -175,7 +166,6 @@ void test_conversion_to_bool()
|
||||
|
||||
void test_conversion_to_string()
|
||||
{
|
||||
// *** All the following gives compilation error (ambiguity) on MSVC 6
|
||||
BOOST_CHECK_EQUAL("A", lexical_cast<std::string>('A'));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(' '));
|
||||
BOOST_CHECK_EQUAL("123", lexical_cast<std::string>(123));
|
||||
@@ -186,31 +176,36 @@ void test_conversion_to_string()
|
||||
BOOST_CHECK_EQUAL("Test", lexical_cast<std::string>("Test"));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(" "));
|
||||
BOOST_CHECK_EQUAL("", lexical_cast<std::string>(""));
|
||||
BOOST_CHECK_EQUAL("Test", lexical_cast<std::string>("Test"));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(" "));
|
||||
BOOST_CHECK_EQUAL("", lexical_cast<std::string>(""));
|
||||
BOOST_CHECK_EQUAL("Test", lexical_cast<std::string>(std::string("Test")));
|
||||
BOOST_CHECK_EQUAL(" ", lexical_cast<std::string>(std::string(" ")));
|
||||
BOOST_CHECK_EQUAL("", lexical_cast<std::string>(std::string("")));
|
||||
}
|
||||
|
||||
void test_conversion_from_to_wchar_t_alias()
|
||||
{
|
||||
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned short>("123"));
|
||||
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned int>("123"));
|
||||
BOOST_CHECK_EQUAL(123u, lexical_cast<unsigned long>("123"));
|
||||
BOOST_CHECK_EQUAL(std::string("123"),
|
||||
lexical_cast<std::string>(static_cast<unsigned short>(123)));
|
||||
BOOST_CHECK_EQUAL(std::string("123"), lexical_cast<std::string>(123u));
|
||||
BOOST_CHECK_EQUAL(std::string("123"), lexical_cast<std::string>(123ul));
|
||||
}
|
||||
|
||||
void test_conversion_to_pointer()
|
||||
{
|
||||
BOOST_CHECK_THROW(lexical_cast<char *>("Test"), boost::bad_lexical_cast);
|
||||
#ifndef NO_WIDE_CHAR_SUPPORT
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t *>("Test"), boost::bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversion_from_wchar_t()
|
||||
{
|
||||
#ifndef NO_WIDE_CHAR_SUPPORT
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
BOOST_CHECK_EQUAL(1, lexical_cast<int>(L'1'));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L'A'), boost::bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>(L"123"));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L"Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>(L"123"));
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<int>(L"Test"), boost::bad_lexical_cast);
|
||||
@@ -218,9 +213,6 @@ void test_conversion_from_wchar_t()
|
||||
BOOST_CHECK_EQUAL(1.0, lexical_cast<double>(L'1'));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L'A'), boost::bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>(L"1.23"));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L"Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>(L"1.23"));
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<double>(L"Test"), boost::bad_lexical_cast);
|
||||
@@ -232,16 +224,12 @@ void test_conversion_from_wchar_t()
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(L"0"));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L"Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(L"1"));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(L"0"));
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<bool>(L"Test"), boost::bad_lexical_cast);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversion_to_wchar_t()
|
||||
{
|
||||
#ifndef NO_WIDE_CHAR_SUPPORT
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
BOOST_CHECK_EQUAL(L'1', lexical_cast<wchar_t>(1));
|
||||
BOOST_CHECK_EQUAL(L'0', lexical_cast<wchar_t>(0));
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(123), boost::bad_lexical_cast);
|
||||
@@ -255,10 +243,6 @@ void test_conversion_to_wchar_t()
|
||||
BOOST_CHECK_EQUAL(L' ', lexical_cast<wchar_t>(L" "));
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(L"Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(L'A', lexical_cast<wchar_t>(L"A"));
|
||||
BOOST_CHECK_EQUAL(L' ', lexical_cast<wchar_t>(L" "));
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(L""), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(lexical_cast<wchar_t>(L"Test"), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_EQUAL(L'A', lexical_cast<wchar_t>(std::wstring(L"A")));
|
||||
BOOST_CHECK_EQUAL(L' ', lexical_cast<wchar_t>(std::wstring(L" ")));
|
||||
BOOST_CHECK_THROW(
|
||||
@@ -270,19 +254,13 @@ void test_conversion_to_wchar_t()
|
||||
|
||||
void test_conversion_from_wstring()
|
||||
{
|
||||
#ifndef NO_WIDE_CHAR_SUPPORT
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
BOOST_CHECK_EQUAL(123, lexical_cast<int>(std::wstring(L"123")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::wstring(L"")), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<int>(std::wstring(L"Test")), boost::bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_EQUAL(1.23, lexical_cast<double>(std::wstring(L"1.23")));
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<double>(std::wstring(L"")), boost::bad_lexical_cast);
|
||||
BOOST_CHECK_THROW(
|
||||
lexical_cast<double>(std::wstring(L"Test")), boost::bad_lexical_cast);
|
||||
|
||||
BOOST_CHECK_EQUAL(true, lexical_cast<bool>(std::wstring(L"1")));
|
||||
BOOST_CHECK_EQUAL(false, lexical_cast<bool>(std::wstring(L"0")));
|
||||
BOOST_CHECK_THROW(
|
||||
@@ -294,7 +272,7 @@ void test_conversion_from_wstring()
|
||||
|
||||
void test_conversion_to_wstring()
|
||||
{
|
||||
#ifndef NO_WIDE_CHAR_SUPPORT
|
||||
#ifndef DISABLE_WIDE_CHAR_SUPPORT
|
||||
BOOST_CHECK(L"123" == lexical_cast<std::wstring>(123));
|
||||
BOOST_CHECK(L"1.23" == lexical_cast<std::wstring>(1.23));
|
||||
BOOST_CHECK(L"1.111111111" == lexical_cast<std::wstring>(1.111111111));
|
||||
@@ -305,9 +283,6 @@ void test_conversion_to_wstring()
|
||||
BOOST_CHECK(L"Test" == lexical_cast<std::wstring>(L"Test"));
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(L" "));
|
||||
BOOST_CHECK(L"" == lexical_cast<std::wstring>(L""));
|
||||
BOOST_CHECK(L"Test" == lexical_cast<std::wstring>(L"Test"));
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(L" "));
|
||||
BOOST_CHECK(L"" == lexical_cast<std::wstring>(L""));
|
||||
BOOST_CHECK(L"Test" == lexical_cast<std::wstring>(std::wstring(L"Test")));
|
||||
BOOST_CHECK(L" " == lexical_cast<std::wstring>(std::wstring(L" ")));
|
||||
BOOST_CHECK(L"" == lexical_cast<std::wstring>(std::wstring(L"")));
|
||||
|
Reference in New Issue
Block a user