converter_lexical_streams.hpp 29 KB

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