converter_lexical_streams.hpp 33 KB

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