lexical_cast.hpp improvements: fix bug with floats conversions and improve docs (refs #9046)

[SVN r85852]
This commit is contained in:
Antony Polukhin
2013-09-23 11:59:06 +00:00
parent ce7a54af7f
commit 701963d3c4
2 changed files with 30 additions and 29 deletions

View File

@@ -44,16 +44,19 @@ std::string stringize(const Sequence& seq) {
} }
//` Step 3: Using the `stringize` with different types: //` Step 3: Using the `stringize` with different types:
#include <iostream> #include <cassert>
#include <boost/fusion/adapted/boost_tuple.hpp> #include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/adapted/std_pair.hpp> #include <boost/fusion/adapted/std_pair.hpp>
int main() { int main() {
boost::tuple<char, int, char, int> decim('-', 10, 'e', 5); boost::tuple<char, int, char, int> decim('-', 10, 'e', 5);
std::pair<short, std::string> value_and_type(270, "Kelvin"); assert(stringize(decim) == "-10e5");
std::cout << stringize(decim) << '\n' // outputs '-10e5' std::pair<short, std::string> value_and_type(270, "Kelvin");
<< stringize(value_and_type); // outputs '270Kelvin' assert(stringize(value_and_type) == "270Kelvin");
} }
//] [/lexical_cast_stringize] //] [/lexical_cast_stringize]

View File

@@ -1446,74 +1446,74 @@ namespace boost {
} }
template <class T, class SomeCharT> template <class T, class SomeCharT>
bool shl_real_type(const T& val, SomeCharT* /*begin*/, SomeCharT*& /*end*/) { bool shl_real_type(const T& val, SomeCharT* /*begin*/) {
lcast_set_precision(out_stream, &val); lcast_set_precision(out_stream, &val);
return shl_input_streamable(val); return shl_input_streamable(val);
} }
static bool shl_real_type(float val, char* begin, char*& end) { bool shl_real_type(float val, char* begin) {
using namespace std; using namespace std;
const double val_as_double = val; const double val_as_double = val;
end = begin + finish = start +
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
sprintf_s(begin, end-begin, sprintf_s(begin, CharacterBufferSize,
#else #else
sprintf(begin, sprintf(begin,
#endif #endif
"%.*g", static_cast<int>(boost::detail::lcast_get_precision<float>()), val_as_double); "%.*g", static_cast<int>(boost::detail::lcast_get_precision<float>()), val_as_double);
return end > begin; return finish > start;
} }
static bool shl_real_type(double val, char* begin, char*& end) { bool shl_real_type(double val, char* begin) {
using namespace std; using namespace std;
end = begin + finish = start +
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
sprintf_s(begin, end-begin, sprintf_s(begin, CharacterBufferSize,
#else #else
sprintf(begin, sprintf(begin,
#endif #endif
"%.*g", static_cast<int>(boost::detail::lcast_get_precision<double>()), val); "%.*g", static_cast<int>(boost::detail::lcast_get_precision<double>()), val);
return end > begin; return finish > start;
} }
#ifndef __MINGW32__ #ifndef __MINGW32__
static bool shl_real_type(long double val, char* begin, char*& end) { bool shl_real_type(long double val, char* begin) {
using namespace std; using namespace std;
end = begin + finish = start +
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
sprintf_s(begin, end-begin, sprintf_s(begin, CharacterBufferSize,
#else #else
sprintf(begin, sprintf(begin,
#endif #endif
"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double>()), val ); "%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double>()), val );
return end > begin; return finish > start;
} }
#endif #endif
#if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_SWPRINTF) && !defined(__MINGW32__) #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_SWPRINTF) && !defined(__MINGW32__)
static bool shl_real_type(float val, wchar_t* begin, wchar_t*& end) { bool shl_real_type(float val, wchar_t* begin) {
using namespace std; using namespace std;
const double val_as_double = val; const double val_as_double = val;
end = begin + swprintf(begin, end-begin, finish = start + swprintf(begin, CharacterBufferSize,
L"%.*g", L"%.*g",
static_cast<int>(boost::detail::lcast_get_precision<float >()), static_cast<int>(boost::detail::lcast_get_precision<float >()),
val_as_double ); val_as_double );
return end > begin; return finish > start;
} }
static bool shl_real_type(double val, wchar_t* begin, wchar_t*& end) { bool shl_real_type(double val, wchar_t* begin) {
using namespace std; using namespace std;
end = begin + swprintf(begin, end-begin, finish = start + swprintf(begin, CharacterBufferSize,
L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<double >()), val ); L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
return end > begin; return finish > start;
} }
static bool shl_real_type(long double val, wchar_t* begin, wchar_t*& end) { bool shl_real_type(long double val, wchar_t* begin) {
using namespace std; using namespace std;
end = begin + swprintf(begin, end-begin, finish = start + swprintf(begin, CharacterBufferSize,
L"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val ); L"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
return end > begin; return finish > start;
} }
#endif #endif
template <class T> template <class T>
@@ -1524,9 +1524,7 @@ namespace boost {
return true; return true;
} }
bool const result = shl_real_type(val, static_cast<CharT*>(buffer), tmp_finish); return shl_real_type(val, static_cast<CharT*>(buffer));
finish = tmp_finish;
return result;
} }
/************************************ OPERATORS << ( ... ) ********************************/ /************************************ OPERATORS << ( ... ) ********************************/