converter_lexical_streams.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // Copyright Kevlin Henney, 2000-2005.
  2. // Copyright Alexander Nasonov, 2006-2010.
  3. // Copyright Antony Polukhin, 2011-2019.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // what: lexical_cast custom keyword cast
  10. // who: contributed by Kevlin Henney,
  11. // enhanced with contributions from Terje Slettebo,
  12. // with additional fixes and suggestions from Gennaro Prota,
  13. // Beman Dawes, Dave Abrahams, Daryle Walker, Peter Dimov,
  14. // Alexander Nasonov, Antony Polukhin, Justin Viiret, Michael Hofmann,
  15. // Cheng Yang, Matthew Bradbury, David W. Birdsall, Pavel Korzh and other Boosters
  16. // when: November 2000, March 2003, June 2005, June 2006, March 2011 - 2014, Nowember 2016
  17. #ifndef BOOST_LEXICAL_CAST_DETAIL_CONVERTER_LEXICAL_STREAMS_HPP
  18. #define BOOST_LEXICAL_CAST_DETAIL_CONVERTER_LEXICAL_STREAMS_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_WSTRING)
  24. #define BOOST_LCAST_NO_WCHAR_T
  25. #endif
  26. #include <cstddef>
  27. #include <string>
  28. #include <cstring>
  29. #include <cstdio>
  30. #include <boost/limits.hpp>
  31. #include <boost/type_traits/conditional.hpp>
  32. #include <boost/type_traits/is_pointer.hpp>
  33. #include <boost/static_assert.hpp>
  34. #include <boost/detail/workaround.hpp>
  35. #ifndef BOOST_NO_STD_LOCALE
  36. # include <locale>
  37. #else
  38. # ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
  39. // Getting error at this point means, that your STL library is old/lame/misconfigured.
  40. // If nothing can be done with STL library, define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE,
  41. // but beware: lexical_cast will understand only 'C' locale delimeters and thousands
  42. // separators.
  43. # error "Unable to use <locale> header. Define BOOST_LEXICAL_CAST_ASSUME_C_LOCALE to force "
  44. # error "boost::lexical_cast to use only 'C' locale during conversions."
  45. # endif
  46. #endif
  47. #ifdef BOOST_NO_STRINGSTREAM
  48. #include <strstream>
  49. #else
  50. #include <sstream>
  51. #endif
  52. #include <boost/lexical_cast/detail/lcast_char_constants.hpp>
  53. #include <boost/lexical_cast/detail/lcast_unsigned_converters.hpp>
  54. #include <boost/lexical_cast/detail/inf_nan.hpp>
  55. #include <istream>
  56. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  57. #include <array>
  58. #endif
  59. #include <boost/array.hpp>
  60. #include <boost/type_traits/make_unsigned.hpp>
  61. #include <boost/type_traits/is_integral.hpp>
  62. #include <boost/type_traits/is_float.hpp>
  63. #include <boost/range/iterator_range_core.hpp>
  64. #include <boost/container/container_fwd.hpp>
  65. #include <boost/integer.hpp>
  66. #include <boost/detail/basic_pointerbuf.hpp>
  67. #include <boost/noncopyable.hpp>
  68. #ifndef BOOST_NO_CWCHAR
  69. # include <cwchar>
  70. #endif
  71. namespace boost {
  72. namespace detail // basic_unlockedbuf
  73. {
  74. // acts as a stream buffer which wraps around a pair of pointers
  75. // and gives acces to internals
  76. template <class BufferType, class CharT>
  77. class basic_unlockedbuf : public basic_pointerbuf<CharT, BufferType> {
  78. public:
  79. typedef basic_pointerbuf<CharT, BufferType> base_type;
  80. typedef BOOST_DEDUCED_TYPENAME base_type::streamsize streamsize;
  81. #ifndef BOOST_NO_USING_TEMPLATE
  82. using base_type::pptr;
  83. using base_type::pbase;
  84. using base_type::setbuf;
  85. #else
  86. charT* pptr() const { return base_type::pptr(); }
  87. charT* pbase() const { return base_type::pbase(); }
  88. BufferType* setbuf(char_type* s, streamsize n) { return base_type::setbuf(s, n); }
  89. #endif
  90. };
  91. }
  92. namespace detail
  93. {
  94. struct do_not_construct_out_buffer_t{};
  95. struct do_not_construct_out_stream_t{
  96. do_not_construct_out_stream_t(do_not_construct_out_buffer_t*){}
  97. };
  98. template <class CharT, class Traits>
  99. struct out_stream_helper_trait {
  100. #if defined(BOOST_NO_STRINGSTREAM)
  101. typedef std::ostream out_stream_t;
  102. typedef basic_unlockedbuf<std::strstreambuf, char> stringbuffer_t;
  103. #elif defined(BOOST_NO_STD_LOCALE)
  104. typedef std::ostream out_stream_t;
  105. typedef basic_unlockedbuf<std::stringbuf, char> stringbuffer_t;
  106. typedef basic_unlockedbuf<std::streambuf, char> buffer_t;
  107. #else
  108. typedef std::basic_ostream<CharT, Traits> out_stream_t;
  109. typedef basic_unlockedbuf<std::basic_stringbuf<CharT, Traits>, CharT> stringbuffer_t;
  110. typedef basic_unlockedbuf<std::basic_streambuf<CharT, Traits>, CharT> buffer_t;
  111. #endif
  112. };
  113. }
  114. namespace detail // optimized stream wrappers
  115. {
  116. template< class CharT // a result of widest_char transformation
  117. , class Traits
  118. , bool RequiresStringbuffer
  119. , std::size_t CharacterBufferSize
  120. >
  121. class lexical_istream_limited_src: boost::noncopyable {
  122. typedef BOOST_DEDUCED_TYPENAME boost::conditional<
  123. RequiresStringbuffer,
  124. BOOST_DEDUCED_TYPENAME out_stream_helper_trait<CharT, Traits>::out_stream_t,
  125. do_not_construct_out_stream_t
  126. >::type deduced_out_stream_t;
  127. typedef BOOST_DEDUCED_TYPENAME boost::conditional<
  128. RequiresStringbuffer,
  129. BOOST_DEDUCED_TYPENAME out_stream_helper_trait<CharT, Traits>::stringbuffer_t,
  130. do_not_construct_out_buffer_t
  131. >::type deduced_out_buffer_t;
  132. deduced_out_buffer_t out_buffer;
  133. deduced_out_stream_t out_stream;
  134. CharT buffer[CharacterBufferSize];
  135. // After the `operator <<` finishes, `[start, finish)` is
  136. // the range to output by `operator >>`
  137. const CharT* start;
  138. const CharT* finish;
  139. public:
  140. lexical_istream_limited_src() BOOST_NOEXCEPT
  141. : out_buffer()
  142. , out_stream(&out_buffer)
  143. , start(buffer)
  144. , finish(buffer + CharacterBufferSize)
  145. {}
  146. const CharT* cbegin() const BOOST_NOEXCEPT {
  147. return start;
  148. }
  149. const CharT* cend() const BOOST_NOEXCEPT {
  150. return finish;
  151. }
  152. private:
  153. /************************************ HELPER FUNCTIONS FOR OPERATORS << ( ... ) ********************************/
  154. bool shl_char(CharT ch) BOOST_NOEXCEPT {
  155. Traits::assign(buffer[0], ch);
  156. finish = start + 1;
  157. return true;
  158. }
  159. #ifndef BOOST_LCAST_NO_WCHAR_T
  160. template <class T>
  161. bool shl_char(T ch) {
  162. BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)) ,
  163. "boost::lexical_cast does not support narrowing of char types."
  164. "Use boost::locale instead" );
  165. #ifndef BOOST_LEXICAL_CAST_ASSUME_C_LOCALE
  166. std::locale loc;
  167. CharT const w = BOOST_USE_FACET(std::ctype<CharT>, loc).widen(ch);
  168. #else
  169. CharT const w = static_cast<CharT>(ch);
  170. #endif
  171. Traits::assign(buffer[0], w);
  172. finish = start + 1;
  173. return true;
  174. }
  175. #endif
  176. bool shl_char_array(CharT const* str_value) BOOST_NOEXCEPT {
  177. start = str_value;
  178. finish = start + Traits::length(str_value);
  179. return true;
  180. }
  181. template <class T>
  182. bool shl_char_array(T const* str_value) {
  183. BOOST_STATIC_ASSERT_MSG(( sizeof(T) <= sizeof(CharT)),
  184. "boost::lexical_cast does not support narrowing of char types."
  185. "Use boost::locale instead" );
  186. return shl_input_streamable(str_value);
  187. }
  188. bool shl_char_array_limited(CharT const* str, std::size_t max_size) BOOST_NOEXCEPT {
  189. start = str;
  190. finish = std::find(start, start + max_size, Traits::to_char_type(0));
  191. return true;
  192. }
  193. template<typename InputStreamable>
  194. bool shl_input_streamable(InputStreamable& input) {
  195. #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE)
  196. // If you have compilation error at this point, than your STL library
  197. // does not support such conversions. Try updating it.
  198. BOOST_STATIC_ASSERT((boost::is_same<char, CharT>::value));
  199. #endif
  200. #ifndef BOOST_NO_EXCEPTIONS
  201. out_stream.exceptions(std::ios::badbit);
  202. try {
  203. #endif
  204. bool const result = !(out_stream << input).fail();
  205. const deduced_out_buffer_t* const p = static_cast<deduced_out_buffer_t*>(
  206. out_stream.rdbuf()
  207. );
  208. start = p->pbase();
  209. finish = p->pptr();
  210. return result;
  211. #ifndef BOOST_NO_EXCEPTIONS
  212. } catch (const ::std::ios_base::failure& /*f*/) {
  213. return false;
  214. }
  215. #endif
  216. }
  217. template <class T>
  218. inline bool shl_unsigned(const T n) {
  219. CharT* tmp_finish = buffer + CharacterBufferSize;
  220. start = lcast_put_unsigned<Traits, T, CharT>(n, tmp_finish).convert();
  221. finish = tmp_finish;
  222. return true;
  223. }
  224. template <class T>
  225. inline bool shl_signed(const T n) {
  226. CharT* tmp_finish = buffer + CharacterBufferSize;
  227. typedef BOOST_DEDUCED_TYPENAME boost::make_unsigned<T>::type utype;
  228. CharT* tmp_start = lcast_put_unsigned<Traits, utype, CharT>(lcast_to_unsigned(n), tmp_finish).convert();
  229. if (n < 0) {
  230. --tmp_start;
  231. CharT const minus = lcast_char_constants<CharT>::minus;
  232. Traits::assign(*tmp_start, minus);
  233. }
  234. start = tmp_start;
  235. finish = tmp_finish;
  236. return true;
  237. }
  238. template <class T, class SomeCharT>
  239. bool shl_real_type(const T& val, SomeCharT* /*begin*/) {
  240. lcast_set_precision(out_stream, &val);
  241. return shl_input_streamable(val);
  242. }
  243. bool shl_real_type(float val, char* begin) {
  244. using namespace std;
  245. const double val_as_double = val;
  246. finish = start +
  247. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
  248. sprintf_s(begin, CharacterBufferSize,
  249. #else
  250. sprintf(begin,
  251. #endif
  252. "%.*g", static_cast<int>(boost::detail::lcast_get_precision<float>()), val_as_double);
  253. return finish > start;
  254. }
  255. bool shl_real_type(double val, char* begin) {
  256. using namespace std;
  257. finish = start +
  258. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
  259. sprintf_s(begin, CharacterBufferSize,
  260. #else
  261. sprintf(begin,
  262. #endif
  263. "%.*g", static_cast<int>(boost::detail::lcast_get_precision<double>()), val);
  264. return finish > start;
  265. }
  266. #ifndef __MINGW32__
  267. bool shl_real_type(long double val, char* begin) {
  268. using namespace std;
  269. finish = start +
  270. #if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
  271. sprintf_s(begin, CharacterBufferSize,
  272. #else
  273. sprintf(begin,
  274. #endif
  275. "%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double>()), val );
  276. return finish > start;
  277. }
  278. #endif
  279. #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_SWPRINTF) && !defined(__MINGW32__)
  280. bool shl_real_type(float val, wchar_t* begin) {
  281. using namespace std;
  282. const double val_as_double = val;
  283. finish = start + swprintf(begin, CharacterBufferSize,
  284. L"%.*g",
  285. static_cast<int>(boost::detail::lcast_get_precision<float >()),
  286. val_as_double );
  287. return finish > start;
  288. }
  289. bool shl_real_type(double val, wchar_t* begin) {
  290. using namespace std;
  291. finish = start + swprintf(begin, CharacterBufferSize,
  292. L"%.*g", static_cast<int>(boost::detail::lcast_get_precision<double >()), val );
  293. return finish > start;
  294. }
  295. bool shl_real_type(long double val, wchar_t* begin) {
  296. using namespace std;
  297. finish = start + swprintf(begin, CharacterBufferSize,
  298. L"%.*Lg", static_cast<int>(boost::detail::lcast_get_precision<long double >()), val );
  299. return finish > start;
  300. }
  301. #endif
  302. template <class T>
  303. bool shl_real(T val) {
  304. CharT* tmp_finish = buffer + CharacterBufferSize;
  305. if (put_inf_nan(buffer, tmp_finish, val)) {
  306. finish = tmp_finish;
  307. return true;
  308. }
  309. return shl_real_type(val, static_cast<CharT*>(buffer));
  310. }
  311. /************************************ OPERATORS << ( ... ) ********************************/
  312. public:
  313. template<class Alloc>
  314. bool operator<<(std::basic_string<CharT,Traits,Alloc> const& str) BOOST_NOEXCEPT {
  315. start = str.data();
  316. finish = start + str.length();
  317. return true;
  318. }
  319. template<class Alloc>
  320. bool operator<<(boost::container::basic_string<CharT,Traits,Alloc> const& str) BOOST_NOEXCEPT {
  321. start = str.data();
  322. finish = start + str.length();
  323. return true;
  324. }
  325. bool operator<<(bool value) BOOST_NOEXCEPT {
  326. CharT const czero = lcast_char_constants<CharT>::zero;
  327. Traits::assign(buffer[0], Traits::to_char_type(czero + value));
  328. finish = start + 1;
  329. return true;
  330. }
  331. template <class C>
  332. BOOST_DEDUCED_TYPENAME boost::disable_if<boost::is_const<C>, bool>::type
  333. operator<<(const iterator_range<C*>& rng) BOOST_NOEXCEPT {
  334. return (*this) << iterator_range<const C*>(rng.begin(), rng.end());
  335. }
  336. bool operator<<(const iterator_range<const CharT*>& rng) BOOST_NOEXCEPT {
  337. start = rng.begin();
  338. finish = rng.end();
  339. return true;
  340. }
  341. bool operator<<(const iterator_range<const signed char*>& rng) BOOST_NOEXCEPT {
  342. return (*this) << iterator_range<const char*>(
  343. reinterpret_cast<const char*>(rng.begin()),
  344. reinterpret_cast<const char*>(rng.end())
  345. );
  346. }
  347. bool operator<<(const iterator_range<const unsigned char*>& rng) BOOST_NOEXCEPT {
  348. return (*this) << iterator_range<const char*>(
  349. reinterpret_cast<const char*>(rng.begin()),
  350. reinterpret_cast<const char*>(rng.end())
  351. );
  352. }
  353. bool operator<<(char ch) { return shl_char(ch); }
  354. bool operator<<(unsigned char ch) { return ((*this) << static_cast<char>(ch)); }
  355. bool operator<<(signed char ch) { return ((*this) << static_cast<char>(ch)); }
  356. #if !defined(BOOST_LCAST_NO_WCHAR_T)
  357. bool operator<<(wchar_t const* str) { return shl_char_array(str); }
  358. bool operator<<(wchar_t * str) { return shl_char_array(str); }
  359. #ifndef BOOST_NO_INTRINSIC_WCHAR_T
  360. bool operator<<(wchar_t ch) { return shl_char(ch); }
  361. #endif
  362. #endif
  363. #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
  364. bool operator<<(char16_t ch) { return shl_char(ch); }
  365. bool operator<<(char16_t * str) { return shl_char_array(str); }
  366. bool operator<<(char16_t const * str) { return shl_char_array(str); }
  367. #endif
  368. #if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
  369. bool operator<<(char32_t ch) { return shl_char(ch); }
  370. bool operator<<(char32_t * str) { return shl_char_array(str); }
  371. bool operator<<(char32_t const * str) { return shl_char_array(str); }
  372. #endif
  373. bool operator<<(unsigned char const* ch) { return ((*this) << reinterpret_cast<char const*>(ch)); }
  374. bool operator<<(unsigned char * ch) { return ((*this) << reinterpret_cast<char *>(ch)); }
  375. bool operator<<(signed char const* ch) { return ((*this) << reinterpret_cast<char const*>(ch)); }
  376. bool operator<<(signed char * ch) { return ((*this) << reinterpret_cast<char *>(ch)); }
  377. bool operator<<(char const* str_value) { return shl_char_array(str_value); }
  378. bool operator<<(char* str_value) { return shl_char_array(str_value); }
  379. bool operator<<(short n) { return shl_signed(n); }
  380. bool operator<<(int n) { return shl_signed(n); }
  381. bool operator<<(long n) { return shl_signed(n); }
  382. bool operator<<(unsigned short n) { return shl_unsigned(n); }
  383. bool operator<<(unsigned int n) { return shl_unsigned(n); }
  384. bool operator<<(unsigned long n) { return shl_unsigned(n); }
  385. #if defined(BOOST_HAS_LONG_LONG)
  386. bool operator<<(boost::ulong_long_type n) { return shl_unsigned(n); }
  387. bool operator<<(boost::long_long_type n) { return shl_signed(n); }
  388. #elif defined(BOOST_HAS_MS_INT64)
  389. bool operator<<(unsigned __int64 n) { return shl_unsigned(n); }
  390. bool operator<<( __int64 n) { return shl_signed(n); }
  391. #endif
  392. #ifdef BOOST_HAS_INT128
  393. bool operator<<(const boost::uint128_type& n) { return shl_unsigned(n); }
  394. bool operator<<(const boost::int128_type& n) { return shl_signed(n); }
  395. #endif
  396. bool operator<<(float val) { return shl_real(val); }
  397. bool operator<<(double val) { return shl_real(val); }
  398. bool operator<<(long double val) {
  399. #ifndef __MINGW32__
  400. return shl_real(val);
  401. #else
  402. return shl_real(static_cast<double>(val));
  403. #endif
  404. }
  405. // Adding constness to characters. Constness does not change layout
  406. template <class C, std::size_t N>
  407. BOOST_DEDUCED_TYPENAME boost::disable_if<boost::is_const<C>, bool>::type
  408. operator<<(boost::array<C, N> const& input) BOOST_NOEXCEPT {
  409. BOOST_STATIC_ASSERT_MSG(
  410. (sizeof(boost::array<const C, N>) == sizeof(boost::array<C, N>)),
  411. "boost::array<C, N> and boost::array<const C, N> must have exactly the same layout."
  412. );
  413. return ((*this) << reinterpret_cast<boost::array<const C, N> const& >(input));
  414. }
  415. template <std::size_t N>
  416. bool operator<<(boost::array<const CharT, N> const& input) BOOST_NOEXCEPT {
  417. return shl_char_array_limited(input.data(), N);
  418. }
  419. template <std::size_t N>
  420. bool operator<<(boost::array<const unsigned char, N> const& input) BOOST_NOEXCEPT {
  421. return ((*this) << reinterpret_cast<boost::array<const char, N> const& >(input));
  422. }
  423. template <std::size_t N>
  424. bool operator<<(boost::array<const signed char, N> const& input) BOOST_NOEXCEPT {
  425. return ((*this) << reinterpret_cast<boost::array<const char, N> const& >(input));
  426. }
  427. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  428. // Making a Boost.Array from std::array
  429. template <class C, std::size_t N>
  430. bool operator<<(std::array<C, N> const& input) BOOST_NOEXCEPT {
  431. BOOST_STATIC_ASSERT_MSG(
  432. (sizeof(std::array<C, N>) == sizeof(boost::array<C, N>)),
  433. "std::array and boost::array must have exactly the same layout. "
  434. "Bug in implementation of std::array or boost::array."
  435. );
  436. return ((*this) << reinterpret_cast<boost::array<C, N> const& >(input));
  437. }
  438. #endif
  439. template <class InStreamable>
  440. bool operator<<(const InStreamable& input) { return shl_input_streamable(input); }
  441. };
  442. template <class CharT, class Traits>
  443. class lexical_ostream_limited_src: boost::noncopyable {
  444. //`[start, finish)` is the range to output by `operator >>`
  445. const CharT* start;
  446. const CharT* const finish;
  447. public:
  448. lexical_ostream_limited_src(const CharT* begin, const CharT* end) BOOST_NOEXCEPT
  449. : start(begin)
  450. , finish(end)
  451. {}
  452. /************************************ HELPER FUNCTIONS FOR OPERATORS >> ( ... ) ********************************/
  453. private:
  454. template <typename Type>
  455. bool shr_unsigned(Type& output) {
  456. if (start == finish) return false;
  457. CharT const minus = lcast_char_constants<CharT>::minus;
  458. CharT const plus = lcast_char_constants<CharT>::plus;
  459. bool const has_minus = Traits::eq(minus, *start);
  460. /* We won`t use `start' any more, so no need in decrementing it after */
  461. if (has_minus || Traits::eq(plus, *start)) {
  462. ++start;
  463. }
  464. bool const succeed = lcast_ret_unsigned<Traits, Type, CharT>(output, start, finish).convert();
  465. if (has_minus) {
  466. output = static_cast<Type>(0u - output);
  467. }
  468. return succeed;
  469. }
  470. template <typename Type>
  471. bool shr_signed(Type& output) {
  472. if (start == finish) return false;
  473. CharT const minus = lcast_char_constants<CharT>::minus;
  474. CharT const plus = lcast_char_constants<CharT>::plus;
  475. typedef BOOST_DEDUCED_TYPENAME make_unsigned<Type>::type utype;
  476. utype out_tmp = 0;
  477. bool const has_minus = Traits::eq(minus, *start);
  478. /* We won`t use `start' any more, so no need in decrementing it after */
  479. if (has_minus || Traits::eq(plus, *start)) {
  480. ++start;
  481. }
  482. bool succeed = lcast_ret_unsigned<Traits, utype, CharT>(out_tmp, start, finish).convert();
  483. if (has_minus) {
  484. utype const comp_val = (static_cast<utype>(1) << std::numeric_limits<Type>::digits);
  485. succeed = succeed && out_tmp<=comp_val;
  486. output = static_cast<Type>(0u - out_tmp);
  487. } else {
  488. utype const comp_val = static_cast<utype>((std::numeric_limits<Type>::max)());
  489. succeed = succeed && out_tmp<=comp_val;
  490. output = static_cast<Type>(out_tmp);
  491. }
  492. return succeed;
  493. }
  494. template<typename InputStreamable>
  495. bool shr_using_base_class(InputStreamable& output)
  496. {
  497. BOOST_STATIC_ASSERT_MSG(
  498. (!boost::is_pointer<InputStreamable>::value),
  499. "boost::lexical_cast can not convert to pointers"
  500. );
  501. #if defined(BOOST_NO_STRINGSTREAM) || defined(BOOST_NO_STD_LOCALE)
  502. BOOST_STATIC_ASSERT_MSG((boost::is_same<char, CharT>::value),
  503. "boost::lexical_cast can not convert, because your STL library does not "
  504. "support such conversions. Try updating it."
  505. );
  506. #endif
  507. #if defined(BOOST_NO_STRINGSTREAM)
  508. std::istrstream stream(start, static_cast<std::istrstream::streamsize>(finish - start));
  509. #else
  510. typedef BOOST_DEDUCED_TYPENAME out_stream_helper_trait<CharT, Traits>::buffer_t buffer_t;
  511. buffer_t buf;
  512. // Usually `istream` and `basic_istream` do not modify
  513. // content of buffer; `buffer_t` assures that this is true
  514. buf.setbuf(const_cast<CharT*>(start), static_cast<typename buffer_t::streamsize>(finish - start));
  515. #if defined(BOOST_NO_STD_LOCALE)
  516. std::istream stream(&buf);
  517. #else
  518. std::basic_istream<CharT, Traits> stream(&buf);
  519. #endif // BOOST_NO_STD_LOCALE
  520. #endif // BOOST_NO_STRINGSTREAM
  521. #ifndef BOOST_NO_EXCEPTIONS
  522. stream.exceptions(std::ios::badbit);
  523. try {
  524. #endif
  525. stream.unsetf(std::ios::skipws);
  526. lcast_set_precision(stream, static_cast<InputStreamable*>(0));
  527. return (stream >> output)
  528. && (stream.get() == Traits::eof());
  529. #ifndef BOOST_NO_EXCEPTIONS
  530. } catch (const ::std::ios_base::failure& /*f*/) {
  531. return false;
  532. }
  533. #endif
  534. }
  535. template<class T>
  536. inline bool shr_xchar(T& output) BOOST_NOEXCEPT {
  537. BOOST_STATIC_ASSERT_MSG(( sizeof(CharT) == sizeof(T) ),
  538. "boost::lexical_cast does not support narrowing of character types."
  539. "Use boost::locale instead" );
  540. bool const ok = (finish - start == 1);
  541. if (ok) {
  542. CharT out;
  543. Traits::assign(out, *start);
  544. output = static_cast<T>(out);
  545. }
  546. return ok;
  547. }
  548. template <std::size_t N, class ArrayT>
  549. bool shr_std_array(ArrayT& output) BOOST_NOEXCEPT {
  550. using namespace std;
  551. const std::size_t size = static_cast<std::size_t>(finish - start);
  552. if (size > N - 1) { // `-1` because we need to store \0 at the end
  553. return false;
  554. }
  555. memcpy(&output[0], start, size * sizeof(CharT));
  556. output[size] = Traits::to_char_type(0);
  557. return true;
  558. }
  559. /************************************ OPERATORS >> ( ... ) ********************************/
  560. public:
  561. bool operator>>(unsigned short& output) { return shr_unsigned(output); }
  562. bool operator>>(unsigned int& output) { return shr_unsigned(output); }
  563. bool operator>>(unsigned long int& output) { return shr_unsigned(output); }
  564. bool operator>>(short& output) { return shr_signed(output); }
  565. bool operator>>(int& output) { return shr_signed(output); }
  566. bool operator>>(long int& output) { return shr_signed(output); }
  567. #if defined(BOOST_HAS_LONG_LONG)
  568. bool operator>>(boost::ulong_long_type& output) { return shr_unsigned(output); }
  569. bool operator>>(boost::long_long_type& output) { return shr_signed(output); }
  570. #elif defined(BOOST_HAS_MS_INT64)
  571. bool operator>>(unsigned __int64& output) { return shr_unsigned(output); }
  572. bool operator>>(__int64& output) { return shr_signed(output); }
  573. #endif
  574. #ifdef BOOST_HAS_INT128
  575. bool operator>>(boost::uint128_type& output) { return shr_unsigned(output); }
  576. bool operator>>(boost::int128_type& output) { return shr_signed(output); }
  577. #endif
  578. bool operator>>(char& output) { return shr_xchar(output); }
  579. bool operator>>(unsigned char& output) { return shr_xchar(output); }
  580. bool operator>>(signed char& output) { return shr_xchar(output); }
  581. #if !defined(BOOST_LCAST_NO_WCHAR_T) && !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  582. bool operator>>(wchar_t& output) { return shr_xchar(output); }
  583. #endif
  584. #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
  585. bool operator>>(char16_t& output) { return shr_xchar(output); }
  586. #endif
  587. #if !defined(BOOST_NO_CXX11_CHAR32_T) && !defined(BOOST_NO_CXX11_UNICODE_LITERALS)
  588. bool operator>>(char32_t& output) { return shr_xchar(output); }
  589. #endif
  590. template<class Alloc>
  591. bool operator>>(std::basic_string<CharT,Traits,Alloc>& str) {
  592. str.assign(start, finish); return true;
  593. }
  594. template<class Alloc>
  595. bool operator>>(boost::container::basic_string<CharT,Traits,Alloc>& str) {
  596. str.assign(start, finish); return true;
  597. }
  598. template <std::size_t N>
  599. bool operator>>(boost::array<CharT, N>& output) BOOST_NOEXCEPT {
  600. return shr_std_array<N>(output);
  601. }
  602. template <std::size_t N>
  603. bool operator>>(boost::array<unsigned char, N>& output) BOOST_NOEXCEPT {
  604. return ((*this) >> reinterpret_cast<boost::array<char, N>& >(output));
  605. }
  606. template <std::size_t N>
  607. bool operator>>(boost::array<signed char, N>& output) BOOST_NOEXCEPT {
  608. return ((*this) >> reinterpret_cast<boost::array<char, N>& >(output));
  609. }
  610. #ifndef BOOST_NO_CXX11_HDR_ARRAY
  611. template <class C, std::size_t N>
  612. bool operator>>(std::array<C, N>& output) BOOST_NOEXCEPT {
  613. BOOST_STATIC_ASSERT_MSG(
  614. (sizeof(std::array<C, N>) == sizeof(boost::array<C, N>)),
  615. "std::array<C, N> and boost::array<C, N> must have exactly the same layout."
  616. );
  617. return ((*this) >> reinterpret_cast<boost::array<C, N>& >(output));
  618. }
  619. #endif
  620. bool operator>>(bool& output) BOOST_NOEXCEPT {
  621. output = false; // Suppress warning about uninitalized variable
  622. if (start == finish) return false;
  623. CharT const zero = lcast_char_constants<CharT>::zero;
  624. CharT const plus = lcast_char_constants<CharT>::plus;
  625. CharT const minus = lcast_char_constants<CharT>::minus;
  626. const CharT* const dec_finish = finish - 1;
  627. output = Traits::eq(*dec_finish, zero + 1);
  628. if (!output && !Traits::eq(*dec_finish, zero)) {
  629. return false; // Does not ends on '0' or '1'
  630. }
  631. if (start == dec_finish) return true;
  632. // We may have sign at the beginning
  633. if (Traits::eq(plus, *start) || (Traits::eq(minus, *start) && !output)) {
  634. ++ start;
  635. }
  636. // Skipping zeros
  637. while (start != dec_finish) {
  638. if (!Traits::eq(zero, *start)) {
  639. return false; // Not a zero => error
  640. }
  641. ++ start;
  642. }
  643. return true;
  644. }
  645. private:
  646. // Not optimised converter
  647. template <class T>
  648. bool float_types_converter_internal(T& output) {
  649. if (parse_inf_nan(start, finish, output)) return true;
  650. bool const return_value = shr_using_base_class(output);
  651. /* Some compilers and libraries successfully
  652. * parse 'inf', 'INFINITY', '1.0E', '1.0E-'...
  653. * We are trying to provide a unified behaviour,
  654. * so we just forbid such conversions (as some
  655. * of the most popular compilers/libraries do)
  656. * */
  657. CharT const minus = lcast_char_constants<CharT>::minus;
  658. CharT const plus = lcast_char_constants<CharT>::plus;
  659. CharT const capital_e = lcast_char_constants<CharT>::capital_e;
  660. CharT const lowercase_e = lcast_char_constants<CharT>::lowercase_e;
  661. if ( return_value &&
  662. (
  663. Traits::eq(*(finish-1), lowercase_e) // 1.0e
  664. || Traits::eq(*(finish-1), capital_e) // 1.0E
  665. || Traits::eq(*(finish-1), minus) // 1.0e- or 1.0E-
  666. || Traits::eq(*(finish-1), plus) // 1.0e+ or 1.0E+
  667. )
  668. ) return false;
  669. return return_value;
  670. }
  671. public:
  672. bool operator>>(float& output) { return float_types_converter_internal(output); }
  673. bool operator>>(double& output) { return float_types_converter_internal(output); }
  674. bool operator>>(long double& output) { return float_types_converter_internal(output); }
  675. // Generic istream-based algorithm.
  676. // lcast_streambuf_for_target<InputStreamable>::value is true.
  677. template <typename InputStreamable>
  678. bool operator>>(InputStreamable& output) {
  679. return shr_using_base_class(output);
  680. }
  681. };
  682. }
  683. } // namespace boost
  684. #undef BOOST_LCAST_NO_WCHAR_T
  685. #endif // BOOST_LEXICAL_CAST_DETAIL_CONVERTER_LEXICAL_HPP