converter_numeric.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 - 2016
  17. #ifndef BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP
  18. #define BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP
  19. #include <boost/config.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. # pragma once
  22. #endif
  23. #include <boost/core/cmath.hpp>
  24. #include <boost/core/enable_if.hpp>
  25. #include <boost/limits.hpp>
  26. #include <boost/type_traits/type_identity.hpp>
  27. #include <boost/type_traits/conditional.hpp>
  28. #include <boost/type_traits/make_unsigned.hpp>
  29. #include <boost/type_traits/is_signed.hpp>
  30. #include <boost/type_traits/is_arithmetic.hpp>
  31. #include <boost/type_traits/is_float.hpp>
  32. namespace boost { namespace detail {
  33. template <class Source, class Target>
  34. bool ios_numeric_comparer_float(Source x, Source y) noexcept {
  35. return x == y
  36. || (boost::core::isnan(x) && boost::core::isnan(y))
  37. || (x < (std::numeric_limits<Target>::min)())
  38. ;
  39. }
  40. template <class RangeType, class T>
  41. constexpr bool is_out_of_range_for(T value) noexcept {
  42. return value > static_cast<T>((std::numeric_limits<RangeType>::max)())
  43. || value < static_cast<T>((std::numeric_limits<RangeType>::min)());
  44. }
  45. // integral -> integral
  46. template <typename Target, typename Source>
  47. typename boost::enable_if_c<
  48. !boost::is_floating_point<Source>::value && !boost::is_floating_point<Target>::value, bool
  49. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  50. const Target target_tmp = static_cast<Target>(arg);
  51. const Source arg_restored = static_cast<Source>(target_tmp);
  52. if (arg == arg_restored) {
  53. result = target_tmp;
  54. return true;
  55. }
  56. return false;
  57. }
  58. // integral -> floating point
  59. template <typename Target, typename Source>
  60. typename boost::enable_if_c<
  61. !boost::is_floating_point<Source>::value && boost::is_floating_point<Target>::value, bool
  62. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  63. const Target target_tmp = static_cast<Target>(arg);
  64. result = target_tmp;
  65. return true;
  66. }
  67. // floating point -> floating point
  68. template <typename Target, typename Source>
  69. typename boost::enable_if_c<
  70. boost::is_floating_point<Source>::value && boost::is_floating_point<Target>::value, bool
  71. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  72. const Target target_tmp = static_cast<Target>(arg);
  73. const Source arg_restored = static_cast<Source>(target_tmp);
  74. if (detail::ios_numeric_comparer_float<Source, Target>(arg, arg_restored)) {
  75. result = target_tmp;
  76. return true;
  77. }
  78. return false;
  79. }
  80. // floating point -> integral
  81. template <typename Target, typename Source>
  82. typename boost::enable_if_c<
  83. boost::is_floating_point<Source>::value && !boost::is_floating_point<Target>::value, bool
  84. >::type noexcept_numeric_convert(Source arg, Target& result) noexcept {
  85. if (detail::is_out_of_range_for<Target>(arg)) {
  86. return false;
  87. }
  88. const Target target_tmp = static_cast<Target>(arg);
  89. const Source arg_restored = static_cast<Source>(target_tmp);
  90. if (detail::ios_numeric_comparer_float<Source, Target>(arg, arg_restored)) {
  91. result = target_tmp;
  92. return true;
  93. }
  94. return false;
  95. }
  96. struct lexical_cast_dynamic_num_not_ignoring_minus
  97. {
  98. template <typename Target, typename Source>
  99. static inline bool try_convert(Source arg, Target& result) noexcept {
  100. return boost::detail::noexcept_numeric_convert<Target, Source >(arg, result);
  101. }
  102. };
  103. struct lexical_cast_dynamic_num_ignoring_minus
  104. {
  105. template <typename Target, typename Source>
  106. #if defined(__clang__) && (__clang_major__ > 3 || __clang_minor__ > 6)
  107. __attribute__((no_sanitize("unsigned-integer-overflow")))
  108. #endif
  109. static inline bool try_convert(Source arg, Target& result) noexcept {
  110. typedef typename boost::conditional<
  111. boost::is_float<Source>::value,
  112. boost::type_identity<Source>,
  113. boost::make_unsigned<Source>
  114. >::type usource_lazy_t;
  115. typedef typename usource_lazy_t::type usource_t;
  116. if (arg < 0) {
  117. const bool res = boost::detail::noexcept_numeric_convert<Target, usource_t>(
  118. static_cast<usource_t>(0u - static_cast<usource_t>(arg)), result
  119. );
  120. result = static_cast<Target>(0u - result);
  121. return res;
  122. } else {
  123. return boost::detail::noexcept_numeric_convert<Target, usource_t>(arg, result);
  124. }
  125. }
  126. };
  127. /*
  128. * dynamic_num_converter_impl follows the rules:
  129. * 1) If Source can be converted to Target without precision loss and
  130. * without overflows, then assign Source to Target and return
  131. *
  132. * 2) If Source is less than 0 and Target is an unsigned integer,
  133. * then negate Source, check the requirements of rule 1) and if
  134. * successful, assign static_casted Source to Target and return
  135. *
  136. * 3) Otherwise throw a bad_lexical_cast exception
  137. *
  138. *
  139. * Rule 2) required because boost::lexical_cast has the behavior of
  140. * stringstream, which uses the rules of scanf for conversions. And
  141. * in the C99 standard for unsigned input value minus sign is
  142. * optional, so if a negative number is read, no errors will arise
  143. * and the result will be the two's complement.
  144. */
  145. template <typename Target, typename Source>
  146. struct dynamic_num_converter_impl
  147. {
  148. static inline bool try_convert(Source arg, Target& result) noexcept {
  149. typedef typename boost::conditional<
  150. boost::is_unsigned<Target>::value &&
  151. (boost::is_signed<Source>::value || boost::is_float<Source>::value) &&
  152. !(boost::is_same<Source, bool>::value) &&
  153. !(boost::is_same<Target, bool>::value),
  154. lexical_cast_dynamic_num_ignoring_minus,
  155. lexical_cast_dynamic_num_not_ignoring_minus
  156. >::type caster_type;
  157. return caster_type::try_convert(arg, result);
  158. }
  159. };
  160. }} // namespace boost::detail
  161. #endif // BOOST_LEXICAL_CAST_DETAIL_CONVERTER_NUMERIC_HPP